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