Cobalt Status LCD, NetBSD Version

Here is a NetBSD/Cobalt version of my Cobalt Qube/Raq LCD status screen updater.

The Debian linux version is described in an earlier post on this blog.

This script works fine on NetBSD/Cobalt 3.0, I haven’t tested it on other versions yet.

You can view or download the new NetBSD version below:

(lcdbanner-netbsd.sh) download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/pkg/bin/bash

# Cobalt LCD status screen updater, NetBSD version
# Hessel Schut, hessel@isquared.nl, 2008-11-09


# VARIABLES

lcdv_sleep=5           # time between updates
lcdv_tfmt="%a %d %b %R"      # timestamp format, see strftime(3)

lcdv_width=16          # number of display columns 
              # (you shouldn't need to change this)

lcdv_device=/dev/lcd       # LCD/ Panel device

# Display format definitions follow below, each format should be written
# as a function with a unique identifier starting with lcdf_, so valid
# identifiers would be lcdf_foo and lcdf_bar or even lcdf_3_baz73.

# A display format function may return one or two lines, if one line is
# returned, the first line of the display will be used to display the 
# time of day using the lcdv_tfmt variable.

# Display definitions will be displayed in alphabetical order.

## DISPLAY DEFINITIONS ################################################

function lcdf_load {
  # display 1m loadavg:
  echo Load: $(uptime | awk -F, '{print $(NF-1)}')
}

function lcdf_host_version {
  # display hostname + OS version
  echo Host: $(hostname)
  echo "("$(uname -rs)")"
}

# function lcdf_mem_free {
#     # display free real memory (excl. swap)
#     local m=$(free -k | gawk '
#         /^Mem/ {
#             printf("%.2f kB", ($4)/1024);
#         }
#     ')
#     echo Free: $m
# }

function lcdf_users {
  # display count of logged in users
  local t=0 i=0
  who | cut -d\  -f1 | sort | uniq -c | {
          while read l
          do
                  ((t+=${l/ *}))    # sum user session counts
          ((i++))       # sum unique users
          done
          echo $i users
      echo $t total
  }
}

function lcdf_cpu_stat {
  # display cpu usage
  vmstat | awk '
     NR > 1 { 
         printf("%-2s %-2s %-2s %-2s %-2s\n", 
             $(NF-4),$(NF-3),$(NF-2),$(NF-1),$NF)
     }
 '
}

#######################################################################

# debug flag
_lcd_dbg=0

# get all display function definitions
# list is formatted as: numfunc func-1 func-2 ... func-n
_lcd_funclist=$(declare -f | awk '
     BEGIN { i=0 }
     /^lcdf_/ { 
         dfuncs=dfuncs" "$1;
         i++;
     } 
     END { print i dfuncs }
 ' )

# strip numfunc from funclist
_lcd_numfuncs=${_lcd_funclist/ *} # car 
_lcd_funclist=${_lcd_funclist#* } # cdr ;)

# (infinite) main loop
while [ == ]
do
  # iterate over all defined display definitions
  for func in ${_lcd_funclist}
  do
      # update timestamp
      _lcd_timestamp=$(date +"${lcdv_tfmt}")
  
      # (re-)initialize display array
      _lcd[1]=''; _lcd[2]=''
      ${func} | {
          # get display function output for at most two rows
          while read line && [ -z "${_lcd[2]}" ]
          do
              [ -z "${_lcd[1]}" ] && _lcd[1]=${line} || _lcd[2]=${line}
          done
          # swap top and bottom when bottom is empty and use timestamp as top
          if [ -z "${_lcd[2]}" ]
          then
              _lcd[2]=${_lcd[1]}
              _lcd[1]=${_lcd_timestamp}
          fi

          # pad lcd rows to lcdv_width and clip long ones
          [ ${#_lcd[1]} -le ${lcdv_width} ] && {
              i=$((${lcdv_width} - ${#_lcd[1]}))
              while [ ${i} -ge 0 ]
              do
                  _lcd[1]="${_lcd[1]} "
                  ((i--))
              done;
          } || _lcd[1]=${_lcd[1]:0:16}

          [ ${#_lcd[2]} -le ${lcdv_width} ] && {
              i=$((${lcdv_width} - ${#_lcd[2]}))
              while [ ${i} -ge 0 ]
              do
                  _lcd[2]="${_lcd[2]} "
                  ((i--))
              done;
          } || _lcd[2]=${_lcd[2]:0:16}

          # concatenate display lines and send to display
          [ ${_lcd_dbg} == 0 ] && {
              echo "${_lcd[1]}${_lcd[2]}" > ${lcdv_device};
          } || { echo "${_lcd[1]}"; echo "${_lcd[2]}"; }


      }
      # nap for lcdv_sleep seconds
      sleep ${lcdv_sleep}
  done
done

Comments