-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
chkreboot
executable file
·85 lines (74 loc) · 2.16 KB
/
chkreboot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# can you just reboot a linux computer? sure you could
# this does some checks giving you a hint about its usage
# returns 0 if it's safe to reboot
# returns 1-255 for the gore score you cause when rebooting
# dkms should be installed
# nfs-common for showmount
ansi_bold="\033[1m"
ansi_warn='\033[0;33m'${ansi_bold}
ansi_err='\033[0;31m'${ansi_bold}
ansi_ok='\033[0;32m'${ansi_bold}
ansi_reset="\033[0m"
f=" "
r=0
color () {
result="$1"
if [ "$1" -eq 0 ]; then
result="${ansi_bold}${ansi_ok}$1${ansi_reset}"
else
if [ "$1" -eq 1 ]; then
result="${ansi_bold}${ansi_warn}$1${ansi_reset}"
else
result="${ansi_bold}${ansi_err}$1${ansi_reset}"
fi
fi
}
# logged in users
echo -n "Users on system: "
liun=$(who |grep -v root | awk '{print $1}' |sort -u|wc -l)
color "$liun"
echo -e "$result"
who |grep -v root | awk '{print $1}' |sort -u | while read a; do
echo "${f}${a}"
done
r=$((r + liun))
# screen or tmux sessions
echo -n "screen or tmux sessions of users on system: "
sotn=$(ps -ef |grep -i 'screen\|tmux' |grep -v grep | awk '{print $1 " " $NF}' |sort -u|wc -l)
color "$sotn"
echo -e "$result"
ps -ef |grep -i 'screen\|tmux' |grep -v grep | awk '{$2=$3=$4=$5=$6=$7="";print}' |sort -u| while read a b; do
echo "${f}${a} [${b}]"
done
r=$((r + sotn))
# something is running
echo -n "CPU load: "
cores=$(grep -c ^proc /proc/cpuinfo)
cpu=$(uptime | sed s,.*:,,g | sed s/,//g|awk '{printf("%.0f\n",$(NF-2))}')
color "$cpu"
echo -e "$result"
echo "${f}${cores} cores"
r=$((r + cpu))
# remote moutns?
echo -n "Network filesystems: "
nfs=$(showmount -a | sed 1d| sed "s,^,&${f},g")
nfsn=$(showmount -a | sed 1d | wc -l)
color "$nfsn"
echo -e "$result"
if [ ! "$nfsn" = 0 ]; then
echo "$nfs"
fi
r=$((r + nfsn))
# check if there is GPU
# check if nvidia gpus are doing something
echo -n "GPU load: "
gpu="0"
dkms status | grep nvidia &>/dev/null && gpu=$(nvidia-smi dmon -c 1|sed 1,2d|awk '{$1=$2=$3=$4=$9=$10="";sum+=$5+$6+$7+$8;} END{print sum}')
color "$gpu"
echo -e "$result"
r=$((r + gpu))
# exit 0 means rebootable
# the higher the return code the less reboot is recommended (1-255)
if [ $r -gt 255 ]; then r=255; fi
exit $r