@ -12,51 +12,76 @@ class SNMPMonitorType extends MonitorType {
* @ param { object } _server Unused server object .
* /
async check ( monitor , heartbeat , _server ) {
console . log ( "IP Address:" , monitor . _hostname ) ;
console . log ( "SNMP Community String:" , monitor . _snmpCommunityString ) ;
console . log ( "SNMP OID:" , monitor . _snmpOid ) ;
console . log ( "SNMP Version:" , monitor . _snmpVersion ) ;
console . log ( "SNMP Condition:" , monitor . _snmpCondition ) ;
console . log ( "SNMP Control Value:" , monitor . _snmpControlValue ) ;
const options = {
port : monitor . _port || 161 ,
retries : 1 ,
timeout : 1000 ,
version : getKey ( snmp . Version , monitor . _snmpVersion ) || snmp . Version2c ,
} ;
function getKey ( obj , value ) {
return Object . keys ( obj ) . find ( key => obj [ key ] === value ) || null ;
}
try {
const session = new snmp . Session ( { host : monitor . ipAddress , community : monitor . snmpCommunityString , version : monitor . snmpVersion } ) ;
const session = snmp . createSession ( monitor . _ipAddress , monitor . _snmpCommunityString , options ) ;
session . get ( { oid : monitor . snmpOid } , ( err , varbinds ) => {
if ( err ) {
heartbeat . status = DOWN ;
heartbeat . msg = ` Error: ${ err . message } ` ;
return ;
}
const varbinds = await new Promise ( ( resolve , reject ) => {
session . get ( [ monitor . _snmpOid ] , ( error , varbinds ) => {
if ( error ) {
reject ( error ) ;
} else {
resolve ( varbinds ) ;
}
} ) ;
} ) ;
// Assuming only one varbind is returned
const value = varbinds [ 0 ] . value ;
console . log ( "Received varbinds:" , varbinds ) ; // Log the received varbinds for debugging
// Convert value to appropriate type based on SNMP type (assuming it's integer or string for simplicity)
if ( varbinds && varbinds . length > 0 ) {
const value = varbinds [ 0 ] . value ;
const numericValue = parseInt ( value ) ;
const stringValue = value . toString ( ) ;
// Check against condition and control value
switch ( monitor . snmpCondition ) {
case '>' :
heartbeat . status = numericValue > monitor . snmpControlValue ? UP : DOWN ;
heartbeat . status = numericValue > monitor . _ snmpControlValue ? UP : DOWN ;
break ;
case '>=' :
heartbeat . status = numericValue >= monitor . snmpControlValue ? UP : DOWN ;
heartbeat . status = numericValue >= monitor . _ snmpControlValue ? UP : DOWN ;
break ;
case '<' :
heartbeat . status = numericValue < monitor . snmpControlValue ? UP : DOWN ;
heartbeat . status = numericValue < monitor . _ snmpControlValue ? UP : DOWN ;
break ;
case '<=' :
heartbeat . status = numericValue <= monitor . snmpControlValue ? UP : DOWN ;
heartbeat . status = numericValue <= monitor . _ snmpControlValue ? UP : DOWN ;
break ;
case '==' :
heartbeat . status = value === monitor . snmpControlValue ? UP : DOWN ;
heartbeat . status = value === monitor . _ snmpControlValue ? UP : DOWN ;
break ;
case 'contains' :
heartbeat . status = stringValue . includes ( monitor . snmpControlValue) ? UP : DOWN ;
heartbeat . status = stringValue . includes ( monitor . _ snmpControlValue) ? UP : DOWN ;
break ;
default :
heartbeat . status = DOWN ;
heartbeat . msg = ` Invalid condition: ${ monitor . snmpCondition} ` ;
heartbeat . msg = ` Invalid condition: ${ monitor . _ snmpCondition} ` ;
}
} else {
heartbeat . status = DOWN ;
heartbeat . msg = 'No varbinds returned from SNMP session' ;
}
session . close ( ) ;
} ) ;
session . close ( ) ; // Close the session after use
} catch ( err ) {
console . error ( "Error in SNMP check:" , err ) ; // Log any errors
heartbeat . status = DOWN ;
heartbeat . msg = ` Error: ${ err . message } ` ;
}