JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
merge upstream
[spectrwm.git] / baraction.sh
1 #!/bin/sh
2 #
3
4 print_date() {
5         # The date is printed to the status bar by default.
6         # To print the date through this script, set clock_enabled to 0
7         # in spectrwm.conf.  Uncomment "print_date" below.
8         FORMAT="%a %b %d %R %Z %Y"
9         DATE=`date "+${FORMAT}"`
10         echo -n "${DATE}     "
11 }
12
13 print_mem() {
14         MEM=`/usr/bin/top | grep Free: | cut -d " " -f6`
15         echo -n "Free mem: $MEM  "
16 }
17
18 _print_cpu() {
19         typeset -R4 _1=${1} _2=${2} _3=${3} _4=${4} _5=${5}
20         echo -n "CPU:${_1}% User${_2}% Nice${_3}% Sys${_4}% Int${_5}% Idle  "
21 }
22
23 print_cpu() {
24         OUT=""
25         # iostat prints each column justified to 3 chars, so if one counter
26         # is 100, it jams up agains the preceeding one. sort this out.
27         while [ "${1}x" != "x" ]; do
28                 if [ ${1} -gt 99 ]; then
29                         OUT="$OUT ${1%100} 100"
30                 else
31                         OUT="$OUT ${1}"
32                 fi
33                 shift;
34         done
35         _print_cpu $OUT
36 }
37
38 print_apm() {
39         BAT_STATUS=$1
40         BAT_LEVEL=$2
41         AC_STATUS=$3
42
43         if [ $AC_STATUS -ne 255 -o $BAT_STATUS -lt 4 ]; then
44                 if [ $AC_STATUS -eq 0 ]; then
45                         echo -n "on battery (${BAT_LEVEL}%)"
46                 else
47                         case $AC_STATUS in
48                         1)
49                                 AC_STRING="on AC: "
50                                 ;;
51                         2)
52                                 AC_STRING="on backup AC: "
53                                 ;;
54                         *)
55                                 AC_STRING=""
56                                 ;;
57                         esac;
58                         case $BAT_STATUS in
59                         4)
60                                 BAT_STRING="(no battery)"
61                                 ;;
62                         [0-3])
63                                 BAT_STRING="(battery ${BAT_LEVEL}%)"
64                                 ;;
65                         *)
66                                 BAT_STRING="(battery unknown)"
67                                 ;;
68                         esac;
69                 
70                         FULL="${AC_STRING}${BAT_STRING}"
71                         if [ "$FULL" != "" ]; then
72                                 echo -n "$FULL"
73                         fi
74                 fi
75         fi
76 }
77
78 print_cpuspeed() {
79         CPU_SPEED=`/sbin/sysctl hw.cpuspeed | cut -d "=" -f2`
80         echo -n "CPU speed: $CPU_SPEED MHz  "
81 }
82
83 while :; do
84         # instead of sleeping, use iostat as the update timer.
85         # cache the output of apm(8), no need to call that every second.
86         /usr/sbin/iostat -C -c 3600 |&  # wish infinity was an option
87         PID="$!"
88         APM_DATA=""
89         I=0
90         trap "kill $PID; exit" TERM
91         while read -p; do
92                 if [ $(( ${I} % 1 )) -eq 0 ]; then
93                         APM_DATA=`/usr/sbin/apm -alb`
94                 fi
95                 if [ $I -ge 2 ]; then
96                         # print_date
97                         print_mem $MEM
98                         print_cpu $REPLY
99                         print_cpuspeed
100                         print_apm $APM_DATA
101                         echo ""
102                 fi
103                 I=$(( ( ${I} + 1 ) % 22 ));
104         done
105 done