|
0 |
#!@SHELL@
|
|
1 |
|
|
2 |
# Define variables
|
|
3 |
scandir="/run/openrc/s6-scan"
|
|
4 |
statfile=/dev/shm/s6-svstat.${USER}
|
|
5 |
|
|
6 |
color_red='\E[01;31m'
|
|
7 |
color_green='\E[32m'
|
|
8 |
color_yellow='\E[01;33m'
|
|
9 |
|
|
10 |
# Time Modules
|
|
11 |
uptimeModules() {
|
|
12 |
# Given a single integer argument representing seconds of uptime...
|
|
13 |
# convert uptime to a friendly human readable string: '2d 16h 58m 46s'
|
|
14 |
# define a variable to keep track of the longest length uptime string
|
|
15 |
uSec=${1:-0}
|
|
16 |
|
|
17 |
uDay=$(( $uSec / 86400 ))
|
|
18 |
uSec=$(( $uSec % 86400 ))
|
|
19 |
uHour=$(( $uSec / 3600 ))
|
|
20 |
uSec=$(( $uSec % 3600 ))
|
|
21 |
uMin=$(( $uSec / 60 ))
|
|
22 |
uSec=$(( $uSec % 60 ))
|
|
23 |
|
|
24 |
[ $uDay -ne 0 ] && pDay="${uDay}d " || pDay=""
|
|
25 |
[ $uHour -ne 0 ] && pHour="${uHour}h " || pHour=""
|
|
26 |
[ $uMin -ne 0 ] && pMin="${uMin}m " || pMin=""
|
|
27 |
[ $uSec -ne 0 ] && pSec="${uSec}s " || pSec=""
|
|
28 |
|
|
29 |
parsedUptime="$( echo ${pDay}${pHour}${pMin}${pSec} | sed 's#[ \t]*$##' )"
|
|
30 |
uCharCount=${#parsedUptime}
|
|
31 |
}
|
|
32 |
|
|
33 |
# Make sure we are running as root
|
|
34 |
if [ $(id -u) != 0 ]; then
|
|
35 |
printf "This command must be run as root\n"
|
|
36 |
exit 1
|
|
37 |
fi
|
|
38 |
|
|
39 |
# Make sure scandir exists
|
|
40 |
if [ ! -d $scandir ]; then
|
|
41 |
printf "%s\n" "$scandir does not exist"
|
|
42 |
exit 1
|
|
43 |
fi
|
|
44 |
|
|
45 |
# Make sure s6-svscan is running
|
|
46 |
if ! pgrep s6-svscan >/dev/null ; then
|
|
47 |
printf "s6-svscan is not running\n"
|
|
48 |
exit 1
|
|
49 |
fi
|
|
50 |
|
|
51 |
# If TERM is undefined (launching sstat through an ssh command) then make it vt100
|
|
52 |
if [ -z $TERM -o $TERM = "dumb" ]; then
|
|
53 |
export TERM=vt100
|
|
54 |
fi
|
|
55 |
|
|
56 |
# Gather list of candidate services s6-supervise may be supervising
|
|
57 |
# filter for folders and symlinks at /run/openrc/s6-scan/* ommiting output starting with '.'
|
|
58 |
services="$(find $scandir -maxdepth 1 -mindepth 1 \( -type d -or -type l \) | awk -F'/' '{ if ( $NF !~ "^\\." ) print $NF}')"
|
|
59 |
if [ -z "$services" ]; then
|
|
60 |
printf "s6 found no services configured for supervision\n"
|
|
61 |
exit 1
|
|
62 |
fi
|
|
63 |
|
|
64 |
# Gather status for each service from s6-svstat
|
|
65 |
# write to tmp file in memory for non I/O bound repeatative access
|
|
66 |
rm -f $statfile 2>/dev/null
|
|
67 |
for service in $services ; do
|
|
68 |
echo "$service $(s6-svstat ${scandir}/${service})" >> $statfile
|
|
69 |
done
|
|
70 |
|
|
71 |
# Define longest string from parsed uptime (default to 7 to match string length of 'Up Time')
|
|
72 |
timeStringLength=7
|
|
73 |
for uptime in $(awk '$2 == "up" {print $5}' $statfile | sort -run)
|
|
74 |
do
|
|
75 |
uptimeModules $uptime
|
|
76 |
[ ${uCharCount} -gt $timeStringLength ] && timeStringLength=$uCharCount
|
|
77 |
done
|
|
78 |
|
|
79 |
|
|
80 |
# Print the status header like so...
|
|
81 |
# Service Name State PID Up Time Start Time
|
|
82 |
#---------------------------- ----- ----- -------------- -------------------
|
|
83 |
printf "\n"
|
|
84 |
printf "%28s %5s %5s %${timeStringLength}s %19s\n" "Service Name" "State" "PID" "Up Time" "Start Time"
|
|
85 |
for dashes in 28 5 5 $timeStringLength 19 ; do
|
|
86 |
printf "%0.s-" $(seq 1 $dashes) ; echo -n ' '
|
|
87 |
done && printf "\n"
|
|
88 |
|
|
89 |
|
|
90 |
# sshd up (pid 26300) 80373 seconds
|
|
91 |
cat $statfile | \
|
|
92 |
while read line
|
|
93 |
do
|
|
94 |
set $line
|
|
95 |
|
|
96 |
service=$1
|
|
97 |
state=$2
|
|
98 |
pid=${4/)/}
|
|
99 |
time=$5
|
|
100 |
|
|
101 |
# call function to convert time in seconds and define additional variables
|
|
102 |
uptimeModules $time
|
|
103 |
|
|
104 |
if [ "$state" = up ]; then
|
|
105 |
if [ $time -lt 30 ]; then
|
|
106 |
# uptime < 30 seconds, color the whole line yellow
|
|
107 |
echo -en "$color_yellow"
|
|
108 |
# 1st 4 columns are printed with printf for space padding
|
|
109 |
printf "%28s %5s %5s %${timeStringLength}s" $service $state $pid "$parsedUptime"
|
|
110 |
# 4th column is output from date -d
|
|
111 |
echo -e " $(date -d "${time} seconds ago" "+%F %T")"
|
|
112 |
# reset terminal colors
|
|
113 |
tput sgr0
|
|
114 |
else
|
|
115 |
printf "%28s" $service
|
|
116 |
# uptime > 30 seconds, color just the "state" value green
|
|
117 |
echo -en "$color_green"
|
|
118 |
printf " %5s" $state
|
|
119 |
# reset terminal colors
|
|
120 |
tput sgr0
|
|
121 |
printf " %5s" $pid
|
|
122 |
printf " %${timeStringLength}s" "$parsedUptime"
|
|
123 |
echo -e " $(date -d "${time} seconds ago" "+%F %T")"
|
|
124 |
fi
|
|
125 |
else
|
|
126 |
printf "%28s" $service
|
|
127 |
echo -en "$color_red"
|
|
128 |
printf " %5s" $state
|
|
129 |
tput sgr0
|
|
130 |
echo ""
|
|
131 |
fi
|
|
132 |
done
|
|
133 |
|
|
134 |
# Cleanup
|
|
135 |
rm -f $statfile 2>/dev/null
|
|
136 |
|
|
137 |
printf "\n\n"
|
|
138 |
|
|
139 |
rc-status
|