Kod: Markera allt
#! /bin/bash
#
function oldpgm {
echo -"Processes running old versions:"
PROCESSES=$(checkrestart --nolsof 2>> /dev/null | grep : | sed 's/://' | sort)
let LOOP=1
IFS=$'\n'
for PROCESS in $PROCESSES; do
CNT=$(echo "$PROCESS" | wc -w)
if [[ $CNT == 1 ]]; then
echo " $PROCESS"
let LOOP+=1
else
#echo $CNT" - "$PROCESS
:
fi
done
}
function krnl {
NAME=$(uname -s)
MACHINE=$(uname -m)
RUNNING_KRNL=$(uname -r)
NEWEST_KRNL=$(ls -l /boot/vmlinuz | awk '{ print $11 }' | sed 's/vmlinuz-//g')
#echo
echo "Running kernel is: $(uname -rms)"
if [[ $RUNNING_KRNL != $NEWEST_KRNL ]]; then
echo -e "\033[1m\033[31m=>A newer kernel exists, consider rebooting !!!!!\033[0m"
#echo "Newer version is: $NAME $NEWEST_KRNL $MACHINE"
echo "Newer kernel is: $NAME $NEWEST_KRNL $MACHINE"
else
echo "-Ok"
fi
echo
}
function chkProc {
RETURN=0
NPROC=$(ps -ef | tail -n +2 | wc -l)
NPROCT=$(ps -efT | tail -n +2 | wc -l)
MAX_PROC=$(cat /proc/sys/kernel/pid_max)
if [[ -z $1 || $1 == "INIT" ]]; then
MAX_PROC=$(cat /proc/sys/kernel/pid_max)
MAX_UPROC=$(ulimit -u)
fi
let Z_count=0
PIDS=$(ls -d /proc/[0-9]* 2> /dev/null | awk -F/ '{ print $3 }' 2>> /dev/null)
for PID in $PIDS; do
PSTATE=$(grep State: /proc/$PID/status 2>> /dev/null| awk '{ print $2 }')
if [[ ! -z $PSTATE ]]; then
if [[ $PSTATE == "X" ]]; then
let X_count+=1
RETURN=1
elif [[ $PSTATE == "Z" ]]; then
let Z_count+=1
RETURN=1
fi
fi
done
echo "Number of processes: $NPROC"
echo "Number of processes+threads: $NPROCT"
echo "Max process limit: $MAX_PROC"
if [[ $X_count -ne 0 ]]; then
echo "-Dead processes: $X_count"
fi
if [[ $Z_count -ne 0 ]]; then
echo "-Zombie processes: $Z_count"
fi
if [[ $X_count -ne 0 || $Z_count -ne 0 ]]; then
echo "*** Consider REBOOT ***"
fi
return $RETURN
}
if [[ -z $EUID ]]; then
EFFUID=$(ps --no-headers -o euid --pid $$ | xargs)
else
EFFUID=$EUID
fi
clear -x
echo
echo "=============================================================================="
echo "-Up in $(uptime -p)"
echo
echo "=============================================================================="
echo '-Checking "/var/run/reboot-recuired"'
if [[ -r /var/run/reboot-required ]]; then
echo -en "\033[1m\033[31m"
cat /var/run/reboot-required
if [[ -r /var/run/reboot-required.pkgs ]]; then
echo "-Packages that need reboot:"
cat /var/run/reboot-required.pkgs | sort -u
else
echo "-No info about pkgs"
fi
echo -en "\033[0m"
else
echo "-Ok"
fi
echo
echo "=============================================================================="
echo "-Checking kernel"
krnl
echo "=============================================================================="
echo "-Checking processes"
chkProc
echo
DIST=$(source /etc/os-release; echo $ID | awk -F \- '{ print $1 }')
if [[ $DIST == "ubuntu" || $DIST == "linuxmint" || $DIST == "debian" ]]; then
echo "=============================================================================="
echo '-Running "needrestart"'
if [[ ! -z $(which needrestart 2> /dev/null) ]]; then
needrestart -r l
PCNT=$(needrestart 2> /dev/null | tail -n +2 | tr -d -c '[:digit:] ' | xargs | wc -w)
echo
echo "Outdated processes: $PCNT"
#needrestart -r l | grep "Your outdated processes"
if [[ $EFFUID != "0" ]]; then
if [[ $1 == "-f" ]]; then
PROCESSES=$(needrestart 2> /dev/null | tail -n +2 | tr -d -c '[:digit:] ' | xargs)
if [[ ! -z $PROCESSES ]]; then
echo "-Own processes that need restart:"
echo "-Info about outdated processes:"
echo "PROCESSES: $PROCESSES"
ps -fp $PROCESSES
:
fi
fi
fi
else
echo 'needrestart is not installed - use "sudo apt install needrestart" to install it..."'
fi
echo
echo "=============================================================================="
if [[ $EFFUID == "0" ]]; then
echo '-Running "checkrestart"'
echo '-Note that most messages from "lsof" can be ignored...'
if [[ ! -z $(which checkrestart 2> /dev/null) ]]; then
echo
OUT=$(checkrestart -t 2>> /dev/null)
let NUM=$(echo $OUT | awk '{ print $1 }')
TXT=$(echo $OUT | awk '{ $1=""; print $0 }' | xargs)
printf "Terse: %3d %s\n" $NUM "$TXT"
OUT=$(checkrestart -atp 2>> /dev/null)
let NUM=$(echo $OUT | awk '{ print $1 }')
TXT=$(echo $OUT | awk '{ $1=""; print $0 }' | xargs)
printf "All: %3d %s\n" $NUM "$TXT"
echo
oldpgm
else
echo 'debian-goodies is not installed - use "sudo apt install debian-goodies" to install it...'
fi
echo
else
echo '-Can not run "checkrestart", as we are not root...'
echo
fi
elif [[ $DIST == "opensuse" || $DIST == "sles" || $DIST == "sled" || $DIST == "sle" ]]; then
echo "=============================================================================="
if [[ -z $(which needs-restarting 2> /dev/null) ]]; then
echo '-Running "zypper ps"'
zypper ps
else
#echo 'yum-utils is not installed - use "sudo zypper install yum-utils" to install it...'
echo '-Running "needs-restarting"'
needs-restarting
fi
echo
elif [[ $DIST == "fedora" || $DIST == "centos" || $DIST == "redhat" || $DIST == "rhel" ]]; then
echo "=============================================================================="
if [[ ! -z $(which needs-restarting 2> /dev/null) ]]; then
echo '-Running "dnf needs-restarting -r"'
dnf needs-restarting -r
echo
else
echo '-Running "needs-restarting -r"'
needs-restarting -r
echo
fi
else
echo "=============================================================================="
echo "-$DIST is not supported yet, sorry!"
echo
fi
