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