slitaz-forge view tank/web/bin/makegraphs @ rev 12

Add tank/web folder.
author Christopher Rogers <slaxemulator@gmail.com>
date Wed Feb 23 22:16:38 2011 +0000 (2011-02-23)
parents
children 08ebcbf62731
line source
1 #!/bin/sh
2 #*/5 * * * * /home/slitaz/www/tank/bin/makegraphs >/dev/null
4 # RRD database directory
5 rrdlog="/home/slitaz/www/tank/rrd"
7 # Images directory
8 rrdgraph="/home/slitaz/www/tank/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:system:COUNTER:600:0:500000000 \
21 DS:idle:COUNTER:600:0:500000000 \
22 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
23 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
24 grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
25 rrdtool update $rrdlog/cpu.rrd -t user:system:idle \
26 N:$(( $user + $nice )):$system:$idle
27 break
28 done
30 [ -e "$rrdlog/cpu2.rrd" ] &&
31 grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
32 rrdtool update $rrdlog/cpu2.rrd -t nice:user:system:idle \
33 N:$nice:$user:$system:$idle
34 break
35 done
36 }
38 updatecpugraph() {
39 period=$1
40 info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
41 | sed 's/ * / /g' | awk '
42 { s=$0 ; n++ }
43 END { if (n > 1) printf " %dx",n; print s }')"
44 rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
45 $rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
46 DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
47 DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
48 DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
49 'CDEF:total=user,system,idle,+,+' \
50 'CDEF:userpct=100,user,total,/,*' \
51 'CDEF:systempct=100,system,total,/,*' \
52 'CDEF:idlepct=100,idle,total,/,*' \
53 'AREA:userpct#0000FF:user cpu usage' \
54 'STACK:systempct#FF0000:system cpu usage' \
55 'STACK:idlepct#00FF00:idle cpu usage\j'
56 }
58 updatememgraph() {
59 period=$1
60 info="$(free | awk '\
61 { \
62 if (/Mem:/) { \
63 if ($2 < 10000) printf "%d KB",$2; \
64 else if ($2 < 10000000) printf "%d MB",$2/1024; \
65 else printf "%d GB",$2/1024/1024; \
66 } \
67 }')"
68 info2="$(free | awk '\
69 { \
70 if (/Swap:/) { \
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 rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
77 $rrdgraphargs -l 0 -u 100 \
78 -t "memory usage per $period [ $info + $info2 swap ]" \
79 DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
80 DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
81 DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
82 DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
83 DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
84 DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
85 DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
86 'CDEF:total=used,free,+' \
87 'CDEF:used2=used,buffer,cache,shared,+,+,-' \
88 'CDEF:usedpct=100,used2,total,/,*' \
89 'CDEF:sharedpct=100,shared,total,/,*' \
90 'CDEF:bufferpct=100,buffer,total,/,*' \
91 'CDEF:cachepct=100,cache,total,/,*' \
92 'CDEF:freepct=100,free,total,/,*' \
93 'CDEF:swtotal=swused,swfree,+' \
94 'CDEF:swusedpct=100,swused,swtotal,/,*' \
95 'AREA:usedpct#0000FF:used memory' \
96 'STACK:sharedpct#FF7F00:shared memory' \
97 'STACK:bufferpct#FF00FF:buffered memory' \
98 'STACK:cachepct#FFFF00:cached memory' \
99 'STACK:freepct#00FF00:free memory' \
100 'LINE2:swusedpct#FF0000:used swap\j'
101 }
103 updatememdata () {
104 [ -e "$rrdlog/mem.rrd" ] ||
105 rrdtool create "$rrdlog/mem.rrd" --step=300 \
106 DS:memused:ABSOLUTE:600:0:5000000000 \
107 DS:memfree:ABSOLUTE:600:0:5000000000 \
108 DS:memshared:ABSOLUTE:600:0:5000000000 \
109 DS:membuffers:ABSOLUTE:600:0:5000000000 \
110 DS:memcache:ABSOLUTE:600:0:5000000000 \
111 DS:swapused:ABSOLUTE:600:0:5000000000 \
112 DS:swapfree:ABSOLUTE:600:0:5000000000 \
113 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
114 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
116 while read tag count unit; do
117 case "$tag" in
118 MemTotal:) memtotal=$(($count * 1024));;
119 MemFree:) memfree=$(($count * 1024))
120 memused=$(($memtotal - $memfree))
121 memshared=0;;
122 MemShared:) memshared=$(($count * 1024));;
123 Buffers:) membuffers=$(($count * 1024));;
124 Cached:) memcache=$(($count * 1024));;
125 SwapTotal:) swaptotal=$(($count * 1024));;
126 SwapFree:) swapfree=$(($count * 1024))
127 swapused=$(( $swaptotal - $swapfree));;
128 esac
129 done < /proc/meminfo
131 rrdtool update "$rrdlog/mem.rrd" \
132 -t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
133 "N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
134 }
136 getmax() {
137 rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
138 BEGIN {max=0} \
139 /^[0-9]/ { \
140 if ($2 != "nan" && $2 > max) max=$2; \
141 if ($3 != "nan" && $3 > max) max=$3; \
142 } \
143 END { print max }' | sed 's/,/./'
144 }
146 updatediskgraph() {
147 period=$1
148 [ "$period" == "day" ] && maxdisk="$(getmax disk)"
149 info=""
150 [ -r $2 ] &&
151 info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
152 sed "s|Disk $2: \(.*\), .*|\1|") ]"
153 if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
154 err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
155 done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
156 rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
157 [ $err -eq 0 ] && rate="0"
158 # --right-axis-label "I/O state %"
159 rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
160 $rrdgraphargs -t "disk access per $period $info" \
161 --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
162 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
163 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
164 DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
165 DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
166 DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
167 "CDEF:readpct=100,read,$maxdisk,/,*" \
168 "CDEF:writepct=100,write,$maxdisk,/,*" \
169 "CDEF:errpct=100,err,req,/,*" \
170 "CDEF:donepct=100,done,req,/,*" \
171 "CDEF:errrate=err,done,/" \
172 'AREA:readpct#0000FF:sectors read from disk' \
173 "COMMENT:I/O error rate $rate" \
174 'STACK:writepct#00FF00:sectors written 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 --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
181 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
182 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
183 "CDEF:readpct=100,read,$maxdisk,/,*" \
184 "CDEF:writepct=100,write,$maxdisk,/,*" \
185 'AREA:readpct#0000FF:sectors read from disk' \
186 'STACK:writepct#00FF00:sectors written to disk'
187 fi
188 }
190 updatediskdata() {
191 dev=$1
192 [ -e "$rrdlog/disk.rrd" ] ||
193 rrdtool create "$rrdlog/disk.rrd" --step=300 \
194 DS:readsect:COUNTER:600:0:5000000000 \
195 DS:writesect:COUNTER:600:0:5000000000 \
196 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
197 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
198 [ -e "$rrdlog/iodisk.rrd" ] ||
199 rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
200 DS:done:GAUGE:600:0:U DS:err:GAUGE:600:0:U \
201 DS:req:GAUGE:600:0:U \
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
205 while read major minor name readreq readsect writereq writesect misc; do
206 [ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
207 [ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
208 rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
209 N:$readsect:$writesect
210 done < /proc/diskstats
211 disk=${dev:0:8}
212 dir=/sys/block/${disk#/dev/}/device
213 done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
214 err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
215 req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
216 rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
217 }
219 updateifgraph() {
220 interface=$1
221 period=$2
222 rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
223 $rrdgraphargs -t "traffic on $interface graph per $period" \
224 --logarithmic -A -v "Bytes/second" --units=si \
225 DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
226 DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
227 'AREA:incoming#00FF00:incoming traffic' \
228 'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \
229 'LINE1:outgoing#0000FF:outgoing traffic\j'
230 }
232 netframes() {
233 ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
234 }
236 netstats() {
237 ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
238 }
240 updateifdata() {
241 interface=$1
242 [ -e "$rrdlog/$interface.rrd" ] ||
243 rrdtool create "$rrdlog/$interface.rrd" --step=300 \
244 DS:incoming:COUNTER:600:0:U \
245 DS:outgoing:COUNTER:600:0:U \
246 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
247 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
248 [ -e "$rrdlog/packets-$interface.rrd" ] ||
249 rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
250 DS:in:COUNTER:600:0:U DS:out:COUNTER:600:0:U \
251 DS:inerr:COUNTER:600:0:U DS:outerr:COUNTER:600:0:U \
252 DS:indrop:COUNTER:600:0:U DS:outdrop:COUNTER:600:0:U \
253 DS:inov:COUNTER:600:0:U DS:outov:COUNTER:600:0:U \
254 DS:frame:COUNTER:600:0:U DS:carrier:COUNTER:600:0:U \
255 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
256 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
257 rx=$(netstats $interface RX)
258 tx=$(netstats $interface TX)
259 rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
260 N:${rx:-U}:${tx:-U}
261 rx=$(netframes $interface RX packets)
262 tx=$(netframes $interface TX packets)
263 rxerr=$(netframes $interface RX errors)
264 txerr=$(netframes $interface TX errors)
265 rxdrop=$(netframes $interface RX dropped)
266 txdrop=$(netframes $interface TX dropped)
267 rxov=$(netframes $interface RX overruns)
268 txov=$(netframes $interface TX overruns)
269 frame=$(netframes $interface RX frame)
270 carrier=$(netframes $interface TX carrier)
271 rrdtool update "$rrdlog/packets-$interface.rrd" \
272 -t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
273 N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
274 }
276 getdisk()
277 {
278 local d
279 local i
280 d=$(stat -c %04D $1)
281 for i in /dev/* ; do
282 [ $(stat -c "%02t%02T" $i) == $d ] || continue
283 echo $i
284 break
285 done
286 }
288 ###
289 ### System graphs
290 ###
292 updatecpudata
293 updatecpugraph day
294 updatecpugraph week
295 updatecpugraph month
296 updatecpugraph year
298 updatememdata
299 updatememgraph day
300 updatememgraph week
301 updatememgraph month
302 updatememgraph year
304 if [ -e /proc/diskstats ]; then
305 disk=$(getdisk $0)
306 updatediskdata $disk
307 updatediskgraph day ${disk:0:8}
308 updatediskgraph week ${disk:0:8}
309 updatediskgraph month ${disk:0:8}
310 updatediskgraph year ${disk:0:8}
311 fi
313 iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
314 updateifdata $iface
315 updateifgraph $iface day
316 updateifgraph $iface week
317 updateifgraph $iface month
318 updateifgraph $iface year