slitaz-forge view tank/web/lib/makegraphs @ rev 713

makegraphs: tune cpuinfo
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jan 15 08:07:41 2021 +0000 (2021-01-15)
parents 061a079077a3
children
line source
1 #!/bin/sh
2 #*/5 * * * * /home/slitaz/www/tank/lib/makegraphs >/dev/null
4 # RRD database directory
5 rrdlog="/home/slitaz/www/tank/log/rrd"
7 # Images directory
8 rrdgraph="/home/slitaz/www/tank/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' /etc/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 if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
168 err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
169 done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
170 rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
171 [ $err -eq 0 ] && rate="0"
172 # --right-axis-label "I/O state %"
173 rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
174 $rrdgraphargs -t "disk access per $period $info" \
175 -v "Sectors/second" --units=si \
176 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
177 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
178 DEF:blkh=$rrdlog/usagedisk.rrd:bhome:AVERAGE \
179 DEF:blkr=$rrdlog/usagedisk.rrd:broot:AVERAGE \
180 "CDEF:readpct=100,read,$maxdisk,/,*" \
181 "CDEF:writepct=100,write,$maxdisk,/,*" \
182 'AREA:readpct#0000FF:sectors written' \
183 'STACK:writepct#00FF00:sectors read' \
184 "COMMENT:I/O error $rate" \
185 'LINE1:blkr#FF00FF:blocks used in /root\g' \
186 'GPRINT:blkr:LAST:%1.0lf%%' \
187 'LINE1:blkh#FF0000:blocks used in /home\g' \
188 'GPRINT:blkh:LAST:%1.0lf%%\j'
189 else
190 rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
191 $rrdgraphargs -t "disk access per $period $info" \
192 --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
193 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
194 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
195 DEF:blk=$rrdlog/usagedisk.rrd:bhome:AVERAGE \
196 DEF:ino=$rrdlog/usagedisk.rrd:ihome:AVERAGE \
197 "CDEF:readpct=100,read,$maxdisk,/,*" \
198 "CDEF:writepct=100,write,$maxdisk,/,*" \
199 'AREA:readpct#0000FF:sectors read from disk' \
200 'STACK:writepct#00FF00:sectors written to disk' \
201 'LINE1:ino#FF00FF:inodes used in /home\g' \
202 'GPRINT:ino:MAX:%1.0lf%%' \
203 'LINE1:blk#FF0000:blocks used in /home\g' \
204 'GPRINT:blk:MAX:%1.0lf%%\j'
205 fi
206 }
208 updatediskdata() {
209 dev=$1
210 [ -e "$rrdlog/disk.rrd" ] ||
211 rrdtool create "$rrdlog/disk.rrd" --step=300 \
212 DS:readsect:COUNTER:600:0:5000000000 \
213 DS:writesect:COUNTER:600:0:5000000000 \
214 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
215 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
216 [ -e "$rrdlog/iodisk.rrd" ] ||
217 rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
218 DS:done:GAUGE:600:0:U DS:err:GAUGE:600:0:U \
219 DS:req:GAUGE:600:0:U \
220 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
221 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
222 [ -e "$rrdlog/usagedisk.rrd" ] ||
223 rrdtool create "$rrdlog/usagedisk.rrd" --step=300 \
224 DS:broot:GAUGE:600:0:U DS:iroot:GAUGE:600:0:U \
225 DS:bhome:GAUGE:600:0:U DS:ihome:GAUGE:600:0:U \
226 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
227 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
229 while read major minor name readreq readsect writereq writesect misc; do
230 [ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
231 [ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
232 rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
233 N:$readsect:$writesect
234 done < /proc/diskstats
235 disk=${dev:0:8}
236 dir=/sys/block/${disk#/dev/}/device
237 done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
238 err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
239 req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
240 rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
241 iroot=$(df -i / | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
242 broot=$(df / | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
243 ihome=$(df -i /home | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
244 bhome=$(df /home | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
245 rrdtool update "$rrdlog/usagedisk.rrd" -t broot:iroot:bhome:ihome N:$broot:$iroot:$bhome:$ihome
246 }
248 updateifgraph() {
249 interface=$1
250 period=$2
251 rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
252 $rrdgraphargs -t "traffic on $interface graph per $period" \
253 --logarithmic -A -v "Bytes/second" --units=si \
254 DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
255 DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
256 DEF:tcp=$rrdlog/proto-$interface.rrd:tcp:AVERAGE \
257 'AREA:incoming#00FF00:incoming traffic\g' \
258 'GPRINT:incoming:MAX:max%8.3lf %sBps' \
259 'LINE1:outgoing#0000FF:outgoing traffic\g' \
260 'GPRINT:outgoing:MAX:max%8.3lf %sBps' \
261 'LINE1:tcp#000000:connections\g' \
262 'GPRINT:tcp:MAX:max %2.0lf\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 return
343 done
344 df $1 | sed '$!d;s/ .*$//'
345 }
347 ###
348 ### System graphs
349 ###
351 updatecpudata
352 updatecpugraph day
353 updatecpugraph week
354 updatecpugraph month
355 updatecpugraph year
357 updatememdata
358 updatememgraph day
359 updatememgraph week
360 updatememgraph month
361 updatememgraph year
363 if [ -e /proc/diskstats ]; then
364 disk=$(getdisk $0)
365 updatediskdata $disk
366 updatediskgraph day ${disk:0:8}
367 updatediskgraph week ${disk:0:8}
368 updatediskgraph month ${disk:0:8}
369 updatediskgraph year ${disk:0:8}
370 fi
372 iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
373 updateifdata $iface
374 updateifgraph $iface day
375 updateifgraph $iface week
376 updateifgraph $iface month
377 updateifgraph $iface year
379 [ ! -s $rrdgraph/boot.html -o /var/log/boot.log -nt $rrdgraph/boot.html ] &&
380 cat > $rrdgraph/boot.html <<EOT
381 <html>
382 <body>
383 $(stat -c %y /var/log/dmesg.log | sed 's/\.0*//')
384 <span style="color: blue"><i>$(cat /proc/cmdline)</i></span>
385 <pre>
386 $(cat /var/log/dmesg.log /var/log/boot.log | \
387 sed -e 's/</\&lt;/g;s/>/\&gt;/g' -e 's/.*\]R//' -e 's/.*\[?8h//' \
388 -e 's|.\[1m|<b>|' -e 's|.\[0m|</b>|' -e 's|.\[[0-9][0-9Gm;]*||g' \
389 -e ':a;s/^\(.\{1,68\}\)\(\[ [A-Za-z]* \]\)/\1 \2/;ta' \
390 -e 's#\[ OK \]#[ <span style="color: green">OK</span> ]#' \
391 -e 's#\[ Failed \]#[ <span style="color: red">Failed</span> ]#' \
392 -e 's|No such .*|<span style="color: red">&</span>|' \
393 -e 's|ERROR .*|<span style="color: red">&</span>|' \
394 -e 's|command line: \(.*\)|command line: <span style="color: blue">\1</span>|' \
395 )
396 </pre>
397 </body>
398 </html>
399 EOT