slitaz-forge view chub/web/lib/makegraphs @ rev 377

tank has moved
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Sep 24 10:18:22 2013 +0000 (2013-09-24)
parents d2d8931bb183
children 061a079077a3
line source
1 #!/bin/sh
2 #*/5 * * * * /home/slitaz/www/chub/lib/makegraphs >/dev/null
4 # RRD database directory
5 rrdlog="/home/slitaz/www/chub/log/rrd"
7 # Images directory
8 rrdgraph="/home/slitaz/www/chub/images/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:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
52 'CDEF:total=user,system,idle,nice,+,+,+' \
53 'CDEF:userpct=100,user,total,/,*' \
54 'CDEF:systempct=100,system,total,/,*' \
55 'CDEF:idlepct=100,idle,total,/,*' \
56 'CDEF:nicepct=100,nice,total,/,*' \
57 'CDEF:temp=celsius,1000,/' \
58 'AREA:userpct#0000FF:user cpu usage' \
59 'STACK:nicepct#C0C0FF:nice cpu usage' \
60 'STACK:systempct#FF0000:system cpu usage' \
61 'STACK:idlepct#00FF00:idle cpu usage' \
62 'LINE1:temp#000000:temperature\g' \
63 'GPRINT:temp:MAX:max %2.0lfC\j'
64 }
66 updatememgraph() {
67 period=$1
68 info="$(free | awk '\
69 { \
70 if (/Mem:/) { \
71 if ($2 < 10000) printf "%d KB",$2; \
72 else if ($2 < 10000000) printf "%d MB",$2/1024; \
73 else printf "%d GB",$2/1024/1024; \
74 } \
75 }')"
76 info2="$(free | awk '\
77 { \
78 if (/Swap:/) { \
79 if ($2 < 10000) printf "%d KB",$2; \
80 else if ($2 < 10000000) printf "%d MB",$2/1024; \
81 else printf "%d GB",$2/1024/1024; \
82 } \
83 }')"
84 rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
85 $rrdgraphargs -l 0 -u 100 \
86 -t "memory usage per $period [ $info + $info2 swap ]" \
87 DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
88 DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
89 DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
90 DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
91 DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
92 DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
93 DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
94 'CDEF:total=used,free,+' \
95 'CDEF:used2=used,buffer,cache,shared,+,+,-' \
96 'CDEF:usedpct=100,used2,total,/,*' \
97 'CDEF:sharedpct=100,shared,total,/,*' \
98 'CDEF:bufferpct=100,buffer,total,/,*' \
99 'CDEF:cachepct=100,cache,total,/,*' \
100 'CDEF:freepct=100,free,total,/,*' \
101 'CDEF:swtotal=swused,swfree,+' \
102 'CDEF:swusedpct=100,swused,swtotal,/,*' \
103 'AREA:usedpct#0000FF:used memory' \
104 'STACK:sharedpct#FF7F00:shared memory' \
105 'STACK:bufferpct#FF00FF:buffered memory' \
106 'STACK:cachepct#FFFF00:cached memory' \
107 'STACK:freepct#00FF00:free memory' \
108 'LINE2:swusedpct#FF0000:used swap\j'
109 }
111 updatememdata () {
112 [ -e "$rrdlog/mem.rrd" ] ||
113 rrdtool create "$rrdlog/mem.rrd" --step=300 \
114 DS:memused:ABSOLUTE:600:0:5000000000 \
115 DS:memfree:ABSOLUTE:600:0:5000000000 \
116 DS:memshared:ABSOLUTE:600:0:5000000000 \
117 DS:membuffers:ABSOLUTE:600:0:5000000000 \
118 DS:memcache:ABSOLUTE:600:0:5000000000 \
119 DS:swapused:ABSOLUTE:600:0:5000000000 \
120 DS:swapfree:ABSOLUTE:600:0:5000000000 \
121 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
122 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
124 while read tag count unit; do
125 case "$tag" in
126 MemTotal:) memtotal=$count;;
127 MemFree:) memfree=$count
128 memused=$(($memtotal - $memfree))
129 memshared=0;;
130 MemShared:) memshared=$count;;
131 Buffers:) membuffers=$count;;
132 Cached:) memcache=$count;;
133 SwapTotal:) swaptotal=$count;;
134 SwapFree:) swapfree=$count
135 swapused=$(( $swaptotal - $swapfree));;
136 esac
137 done < /proc/meminfo
139 rrdtool update "$rrdlog/mem.rrd" \
140 -t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
141 "N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
142 }
144 getmax() {
145 rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
146 BEGIN {max=0} \
147 /^[0-9]/ { \
148 if ($2 != "nan" && $2 > max) max=$2; \
149 if ($3 != "nan" && $3 > max) max=$3; \
150 } \
151 END { print max }' | sed 's/,/./'
152 }
154 updatediskgraph() {
155 period=$1
156 [ "$period" == "day" ] && maxdisk="$(getmax disk)"
157 info=""
158 [ -r $2 ] &&
159 info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
160 sed "s|Disk $2: \(.*\), .*|\1|") ]"
161 if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
162 err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
163 done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
164 rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
165 [ $err -eq 0 ] && rate="0"
166 # --right-axis-label "I/O state %"
167 # BUG workaround: swap readsect and writesect :-/
168 rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
169 $rrdgraphargs -t "disk access per $period $info" \
170 --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
171 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
172 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
173 DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
174 DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
175 DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
176 "CDEF:readpct=100,read,$maxdisk,/,*" \
177 "CDEF:writepct=100,write,$maxdisk,/,*" \
178 "CDEF:errpct=100,err,req,/,*" \
179 "CDEF:donepct=100,done,req,/,*" \
180 "CDEF:errrate=err,done,/" \
181 'AREA:readpct#0000FF:sectors written from disk' \
182 "COMMENT:I/O error rate $rate" \
183 'STACK:writepct#00FF00:sectors read to disk' \
184 'LINE2:donepct#FFFF00:% I/O complete' \
185 'LINE2:errpct#FF0000:% I/O error\j'
186 else
187 rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
188 $rrdgraphargs -t "disk access per $period $info" \
189 --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
190 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
191 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
192 "CDEF:readpct=100,read,$maxdisk,/,*" \
193 "CDEF:writepct=100,write,$maxdisk,/,*" \
194 'AREA:readpct#0000FF:sectors written from disk' \
195 'STACK:writepct#00FF00:sectors read to disk'
196 fi
197 }
199 updatediskdata() {
200 dev=$1
201 [ -e "$rrdlog/disk.rrd" ] ||
202 rrdtool create "$rrdlog/disk.rrd" --step=300 \
203 DS:readsect:COUNTER:600:0:5000000000 \
204 DS:writesect:COUNTER:600:0:5000000000 \
205 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
206 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
207 [ -e "$rrdlog/iodisk.rrd" ] ||
208 rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
209 DS:done:GAUGE:600:0:U DS:err:GAUGE:600:0:U \
210 DS:req:GAUGE:600:0:U \
211 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
212 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
214 while read major minor name readreq readsect writereq writesect misc; do
215 [ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
216 [ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
217 rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
218 N:$readsect:$writesect
219 done < /proc/diskstats
220 disk=${dev:0:8}
221 dir=/sys/block/${disk#/dev/}/device
222 done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
223 err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
224 req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
225 rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
226 }
228 updateifgraph() {
229 interface=$1
230 period=$2
231 rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
232 $rrdgraphargs -t "traffic on $interface graph per $period" \
233 --logarithmic -A -v "Bytes/second" --units=si \
234 DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
235 DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
236 'AREA:incoming#00FF00:incoming traffic' \
237 'GPRINT:incoming:MAX:max input%8.3lf %sBps' \
238 'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \
239 'LINE1:outgoing#0000FF:outgoing traffic\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 updateifdata() {
251 interface=$1
252 [ -e "$rrdlog/$interface.rrd" ] ||
253 rrdtool create "$rrdlog/$interface.rrd" --step=300 \
254 DS:incoming:COUNTER:600:0:U \
255 DS:outgoing:COUNTER:600:0:U \
256 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
257 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
258 [ -e "$rrdlog/packets-$interface.rrd" ] ||
259 rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
260 DS:in:COUNTER:600:0:U DS:out:COUNTER:600:0:U \
261 DS:inerr:COUNTER:600:0:U DS:outerr:COUNTER:600:0:U \
262 DS:indrop:COUNTER:600:0:U DS:outdrop:COUNTER:600:0:U \
263 DS:inov:COUNTER:600:0:U DS:outov:COUNTER:600:0:U \
264 DS:frame:COUNTER:600:0:U DS:carrier:COUNTER:600:0:U \
265 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
266 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
267 rx=$(netstats $interface RX)
268 tx=$(netstats $interface TX)
269 rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
270 N:${rx:-U}:${tx:-U}
271 rx=$(netframes $interface RX packets)
272 tx=$(netframes $interface TX packets)
273 rxerr=$(netframes $interface RX errors)
274 txerr=$(netframes $interface TX errors)
275 rxdrop=$(netframes $interface RX dropped)
276 txdrop=$(netframes $interface TX dropped)
277 rxov=$(netframes $interface RX overruns)
278 txov=$(netframes $interface TX overruns)
279 frame=$(netframes $interface RX frame)
280 carrier=$(netframes $interface TX carrier)
281 rrdtool update "$rrdlog/packets-$interface.rrd" \
282 -t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
283 N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
284 }
286 getdisk()
287 {
288 local d
289 local i
290 d=$(stat -c %04D $1)
291 for i in /dev/* ; do
292 [ $(stat -c "%02t%02T" $i) == $d ] || continue
293 echo $i
294 break
295 done
296 }
298 ###
299 ### System graphs
300 ###
302 updatecpudata
303 updatecpugraph day
304 updatecpugraph week
305 updatecpugraph month
306 updatecpugraph year
308 updatememdata
309 updatememgraph day
310 updatememgraph week
311 updatememgraph month
312 updatememgraph year
314 if [ -e /proc/diskstats ]; then
315 disk=$(getdisk $0)
316 updatediskdata $disk
317 updatediskgraph day ${disk:0:8}
318 updatediskgraph week ${disk:0:8}
319 updatediskgraph month ${disk:0:8}
320 updatediskgraph year ${disk:0:8}
321 fi
323 iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
324 updateifdata $iface
325 updateifgraph $iface day
326 updateifgraph $iface week
327 updateifgraph $iface month
328 updateifgraph $iface year