slitaz-forge view pangolin/makegraphs @ rev 372

tank/makegraphs: add free blocks & connections
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Jun 09 22:46:11 2013 +0200 (2013-06-09)
parents 3fd9f645cdb0
children 5d839fde8a97
line source
1 #!/bin/sh
2 #*/5 * * * * /home/bellard/bin/makegraphs >/dev/null
4 # RRD database directory
5 rrdlog="/var/lib/pangolin/rrd"
7 # Images directory
8 rrdgraph="/home/slitaz/www/pangolin/pics/rrd"
10 # Colors
11 rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF"
12 rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
14 [ -d $rrdlog ] || mkdir -p $rrdlog
15 [ -d $rrdgraph ] || mkdir -p $rrdgraph
17 updatecpudata() {
18 [ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
19 DS:user:COUNTER:600:0:500000000 \
20 DS:nice:COUNTER:600:0:500000000 \
21 DS:system:COUNTER:600:0:500000000 \
22 DS:idle:COUNTER:600:0:500000000 \
23 DS:iowait:COUNTER:600:0:500000000 \
24 DS:irq:COUNTER:600:0:500000000 \
25 DS:softirq:COUNTER:600:0:500000000 \
26 DS:celsius:GAUGE:600:0:100000 \
27 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
28 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
29 grep '^cpu' /proc/stat | while read cpu user nice system idle iowait irq softirq misc; do
30 celsius=$(find /sys | grep /temp._input | xargs cat | \
31 awk '{ if ($0 > max) max=$0 } END { print max/1 }')
32 rrdtool update $rrdlog/cpu.rrd \
33 -t celsius:nice:user:system:idle:iowait:irq:softirq \
34 N:$celsius:$nice:$user:$system:$idle:$iowait:$irq:$softirq
35 break
36 done
37 }
39 updatecpugraph() {
40 period=$1
41 info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
42 | sed 's/ * / /g' | awk '
43 { s=$0 ; n++ }
44 END { if (n > 1) printf " %dx",n; print s }')"
45 rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
46 $rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
47 DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
48 DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
49 DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
50 DEF:nice=$rrdlog/cpu.rrd:nice:AVERAGE \
51 DEF:iowait=$rrdlog/cpu.rrd:iowait:AVERAGE \
52 DEF:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
53 'CDEF:total=user,system,idle,nice,iowait,+,+,+,+' \
54 'CDEF:userpct=100,user,total,/,*' \
55 'CDEF:systempct=100,system,total,/,*' \
56 'CDEF:idlepct=100,idle,total,/,*' \
57 'CDEF:nicepct=100,nice,total,/,*' \
58 'CDEF:iopct=100,iowait,total,/,*' \
59 'CDEF:temp=celsius,1000,/' \
60 'AREA:userpct#0000FF:user cpu' \
61 'STACK:nicepct#C0C0FF:nice cpu' \
62 'STACK:systempct#FF0000:system cpu' \
63 'STACK:iopct#FFFF00:I/O wait' \
64 'STACK:idlepct#00FF00:idle cpu' \
65 'LINE1:temp#000000:temperature\g' \
66 'GPRINT:temp:MAX:max %2.0lfC\j'
67 }
69 updatememgraph() {
70 period=$1
71 info="$(free | awk '\
72 { \
73 if (/Mem:/) { \
74 if ($2 < 10000) printf "%d KB",$2; \
75 else if ($2 < 10000000) printf "%d MB",$2/1024; \
76 else printf "%d GB",$2/1024/1024; \
77 } \
78 }')"
79 info2="$(free | awk '\
80 { \
81 if (/Swap:/) { \
82 if ($2 < 10000) printf "%d KB",$2; \
83 else if ($2 < 10000000) printf "%d MB",$2/1024; \
84 else printf "%d GB",$2/1024/1024; \
85 } \
86 }')"
87 rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
88 $rrdgraphargs -l 0 -u 100 \
89 -t "memory usage per $period [ $info + $info2 swap ]" \
90 DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
91 DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
92 DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
93 DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
94 DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
95 DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
96 DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
97 'CDEF:total=used,free,+' \
98 'CDEF:used2=used,buffer,cache,shared,+,+,-' \
99 'CDEF:usedpct=100,used2,total,/,*' \
100 'CDEF:sharedpct=100,shared,total,/,*' \
101 'CDEF:bufferpct=100,buffer,total,/,*' \
102 'CDEF:cachepct=100,cache,total,/,*' \
103 'CDEF:freepct=100,free,total,/,*' \
104 'CDEF:swtotal=swused,swfree,+' \
105 'CDEF:swusedpct=100,swused,swtotal,/,*' \
106 'AREA:usedpct#0000FF:used memory' \
107 'STACK:sharedpct#FF7F00:shared memory' \
108 'STACK:bufferpct#FF00FF:buffered memory' \
109 'STACK:cachepct#FFFF00:cached memory' \
110 'STACK:freepct#00FF00:free memory' \
111 'LINE2:swusedpct#FF0000:used swap\g' \
112 'GPRINT:swusedpct:MAX:%1.0lf%%\j'
113 }
115 updatememdata () {
116 [ -e "$rrdlog/mem.rrd" ] ||
117 rrdtool create "$rrdlog/mem.rrd" --step=300 \
118 DS:memused:ABSOLUTE:600:0:5000000000 \
119 DS:memfree:ABSOLUTE:600:0:5000000000 \
120 DS:memshared:ABSOLUTE:600:0:5000000000 \
121 DS:membuffers:ABSOLUTE:600:0:5000000000 \
122 DS:memcache:ABSOLUTE:600:0:5000000000 \
123 DS:swapused:ABSOLUTE:600:0:5000000000 \
124 DS:swapfree:ABSOLUTE:600:0:5000000000 \
125 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
126 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
128 while read tag count unit; do
129 case "$tag" in
130 MemTotal:) memtotal=$count;;
131 MemFree:) memfree=$count
132 memused=$(($memtotal - $memfree))
133 memshared=0;;
134 MemShared:) memshared=$count;;
135 Buffers:) membuffers=$count;;
136 Cached:) memcache=$count;;
137 SwapTotal:) swaptotal=$count;;
138 SwapFree:) swapfree=$count
139 swapused=$(( $swaptotal - $swapfree));;
140 esac
141 done < /proc/meminfo
143 rrdtool update "$rrdlog/mem.rrd" \
144 -t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
145 "N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
146 }
148 getmax() {
149 rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
150 BEGIN {max=0} \
151 /^[0-9]/ { \
152 if ($2 != "nan" && $2 > max) max=$2; \
153 if ($3 != "nan" && $3 > max) max=$3; \
154 } \
155 END { print max }' | sed 's/,/./'
156 }
158 updatediskgraph() {
159 period=$1
160 [ "$period" == "day" ] && maxdisk="$(getmax disk)"
161 info=""
162 [ -r $2 ] &&
163 info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
164 sed "s|Disk $2: \(.*\), .*|\1|") ]"
165 #--logarithmic --lower-limit 1 -v "Sectors/second" --units=si
166 rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
167 $rrdgraphargs -t "disk access per $period $info" \
168 -v "Sectors/second" --units=si \
169 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
170 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
171 DEF:blk=$rrdlog/usagedisk.rrd:bhome:AVERAGE \
172 DEF:ino=$rrdlog/usagedisk.rrd:ihome:AVERAGE \
173 "CDEF:readpct=100,read,$maxdisk,/,*" \
174 "CDEF:writepct=100,write,$maxdisk,/,*" \
175 'AREA:readpct#0000FF:sectors read from disk' \
176 'STACK:writepct#00FF00:sectors written to disk' \
177 'LINE1:ino#FF00FF:inodes used in /home\g' \
178 'GPRINT:ino:MAX:%1.0lf%%' \
179 'LINE1:blk#FF0000:blocks used in /home\g' \
180 'GPRINT:blk:MAX:%1.0lf%%\j'
181 }
183 updatediskdata() {
184 dev=$1
185 [ -e "$rrdlog/disk.rrd" ] ||
186 rrdtool create "$rrdlog/disk.rrd" --step=300 \
187 DS:readsect:COUNTER:600:0:5000000000 \
188 DS:writesect:COUNTER:600:0:5000000000 \
189 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
190 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
191 [ -e "$rrdlog/iodisk.rrd" ] ||
192 rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
193 DS:done:GAUGE:600:0:U DS:err:GAUGE:600:0:U \
194 DS:req:GAUGE:600:0:U \
195 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
196 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
197 [ -e "$rrdlog/usagedisk.rrd" ] ||
198 rrdtool create "$rrdlog/usagedisk.rrd" --step=300 \
199 DS:broot:GAUGE:600:0:U DS:iroot:GAUGE:600:0:U \
200 DS:bhome:GAUGE:600:0:U DS:ihome:GAUGE:600:0:U \
201 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
202 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
204 while read major minor name readreq readsect writereq writesect misc; do
205 [ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
206 [ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
207 rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
208 N:$readsect:$writesect
209 done < /proc/diskstats
210 disk=${dev:0:8}
211 dir=/sys/block/${disk#/dev/}/device
212 done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
213 err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
214 req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
215 rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
216 iroot=$(df -i / | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
217 broot=$(df / | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
218 ihome=$(df -i /home | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
219 bhome=$(df /home | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
220 rrdtool update "$rrdlog/usagedisk.rrd" -t broot:iroot:bhome:ihome N:$broot:$iroot:$bhome:$ihome
221 }
223 updateifgraph() {
224 interface=$1
225 period=$2
226 rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
227 $rrdgraphargs -t "traffic on $interface graph per $period" \
228 --logarithmic -A -v "Bytes/second" --units=si \
229 DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
230 DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
231 DEF:tcp=$rrdlog/proto-$interface.rrd:tcp:AVERAGE \
232 'AREA:incoming#00FF00:incoming traffic\g' \
233 'GPRINT:incoming:MAX:max%8.3lf %sBps' \
234 'LINE1:outgoing#0000FF:outgoing traffic\g' \
235 'GPRINT:outgoing:MAX:max%8.3lf %sBps' \
236 'LINE1:tcp#000000:connections\g' \
237 'GPRINT:tcp:MAX:max %2.0lf\j'
238 }
240 netframes() {
241 ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
242 }
244 netstats() {
245 ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
246 }
248 netproto()
249 {
250 proto=${1:-tcp}
251 if [ -n "$2" ]; then
252 netstat -an 2> /dev/null | grep -v '0.0.0.0:*' | grep "^$proto"| grep ":$2 " | wc -l
253 else
254 netstat -an 2> /dev/null | grep -v '0.0.0.0:*' | grep "^$proto"| wc -l
255 fi
256 }
258 updateifdata() {
259 interface=$1
260 [ -e "$rrdlog/$interface.rrd" ] ||
261 rrdtool create "$rrdlog/$interface.rrd" --step=300 \
262 DS:incoming:COUNTER:600:0:U \
263 DS:outgoing:COUNTER:600:0:U \
264 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
265 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
266 [ -e "$rrdlog/packets-$interface.rrd" ] ||
267 rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
268 DS:in:COUNTER:600:0:U DS:out:COUNTER:600:0:U \
269 DS:inerr:COUNTER:600:0:U DS:outerr:COUNTER:600:0:U \
270 DS:indrop:COUNTER:600:0:U DS:outdrop:COUNTER:600:0:U \
271 DS:inov:COUNTER:600:0:U DS:outov:COUNTER:600:0:U \
272 DS:frame:COUNTER:600:0:U DS:carrier:COUNTER:600:0:U \
273 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
274 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
275 [ -e "$rrdlog/proto-$interface.rrd" ] ||
276 rrdtool create "$rrdlog/proto-$interface.rrd" --step=300 \
277 DS:tcp:GAUGE:600:0:U DS:udp:GAUGE:600:0:U \
278 DS:rsync:GAUGE:600:0:U DS:www:GAUGE:600:0:U \
279 DS:ssh:GAUGE:600:0:U \
280 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
281 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
282 rx=$(netstats $interface RX)
283 tx=$(netstats $interface TX)
284 rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
285 N:${rx:-U}:${tx:-U}
286 rx=$(netframes $interface RX packets)
287 tx=$(netframes $interface TX packets)
288 rxerr=$(netframes $interface RX errors)
289 txerr=$(netframes $interface TX errors)
290 rxdrop=$(netframes $interface RX dropped)
291 txdrop=$(netframes $interface TX dropped)
292 rxov=$(netframes $interface RX overruns)
293 txov=$(netframes $interface TX overruns)
294 frame=$(netframes $interface RX frame)
295 carrier=$(netframes $interface TX carrier)
296 rrdtool update "$rrdlog/packets-$interface.rrd" \
297 -t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
298 N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
299 rsync=$(netproto tcp 873)
300 www=$(netproto tcp 80)
301 ssh=$(netproto tcp 22)
302 tcp=$(netproto tcp)
303 udp=$(netproto udp)
304 rrdtool update "$rrdlog/proto-$interface.rrd" \
305 -t tcp:udp:rsync:www:ssh \
306 N:${tcp:-U}:${udp:-U}:${rsync:-U}:${www:-U}:${ssh:-U}
307 }
309 getdisk()
310 {
311 local d
312 local i
313 d=$(stat -c %04D $1)
314 for i in /dev/* ; do
315 [ $(stat -c "%02t%02T" $i) == $d ] || continue
316 echo $i
317 break
318 done
319 }
321 ###
322 ### System graphs
323 ###
325 updatecpudata
326 updatecpugraph day
327 updatecpugraph week
328 updatecpugraph month
329 updatecpugraph year
331 updatememdata
332 updatememgraph day
333 updatememgraph week
334 updatememgraph month
335 updatememgraph year
337 if [ -e /proc/diskstats ]; then
338 disk=$(getdisk $0)
339 updatediskdata $disk
340 updatediskgraph day ${disk:0:8}
341 updatediskgraph week ${disk:0:8}
342 updatediskgraph month ${disk:0:8}
343 updatediskgraph year ${disk:0:8}
344 fi
346 iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
347 updateifdata $iface
348 updateifgraph $iface day
349 updateifgraph $iface week
350 updateifgraph $iface month
351 updateifgraph $iface year
353 #rsync --bwlimit=40 -t -e 'ssh -p 222 -i /home/bellard/.ssh/id_rsa' $rrdlog/* \
354 # bellard@mirror.slitaz.org:/var/spool/rrd/tank/ > /dev/null 2>&1
356 [ ! -s $rrdgraph/boot.html -o /var/log/boot.log -nt $rrdgraph/boot.html ] &&
357 cat > $rrdgraph/boot.html <<EOT
358 <html>
359 <body>
360 $(stat -c %y /var/log/dmesg.log | sed 's/\.0*//')
361 <span style="color: blue"><i>$(cat /proc/cmdline)</i></span>
362 <pre>
363 $(cat /var/log/dmesg.log /var/log/boot.log | \
364 sed -e 's/</\&lt;/g;s/>/\&gt;/g' -e 's/.*\]R//' -e 's/.*\[?8h//' \
365 -e 's|.\[1m|<b>|' -e 's|.\[0m|</b>|' -e 's|.\[[0-9][0-9Gm;]*||g' \
366 -e ':a;s/^\(.\{1,68\}\)\(\[ [A-Za-z]* \]\)/\1 \2/;ta' \
367 -e 's#\[ OK \]#[ <span style="color: green">OK</span> ]#' \
368 -e 's#\[ Failed \]#[ <span style="color: red">Failed</span> ]#' \
369 -e 's|No such .*|<span style="color: red">&</span>|' \
370 -e 's|ERROR .*|<span style="color: red">&</span>|' \
371 -e 's|command line: \(.*\)|command line: <span style="color: blue">\1</span>|' \
372 )
373 </pre>
374 </body>
375 </html>
376 EOT