2 <html lang="en" class="black bg-white sans-serif lh-copy">
3 <title>RRDtool looks nicer... - High5!</title>
6 <h1 id="RRDtool%20looks%20nicer...">RRDtool looks nicer...</h1>
8 <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 <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 <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>
20 <h3 id="uptime.sh">uptime.sh</h3>
23 test -n "$1" || exit 1
25 COMMUNITY="public"
26 UPTIMEINFO="/tmp/${HOST}-uptime.txt"
27 TICKS=$(snmpctl snmp get ${HOST} community ${COMMUNITY} oid hrSystemUptime.0 | cut -d= -f2)
28 DAYS=$(echo "${TICKS}/8640000" | bc -l)
29 HOURS=$(echo "0.${DAYS##*.} * 24" | bc -l)
30 MINUTES=$(echo "0.${HOURS##*.} * 60" | bc -l)
31 SECS=$(echo "0.${MINUTES##*.} * 60" | bc -l)
32 test -n "$DAYS" && printf '%s days, ' "${DAYS%.*}" > ${UPTIMEINFO}
33 printf '%02d\\:%02d\\:%02d\n' "${HOURS%.*}" "${MINUTES%.*}" "${SECS%.*}" >> ${UPTIMEINFO}
36 <p>This is a seperate script, due to the uptime usage of both hosts in both graphs. </p>
38 <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 <h3 id="cpu_load.sh">cpu_load.sh</h3>
42 <pre><code>test -n "$1" || exit 1
44 COMMUNITY="public"
45 RRDFILES="/var/rrdtool"
46 IMAGES="/var/www/htdocs"
47 WATERMARK="OpenBSD Amsterdam - https://obsda.ms"
48 RRDTOOL="/usr/local/bin/rrdtool"
49 CPUINFO="/tmp/${HOST}-cpu.txt"
50 UPTIME=$(cat /tmp/${HOST}-uptime.txt)
51 NOW=$(date "+%Y-%m-%d %H:%M:%S %Z" | sed 's/:/\\:/g')
53 if ! test -f "${RRDFILES}/${HOST}-cpu.rrd"
55 echo "Creating ${RRDFILES}/${HOST}-cpu.rrd"
56 ${RRDTOOL} create ${RRDFILES}/${HOST}-cpu.rrd \
58 DS:ds0:GAUGE:600:U:U \
62 snmpctl snmp walk ${HOST} community ${COMMUNITY} oid hrProcessorLoad | cut -d= -f2 > ${CPUINFO}
63 CORES=$(grep -cv "^0$" ${CPUINFO})
64 CPU_LOAD_SUM=$(awk '{sum += $1} END {print sum}' ${CPUINFO})
65 CPU_LOAD=$(echo "scale=2; ${CPU_LOAD_SUM}/${CORES}" | bc -l)
67 ${RRDTOOL} update ${RRDFILES}/${HOST}-cpu.rrd N:${CPU_LOAD}
69 ${RRDTOOL} graph ${IMAGES}/${HOST}-cpu.png \
71 --title "${HOST} - CPU" \
72 --vertical-label "% CPU Used" \
73 --watermark "${WATERMARK}" \
74 DEF:CPU=${RRDFILES}/${HOST}-cpu.rrd:ds0:AVERAGE \
76 LINE2:CPU#CC0033:"CPU" \
77 GPRINT:CPU:MAX:"Max\:%2.2lf %s" \
78 GPRINT:CPU:AVERAGE:"Average\:%2.2lf %s" \
79 GPRINT:CPU:LAST:" Current\:%2.2lf %s\n" \
80 COMMENT:"\\n" \
81 COMMENT:" SUM CPU Load / Active Cores = % CPU Used\n" \
82 COMMENT:" Up for ${UPTIME} at ${NOW}"
85 <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 <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 <h3 id="interface.sh">interface.sh</h3>
91 <pre><code>test -n "$1" || exit 1
92 test -n "$2" || exit 1
94 INTERFACE="$2"
95 COMMUNITY="public"
96 RRDFILES="/var/rrdtool"
97 IMAGES="/var/www/htdocs"
98 WATERMARK="OpenBSD Amsterdam - https://obsda.ms"
99 RRDTOOL="/usr/local/bin/rrdtool"
100 UPTIME=$(cat /tmp/${HOST}-uptime.txt)
101 NOW=$(date "+%Y-%m-%d %H:%M:%S %Z" | sed 's/:/\\:/g')
103 if ! test -f "${RRDFILES}/${HOST}-${INTERFACE}.rrd"
105 echo "Creating ${RRDFILES}/${HOST}-${INTERFACE}.rrd"
106 ${RRDTOOL} create ${RRDFILES}/${HOST}-${INTERFACE}.rrd \
108 DS:ds0:COUNTER:600:0:1250000000 \
109 DS:ds1:COUNTER:600:0:1250000000 \
110 RRA:AVERAGE:0.5:1:600 \
111 RRA:AVERAGE:0.5:6:700 \
112 RRA:AVERAGE:0.5:24:775 \
113 RRA:AVERAGE:0.5:288:797 \
120 IN=$(snmpctl snmp get ${HOST} community ${COMMUNITY} oid ifInOctets.${INTERFACE} | cut -d= -f2)
121 OUT=$(snmpctl snmp get ${HOST} community ${COMMUNITY} oid ifOutOctets.${INTERFACE} | cut -d= -f2)
122 DESCR=$(snmpctl snmp get ${HOST} community ${COMMUNITY} oid ifDescr.${INTERFACE} | cut -d= -f2 | tr
125 ${RRDTOOL} update ${RRDFILES}/${HOST}-${INTERFACE}.rrd N:${IN}:${OUT}
127 ${RRDTOOL} graph ${IMAGES}/${HOST}-${INTERFACE}.png \
129 --title "${HOST} - ${DESCR}" \
130 --vertical-label "Bits per Second" \
131 --watermark "${WATERMARK}" \
132 DEF:IN=${RRDFILES}/${HOST}-${INTERFACE}.rrd:ds0:AVERAGE \
133 DEF:OUT=${RRDFILES}/${HOST}-${INTERFACE}.rrd:ds1:AVERAGE \
134 CDEF:IN_CDEF="IN,8,*" \
135 CDEF:OUT_CDEF="OUT,8,*" \
136 AREA:IN_CDEF#00FF00:"In " \
137 GPRINT:IN_CDEF:MAX:"Max\:%5.2lf %s" \
138 GPRINT:IN_CDEF:AVERAGE:"Average\:%5.2lf %s" \
139 GPRINT:IN_CDEF:LAST:" Current\:%5.2lf %s\n" \
140 LINE2:OUT_CDEF#0000FF:"Out" \
141 GPRINT:OUT_CDEF:MAX:"Max\:%5.2lf %s" \
142 GPRINT:OUT_CDEF:AVERAGE:"Average\:%5.2lf %s" \
143 GPRINT:OUT_CDEF:LAST:" Current\:%5.2lf %s\n" \
144 COMMENT:"\\n" \
145 COMMENT:" Up for ${UPTIME} at ${NOW}"
148 <p>To pinpoint the network interface you want to measure the bandwith for, this command prints the available interfaces:</p>
150 <pre><code>snmpctl snmp walk community oid ifDescr
153 <p>This will output a list like:</p>
155 <pre><code>ifDescr.1="em0"
156 ifDescr.2="em1"
157 ifDescr.3="enc0"
158 ifDescr.4="lo0"
159 ifDescr.5="bridge880"
160 ifDescr.6="vlan880"
161 ifDescr.13="pflog0"
162 ifDescr.669="tap0"
163 ifDescr.670="tap1"
166 <p>The number behind <code>ifDescr</code> is the one that you need to feed to <strong>interface.sh</strong>, for example:</p>
168 <pre><code># interface.sh 5
171 <p>Finally the <strong>wrapper.sh</strong> script calls all the aforementioned scripts:</p>
174 SCRIPTS="/var/rrdtool"
175 for i in $(jot 2 1); do ${SCRIPTS}/uptime.sh host${i}.domain.tld; done
176 for i in $(jot 2 1); do ${SCRIPTS}/cpu_load.sh host${i}.domain.tld; done
177 ${SCRIPTS}/interface.sh host1.domain.tld 12
178 ${SCRIPTS}/interface.sh host2.domain.tld 11
181 <p>The resulting graphs:</p>
183 <p><img src="https://ops.high5.nl/obsda.ms/s1.obsda.ms-cpu.png" alt="" />
184 <img src="https://ops.high5.nl/obsda.ms/s1.obsda.ms-12.png" alt="" /></p>
186 <p>You can follow us on <a href="https://twitter.com/@OpenBSDAms">Twitter</a> and <a href="https://bsd.network/@OpenBSDAms">Mastodon</a>.</p>