slitaz-forge view mirror/info/bin/makegraphs @ rev 393

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