Codebase list openrc / d2ce07e
Add rc-sstat script The rc-sstat script is written to display status of s6 services and run rc-status to display all services status. This currently only works on Linux. William Hubbs 8 years ago
5 changed file(s) with 210 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
0 MK= ../mk
1 include ${MK}/sys.mk
2 include ${MK}/os.mk
3
04 MAN3= einfo.3 \
15 rc_config.3 rc_deptree.3 rc_find_pids.3 rc_plugin_hook.3 \
26 rc_runlevel.3 rc_service.3 rc_stringlist.3
37 MAN8= rc-service.8 rc-status.8 rc-update.8 openrc.8 openrc-run.8 \
48 service.8 start-stop-daemon.8
9
10 ifeq (${OS},Linux)
11 MAN8 += rc-sstat.8
12 endif
513
614 # Handy macro to create symlinks
715 # This does rely on correctly formatting our manpages!
1523 fi; \
1624 done;
1725
18 MK= ../mk
19 include ${MK}/sys.mk
2026 include ${MK}/gitignore.mk
2127
2228 all:
0 .\" Copyright (c) 2015 William Hubbs
1 .\"
2 .\" Redistribution and use in source and binary forms, with or without
3 .\" modification, are permitted provided that the following conditions
4 .\" are met:
5 .\" 1. Redistributions of source code must retain the above copyright
6 .\" notice, this list of conditions and the following disclaimer.
7 .\" 2. Redistributions in binary form must reproduce the above copyright
8 .\" notice, this list of conditions and the following disclaimer in the
9 .\" documentation and/or other materials provided with the distribution.
10 .\"
11 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
12 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
20 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21 .\" SUCH DAMAGE.
22 .\"
23 .Dd April 24, 2008
24 .Dt RC-sstat 8 SMM
25 .Os OpenRC
26 .Sh NAME
27 .Nm rc-sstat
28 .Nd show status info about services supervised by s6 then rc-status
29 info
30 .Sh SYNOPSIS
31 .Nm
32 .Sh DESCRIPTION
33 .Nm
34 gathers and displays information about the status of services supervised
35 by s6 then runs rc-status to show info about nnormal OpenRC services.
36 .Pp
37 .Sh EXIT STATUS
38 .Nm
39 exits 1 if there is an internal error or exits with the same exit codes
40 as rc-status.
41 .Sh SEE ALSO
42 .Xr rc-status 8 ,
43 .Xr rc-update 8
44 .Sh AUTHORS
45 .An William Hubbs <w.d.hubbs@gmail.com>
0 MK= ../mk
1 include ${MK}/os.mk
2
03 DIR= ${LIBEXECDIR}/bin
14 BIN= on_ac_power
5 INSTALLAFTER = _installafter
26
3 MK= ../mk
7 ifeq (${OS},Linux)
8 SRCS+= rc-sstat.in
9 BIN+= rc-sstat
10 endif
11
12 _installafter:
13 ifeq (${OS},Linux)
14 ${INSTALL} -d ${DESTDIR}${SBINDIR}
15 ln -s ${DIR}/rc-sstat ${DESTDIR}/${SBINDIR}/rc-sstat
16 endif
17
418 include ${MK}/scripts.mk
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