How to make an SNMP Counter32 continuous

Looking for some help, while the attached flow gets me the counter32 for in and out traffic on my local router I fall into the shortcomings of counter32 resetting

The Counter32 type represents a non-negative integer which monotonically increases until it reaches a maximum value of 2^32-1 (4294967295 decimal), when it wraps around and starts increasing again from zero.

I need to find a solution for keeping the counter going somehow

[{"id":"77df699bd0d7783b","type":"comment","z":"fe08443c6ec4dd06","name":"snmp db log","info":"table snmp\nun snmp\npwd\n\nCREATE TABLE pppoe_stats(\nentry_id int auto_increment,\nstatus varchar(255) not null,\ncheck_date timestamp default current_timestamp,\nprimary key(entry_id)\n);\n    \nCREATE TABLE pppoe_interface_stats(\nentry_id int auto_increment,\nDown_Bytes text,\nUp_Bytes text,\ncheck_date timestamp default current_timestamp,\nprimary key(entry_id)\n);    \n\nINSERT into pppoe_stats (status) values ('UP');","x":790,"y":320,"wires":[]}]

Hi, even if we provided a method of handling the wrap around, you would soon hit the the upper limit of JS numbers (2^53 -1)

you could of course go to BigInt but there are issues related to that.

A better solution might be to only store the difference between reads. When the value wraps around, some simple maths to calculate new value from previous number and 2^32-1 can be done.


PS, you only posted a single comment node in your flow export. When exporting flows, be sure to select all the nodes of interest first. Also, ensure it is surrounded by triple backticks above and below
```
like this
```

1 Like

I think the hardest piece is understanding the wrap around its all about usage

For a 10 Mbps interface, a 32-bit counter could theoretically wrap in 57 minutes. It is easy to avoid discontinuities with such a long period. But for 100 Mbps, the minimum theoretical wrap time is 5.7 minutes. For 1 Gbps interfaces, it falls to 34 seconds.

Basically every 5gig the counters wrap not sure what my best logic is

Fixed the post also danke

If you have an interface that wraps the counter at 34 secs, then you should probably be polling the SNMP item every 30 secs.

The math is pretty easy.
Store the last value in flow context. Then when a new value arrives, deduct the previous value. If the result is < 0, then add 4294967295.

example...
chrome_NgbDW6WhiQ

[{"id":"1ef2f41d2a608de7","type":"inject","z":"a55f8f0f93e84992","name":"set 4294966095","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"set","payload":"4294966095","payloadType":"num","x":1540,"y":140,"wires":[["68487ffb889abffe"]]},{"id":"68487ffb889abffe","type":"function","z":"a55f8f0f93e84992","name":"monitor counter","func":"if(msg.topic == \"reset\") {\n    context.set(\"previous\", null);\n    node.status({});\n    return null;\n}\n\nif (msg.topic == \"set\") {\n    context.set(\"previous\", msg.payload);\n    node.status({});\n    return null;\n}\n\nvar prev = context.get(\"previous\");\nvar current = msg.payload;\nif(prev == null) {\n    context.set(\"previous\", current);\n    return null;\n}\n\n\nvar diff = current - prev;\n\nif(diff < 0) {\n    diff += 4294967295;\n}\ncontext.set(\"previous\", current);\nnode.status({ fill: \"green\", shape: \"ring\", text: \"difference: \" + diff});\n\nmsg.payload = {\n    current: current,\n    previous: prev,\n    difference: diff\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1740,"y":200,"wires":[["3bb3c48bba1a2509"]]},{"id":"3bb3c48bba1a2509","type":"debug","z":"a55f8f0f93e84992","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1770,"y":140,"wires":[]},{"id":"f427389ca98440a2","type":"inject","z":"a55f8f0f93e84992","name":"data 100","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":1520,"y":200,"wires":[["68487ffb889abffe"]]},{"id":"06926531b812f824","type":"inject","z":"a55f8f0f93e84992","name":"data 300","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"300","payloadType":"num","x":1520,"y":240,"wires":[["68487ffb889abffe"]]},{"id":"b534595d87913956","type":"inject","z":"a55f8f0f93e84992","name":"data 1188","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1188","payloadType":"num","x":1520,"y":280,"wires":[["68487ffb889abffe"]]},{"id":"d0895a6b6367f19d","type":"inject","z":"a55f8f0f93e84992","name":"data 4294967000","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4294967000","payloadType":"num","x":1540,"y":320,"wires":[["68487ffb889abffe"]]}]

This outputs the following format message...

{"current":300,"previous":100,"difference":200}

You can then use these in your SQL parameters - along with a time stamp.

1 Like

Thanks for the advice I will go play with the logic to deal with the snmp payloads coming out appreciate it.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.