1 ef118436 2019-03-14 mischa <!DOCTYPE html>
2 ef118436 2019-03-14 mischa <html lang="en" class="black bg-white sans-serif lh-copy">
3 ef118436 2019-03-14 mischa <title>RRDtool looks nicer... - High5!</title>
4 ef118436 2019-03-14 mischa <meta charset="UTF-8">
6 ef118436 2019-03-14 mischa <h1 id="RRDtool%20looks%20nicer...">RRDtool looks nicer...</h1>
8 ef118436 2019-03-14 mischa <p><a href="https://openbsd.amsterdam/">OpenBSD Amsterdam</a> was in search of a lightweight toolset to keep track of resource usage, at a minimum the CPU load generated by the <a href="https://man.openbsd.org/vmm.4">vmm(4)</a>/<a href="https://man.openbsd.org/vmd.8">vmd(8)</a> hosts and the traffic from and to the hosts. A couple of weeks ago we ended up with a workable [MRTG setup]. While it worked, it didn't look very pretty.</p>
10 ef118436 2019-03-14 mischa <p>In a moment of clarity, we thought about using <a href="https://oss.oetiker.ch/rrdtool/">RRDtool</a>. Heck, why shouldn't we give it a try? From the previous tooling, we already had some required building blocks in place to make <a href="https://oss.oetiker.ch/mrtg/">MRTG</a> understand the CPU Cores and uptime from <a href="https://openbsd.org/">OpenBSD</a>.</p>
12 ef118436 2019-03-14 mischa <p>We decided to split the collection of the different OIDs (SNMP Object Identifiers) into three different scripts, which <a href="https://man.openbsd.org/cron.1">cron(1)</a> calls, from a wrapper script.</p>
15 ef118436 2019-03-14 mischa <li>uptime.sh</li>
16 ef118436 2019-03-14 mischa <li>cpu_load.sh</li>
17 ef118436 2019-03-14 mischa <li>interface.sh</li>
20 ef118436 2019-03-14 mischa <h3 id="uptime.sh">uptime.sh</h3>
22 ef118436 2019-03-14 mischa <pre><code>#!/bin/sh
23 ef118436 2019-03-14 mischa test -n "$1" || exit 1
24 ef118436 2019-03-14 mischa HOST="$1"
25 ef118436 2019-03-14 mischa COMMUNITY="public"
26 ef118436 2019-03-14 mischa UPTIMEINFO="/tmp/${HOST}-uptime.txt"
27 ef118436 2019-03-14 mischa TICKS=$(snmpctl snmp get ${HOST} community ${COMMUNITY} oid hrSystemUptime.0 | cut -d= -f2)
28 ef118436 2019-03-14 mischa DAYS=$(echo "${TICKS}/8640000" | bc -l)
29 ef118436 2019-03-14 mischa HOURS=$(echo "0.${DAYS##*.} * 24" | bc -l)
30 ef118436 2019-03-14 mischa MINUTES=$(echo "0.${HOURS##*.} * 60" | bc -l)
31 ef118436 2019-03-14 mischa SECS=$(echo "0.${MINUTES##*.} * 60" | bc -l)
32 ef118436 2019-03-14 mischa test -n "$DAYS" && printf '%s days, ' "${DAYS%.*}" > ${UPTIMEINFO}
33 ef118436 2019-03-14 mischa printf '%02d\\:%02d\\:%02d\n' "${HOURS%.*}" "${MINUTES%.*}" "${SECS%.*}" >> ${UPTIMEINFO}
34 ef118436 2019-03-14 mischa </code></pre>
36 ef118436 2019-03-14 mischa <p>This is a seperate script, due to the uptime usage of both hosts in both graphs. </p>
38 ef118436 2019-03-14 mischa <p>The origins for this script can be found detailled in our <a href="https://chargen.one/obsdams/using-mrtg-again">MRTG Setup</a>.</p>
40 ef118436 2019-03-14 mischa <h3 id="cpu_load.sh">cpu_load.sh</h3>
42 ef118436 2019-03-14 mischa <pre><code>test -n "$1" || exit 1
43 ef118436 2019-03-14 mischa HOST="$1"
44 ef118436 2019-03-14 mischa COMMUNITY="public"
45 ef118436 2019-03-14 mischa RRDFILES="/var/rrdtool"
46 ef118436 2019-03-14 mischa IMAGES="/var/www/htdocs"
47 ef118436 2019-03-14 mischa WATERMARK="OpenBSD Amsterdam - https://obsda.ms"
48 ef118436 2019-03-14 mischa RRDTOOL="/usr/local/bin/rrdtool"
49 ef118436 2019-03-14 mischa CPUINFO="/tmp/${HOST}-cpu.txt"
50 ef118436 2019-03-14 mischa UPTIME=$(cat /tmp/${HOST}-uptime.txt)
51 ef118436 2019-03-14 mischa NOW=$(date "+%Y-%m-%d %H:%M:%S %Z" | sed 's/:/\\:/g')
53 ef118436 2019-03-14 mischa if ! test -f "${RRDFILES}/${HOST}-cpu.rrd"
55 ef118436 2019-03-14 mischa echo "Creating ${RRDFILES}/${HOST}-cpu.rrd"
56 ef118436 2019-03-14 mischa ${RRDTOOL} create ${RRDFILES}/${HOST}-cpu.rrd \
57 ef118436 2019-03-14 mischa --step 300 \
58 ef118436 2019-03-14 mischa DS:ds0:GAUGE:600:U:U \
59 ef118436 2019-03-14 mischa RRA:MAX:0.5:1:20000
62 ef118436 2019-03-14 mischa snmpctl snmp walk ${HOST} community ${COMMUNITY} oid hrProcessorLoad | cut -d= -f2 > ${CPUINFO}
63 ef118436 2019-03-14 mischa CORES=$(grep -cv "^0$" ${CPUINFO})
64 ef118436 2019-03-14 mischa CPU_LOAD_SUM=$(awk '{sum += $1} END {print sum}' ${CPUINFO})
65 ef118436 2019-03-14 mischa CPU_LOAD=$(echo "scale=2; ${CPU_LOAD_SUM}/${CORES}" | bc -l)
67 ef118436 2019-03-14 mischa ${RRDTOOL} update ${RRDFILES}/${HOST}-cpu.rrd N:${CPU_LOAD}
69 ef118436 2019-03-14 mischa ${RRDTOOL} graph ${IMAGES}/${HOST}-cpu.png \
70 ef118436 2019-03-14 mischa --start -43200 \
71 ef118436 2019-03-14 mischa --title "${HOST} - CPU" \
72 ef118436 2019-03-14 mischa --vertical-label "% CPU Used" \
73 ef118436 2019-03-14 mischa --watermark "${WATERMARK}" \
74 ef118436 2019-03-14 mischa DEF:CPU=${RRDFILES}/${HOST}-cpu.rrd:ds0:AVERAGE \
75 ef118436 2019-03-14 mischa AREA:CPU#FFCC00 \
76 ef118436 2019-03-14 mischa LINE2:CPU#CC0033:"CPU" \
77 ef118436 2019-03-14 mischa GPRINT:CPU:MAX:"Max\:%2.2lf %s" \
78 ef118436 2019-03-14 mischa GPRINT:CPU:AVERAGE:"Average\:%2.2lf %s" \
79 ef118436 2019-03-14 mischa GPRINT:CPU:LAST:" Current\:%2.2lf %s\n" \
80 ef118436 2019-03-14 mischa COMMENT:"\\n" \
81 ef118436 2019-03-14 mischa COMMENT:" SUM CPU Load / Active Cores = % CPU Used\n" \
82 ef118436 2019-03-14 mischa COMMENT:" Up for ${UPTIME} at ${NOW}"
83 ef118436 2019-03-14 mischa </code></pre>
85 ef118436 2019-03-14 mischa <p>On the first run, <a href="https://oss.oetiker.ch/rrdtool/">RRDtool</a> will create the .rrd file. On every subsequent run, it will update the file with the collected values and update the graph.</p>
87 ef118436 2019-03-14 mischa <p>The origins for this script can be found detailed in our <a href="https://chargen.one/obsdams/using-mrtg-again">MRTG Setup</a>.</p>
89 ef118436 2019-03-14 mischa <h3 id="interface.sh">interface.sh</h3>
91 ef118436 2019-03-14 mischa <pre><code>test -n "$1" || exit 1
92 ef118436 2019-03-14 mischa test -n "$2" || exit 1
93 ef118436 2019-03-14 mischa HOST="$1"
94 ef118436 2019-03-14 mischa INTERFACE="$2"
95 ef118436 2019-03-14 mischa COMMUNITY="public"
96 ef118436 2019-03-14 mischa RRDFILES="/var/rrdtool"
97 ef118436 2019-03-14 mischa IMAGES="/var/www/htdocs"
98 ef118436 2019-03-14 mischa WATERMARK="OpenBSD Amsterdam - https://obsda.ms"
99 ef118436 2019-03-14 mischa RRDTOOL="/usr/local/bin/rrdtool"
100 ef118436 2019-03-14 mischa UPTIME=$(cat /tmp/${HOST}-uptime.txt)
101 ef118436 2019-03-14 mischa NOW=$(date "+%Y-%m-%d %H:%M:%S %Z" | sed 's/:/\\:/g')
103 ef118436 2019-03-14 mischa if ! test -f "${RRDFILES}/${HOST}-${INTERFACE}.rrd"
105 ef118436 2019-03-14 mischa echo "Creating ${RRDFILES}/${HOST}-${INTERFACE}.rrd"
106 ef118436 2019-03-14 mischa ${RRDTOOL} create ${RRDFILES}/${HOST}-${INTERFACE}.rrd \
107 ef118436 2019-03-14 mischa --step 300 \
108 ef118436 2019-03-14 mischa DS:ds0:COUNTER:600:0:1250000000 \
109 ef118436 2019-03-14 mischa DS:ds1:COUNTER:600:0:1250000000 \
110 ef118436 2019-03-14 mischa RRA:AVERAGE:0.5:1:600 \
111 ef118436 2019-03-14 mischa RRA:AVERAGE:0.5:6:700 \
112 ef118436 2019-03-14 mischa RRA:AVERAGE:0.5:24:775 \
113 ef118436 2019-03-14 mischa RRA:AVERAGE:0.5:288:797 \
114 ef118436 2019-03-14 mischa RRA:MAX:0.5:1:600 \
115 ef118436 2019-03-14 mischa RRA:MAX:0.5:6:700 \
116 ef118436 2019-03-14 mischa RRA:MAX:0.5:24:775 \
117 ef118436 2019-03-14 mischa RRA:MAX:0.5:288:797
120 ef118436 2019-03-14 mischa IN=$(snmpctl snmp get ${HOST} community ${COMMUNITY} oid ifInOctets.${INTERFACE} | cut -d= -f2)
121 ef118436 2019-03-14 mischa OUT=$(snmpctl snmp get ${HOST} community ${COMMUNITY} oid ifOutOctets.${INTERFACE} | cut -d= -f2)
122 ef118436 2019-03-14 mischa DESCR=$(snmpctl snmp get ${HOST} community ${COMMUNITY} oid ifDescr.${INTERFACE} | cut -d= -f2 | tr
123 ef118436 2019-03-14 mischa -d '"')
125 ef118436 2019-03-14 mischa ${RRDTOOL} update ${RRDFILES}/${HOST}-${INTERFACE}.rrd N:${IN}:${OUT}
127 ef118436 2019-03-14 mischa ${RRDTOOL} graph ${IMAGES}/${HOST}-${INTERFACE}.png \
128 ef118436 2019-03-14 mischa --start -43200 \
129 ef118436 2019-03-14 mischa --title "${HOST} - ${DESCR}" \
130 ef118436 2019-03-14 mischa --vertical-label "Bits per Second" \
131 ef118436 2019-03-14 mischa --watermark "${WATERMARK}" \
132 ef118436 2019-03-14 mischa DEF:IN=${RRDFILES}/${HOST}-${INTERFACE}.rrd:ds0:AVERAGE \
133 ef118436 2019-03-14 mischa DEF:OUT=${RRDFILES}/${HOST}-${INTERFACE}.rrd:ds1:AVERAGE \
134 ef118436 2019-03-14 mischa CDEF:IN_CDEF="IN,8,*" \
135 ef118436 2019-03-14 mischa CDEF:OUT_CDEF="OUT,8,*" \
136 ef118436 2019-03-14 mischa AREA:IN_CDEF#00FF00:"In " \
137 ef118436 2019-03-14 mischa GPRINT:IN_CDEF:MAX:"Max\:%5.2lf %s" \
138 ef118436 2019-03-14 mischa GPRINT:IN_CDEF:AVERAGE:"Average\:%5.2lf %s" \
139 ef118436 2019-03-14 mischa GPRINT:IN_CDEF:LAST:" Current\:%5.2lf %s\n" \
140 ef118436 2019-03-14 mischa LINE2:OUT_CDEF#0000FF:"Out" \
141 ef118436 2019-03-14 mischa GPRINT:OUT_CDEF:MAX:"Max\:%5.2lf %s" \
142 ef118436 2019-03-14 mischa GPRINT:OUT_CDEF:AVERAGE:"Average\:%5.2lf %s" \
143 ef118436 2019-03-14 mischa GPRINT:OUT_CDEF:LAST:" Current\:%5.2lf %s\n" \
144 ef118436 2019-03-14 mischa COMMENT:"\\n" \
145 ef118436 2019-03-14 mischa COMMENT:" Up for ${UPTIME} at ${NOW}"
146 ef118436 2019-03-14 mischa </code></pre>
148 ef118436 2019-03-14 mischa <p>To pinpoint the network interface you want to measure the bandwith for, this command prints the available interfaces:</p>
150 ef118436 2019-03-14 mischa <pre><code>snmpctl snmp walk community oid ifDescr
151 ef118436 2019-03-14 mischa </code></pre>
153 ef118436 2019-03-14 mischa <p>This will output a list like:</p>
155 ef118436 2019-03-14 mischa <pre><code>ifDescr.1="em0"
156 ef118436 2019-03-14 mischa ifDescr.2="em1"
157 ef118436 2019-03-14 mischa ifDescr.3="enc0"
158 ef118436 2019-03-14 mischa ifDescr.4="lo0"
159 ef118436 2019-03-14 mischa ifDescr.5="bridge880"
160 ef118436 2019-03-14 mischa ifDescr.6="vlan880"
161 ef118436 2019-03-14 mischa ifDescr.13="pflog0"
162 ef118436 2019-03-14 mischa ifDescr.669="tap0"
163 ef118436 2019-03-14 mischa ifDescr.670="tap1"
164 ef118436 2019-03-14 mischa </code></pre>
166 ef118436 2019-03-14 mischa <p>The number behind <code>ifDescr</code> is the one that you need to feed to <strong>interface.sh</strong>, for example:</p>
168 ef118436 2019-03-14 mischa <pre><code># interface.sh 5
169 ef118436 2019-03-14 mischa </code></pre>
171 ef118436 2019-03-14 mischa <p>Finally the <strong>wrapper.sh</strong> script calls all the aforementioned scripts:</p>
173 ef118436 2019-03-14 mischa <pre><code>#!/bin/sh
174 ef118436 2019-03-14 mischa SCRIPTS="/var/rrdtool"
175 ef118436 2019-03-14 mischa for i in $(jot 2 1); do ${SCRIPTS}/uptime.sh host${i}.domain.tld; done
176 ef118436 2019-03-14 mischa for i in $(jot 2 1); do ${SCRIPTS}/cpu_load.sh host${i}.domain.tld; done
177 ef118436 2019-03-14 mischa ${SCRIPTS}/interface.sh host1.domain.tld 12
178 ef118436 2019-03-14 mischa ${SCRIPTS}/interface.sh host2.domain.tld 11
179 ef118436 2019-03-14 mischa </code></pre>
181 ef118436 2019-03-14 mischa <p>The resulting graphs:</p>
183 ef118436 2019-03-14 mischa <p><img src="https://ops.high5.nl/obsda.ms/s1.obsda.ms-cpu.png" alt="" />
184 ef118436 2019-03-14 mischa <img src="https://ops.high5.nl/obsda.ms/s1.obsda.ms-12.png" alt="" /></p>
186 ef118436 2019-03-14 mischa <p>You can follow us on <a href="https://twitter.com/@OpenBSDAms">Twitter</a> and <a href="https://bsd.network/@OpenBSDAms">Mastodon</a>.</p>