-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathruptime
executable file
·79 lines (71 loc) · 1.92 KB
/
ruptime
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
#!/usr/bin/env bash
# ruptime -- collect uptime information from multiple hosts
#
# Inspired by the RPC-based 'rup' from SunOS 4 manual.
. lib.bash || exit
usage() {
echo "Usage: $progname [-H HOSTS] [-S] [-d|-m]"
echo
echo_opt "-H HOSTS" "list of hosts to query"
echo_opt "-S" "connect as superuser (root)"
echo_opt "-d" "query root filesystem usage"
echo_opt "-m" "query memory usage"
}
hosts=
user=
what=kernel
while getopts ":H:Sdm" OPT; do
case $OPT in
H) hosts=${OPTARG//,/ };;
S) user=root@;;
d) what=disk;;
m) what=mem;;
*) lib:die_getopts;;
esac
done; shift $((OPTIND-1))
if (( $# )); then
die "Unrecognized arguments '$*'"
fi
hosts=$(rlisthosts "$hosts") || exit
getdata() {
local user=$1
local hosts=$2
local cmd=$3
(for host in $hosts; do
ssh $user$host "$cmd" &
done; wait)
}
case $what in
kernel)
cmd='printf "%s\t%s\t%s\t%s\t%s\n" \
"$(hostname)" \
"$(uname -r)" \
"$(uptime | sed -E "s/.* up ([0-9]+ days).*/\1/")" \
"$(uptime | sed -E "s/.*load average: //")" \
'
getdata "$user" "$hosts" "$cmd" | sort -V -k 2,1 | column -s $'\t' -t \
--table-columns "HOST,KERNEL,UPTIME,LOADAVG"
;;
mem)
cmd='printf "%s\t%s\t%s\t%s\t%s\n" \
"$(hostname)" \
"$(free -b | awk "/^Mem:/{printf \"%.0f%%\", \$3*100/\$2}")" \
"$(free -b | awk "/^Mem:/{print \$7}" | numfmt --to=iec)" \
"$(free -b | awk "/^Mem:/{print \$2}" | numfmt --to=iec)" \
'
getdata "$user" "$hosts" "$cmd" | sort | column -s $'\t' -t \
--table-columns "HOST,MEM USE,MEM FREE,MEM TOTAL" \
--table-right "MEM USE,MEM FREE,MEM TOTAL"
;;
disk)
cmd='printf "%s\t%s\t%s\t%s\n" \
"$(hostname)" \
"$(df -h / | awk "NR==2{print \$5}")" \
"$(df -h / | awk "NR==2{print \$4}")" \
"$(df -h / | awk "NR==2{print \$2}")" \
'
getdata "$user" "$hosts" "$cmd" | sort | column -s $'\t' -t \
--table-columns "HOST,DISK USE,DISK FREE,DISK TOTAL" \
--table-right "DISK USE,DISK FREE,DISK TOTAL"
;;
esac