@ -52,40 +52,36 @@ class SNMPMonitorType extends MonitorType {
throw new Error ( ` No varbinds returned from SNMP session (OID: ${ monitor . snmpOid } ) ` ) ;
throw new Error ( ` No varbinds returned from SNMP session (OID: ${ monitor . snmpOid } ) ` ) ;
} else {
} else {
const value = varbinds [ 0 ] . value ;
const value = varbinds [ 0 ] . value ;
const numericValue = parseInt ( value ) ;
const stringValue = value . toString ( "ascii" ) ;
// Check if inputs are numeric. If not, re-parse as strings. This ensures comparisons are handled correctly.
let snmpValue = isNaN ( value ) ? value . toString ( ) : parseFloat ( value ) ;
let snmpControlValue = isNaN ( monitor . snmpControlValue ) ? monitor . snmpControlValue . toString ( ) : parseFloat ( monitor . snmpControlValue ) ;
switch ( monitor . snmpCondition ) {
switch ( monitor . snmpCondition ) {
case ">" :
case ">" :
heartbeat . status = numericValue > monitor . snmpControlValue ? UP : DOWN ;
heartbeat . status = snmpValue > snmpControlValue ? UP : DOWN ;
break ;
break ;
case ">=" :
case ">=" :
heartbeat . status = numericValue >= monitor . snmpControlValue ? UP : DOWN ;
heartbeat . status = snmpValue >= snmpControlValue ? UP : DOWN ;
break ;
break ;
case "<" :
case "<" :
heartbeat . status = numericValue < monitor . snmpControlValue ? UP : DOWN ;
heartbeat . status = snmpValue < snmpControlValue ? UP : DOWN ;
break ;
break ;
case "<=" :
case "<=" :
heartbeat . status = numericValue <= monitor . snmpControlValue ? UP : DOWN ;
heartbeat . status = snmpValue <= snmpControlValue ? UP : DOWN ;
break ;
break ;
case "==" :
case "==" :
if ( ! isNaN ( value ) && ! isNaN ( monitor . snmpControlValue ) ) {
heartbeat . status = snmpValue . toString ( ) === snmpControlValue . toString ( ) ? UP : DOWN ;
// Both values are numeric, parse them as numbers
heartbeat . status = parseFloat ( value ) === parseFloat ( monitor . snmpControlValue ) ? UP : DOWN ;
} else {
// At least one of the values is not numeric, compare them as strings
heartbeat . status = value . toString ( ) === monitor . snmpControlValue . toString ( ) ? UP : DOWN ;
}
break ;
break ;
case "contains" :
case "contains" :
heartbeat . status = s tringValue. includes ( monitor . snmpControlValue ) ? UP : DOWN ;
heartbeat . status = snmpValue . toString ( ) . includes ( snmpControlValue . toString ( ) ) ? UP : DOWN ;
break ;
break ;
default :
default :
heartbeat . status = DOWN ;
heartbeat . status = DOWN ;
heartbeat . msg = ` Invalid condition: ${ monitor . snmpCondition } ` ;
heartbeat . msg = ` Invalid condition: ${ monitor . snmpCondition } ` ;
break ;
break ;
}
}
heartbeat . msg = "SNMP value " + ( heartbeat . status ? "passes" : "does not pass" ) + ` comparison: ${ value . toString ( "ascii" ) } ${ monitor . snmpCondition } ${ monitor . snmpControlValue } ` ;
heartbeat . msg = "SNMP value " + ( heartbeat . status ? "passes" : "does not pass" ) + ` comparison: ${ value . toString ( ) } ${ monitor . snmpCondition } ${ snmpControlValue } ` ;
}
}
session . close ( ) ;
session . close ( ) ;