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

mirror: Add mirror info files
author Christophe Lincoln <pankso@slitaz.org>
date Tue Mar 20 13:13:22 2012 +0100 (2012-03-20)
parents
children 1f27742b054c
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/pics/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 updatecpudata() {
19 [ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
20 DS:user:COUNTER:600:0:500000000 \
21 DS:system:COUNTER:600:0:500000000 \
22 DS:idle:COUNTER:600:0:500000000 \
23 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
24 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
25 grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
26 rrdtool update $rrdlog/cpu.rrd -t user:system:idle \
27 N:$(( $user + $nice )):$system:$idle
28 break
29 done
31 [ -e "$rrdlog/cpu2.rrd" ] &&
32 grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
33 rrdtool update $rrdlog/cpu2.rrd -t nice:user:system:idle \
34 N:$nice:$user:$system:$idle
35 break
36 done
37 }
39 updatecpugraph() {
40 period=$1
41 info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
42 | sed 's/ * / /g' | head -1)"
43 rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
44 $rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
45 DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
46 DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
47 DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
48 'CDEF:total=user,system,idle,+,+' \
49 'CDEF:userpct=100,user,total,/,*' \
50 'CDEF:systempct=100,system,total,/,*' \
51 'CDEF:idlepct=100,idle,total,/,*' \
52 'AREA:userpct#0000FF:user cpu usage' \
53 'STACK:systempct#FF0000:system cpu usage' \
54 'STACK:idlepct#00FF00:idle cpu usage\j'
55 }
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 # --right-axis-label "I/O state %"
155 rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
156 $rrdgraphargs -t "disk access per $period $info" \
157 --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
158 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
159 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
160 DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
161 DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
162 DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
163 "CDEF:readpct=100,read,$maxdisk,/,*" \
164 "CDEF:writepct=100,write,$maxdisk,/,*" \
165 "CDEF:errpct=100,err,req,/,*" \
166 "CDEF:donepct=100,done,req,/,*" \
167 'AREA:readpct#0000FF:sectors read from disk' \
168 'STACK:writepct#00FF00:sectors written to disk' \
169 'LINE2:donepct#FFFF00:I/O complete' \
170 'LINE2:errpct#FF0000:I/O error\j'
171 else
172 rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
173 $rrdgraphargs -t "disk access per $period $info" \
174 --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
175 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
176 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
177 "CDEF:readpct=100,read,$maxdisk,/,*" \
178 "CDEF:writepct=100,write,$maxdisk,/,*" \
179 'AREA:readpct#0000FF:sectors read from disk' \
180 'STACK:writepct#00FF00:sectors written to disk'
181 fi
182 }
184 updatediskdata() {
185 dev=$1
186 [ -e "$rrdlog/disk.rrd" ] ||
187 rrdtool create "$rrdlog/disk.rrd" --step=300 \
188 DS:readsect:COUNTER:600:0:5000000000 \
189 DS:writesect:COUNTER:600:0:5000000000 \
190 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
191 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
192 [ -e "$rrdlog/iodisk.rrd" ] ||
193 rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
194 DS:done:GAUGE:600:0:U DS:err:GAUGE:600:0:U \
195 DS:req:GAUGE:600:0:U \
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
199 while read major minor name readreq readsect writereq writesect misc; do
200 [ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
201 [ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
202 rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
203 N:$readsect:$writesect
204 done < /proc/diskstats
205 disk=${dev:0:8}
206 dir=/sys/block/${disk#/dev/}/device
207 done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
208 err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
209 req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
210 rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
211 }
213 updateifgraph() {
214 interface=$1
215 period=$2
216 rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
217 $rrdgraphargs -t "traffic on $interface graph per $period" \
218 --logarithmic -A -v "Bytes/second" --units=si \
219 DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
220 DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
221 'AREA:incoming#00FF00:incoming traffic' \
222 'LINE1:outgoing#0000FF:outgoing traffic\j'
223 }
225 netframes() {
226 ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
227 }
229 netstats() {
230 ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
231 }
233 updateifdata() {
234 interface=$1
235 [ -e "$rrdlog/$interface.rrd" ] ||
236 rrdtool create "$rrdlog/$interface.rrd" --step=300 \
237 DS:incoming:COUNTER:600:0:U \
238 DS:outgoing:COUNTER:600:0:U \
239 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
240 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
241 [ -e "$rrdlog/packets-$interface.rrd" ] ||
242 rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
243 DS:in:COUNTER:600:0:U DS:out:COUNTER:600:0:U \
244 DS:inerr:COUNTER:600:0:U DS:outerr:COUNTER:600:0:U \
245 DS:indrop:COUNTER:600:0:U DS:outdrop:COUNTER:600:0:U \
246 DS:inov:COUNTER:600:0:U DS:outov:COUNTER:600:0:U \
247 DS:frame:COUNTER:600:0:U DS:carrier:COUNTER:600:0:U \
248 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
249 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
250 rx=$(netstats $interface RX)
251 tx=$(netstats $interface TX)
252 rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
253 N:${rx:-U}:${tx:-U}
254 rx=$(netframes $interface RX packets)
255 tx=$(netframes $interface TX packets)
256 rxerr=$(netframes $interface RX errors)
257 txerr=$(netframes $interface TX errors)
258 rxdrop=$(netframes $interface RX dropped)
259 txdrop=$(netframes $interface TX dropped)
260 rxov=$(netframes $interface RX overruns)
261 txov=$(netframes $interface TX overruns)
262 frame=$(netframes $interface RX frame)
263 carrier=$(netframes $interface TX carrier)
264 rrdtool update "$rrdlog/packets-$interface.rrd" \
265 -t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
266 N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
267 }
269 getdisk()
270 {
271 local d
272 local i
273 d=$(stat -c %04D $1)
274 for i in /dev/* ; do
275 [ $(stat -c "%02t%02T" $i) == $d ] || continue
276 echo $i
277 break
278 done
279 }
281 ###
282 ### System graphs
283 ###
285 updatecpudata
286 updatecpugraph day
287 updatecpugraph week
288 updatecpugraph month
289 updatecpugraph year
291 updatememdata
292 updatememgraph day
293 updatememgraph week
294 updatememgraph month
295 updatememgraph year
297 if [ -e /proc/diskstats ]; then
298 disk=$(getdisk $0)
299 updatediskdata $disk
300 updatediskgraph day ${disk:0:8}
301 updatediskgraph week ${disk:0:8}
302 updatediskgraph month ${disk:0:8}
303 updatediskgraph year ${disk:0:8}
304 fi
306 iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
307 updateifdata $iface
308 updateifgraph $iface day
309 updateifgraph $iface week
310 updateifgraph $iface month
311 updateifgraph $iface year