update to use net-snmp

This commit is contained in:
2025-03-26 13:13:01 +00:00
parent 2737fca294
commit 5a13e49803
11 changed files with 404 additions and 1947 deletions

View File

@@ -1,8 +1,7 @@
import * as dgram from 'dgram';
import * as snmp from 'net-snmp';
import type { IOidSet, ISnmpConfig, TUpsModel, IUpsStatus } from './types.js';
import { UpsOidSets } from './oid-sets.js';
import { SnmpPacketCreator } from './packet-creator.js';
import { SnmpPacketParser } from './packet-parser.js';
/**
* Class for SNMP communication with UPS devices
@@ -13,6 +12,8 @@ export class NupstSnmp {
private activeOIDs: IOidSet;
// Reference to the parent Nupst instance
private nupst: any; // Type 'any' to avoid circular dependency
// Debug mode flag
private debug: boolean = false;
// Default SNMP configuration
private readonly DEFAULT_CONFIG: ISnmpConfig = {
@@ -24,13 +25,6 @@ export class NupstSnmp {
upsModel: 'cyberpower', // Default UPS model
};
// SNMPv3 engine ID and counters
private engineID: Buffer = Buffer.from([0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
private engineBoots: number = 0;
private engineTime: number = 0;
private requestID: number = 1;
private debug: boolean = false; // Enable for debug output
/**
* Create a new SNMP manager
* @param debug Whether to enable debug mode
@@ -56,6 +50,14 @@ export class NupstSnmp {
return this.nupst;
}
/**
* Enable debug mode
*/
public enableDebug(): void {
this.debug = true;
console.log('SNMP debug mode enabled - detailed logs will be shown');
}
/**
* Set active OID set based on UPS model
* @param config SNMP configuration
@@ -78,121 +80,122 @@ export class NupstSnmp {
console.log(`Using OIDs for UPS model: ${model}`);
}
}
/**
* Enable debug mode
*/
public enableDebug(): void {
this.debug = true;
console.log('SNMP debug mode enabled - detailed logs will be shown');
}
/**
* Send an SNMP GET request
* Send an SNMP GET request using the net-snmp package
* @param oid OID to query
* @param config SNMP configuration
* @param retryCount Current retry count (unused in this implementation)
* @returns Promise resolving to the SNMP response value
*/
public async snmpGet(oid: string, config = this.DEFAULT_CONFIG): Promise<any> {
public async snmpGet(
oid: string,
config = this.DEFAULT_CONFIG,
retryCount = 0
): Promise<any> {
return new Promise((resolve, reject) => {
const socket = dgram.createSocket('udp4');
// Create appropriate request based on SNMP version
let request: Buffer;
if (config.version === 3) {
request = SnmpPacketCreator.createSnmpV3GetRequest(
oid,
config,
this.engineID,
this.engineBoots,
this.engineTime,
this.requestID++,
this.debug
);
} else {
request = SnmpPacketCreator.createSnmpGetRequest(oid, config.community || 'public', this.debug);
}
if (this.debug) {
console.log(`Sending SNMP ${config.version === 3 ? 'v3' : ('v' + config.version)} request to ${config.host}:${config.port}`);
console.log('Request length:', request.length);
console.log('First 16 bytes of request:', request.slice(0, 16).toString('hex'));
console.log('Full request hex:', request.toString('hex'));
console.log(`Sending SNMP v${config.version} GET request for OID ${oid} to ${config.host}:${config.port}`);
console.log('Using community:', config.community);
}
// Create SNMP options based on configuration
const options: any = {
port: config.port,
retries: 2, // Number of retries
timeout: config.timeout,
transport: 'udp4',
idBitsSize: 32
};
// Set version based on config
if (config.version === 1) {
options.version = snmp.Version1;
} else if (config.version === 2 || config.version === 2) {
options.version = snmp.Version2c;
} else {
options.version = snmp.Version3;
}
// Create appropriate session based on SNMP version
let session;
// Set timeout - add extra logging for debugging
const timeout = setTimeout(() => {
socket.close();
if (this.debug) {
console.error('---------------------------------------');
console.error('SNMP request timed out after', config.timeout, 'ms');
console.error('SNMP Version:', config.version);
if (config.version === 3) {
console.error('SNMPv3 Security Level:', config.securityLevel);
console.error('SNMPv3 Username:', config.username);
console.error('SNMPv3 Auth Protocol:', config.authProtocol || 'None');
console.error('SNMPv3 Privacy Protocol:', config.privProtocol || 'None');
}
console.error('OID:', oid);
console.error('Host:', config.host);
console.error('Port:', config.port);
console.error('---------------------------------------');
}
reject(new Error(`SNMP request timed out after ${config.timeout}ms`));
}, config.timeout);
// Listen for responses
socket.on('message', (message, rinfo) => {
clearTimeout(timeout);
if (config.version === 3) {
// For SNMPv3, we need to set up authentication and privacy
const user = {
name: config.username || '',
level: snmp.SecurityLevel[config.securityLevel || 'noAuthNoPriv'],
authProtocol: config.authProtocol ? snmp.AuthProtocols[config.authProtocol] : undefined,
authKey: config.authKey || '',
privProtocol: config.privProtocol ? snmp.PrivProtocols[config.privProtocol] : undefined,
privKey: config.privKey || ''
};
if (this.debug) {
console.log(`Received SNMP response from ${rinfo.address}:${rinfo.port}`);
console.log('Response length:', message.length);
console.log('First 16 bytes of response:', message.slice(0, 16).toString('hex'));
console.log('Full response hex:', message.toString('hex'));
}
try {
const result = SnmpPacketParser.parseSnmpResponse(message, config, this.debug);
if (this.debug) {
console.log('Parsed SNMP response:', result);
}
socket.close();
resolve(result);
} catch (error) {
if (this.debug) {
console.error('Error parsing SNMP response:', error);
}
socket.close();
reject(error);
}
});
// Handle errors
socket.on('error', (error) => {
clearTimeout(timeout);
socket.close();
if (this.debug) {
console.error('Socket error during SNMP request:', error);
}
reject(error);
});
// First send the request directly without binding to a specific port
// This lets the OS pick an available port instead of trying to bind to one
socket.send(request, 0, request.length, config.port, config.host, (error) => {
session = snmp.createV3Session(config.host, user, options);
} else {
// For SNMPv1/v2c, we use the community string
session = snmp.createSession(config.host, config.community || 'public', options);
}
// Convert the OID string to an array of OIDs if multiple OIDs are needed
const oids = [oid];
// Send the GET request
session.get(oids, (error: any, varbinds: any[]) => {
// Close the session to release resources
session.close();
if (error) {
clearTimeout(timeout);
socket.close();
if (this.debug) {
console.error('Error sending SNMP request:', error);
console.error('SNMP GET error:', error);
}
reject(error);
} else if (this.debug) {
console.log('SNMP request sent successfully');
reject(new Error(`SNMP GET error: ${error.message || error}`));
return;
}
if (!varbinds || varbinds.length === 0) {
if (this.debug) {
console.error('No varbinds returned in response');
}
reject(new Error('No varbinds returned in response'));
return;
}
// Check for SNMP errors in the response
if (varbinds[0].type === snmp.ObjectType.NoSuchObject ||
varbinds[0].type === snmp.ObjectType.NoSuchInstance ||
varbinds[0].type === snmp.ObjectType.EndOfMibView) {
if (this.debug) {
console.error('SNMP error:', snmp.ObjectType[varbinds[0].type]);
}
reject(new Error(`SNMP error: ${snmp.ObjectType[varbinds[0].type]}`));
return;
}
// Process the response value based on its type
let value = varbinds[0].value;
// Handle specific types that might need conversion
if (Buffer.isBuffer(value)) {
// If value is a Buffer, try to convert it to a string if it's printable ASCII
const isPrintableAscii = value.every(byte => byte >= 32 && byte <= 126);
if (isPrintableAscii) {
value = value.toString();
}
} else if (typeof value === 'bigint') {
// Convert BigInt to a normal number or string if needed
value = Number(value);
}
if (this.debug) {
console.log('SNMP response:', {
oid: varbinds[0].oid,
type: varbinds[0].type,
value: value
});
}
resolve(value);
});
});
}
@@ -230,157 +233,16 @@ export class NupstSnmp {
console.log('---------------------------------------');
}
// For SNMPv3, we need to discover the engine ID first
if (config.version === 3) {
if (this.debug) {
console.log('SNMPv3 detected, starting engine ID discovery');
}
try {
const discoveredEngineId = await this.discoverEngineId(config);
if (discoveredEngineId) {
this.engineID = discoveredEngineId;
if (this.debug) {
console.log('Using discovered engine ID:', this.engineID.toString('hex'));
}
}
} catch (error) {
if (this.debug) {
console.warn('Engine ID discovery failed, using default:', error);
}
}
}
// Helper function to get SNMP value with retry
const getSNMPValueWithRetry = async (oid: string, description: string) => {
if (oid === '') {
if (this.debug) {
console.log(`No OID provided for ${description}, skipping`);
}
return 0;
}
if (this.debug) {
console.log(`Getting ${description} OID: ${oid}`);
}
try {
const value = await this.snmpGet(oid, config);
if (this.debug) {
console.log(`${description} value:`, value);
}
return value;
} catch (error) {
if (this.debug) {
console.error(`Error getting ${description}:`, error.message);
}
// If we got a timeout and it's SNMPv3, try with different security levels
if (error.message.includes('timed out') && config.version === 3) {
if (this.debug) {
console.log(`Retrying ${description} with fallback settings...`);
}
// Create a retry config with lower security level
if (config.securityLevel === 'authPriv') {
const retryConfig = { ...config, securityLevel: 'authNoPriv' as 'authNoPriv' };
try {
if (this.debug) {
console.log(`Retrying with authNoPriv security level`);
}
const value = await this.snmpGet(oid, retryConfig);
if (this.debug) {
console.log(`${description} retry value:`, value);
}
return value;
} catch (retryError) {
if (this.debug) {
console.error(`Retry failed for ${description}:`, retryError.message);
}
}
}
}
// If we're still having trouble, try with standard OIDs
if (config.upsModel !== 'custom') {
try {
// Try RFC 1628 standard UPS MIB OIDs
const standardOIDs = UpsOidSets.getStandardOids();
if (this.debug) {
console.log(`Trying standard RFC 1628 OID for ${description}: ${standardOIDs[description]}`);
}
const standardValue = await this.snmpGet(standardOIDs[description], config);
if (this.debug) {
console.log(`${description} standard OID value:`, standardValue);
}
return standardValue;
} catch (stdError) {
if (this.debug) {
console.error(`Standard OID retry failed for ${description}:`, stdError.message);
}
}
}
// Return a default value if all attempts fail
if (this.debug) {
console.log(`Using default value 0 for ${description}`);
}
return 0;
}
};
// Get all values with independent retry logic
const powerStatusValue = await getSNMPValueWithRetry(this.activeOIDs.POWER_STATUS, 'power status');
const batteryCapacity = await getSNMPValueWithRetry(this.activeOIDs.BATTERY_CAPACITY, 'battery capacity') || 0;
const batteryRuntime = await getSNMPValueWithRetry(this.activeOIDs.BATTERY_RUNTIME, 'battery runtime') || 0;
const powerStatusValue = await this.getSNMPValueWithRetry(this.activeOIDs.POWER_STATUS, 'power status', config);
const batteryCapacity = await this.getSNMPValueWithRetry(this.activeOIDs.BATTERY_CAPACITY, 'battery capacity', config) || 0;
const batteryRuntime = await this.getSNMPValueWithRetry(this.activeOIDs.BATTERY_RUNTIME, 'battery runtime', config) || 0;
// Determine power status - handle different values for different UPS models
let powerStatus: 'online' | 'onBattery' | 'unknown' = 'unknown';
// Different UPS models use different values for power status
if (config.upsModel === 'cyberpower') {
// CyberPower RMCARD205: upsBaseOutputStatus values
// 2=onLine, 3=onBattery, 4=onBoost, 5=onSleep, 6=off, etc.
if (powerStatusValue === 2) {
powerStatus = 'online';
} else if (powerStatusValue === 3) {
powerStatus = 'onBattery';
}
} else if (config.upsModel === 'eaton') {
// Eaton UPS: xupsOutputSource values
// 3=normal/mains, 5=battery, etc.
if (powerStatusValue === 3) {
powerStatus = 'online';
} else if (powerStatusValue === 5) {
powerStatus = 'onBattery';
}
} else {
// Default interpretation for other UPS models
if (powerStatusValue === 1) {
powerStatus = 'online';
} else if (powerStatusValue === 2) {
powerStatus = 'onBattery';
}
}
const powerStatus = this.determinePowerStatus(config.upsModel, powerStatusValue);
// Convert to minutes for UPS models with different time units
let processedRuntime = batteryRuntime;
if (config.upsModel === 'cyberpower' && batteryRuntime > 0) {
// CyberPower: TimeTicks is in 1/100 seconds, convert to minutes
processedRuntime = Math.floor(batteryRuntime / 6000); // 6000 ticks = 1 minute
if (this.debug) {
console.log(`Converting CyberPower runtime from ${batteryRuntime} ticks to ${processedRuntime} minutes`);
}
} else if (config.upsModel === 'eaton' && batteryRuntime > 0) {
// Eaton: Runtime is in seconds, convert to minutes
processedRuntime = Math.floor(batteryRuntime / 60);
if (this.debug) {
console.log(`Converting Eaton runtime from ${batteryRuntime} seconds to ${processedRuntime} minutes`);
}
}
const processedRuntime = this.processRuntimeValue(config.upsModel, batteryRuntime);
const result = {
powerStatus,
@@ -414,109 +276,231 @@ export class NupstSnmp {
}
/**
* Discover SNMP engine ID (for SNMPv3)
* Sends a proper discovery message to get the engine ID from the device
* Helper method to get SNMP value with retry and fallback logic
* @param oid OID to query
* @param description Description of the value for logging
* @param config SNMP configuration
* @returns Promise resolving to the discovered engine ID
* @returns Promise resolving to the SNMP value
*/
public async discoverEngineId(config: ISnmpConfig): Promise<Buffer> {
return new Promise((resolve, reject) => {
const socket = dgram.createSocket('udp4');
// Create a proper discovery message (SNMPv3 with noAuthNoPriv)
const discoveryConfig: ISnmpConfig = {
...config,
securityLevel: 'noAuthNoPriv',
username: '', // Empty username for discovery
};
// Create a simple GetRequest for sysDescr (a commonly available OID)
const request = SnmpPacketCreator.createDiscoveryMessage(discoveryConfig, this.requestID++);
private async getSNMPValueWithRetry(
oid: string,
description: string,
config: ISnmpConfig
): Promise<any> {
if (oid === '') {
if (this.debug) {
console.log('Sending SNMPv3 discovery message');
console.log('SNMPv3 Discovery message:', request.toString('hex'));
console.log(`No OID provided for ${description}, skipping`);
}
return 0;
}
if (this.debug) {
console.log(`Getting ${description} OID: ${oid}`);
}
try {
const value = await this.snmpGet(oid, config);
if (this.debug) {
console.log(`${description} value:`, value);
}
return value;
} catch (error) {
if (this.debug) {
console.error(`Error getting ${description}:`, error.message);
}
// Set timeout - use a longer timeout for discovery phase
const discoveryTimeout = Math.max(config.timeout, 15000); // At least 15 seconds for discovery
const timeout = setTimeout(() => {
socket.close();
// Fall back to default engine ID if discovery fails
if (this.debug) {
console.error('---------------------------------------');
console.error('Engine ID discovery timed out after', discoveryTimeout, 'ms');
console.error('SNMPv3 settings:');
console.error(' Username:', config.username);
console.error(' Security Level:', config.securityLevel);
console.error(' Host:', config.host);
console.error(' Port:', config.port);
console.error('Using default engine ID:', this.engineID.toString('hex'));
console.error('---------------------------------------');
}
resolve(this.engineID);
}, discoveryTimeout);
// If we're using SNMPv3, try with different security levels
if (config.version === 3) {
return await this.tryFallbackSecurityLevels(oid, description, config);
}
// Listen for responses
socket.on('message', (message, rinfo) => {
clearTimeout(timeout);
if (this.debug) {
console.log(`Received SNMPv3 discovery response from ${rinfo.address}:${rinfo.port}`);
console.log('Response:', message.toString('hex'));
}
try {
// Extract engine ID from response
const engineId = SnmpPacketParser.extractEngineId(message, this.debug);
if (engineId) {
this.engineID = engineId; // Update the engine ID
if (this.debug) {
console.log('Discovered engine ID:', engineId.toString('hex'));
}
socket.close();
resolve(engineId);
} else {
if (this.debug) {
console.log('Could not extract engine ID, using default');
}
socket.close();
resolve(this.engineID);
}
} catch (error) {
if (this.debug) {
console.error('Error extracting engine ID:', error);
}
socket.close();
resolve(this.engineID); // Fall back to default engine ID
}
});
// Try with standard OIDs as fallback
if (config.upsModel !== 'custom') {
return await this.tryStandardOids(oid, description, config);
}
// Handle errors
socket.on('error', (error) => {
clearTimeout(timeout);
socket.close();
if (this.debug) {
console.error('Engine ID discovery socket error:', error);
}
resolve(this.engineID); // Fall back to default engine ID
});
// Send request directly without binding
socket.send(request, 0, request.length, config.port, config.host, (error) => {
if (error) {
clearTimeout(timeout);
socket.close();
if (this.debug) {
console.error('Error sending discovery message:', error);
}
resolve(this.engineID); // Fall back to default engine ID
} else if (this.debug) {
console.log('Discovery message sent successfully');
}
});
});
// Return a default value if all attempts fail
if (this.debug) {
console.log(`Using default value 0 for ${description}`);
}
return 0;
}
}
// initiateShutdown method has been moved to the NupstDaemon class
/**
* Try fallback security levels for SNMPv3
* @param oid OID to query
* @param description Description of the value for logging
* @param config SNMP configuration
* @returns Promise resolving to the SNMP value
*/
private async tryFallbackSecurityLevels(
oid: string,
description: string,
config: ISnmpConfig
): Promise<any> {
if (this.debug) {
console.log(`Retrying ${description} with fallback security level...`);
}
// Try with authNoPriv if current level is authPriv
if (config.securityLevel === 'authPriv') {
const retryConfig = { ...config, securityLevel: 'authNoPriv' as 'authNoPriv' };
try {
if (this.debug) {
console.log(`Retrying with authNoPriv security level`);
}
const value = await this.snmpGet(oid, retryConfig);
if (this.debug) {
console.log(`${description} retry value:`, value);
}
return value;
} catch (retryError) {
if (this.debug) {
console.error(`Retry failed for ${description}:`, retryError.message);
}
}
}
// Try with noAuthNoPriv as a last resort
if (config.securityLevel === 'authPriv' || config.securityLevel === 'authNoPriv') {
const retryConfig = { ...config, securityLevel: 'noAuthNoPriv' as 'noAuthNoPriv' };
try {
if (this.debug) {
console.log(`Retrying with noAuthNoPriv security level`);
}
const value = await this.snmpGet(oid, retryConfig);
if (this.debug) {
console.log(`${description} retry value:`, value);
}
return value;
} catch (retryError) {
if (this.debug) {
console.error(`Retry failed for ${description}:`, retryError.message);
}
}
}
return 0;
}
/**
* Try standard OIDs as fallback
* @param oid OID to query
* @param description Description of the value for logging
* @param config SNMP configuration
* @returns Promise resolving to the SNMP value
*/
private async tryStandardOids(
oid: string,
description: string,
config: ISnmpConfig
): Promise<any> {
try {
// Try RFC 1628 standard UPS MIB OIDs
const standardOIDs = UpsOidSets.getStandardOids();
if (this.debug) {
console.log(`Trying standard RFC 1628 OID for ${description}: ${standardOIDs[description]}`);
}
const standardValue = await this.snmpGet(standardOIDs[description], config);
if (this.debug) {
console.log(`${description} standard OID value:`, standardValue);
}
return standardValue;
} catch (stdError) {
if (this.debug) {
console.error(`Standard OID retry failed for ${description}:`, stdError.message);
}
}
return 0;
}
/**
* Determine power status based on UPS model and raw value
* @param upsModel UPS model
* @param powerStatusValue Raw power status value
* @returns Standardized power status
*/
private determinePowerStatus(
upsModel: TUpsModel | undefined,
powerStatusValue: number
): 'online' | 'onBattery' | 'unknown' {
if (upsModel === 'cyberpower') {
// CyberPower RMCARD205: upsBaseOutputStatus values
// 2=onLine, 3=onBattery, 4=onBoost, 5=onSleep, 6=off, etc.
if (powerStatusValue === 2) {
return 'online';
} else if (powerStatusValue === 3) {
return 'onBattery';
}
} else if (upsModel === 'eaton') {
// Eaton UPS: xupsOutputSource values
// 3=normal/mains, 5=battery, etc.
if (powerStatusValue === 3) {
return 'online';
} else if (powerStatusValue === 5) {
return 'onBattery';
}
} else if (upsModel === 'apc') {
// APC UPS: upsBasicOutputStatus values
// 2=online, 3=onBattery, etc.
if (powerStatusValue === 2) {
return 'online';
} else if (powerStatusValue === 3) {
return 'onBattery';
}
} else {
// Default interpretation for other UPS models
if (powerStatusValue === 1) {
return 'online';
} else if (powerStatusValue === 2) {
return 'onBattery';
}
}
return 'unknown';
}
/**
* Process runtime value based on UPS model
* @param upsModel UPS model
* @param batteryRuntime Raw battery runtime value
* @returns Processed runtime in minutes
*/
private processRuntimeValue(
upsModel: TUpsModel | undefined,
batteryRuntime: number
): number {
if (this.debug) {
console.log('Raw runtime value:', batteryRuntime);
}
if (upsModel === 'cyberpower' && batteryRuntime > 0) {
// CyberPower: TimeTicks is in 1/100 seconds, convert to minutes
const minutes = Math.floor(batteryRuntime / 6000); // 6000 ticks = 1 minute
if (this.debug) {
console.log(`Converting CyberPower runtime from ${batteryRuntime} ticks to ${minutes} minutes`);
}
return minutes;
} else if (upsModel === 'eaton' && batteryRuntime > 0) {
// Eaton: Runtime is in seconds, convert to minutes
const minutes = Math.floor(batteryRuntime / 60);
if (this.debug) {
console.log(`Converting Eaton runtime from ${batteryRuntime} seconds to ${minutes} minutes`);
}
return minutes;
} else if (batteryRuntime > 10000) {
// Generic conversion for large tick values (likely TimeTicks)
const minutes = Math.floor(batteryRuntime / 6000);
if (this.debug) {
console.log(`Converting ${batteryRuntime} ticks to ${minutes} minutes`);
}
return minutes;
}
return batteryRuntime;
}
}