slitaz-dev-tools view mirror-tools/rootfs/usr/share/rrd/makegraphs @ rev 309

Remove ashism ==
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 26 08:23:49 2019 +0100 (2019-02-26)
parents e0a09d90b2cb
children 3a966a7b4ac9
line source
1 #!/bin/sh
2 #*/5 * * * * /usr/share/rrd/makegraphs >/dev/null
4 # RRD database directory
5 rrdlog="/var/spool/rrd"
7 # Images directory
8 rrdgraph="/var/spool/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 | head -1)"
42 rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
43 $rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
44 DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
45 DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
46 DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
47 'CDEF:total=user,system,idle,+,+' \
48 'CDEF:userpct=100,user,total,/,*' \
49 'CDEF:systempct=100,system,total,/,*' \
50 'CDEF:idlepct=100,idle,total,/,*' \
51 'AREA:userpct#0000FF:user cpu usage' \
52 'STACK:systempct#FF0000:system cpu usage' \
53 'STACK:idlepct#00FF00:idle cpu usage\j'
54 }
57 updatememgraph() {
58 period=$1
59 info="$(free | awk '\
60 { \
61 if (/Mem:/) { \
62 if ($2 < 10000) printf "%d KB",$2; \
63 else if ($2 < 10000000) printf "%d MB",$2/1024; \
64 else printf "%d GB",$2/1024/1024; \
65 } \
66 }')"
67 info2="$(free | awk '\
68 { \
69 if (/Swap:/) { \
70 if ($2 < 10000) printf "%d KB",$2; \
71 else if ($2 < 10000000) printf "%d MB",$2/1024; \
72 else printf "%d GB",$2/1024/1024; \
73 } \
74 }')"
75 rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
76 $rrdgraphargs -l 0 -u 100 \
77 -t "memory usage per $period [ $info + $info2 swap ]" \
78 DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
79 DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
80 DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
81 DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
82 DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
83 DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
84 DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
85 'CDEF:total=used,free,+' \
86 'CDEF:used2=used,buffer,cache,shared,+,+,-' \
87 'CDEF:usedpct=100,used2,total,/,*' \
88 'CDEF:sharedpct=100,shared,total,/,*' \
89 'CDEF:bufferpct=100,buffer,total,/,*' \
90 'CDEF:cachepct=100,cache,total,/,*' \
91 'CDEF:freepct=100,free,total,/,*' \
92 'CDEF:swtotal=swused,swfree,+' \
93 'CDEF:swusedpct=100,swused,swtotal,/,*' \
94 'AREA:usedpct#0000FF:used memory' \
95 'STACK:sharedpct#FF7F00:shared memory' \
96 'STACK:bufferpct#FF00FF:buffered memory' \
97 'STACK:cachepct#FFFF00:cached memory' \
98 'STACK:freepct#00FF00:free memory' \
99 'LINE2:swusedpct#FF0000:used swap\j'
100 }
102 updatememdata () {
103 [ -e "$rrdlog/mem.rrd" ] ||
104 rrdtool create "$rrdlog/mem.rrd" --step=300 \
105 DS:memused:ABSOLUTE:600:0:5000000000 \
106 DS:memfree:ABSOLUTE:600:0:5000000000 \
107 DS:memshared:ABSOLUTE:600:0:5000000000 \
108 DS:membuffers:ABSOLUTE:600:0:5000000000 \
109 DS:memcache:ABSOLUTE:600:0:5000000000 \
110 DS:swapused:ABSOLUTE:600:0:5000000000 \
111 DS:swapfree:ABSOLUTE:600:0:5000000000 \
112 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
113 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
115 while read tag count unit; do
116 case "$tag" in
117 MemTotal:) memtotal=$(($count * 1024));;
118 MemFree:) memfree=$(($count * 1024))
119 memused=$(($memtotal - $memfree))
120 memshared=0;;
121 MemShared:) memshared=$(($count * 1024));;
122 Buffers:) membuffers=$(($count * 1024));;
123 Cached:) memcache=$(($count * 1024));;
124 SwapTotal:) swaptotal=$(($count * 1024));;
125 SwapFree:) swapfree=$(($count * 1024))
126 swapused=$(( $swaptotal - $swapfree));;
127 esac
128 done < /proc/meminfo
130 rrdtool update "$rrdlog/mem.rrd" \
131 -t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
132 "N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
133 }
135 getmax() {
136 rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
137 BEGIN {max=0} \
138 /^[0-9]/ { \
139 if ($2 != "nan" && $2 > max) max=$2; \
140 if ($3 != "nan" && $3 > max) max=$3; \
141 } \
142 END { print max }' | sed 's/,/./'
143 }
145 updatediskgraph() {
146 period=$1
147 [ "$period" = "day" ] && maxdisk="$(getmax disk)"
148 info=""
149 [ -r $2 ] &&
150 info="[ $(fdisk -l | grep "^Disk $2:" | \
151 sed "s|Disk $2: \(.*\), .*|\1|") ]"
152 rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
153 $rrdgraphargs -t "disk access per $period $info" \
154 --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
155 DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
156 DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
157 DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
158 DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
159 DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
160 "CDEF:readpct=100,read,$maxdisk,/,*" \
161 "CDEF:writepct=100,write,$maxdisk,/,*" \
162 "CDEF:errpct=100,err,req,/,*" \
163 "CDEF:donepct=100,done,req,/,*" \
164 'AREA:readpct#0000FF:sectors read from disk' \
165 'STACK:writepct#00FF00:sectors written to disk' \
166 'LINE2:donepct#FFFF00:I/O complete' \
167 'LINE2:errpct#FF0000:I/O error\j'
168 }
170 updatediskdata() {
171 dev=$1
172 [ -e "$rrdlog/disk.rrd" ] ||
173 rrdtool create "$rrdlog/disk.rrd" --step=300 \
174 DS:readsect:COUNTER:600:0:5000000000 \
175 DS:writesect:COUNTER:600:0:5000000000 \
176 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
177 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
178 [ -e "$rrdlog/iodisk.rrd" ] ||
179 rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
180 DS:done:GAUGE:600:0:U DS:err:GAUGE:600:0:U \
181 DS:req:GAUGE:600:0:U \
182 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
183 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
185 while read major minor name readreq readsect writereq writesect misc; do
186 [ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
187 [ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
188 rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
189 N:$readsect:$writesect
190 done < /proc/diskstats
191 dir=/sys/block/${dev#/dev/}/device
192 done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
193 err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
194 req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
195 rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
196 }
198 updateifgraph() {
199 interface=$1
200 period=$2
201 [ "$period" = "day" ] && maxif="$(getmax $interface)"
202 rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
203 $rrdgraphargs -t "traffic on $interface graph per $period" \
204 --logarithmic -A -v "Bytes/second" --units=si \
205 DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
206 DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
207 "CDEF:inpct=100,incoming,$maxif,/,*" \
208 "CDEF:outpct=100,outgoing,$maxif,/,*" \
209 'AREA:inpct#00FF00:incoming traffic' \
210 'LINE1:outpct#0000FF:outgoing traffic\j'
211 }
213 netframes() {
214 ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
215 }
217 netstats() {
218 ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
219 }
221 updateifdata() {
222 interface=$1
223 [ -e "$rrdlog/$interface.rrd" ] ||
224 rrdtool create "$rrdlog/$interface.rrd" --step=300 \
225 DS:incoming:COUNTER:600:0:U \
226 DS:outgoing:COUNTER:600:0:U \
227 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
228 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
229 [ -e "$rrdlog/packets-$interface.rrd" ] ||
230 rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
231 DS:in:COUNTER:600:0:U DS:out:COUNTER:600:0:U \
232 DS:inerr:COUNTER:600:0:U DS:outerr:COUNTER:600:0:U \
233 DS:indrop:COUNTER:600:0:U DS:outdrop:COUNTER:600:0:U \
234 DS:inov:COUNTER:600:0:U DS:outov:COUNTER:600:0:U \
235 DS:frame:COUNTER:600:0:U DS:carrier:COUNTER:600:0:U \
236 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \
237 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
238 rx=$(netstats $interface RX)
239 tx=$(netstats $interface TX)
240 rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
241 N:${rx:-U}:${tx:-U}
242 rx=$(netframes $interface RX packets)
243 tx=$(netframes $interface TX packets)
244 rxerr=$(netframes $interface RX errors)
245 txerr=$(netframes $interface TX errors)
246 rxdrop=$(netframes $interface RX dropped)
247 txdrop=$(netframes $interface TX dropped)
248 rxov=$(netframes $interface RX overruns)
249 txov=$(netframes $interface TX overruns)
250 frame=$(netframes $interface RX frame)
251 carrier=$(netframes $interface TX carrier)
252 rrdtool update "$rrdlog/packets-$interface.rrd" \
253 -t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
254 N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
255 }
257 getdisk()
258 {
259 local d
260 local i
261 d=$(stat -c %D $1)
262 for i in /dev/* ; do
263 [ $(stat -c "%02t%02T" $i) = $d ] || continue
264 echo $i
265 break
266 done
267 }
269 ###
270 ### System graphs
271 ###
273 updatecpudata
274 updatecpugraph day
275 updatecpugraph week
276 updatecpugraph month
277 updatecpugraph year
279 updatememdata
280 updatememgraph day
281 updatememgraph week
282 updatememgraph month
283 updatememgraph year
285 if [ -e /proc/diskstats ]; then
286 disk=$(getdisk $0)
287 updatediskdata $disk
288 updatediskgraph day ${disk:0:8}
289 updatediskgraph week ${disk:0:8}
290 updatediskgraph month ${disk:0:8}
291 updatediskgraph year ${disk:0:8}
292 fi
294 iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
295 updateifdata $iface
296 updateifgraph $iface day
297 updateifgraph $iface week
298 updateifgraph $iface month
299 updateifgraph $iface year