@tnwnl said in New Plugin - SNMP:
How do you do this in pimatic
Lets’ assume you’re using the 32-bit octet counters and you have want to calculate the accumulated in- and outbound traffic every 60 seconds, the calculus is the following:
max(∆ ifInOctets + ∆ ifOutOctets) x 8
-------------------------------------
60
which gives you bit per second (bps) for a full duplex interface, where ∆ is the difference between two poll cycles.
To simplify the example further, lets assume you calculate the inbound and outbound traffic separately. Now, let’s have a look at inbound traffic. The calculus above boils down to the following which gives you the bits/second:
(ifInOctetsCurrent - ifInOctetsPrevious) x 8
--------------------------------------------
60
There are various ways of doing this with pimatic. In the following I’ll give just one example and you may find a smarter way of doing this. For the given example lets assume you have a SNMP device “snmp-” which exposes an attribute ifInOctets.
-
Define three variables ifInOctetsCurrent, ifInOctetsPrevious, and trafficIn. Set all valriables to value 0.
-
Create a rule which does the following
WHEN
every 60s
THEN
set ifInOctetsCurrent to $snmp-1.ifInOctets and set $trafficIn to ($ifInOctetsCurrent- $ifInOctetsPrevious) * 8 * 60 after 1 seconds and set $ifInOctetsPrevious to $ifInOctetsCurrent after 2 seconds
The “after 1 seconds” and “after 2 seconds” modifiers is used two sequentialize the actions which depend on each other.
The calculation above does not take into to account “roll over” of the octet counter which generally needs to be handled as follows:
if (ifInOctetsCurrent< ifInOctetsPrevious) then ifInOctetsCurrent = 4294967295 + ifInOctetsCurrent
Unfortunately, pimatic does not provide for conditions as part of the action expression. For this reason, the rule above needs to be broken down inro the 4 rules as follows:
WHEN every 60s and if $snmp-1.ifInOctets >= $ifInOctetsPrevious THEN set $ifInOctetsCurrent to $snmp-1.ifInOctets
WHEN every 60s and if $snmp-1.ifInOctets < $ifInOctetsPrevious THEN set $ifInOctetsCurrent to $snmp-1.ifInOctets + 4294967295
WHEN $ifInOctetsCurrent changes THEN set $trafficIn to ($ifInOctetsCurrent - $ifInOctetsPrevious) * 8 * 60
WHEN $trafficIn changes THEN set $ifInOctetsPrevious to $ifInOctetsCurrent
To be able display the trafficIn variable as a device including the graph you can set a VariablesDevice.
Hope, that makes sense to you
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law