[SOLVED] SNMPget does not work

It’s always good to learn something new :grin:. Actually I did it because of three reasons:

  1. Laziness: I currently also have another virtual instance running Observium, just to keep track of volumes, loads, suspicious traffic and port states. That’s another system to maintain and to look at. Now (or fully functioning in the future) I just use another dashboard in Grafana and send my alarms to my Telegram Bot.
  2. Look: Grafana looks much better than these “old fashioned” CACTI graphs. Yes, they work and yes they are sometimes easier to implement. But there is only one way to display the data.
  3. Daily Business: I’ve come along multiple Grafana installation during my work, always focussing on complete different data and then also differentiating between a technical view and a “management” view.

But yes as you said, it is currently a bit more configuration to be done. I think not every destination device needs the calculation, I was happy to get it working with that way for now. Maybe I find out the correct OID for throughput :face_with_raised_eyebrow:. But I think with a small script this “addSNMPItem”-process gets easy.
Openhab will help to watch my switchports(state), I saw someone adding printer ink level, so there is really a lot more to add, without using another different tool.

Regarding the Performance: I currently run 12 + 48 SNMP request every 60s, and it does nothing really noticable to my 1GB RAM VM:

  • Java openhab is taking ~50% all the time
  • Grafana takes ~3% every minute
  • influxdb takes ~8% every now and then (I guess if incoming data is processed)

I think, for a big installation just for collecting snmp from over 10.000+ sources openhab2 will not be in my consideration, but for a small “AllinOne”-solution it is really usefull.

1 Like

Thanks for this, looks great. Using your Rules and Items I’m having an issue however. I get the error below, which I believe is something to do with persistence not working as I’d expect.

[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Internet Traffic Out, calculate difference': 'previousStateValue' is not a member of 'org.eclipse.smarthome.core.library.items.NumberItem'; line 16, column 35, length 39

Here’s my jdbc.persistence config

Strategies {
  default = everyUpdate
}

Items {
  * : strategy = everyChange, restoreOnStartup
}

Any ideas?

the error is related to the rule
did you modify anything versus what Maximo posted? (watch out: his second rule is cut off and he didn’t list all Items)

it should be:

rule "fw traffic dmz out updated, calculate difference"
when
    Item fw_traffic_dmz_out received update
then
    var float currentStateValue = (fw_traffic_dmz_out.state as DecimalType).floatValue
    var float previousStateValue = (fw_traffic_dmz_out.previousState.state as DecimalType).floatValue
    var float diff = (currentStateValue - previousStateValue)/8
    fw_traffic_dmz_out_diff.postUpdate(diff)
end

you need to create the corresponding Items of course.
He gave some examples, not the complete list

Here’s the rules

rule "Internet Traffic In, calculate difference"
when
        Item Internet_Traffic_In received update
then
        var Number currentStateValue = (Internet_Traffic_In.state as DecimalType)
        var Number previousStateValue = (Internet_Traffic_In.previousStateValue.state as DecimalType)
        var Number diff = (currentStateValue - previousStateValue)/8
        internet_in_diff.postUpdate(diff)
end

rule "Internet Traffic Out, calculate difference"
when
        Item Internet_Traffic_Out received update
then
        var Number currentStateValue = (Internet_Traffic_Out.state as DecimalType)
        var Number previousStateValue = (Internet_Traffic_Out.previousStateValue.state as DecimalType)
        var Number diff = (currentStateValue - previousStateValue)/8
        internet_out_diff.postUpdate(diff)
end

can you post your Items also plz

Why var Number and not var float ?

Here you go.

Number Internet_Traffic_In  "Internet Traffic IN" { snmp="<[46.33.148.73:v2c:public:.1.3.6.1.2.1.2.2.1.10.20:60000]" }
Number internet_in_diff "wan1indiff" (Internet)
Number Internet_Traffic_Out  "Internet Traffic OUT" { snmp="<[46.33.148.73:v2c:public:.1.3.6.1.2.1.2.2.1.16.20:60000]" }
Number internet_out_diff "wan1outdiff" (Internet)

The data I receive from my router isn’t a floating point value. That’s why I change float to Number.

var Number previousStateValue = (Internet_Traffic_Out.previousState.state as DecimalType)

I now get this error.

[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Internet Traffic In, calculate difference': cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null

did you apply the same fix here also?

Sure did, I’m still thinking it’s something to do with persistence not working right.

yeah… seems that the previousState returns null

is JDBC your default ?

Yes JDBC is my default, I also have a number of states going to influxdb that I use with Grafana.

if you recently deployed JDBC to make this work: try to troubleshoot it
post the services/jdbc.cfg and check the logs for related entries
check also the destination DB for entered data

Hej @Maximo

in the meantime I found a other way to get and display the “difference”-value in Grafana and no longer use the rule in Openhab.
traffic_wan_in_new

It’s not the solution for your error, but maybe the calculation gets needless.

2 Likes

Thanks @tmcgee I’ll give that a go and see what results I get.

I think I’ve got an issue with my persistence configuration and that’s why I’m getting the error. So I need to troubleshoot that and get it working. After doing a reboot of my server I found that all of my previous values and states were null. So it would suggest either I’ve changed something and got it wrong or because I’m using two different persistence databases I’ve caused an unusual scenario.

Maybe a good time to complete a refresh build of my server and update to the latest version of openHAB.

Once again, thanks for your help and to everyone else that has contributed to this thread.

1 Like

I am trying to make your solution work, but I am a little confused.

I think the data might be incorrect this way as you are polling the difference on SNMP every 60 seconds, do you not need to divide it by 60 at some point to get a correct value?

I was loading my network down and the numbers I get does not seem right. maybe we need some more calculation to get the values to be correct?

By the way I am using the following script to change it to Mbps but it does also not take in the factor of the polling every 60 seconds

(function getReadableFileSizeString(fileSizeInBytes) {

    var i = -1;
    var byteUnits = [' kbps', ' Mbps', ' Gbps', ' Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'];
    do {
        fileSizeInBytes = fileSizeInBytes / 1024;
        i++;
    } while (fileSizeInBytes > 1024);

    return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
    })(input)

I think the correct way might be? or am I wrong here

rule "WAN_IN, calculate difference" 
when
    Item WAN_IN received update 
then
    var float currentStateValue = (WAN_IN.state as DecimalType).floatValue
    var float previousStateValue = (WAN_IN.historicState(now.minusSeconds(60)).state as DecimalType).floatValue
    var float diff = (currentStateValue - previousStateValue)*8/60
    WAN_IN_diff.postUpdate(diff)
end

My item file

Number WAN_IN_diff (Bandwith_Chart)
Number WAN_IN  { snmp="<[192.168.1.1:public:.1.3.6.1.2.1.2.2.1.10.4:60000]" }

and for my sitemap I use the following

Default item=LAN_IN_diff label="LAN IN[JS(bps.js):%s]"

Hej @treks

to be honest, I stopped investigating and changing my function, because I found a better (easier) way in grafana.
But I think you are right, I forgot the devision by the time between two values.

Does it work with the “now.minusSecond(60)”? Can’t it happen that you don’t get a value back? Because you only trigger the request every 60 seconds, it could be, that it take 1 second that the value is actually requested. And if the next value is requested on time you might don’t get a value back.

I do sometimes get a wrong reading might have to tweak it a little

Guess I have to start looking at grafana