I have set up SNMP binding to monitor my firewall uptime and need some help converting the value received from the device. The firewall sends it’s uptime in hundredths of a second and I want to convert this to a more useful value (seconds, minutes, hours, days).
This is how far I’ve got
snmp.things
Thing snmp:target:Firewall "Firewall"[ hostname="10.16.10.1", protocol="v2c", community="public", refresh="10" ]{
Channels:
Type number : sysUptime [ oid=".1.3.6.1.2.1.1.3.0" ]
}
snmp.items
Number sysUptime "Firewall Uptime [%.0f]" { channel="snmp:target:Firewall:sysUptime" }
snmp.rules
when
Item sysUptime changed
then
var totalMillis = (sysUptime.state as Number).intValue
val int totalSecs = (totalMillis / 100 as Number).intValue
logInfo("SNMP", + totalSecs)
end
The error I get is
2020-03-27 11:35:25.005 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'millis to seconds': An error occurred during the script execution: index=1, size=1
You can also use the Java Duration class so you do not need Javascript transformations.
Example:
import java.time.Duration
rule "millis to seconds"
when
Item sysUptime changed
then
val duration = Duration.ofMillis((sysUptime.state as Number).intValue * 10)
logInfo("SNMP", String.format("%d:%02d:%02d.%03d", duration.toHours(), duration.toMinutesPart(), duration.toSecondsPart(), duration.toMillisPart()))
logInfo("SNMP", Long.toString(duration.getSeconds()))
end
Output:
[smarthome.event.ItemStateChangedEvent] - sysUptime changed from 1200 to 1201
[g.eclipse.smarthome.model.script.SNMP] - 0:00:12.010
[g.eclipse.smarthome.model.script.SNMP] - 12
[smarthome.event.ItemStateChangedEvent] - sysUptime changed from 1201 to 120100
[g.eclipse.smarthome.model.script.SNMP] - 0:20:01.000
[g.eclipse.smarthome.model.script.SNMP] - 1201
[smarthome.event.ItemStateChangedEvent] - sysUptime changed from 120100 to 12345678
[g.eclipse.smarthome.model.script.SNMP] - 34:17:36.780
[g.eclipse.smarthome.model.script.SNMP] - 123456