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