Blame


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">
5 ef118436 2019-03-14 mischa <body>
6 ef118436 2019-03-14 mischa <h1 id="RRDtool%20looks%20nicer...">RRDtool looks nicer...</h1>
7 ef118436 2019-03-14 mischa
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&#39;t look very pretty.</p>
9 ef118436 2019-03-14 mischa
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&#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>
11 ef118436 2019-03-14 mischa
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>
13 ef118436 2019-03-14 mischa
14 ef118436 2019-03-14 mischa <ul>
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>
18 ef118436 2019-03-14 mischa </ul>
19 ef118436 2019-03-14 mischa
20 ef118436 2019-03-14 mischa <h3 id="uptime.sh">uptime.sh</h3>
21 ef118436 2019-03-14 mischa
22 ef118436 2019-03-14 mischa <pre><code>#!/bin/sh
23 ef118436 2019-03-14 mischa test -n &quot;$1&quot; || exit 1
24 ef118436 2019-03-14 mischa HOST=&quot;$1&quot;
25 ef118436 2019-03-14 mischa COMMUNITY=&quot;public&quot;
26 ef118436 2019-03-14 mischa UPTIMEINFO=&quot;/tmp/${HOST}-uptime.txt&quot;
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 &quot;${TICKS}/8640000&quot; | bc -l)
29 ef118436 2019-03-14 mischa HOURS=$(echo &quot;0.${DAYS##*.} * 24&quot; | bc -l)
30 ef118436 2019-03-14 mischa MINUTES=$(echo &quot;0.${HOURS##*.} * 60&quot; | bc -l)
31 ef118436 2019-03-14 mischa SECS=$(echo &quot;0.${MINUTES##*.} * 60&quot; | bc -l)
32 ef118436 2019-03-14 mischa test -n &quot;$DAYS&quot; &amp;&amp; printf &#39;%s days, &#39; &quot;${DAYS%.*}&quot; &gt; ${UPTIMEINFO}
33 ef118436 2019-03-14 mischa printf &#39;%02d\\:%02d\\:%02d\n&#39; &quot;${HOURS%.*}&quot; &quot;${MINUTES%.*}&quot; &quot;${SECS%.*}&quot; &gt;&gt; ${UPTIMEINFO}
34 ef118436 2019-03-14 mischa </code></pre>
35 ef118436 2019-03-14 mischa
36 ef118436 2019-03-14 mischa <p>This is a seperate script, due to the uptime usage of both hosts in both graphs. </p>
37 ef118436 2019-03-14 mischa
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>
39 ef118436 2019-03-14 mischa
40 ef118436 2019-03-14 mischa <h3 id="cpu_load.sh">cpu_load.sh</h3>
41 ef118436 2019-03-14 mischa
42 ef118436 2019-03-14 mischa <pre><code>test -n &quot;$1&quot; || exit 1
43 ef118436 2019-03-14 mischa HOST=&quot;$1&quot;
44 ef118436 2019-03-14 mischa COMMUNITY=&quot;public&quot;
45 ef118436 2019-03-14 mischa RRDFILES=&quot;/var/rrdtool&quot;
46 ef118436 2019-03-14 mischa IMAGES=&quot;/var/www/htdocs&quot;
47 ef118436 2019-03-14 mischa WATERMARK=&quot;OpenBSD Amsterdam - https://obsda.ms&quot;
48 ef118436 2019-03-14 mischa RRDTOOL=&quot;/usr/local/bin/rrdtool&quot;
49 ef118436 2019-03-14 mischa CPUINFO=&quot;/tmp/${HOST}-cpu.txt&quot;
50 ef118436 2019-03-14 mischa UPTIME=$(cat /tmp/${HOST}-uptime.txt)
51 ef118436 2019-03-14 mischa NOW=$(date &quot;+%Y-%m-%d %H:%M:%S %Z&quot; | sed &#39;s/:/\\:/g&#39;)
52 ef118436 2019-03-14 mischa
53 ef118436 2019-03-14 mischa if ! test -f &quot;${RRDFILES}/${HOST}-cpu.rrd&quot;
54 ef118436 2019-03-14 mischa then
55 ef118436 2019-03-14 mischa echo &quot;Creating ${RRDFILES}/${HOST}-cpu.rrd&quot;
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
60 ef118436 2019-03-14 mischa fi
61 ef118436 2019-03-14 mischa
62 ef118436 2019-03-14 mischa snmpctl snmp walk ${HOST} community ${COMMUNITY} oid hrProcessorLoad | cut -d= -f2 &gt; ${CPUINFO}
63 ef118436 2019-03-14 mischa CORES=$(grep -cv &quot;^0$&quot; ${CPUINFO})
64 ef118436 2019-03-14 mischa CPU_LOAD_SUM=$(awk &#39;{sum += $1} END {print sum}&#39; ${CPUINFO})
65 ef118436 2019-03-14 mischa CPU_LOAD=$(echo &quot;scale=2; ${CPU_LOAD_SUM}/${CORES}&quot; | bc -l)
66 ef118436 2019-03-14 mischa
67 ef118436 2019-03-14 mischa ${RRDTOOL} update ${RRDFILES}/${HOST}-cpu.rrd N:${CPU_LOAD}
68 ef118436 2019-03-14 mischa
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 &quot;${HOST} - CPU&quot; \
72 ef118436 2019-03-14 mischa --vertical-label &quot;% CPU Used&quot; \
73 ef118436 2019-03-14 mischa --watermark &quot;${WATERMARK}&quot; \
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:&quot;CPU&quot; \
77 ef118436 2019-03-14 mischa GPRINT:CPU:MAX:&quot;Max\:%2.2lf %s&quot; \
78 ef118436 2019-03-14 mischa GPRINT:CPU:AVERAGE:&quot;Average\:%2.2lf %s&quot; \
79 ef118436 2019-03-14 mischa GPRINT:CPU:LAST:&quot; Current\:%2.2lf %s\n&quot; \
80 ef118436 2019-03-14 mischa COMMENT:&quot;\\n&quot; \
81 ef118436 2019-03-14 mischa COMMENT:&quot; SUM CPU Load / Active Cores = % CPU Used\n&quot; \
82 ef118436 2019-03-14 mischa COMMENT:&quot; Up for ${UPTIME} at ${NOW}&quot;
83 ef118436 2019-03-14 mischa </code></pre>
84 ef118436 2019-03-14 mischa
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>
86 ef118436 2019-03-14 mischa
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>
88 ef118436 2019-03-14 mischa
89 ef118436 2019-03-14 mischa <h3 id="interface.sh">interface.sh</h3>
90 ef118436 2019-03-14 mischa
91 ef118436 2019-03-14 mischa <pre><code>test -n &quot;$1&quot; || exit 1
92 ef118436 2019-03-14 mischa test -n &quot;$2&quot; || exit 1
93 ef118436 2019-03-14 mischa HOST=&quot;$1&quot;
94 ef118436 2019-03-14 mischa INTERFACE=&quot;$2&quot;
95 ef118436 2019-03-14 mischa COMMUNITY=&quot;public&quot;
96 ef118436 2019-03-14 mischa RRDFILES=&quot;/var/rrdtool&quot;
97 ef118436 2019-03-14 mischa IMAGES=&quot;/var/www/htdocs&quot;
98 ef118436 2019-03-14 mischa WATERMARK=&quot;OpenBSD Amsterdam - https://obsda.ms&quot;
99 ef118436 2019-03-14 mischa RRDTOOL=&quot;/usr/local/bin/rrdtool&quot;
100 ef118436 2019-03-14 mischa UPTIME=$(cat /tmp/${HOST}-uptime.txt)
101 ef118436 2019-03-14 mischa NOW=$(date &quot;+%Y-%m-%d %H:%M:%S %Z&quot; | sed &#39;s/:/\\:/g&#39;)
102 ef118436 2019-03-14 mischa
103 ef118436 2019-03-14 mischa if ! test -f &quot;${RRDFILES}/${HOST}-${INTERFACE}.rrd&quot;
104 ef118436 2019-03-14 mischa then
105 ef118436 2019-03-14 mischa echo &quot;Creating ${RRDFILES}/${HOST}-${INTERFACE}.rrd&quot;
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
118 ef118436 2019-03-14 mischa fi
119 ef118436 2019-03-14 mischa
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 &#39;&quot;&#39;)
124 ef118436 2019-03-14 mischa
125 ef118436 2019-03-14 mischa ${RRDTOOL} update ${RRDFILES}/${HOST}-${INTERFACE}.rrd N:${IN}:${OUT}
126 ef118436 2019-03-14 mischa
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 &quot;${HOST} - ${DESCR}&quot; \
130 ef118436 2019-03-14 mischa --vertical-label &quot;Bits per Second&quot; \
131 ef118436 2019-03-14 mischa --watermark &quot;${WATERMARK}&quot; \
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=&quot;IN,8,*&quot; \
135 ef118436 2019-03-14 mischa CDEF:OUT_CDEF=&quot;OUT,8,*&quot; \
136 ef118436 2019-03-14 mischa AREA:IN_CDEF#00FF00:&quot;In &quot; \
137 ef118436 2019-03-14 mischa GPRINT:IN_CDEF:MAX:&quot;Max\:%5.2lf %s&quot; \
138 ef118436 2019-03-14 mischa GPRINT:IN_CDEF:AVERAGE:&quot;Average\:%5.2lf %s&quot; \
139 ef118436 2019-03-14 mischa GPRINT:IN_CDEF:LAST:&quot; Current\:%5.2lf %s\n&quot; \
140 ef118436 2019-03-14 mischa LINE2:OUT_CDEF#0000FF:&quot;Out&quot; \
141 ef118436 2019-03-14 mischa GPRINT:OUT_CDEF:MAX:&quot;Max\:%5.2lf %s&quot; \
142 ef118436 2019-03-14 mischa GPRINT:OUT_CDEF:AVERAGE:&quot;Average\:%5.2lf %s&quot; \
143 ef118436 2019-03-14 mischa GPRINT:OUT_CDEF:LAST:&quot; Current\:%5.2lf %s\n&quot; \
144 ef118436 2019-03-14 mischa COMMENT:&quot;\\n&quot; \
145 ef118436 2019-03-14 mischa COMMENT:&quot; Up for ${UPTIME} at ${NOW}&quot;
146 ef118436 2019-03-14 mischa </code></pre>
147 ef118436 2019-03-14 mischa
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>
149 ef118436 2019-03-14 mischa
150 ef118436 2019-03-14 mischa <pre><code>snmpctl snmp walk community oid ifDescr
151 ef118436 2019-03-14 mischa </code></pre>
152 ef118436 2019-03-14 mischa
153 ef118436 2019-03-14 mischa <p>This will output a list like:</p>
154 ef118436 2019-03-14 mischa
155 ef118436 2019-03-14 mischa <pre><code>ifDescr.1=&quot;em0&quot;
156 ef118436 2019-03-14 mischa ifDescr.2=&quot;em1&quot;
157 ef118436 2019-03-14 mischa ifDescr.3=&quot;enc0&quot;
158 ef118436 2019-03-14 mischa ifDescr.4=&quot;lo0&quot;
159 ef118436 2019-03-14 mischa ifDescr.5=&quot;bridge880&quot;
160 ef118436 2019-03-14 mischa ifDescr.6=&quot;vlan880&quot;
161 ef118436 2019-03-14 mischa ifDescr.13=&quot;pflog0&quot;
162 ef118436 2019-03-14 mischa ifDescr.669=&quot;tap0&quot;
163 ef118436 2019-03-14 mischa ifDescr.670=&quot;tap1&quot;
164 ef118436 2019-03-14 mischa </code></pre>
165 ef118436 2019-03-14 mischa
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>
167 ef118436 2019-03-14 mischa
168 ef118436 2019-03-14 mischa <pre><code># interface.sh 5
169 ef118436 2019-03-14 mischa </code></pre>
170 ef118436 2019-03-14 mischa
171 ef118436 2019-03-14 mischa <p>Finally the <strong>wrapper.sh</strong> script calls all the aforementioned scripts:</p>
172 ef118436 2019-03-14 mischa
173 ef118436 2019-03-14 mischa <pre><code>#!/bin/sh
174 ef118436 2019-03-14 mischa SCRIPTS=&quot;/var/rrdtool&quot;
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>
180 ef118436 2019-03-14 mischa
181 ef118436 2019-03-14 mischa <p>The resulting graphs:</p>
182 ef118436 2019-03-14 mischa
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>
185 ef118436 2019-03-14 mischa
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>
187 ef118436 2019-03-14 mischa </body>
188 ef118436 2019-03-14 mischa </html>