Blob


1 <!DOCTYPE html>
2 <html lang="en" class="black bg-white sans-serif lh-copy">
3 <title>RRDtool looks nicer... - High5!</title>
4 <meta charset="UTF-8">
5 <body>
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&#39;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&#39;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>
14 <ul>
15 <li>uptime.sh</li>
16 <li>cpu_load.sh</li>
17 <li>interface.sh</li>
18 </ul>
20 <h3 id="uptime.sh">uptime.sh</h3>
22 <pre><code>#!/bin/sh
23 test -n &quot;$1&quot; || exit 1
24 HOST=&quot;$1&quot;
25 COMMUNITY=&quot;public&quot;
26 UPTIMEINFO=&quot;/tmp/${HOST}-uptime.txt&quot;
27 TICKS=$(snmpctl snmp get ${HOST} community ${COMMUNITY} oid hrSystemUptime.0 | cut -d= -f2)
28 DAYS=$(echo &quot;${TICKS}/8640000&quot; | bc -l)
29 HOURS=$(echo &quot;0.${DAYS##*.} * 24&quot; | bc -l)
30 MINUTES=$(echo &quot;0.${HOURS##*.} * 60&quot; | bc -l)
31 SECS=$(echo &quot;0.${MINUTES##*.} * 60&quot; | bc -l)
32 test -n &quot;$DAYS&quot; &amp;&amp; printf &#39;%s days, &#39; &quot;${DAYS%.*}&quot; &gt; ${UPTIMEINFO}
33 printf &#39;%02d\\:%02d\\:%02d\n&#39; &quot;${HOURS%.*}&quot; &quot;${MINUTES%.*}&quot; &quot;${SECS%.*}&quot; &gt;&gt; ${UPTIMEINFO}
34 </code></pre>
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 &quot;$1&quot; || exit 1
43 HOST=&quot;$1&quot;
44 COMMUNITY=&quot;public&quot;
45 RRDFILES=&quot;/var/rrdtool&quot;
46 IMAGES=&quot;/var/www/htdocs&quot;
47 WATERMARK=&quot;OpenBSD Amsterdam - https://obsda.ms&quot;
48 RRDTOOL=&quot;/usr/local/bin/rrdtool&quot;
49 CPUINFO=&quot;/tmp/${HOST}-cpu.txt&quot;
50 UPTIME=$(cat /tmp/${HOST}-uptime.txt)
51 NOW=$(date &quot;+%Y-%m-%d %H:%M:%S %Z&quot; | sed &#39;s/:/\\:/g&#39;)
53 if ! test -f &quot;${RRDFILES}/${HOST}-cpu.rrd&quot;
54 then
55 echo &quot;Creating ${RRDFILES}/${HOST}-cpu.rrd&quot;
56 ${RRDTOOL} create ${RRDFILES}/${HOST}-cpu.rrd \
57 --step 300 \
58 DS:ds0:GAUGE:600:U:U \
59 RRA:MAX:0.5:1:20000
60 fi
62 snmpctl snmp walk ${HOST} community ${COMMUNITY} oid hrProcessorLoad | cut -d= -f2 &gt; ${CPUINFO}
63 CORES=$(grep -cv &quot;^0$&quot; ${CPUINFO})
64 CPU_LOAD_SUM=$(awk &#39;{sum += $1} END {print sum}&#39; ${CPUINFO})
65 CPU_LOAD=$(echo &quot;scale=2; ${CPU_LOAD_SUM}/${CORES}&quot; | bc -l)
67 ${RRDTOOL} update ${RRDFILES}/${HOST}-cpu.rrd N:${CPU_LOAD}
69 ${RRDTOOL} graph ${IMAGES}/${HOST}-cpu.png \
70 --start -43200 \
71 --title &quot;${HOST} - CPU&quot; \
72 --vertical-label &quot;% CPU Used&quot; \
73 --watermark &quot;${WATERMARK}&quot; \
74 DEF:CPU=${RRDFILES}/${HOST}-cpu.rrd:ds0:AVERAGE \
75 AREA:CPU#FFCC00 \
76 LINE2:CPU#CC0033:&quot;CPU&quot; \
77 GPRINT:CPU:MAX:&quot;Max\:%2.2lf %s&quot; \
78 GPRINT:CPU:AVERAGE:&quot;Average\:%2.2lf %s&quot; \
79 GPRINT:CPU:LAST:&quot; Current\:%2.2lf %s\n&quot; \
80 COMMENT:&quot;\\n&quot; \
81 COMMENT:&quot; SUM CPU Load / Active Cores = % CPU Used\n&quot; \
82 COMMENT:&quot; Up for ${UPTIME} at ${NOW}&quot;
83 </code></pre>
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 &quot;$1&quot; || exit 1
92 test -n &quot;$2&quot; || exit 1
93 HOST=&quot;$1&quot;
94 INTERFACE=&quot;$2&quot;
95 COMMUNITY=&quot;public&quot;
96 RRDFILES=&quot;/var/rrdtool&quot;
97 IMAGES=&quot;/var/www/htdocs&quot;
98 WATERMARK=&quot;OpenBSD Amsterdam - https://obsda.ms&quot;
99 RRDTOOL=&quot;/usr/local/bin/rrdtool&quot;
100 UPTIME=$(cat /tmp/${HOST}-uptime.txt)
101 NOW=$(date &quot;+%Y-%m-%d %H:%M:%S %Z&quot; | sed &#39;s/:/\\:/g&#39;)
103 if ! test -f &quot;${RRDFILES}/${HOST}-${INTERFACE}.rrd&quot;
104 then
105 echo &quot;Creating ${RRDFILES}/${HOST}-${INTERFACE}.rrd&quot;
106 ${RRDTOOL} create ${RRDFILES}/${HOST}-${INTERFACE}.rrd \
107 --step 300 \
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 \
114 RRA:MAX:0.5:1:600 \
115 RRA:MAX:0.5:6:700 \
116 RRA:MAX:0.5:24:775 \
117 RRA:MAX:0.5:288:797
118 fi
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
123 -d &#39;&quot;&#39;)
125 ${RRDTOOL} update ${RRDFILES}/${HOST}-${INTERFACE}.rrd N:${IN}:${OUT}
127 ${RRDTOOL} graph ${IMAGES}/${HOST}-${INTERFACE}.png \
128 --start -43200 \
129 --title &quot;${HOST} - ${DESCR}&quot; \
130 --vertical-label &quot;Bits per Second&quot; \
131 --watermark &quot;${WATERMARK}&quot; \
132 DEF:IN=${RRDFILES}/${HOST}-${INTERFACE}.rrd:ds0:AVERAGE \
133 DEF:OUT=${RRDFILES}/${HOST}-${INTERFACE}.rrd:ds1:AVERAGE \
134 CDEF:IN_CDEF=&quot;IN,8,*&quot; \
135 CDEF:OUT_CDEF=&quot;OUT,8,*&quot; \
136 AREA:IN_CDEF#00FF00:&quot;In &quot; \
137 GPRINT:IN_CDEF:MAX:&quot;Max\:%5.2lf %s&quot; \
138 GPRINT:IN_CDEF:AVERAGE:&quot;Average\:%5.2lf %s&quot; \
139 GPRINT:IN_CDEF:LAST:&quot; Current\:%5.2lf %s\n&quot; \
140 LINE2:OUT_CDEF#0000FF:&quot;Out&quot; \
141 GPRINT:OUT_CDEF:MAX:&quot;Max\:%5.2lf %s&quot; \
142 GPRINT:OUT_CDEF:AVERAGE:&quot;Average\:%5.2lf %s&quot; \
143 GPRINT:OUT_CDEF:LAST:&quot; Current\:%5.2lf %s\n&quot; \
144 COMMENT:&quot;\\n&quot; \
145 COMMENT:&quot; Up for ${UPTIME} at ${NOW}&quot;
146 </code></pre>
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
151 </code></pre>
153 <p>This will output a list like:</p>
155 <pre><code>ifDescr.1=&quot;em0&quot;
156 ifDescr.2=&quot;em1&quot;
157 ifDescr.3=&quot;enc0&quot;
158 ifDescr.4=&quot;lo0&quot;
159 ifDescr.5=&quot;bridge880&quot;
160 ifDescr.6=&quot;vlan880&quot;
161 ifDescr.13=&quot;pflog0&quot;
162 ifDescr.669=&quot;tap0&quot;
163 ifDescr.670=&quot;tap1&quot;
164 </code></pre>
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
169 </code></pre>
171 <p>Finally the <strong>wrapper.sh</strong> script calls all the aforementioned scripts:</p>
173 <pre><code>#!/bin/sh
174 SCRIPTS=&quot;/var/rrdtool&quot;
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
179 </code></pre>
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>
187 </body>
188 </html>