initial
This commit is contained in:
271
ts/snmp/snmp.classes.snmpdevice.ts
Normal file
271
ts/snmp/snmp.classes.snmpdevice.ts
Normal file
@@ -0,0 +1,271 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import { Device } from '../abstract/device.abstract.js';
|
||||
import { SnmpProtocol, SNMP_OIDS, type ISnmpOptions, type ISnmpVarbind } from './snmp.classes.snmpprotocol.js';
|
||||
import type { IDeviceInfo, IRetryOptions, TDeviceStatus } from '../interfaces/index.js';
|
||||
|
||||
/**
|
||||
* SNMP device information
|
||||
*/
|
||||
export interface ISnmpDeviceInfo extends IDeviceInfo {
|
||||
type: 'snmp';
|
||||
sysDescr: string;
|
||||
sysObjectID: string;
|
||||
sysUpTime: number;
|
||||
sysContact?: string;
|
||||
sysName?: string;
|
||||
sysLocation?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* SNMP Device class for generic SNMP-enabled devices
|
||||
*/
|
||||
export class SnmpDevice extends Device {
|
||||
private protocol: SnmpProtocol | null = null;
|
||||
private snmpOptions: ISnmpOptions;
|
||||
|
||||
private _sysDescr: string = '';
|
||||
private _sysObjectID: string = '';
|
||||
private _sysUpTime: number = 0;
|
||||
private _sysContact?: string;
|
||||
private _sysName?: string;
|
||||
private _sysLocation?: string;
|
||||
|
||||
constructor(
|
||||
info: IDeviceInfo,
|
||||
snmpOptions?: ISnmpOptions,
|
||||
retryOptions?: IRetryOptions
|
||||
) {
|
||||
super(info, retryOptions);
|
||||
this.snmpOptions = { port: info.port, ...snmpOptions };
|
||||
}
|
||||
|
||||
// Getters for SNMP properties
|
||||
public get sysDescr(): string {
|
||||
return this._sysDescr;
|
||||
}
|
||||
|
||||
public get sysObjectID(): string {
|
||||
return this._sysObjectID;
|
||||
}
|
||||
|
||||
public get sysUpTime(): number {
|
||||
return this._sysUpTime;
|
||||
}
|
||||
|
||||
public get sysContact(): string | undefined {
|
||||
return this._sysContact;
|
||||
}
|
||||
|
||||
public get sysName(): string | undefined {
|
||||
return this._sysName;
|
||||
}
|
||||
|
||||
public get sysLocation(): string | undefined {
|
||||
return this._sysLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the SNMP device
|
||||
*/
|
||||
protected async doConnect(): Promise<void> {
|
||||
this.protocol = new SnmpProtocol(this.address, this.snmpOptions);
|
||||
|
||||
// Verify connection by fetching system info
|
||||
const sysInfo = await this.protocol.getSystemInfo();
|
||||
|
||||
this._sysDescr = sysInfo.sysDescr;
|
||||
this._sysObjectID = sysInfo.sysObjectID;
|
||||
this._sysUpTime = sysInfo.sysUpTime;
|
||||
this._sysContact = sysInfo.sysContact || undefined;
|
||||
this._sysName = sysInfo.sysName || undefined;
|
||||
this._sysLocation = sysInfo.sysLocation || undefined;
|
||||
|
||||
// Update device name if sysName is available
|
||||
if (sysInfo.sysName && !this.name.includes('SNMP Device')) {
|
||||
// Keep custom name
|
||||
} else if (sysInfo.sysName) {
|
||||
(this as { name: string }).name = sysInfo.sysName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect from the SNMP device
|
||||
*/
|
||||
protected async doDisconnect(): Promise<void> {
|
||||
if (this.protocol) {
|
||||
this.protocol.close();
|
||||
this.protocol = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh device status
|
||||
*/
|
||||
public async refreshStatus(): Promise<void> {
|
||||
if (!this.protocol) {
|
||||
throw new Error('Not connected');
|
||||
}
|
||||
|
||||
const sysInfo = await this.protocol.getSystemInfo();
|
||||
this._sysUpTime = sysInfo.sysUpTime;
|
||||
this.emit('status:updated', this.getDeviceInfo());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single OID value
|
||||
*/
|
||||
public async get(oid: string): Promise<ISnmpVarbind> {
|
||||
if (!this.protocol) {
|
||||
throw new Error('Not connected');
|
||||
}
|
||||
return this.protocol.get(oid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get multiple OID values
|
||||
*/
|
||||
public async getMultiple(oids: string[]): Promise<ISnmpVarbind[]> {
|
||||
if (!this.protocol) {
|
||||
throw new Error('Not connected');
|
||||
}
|
||||
return this.protocol.getMultiple(oids);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next OID in the MIB tree
|
||||
*/
|
||||
public async getNext(oid: string): Promise<ISnmpVarbind> {
|
||||
if (!this.protocol) {
|
||||
throw new Error('Not connected');
|
||||
}
|
||||
return this.protocol.getNext(oid);
|
||||
}
|
||||
|
||||
/**
|
||||
* GETBULK operation for efficient table retrieval
|
||||
*/
|
||||
public async getBulk(
|
||||
oids: string[],
|
||||
nonRepeaters?: number,
|
||||
maxRepetitions?: number
|
||||
): Promise<ISnmpVarbind[]> {
|
||||
if (!this.protocol) {
|
||||
throw new Error('Not connected');
|
||||
}
|
||||
return this.protocol.getBulk(oids, nonRepeaters, maxRepetitions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk a MIB tree
|
||||
*/
|
||||
public async walk(baseOid: string): Promise<ISnmpVarbind[]> {
|
||||
if (!this.protocol) {
|
||||
throw new Error('Not connected');
|
||||
}
|
||||
return this.protocol.walk(baseOid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an OID value
|
||||
*/
|
||||
public async set(
|
||||
oid: string,
|
||||
type: 'Integer' | 'OctetString' | 'ObjectIdentifier' | 'IpAddress',
|
||||
value: unknown
|
||||
): Promise<ISnmpVarbind> {
|
||||
if (!this.protocol) {
|
||||
throw new Error('Not connected');
|
||||
}
|
||||
return this.protocol.set(oid, type, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get device information
|
||||
*/
|
||||
public getDeviceInfo(): ISnmpDeviceInfo {
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
type: 'snmp',
|
||||
address: this.address,
|
||||
port: this.port,
|
||||
status: this.status,
|
||||
sysDescr: this._sysDescr,
|
||||
sysObjectID: this._sysObjectID,
|
||||
sysUpTime: this._sysUpTime,
|
||||
sysContact: this._sysContact,
|
||||
sysName: this._sysName,
|
||||
sysLocation: this._sysLocation,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create SnmpDevice from discovery data
|
||||
*/
|
||||
public static fromDiscovery(
|
||||
data: {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
port?: number;
|
||||
community?: string;
|
||||
},
|
||||
retryOptions?: IRetryOptions
|
||||
): SnmpDevice {
|
||||
const info: IDeviceInfo = {
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
type: 'snmp',
|
||||
address: data.address,
|
||||
port: data.port ?? 161,
|
||||
status: 'unknown',
|
||||
};
|
||||
return new SnmpDevice(
|
||||
info,
|
||||
{ community: data.community ?? 'public' },
|
||||
retryOptions
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Probe an IP address for SNMP device
|
||||
*/
|
||||
public static async probe(
|
||||
address: string,
|
||||
port: number = 161,
|
||||
community: string = 'public',
|
||||
timeout: number = 5000
|
||||
): Promise<ISnmpDeviceInfo | null> {
|
||||
const protocol = new SnmpProtocol(address, {
|
||||
community,
|
||||
port,
|
||||
timeout,
|
||||
retries: 0,
|
||||
});
|
||||
|
||||
try {
|
||||
const sysInfo = await protocol.getSystemInfo();
|
||||
|
||||
return {
|
||||
id: `snmp:${address}:${port}`,
|
||||
name: sysInfo.sysName || `SNMP Device at ${address}`,
|
||||
type: 'snmp',
|
||||
address,
|
||||
port,
|
||||
status: 'online',
|
||||
sysDescr: sysInfo.sysDescr,
|
||||
sysObjectID: sysInfo.sysObjectID,
|
||||
sysUpTime: sysInfo.sysUpTime,
|
||||
sysContact: sysInfo.sysContact || undefined,
|
||||
sysName: sysInfo.sysName || undefined,
|
||||
sysLocation: sysInfo.sysLocation || undefined,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
protocol.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { SNMP_OIDS };
|
||||
439
ts/snmp/snmp.classes.snmpprotocol.ts
Normal file
439
ts/snmp/snmp.classes.snmpprotocol.ts
Normal file
@@ -0,0 +1,439 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
|
||||
/**
|
||||
* Common SNMP OIDs (Object Identifiers)
|
||||
*/
|
||||
export const SNMP_OIDS = {
|
||||
// System MIB (RFC 1213)
|
||||
sysDescr: '1.3.6.1.2.1.1.1.0',
|
||||
sysObjectID: '1.3.6.1.2.1.1.2.0',
|
||||
sysUpTime: '1.3.6.1.2.1.1.3.0',
|
||||
sysContact: '1.3.6.1.2.1.1.4.0',
|
||||
sysName: '1.3.6.1.2.1.1.5.0',
|
||||
sysLocation: '1.3.6.1.2.1.1.6.0',
|
||||
sysServices: '1.3.6.1.2.1.1.7.0',
|
||||
|
||||
// IF-MIB - Interfaces
|
||||
ifNumber: '1.3.6.1.2.1.2.1.0',
|
||||
ifTable: '1.3.6.1.2.1.2.2',
|
||||
|
||||
// Host resources
|
||||
hrSystemUptime: '1.3.6.1.2.1.25.1.1.0',
|
||||
hrMemorySize: '1.3.6.1.2.1.25.2.2.0',
|
||||
|
||||
// UPS-MIB (RFC 1628)
|
||||
upsIdentManufacturer: '1.3.6.1.2.1.33.1.1.1.0',
|
||||
upsIdentModel: '1.3.6.1.2.1.33.1.1.2.0',
|
||||
upsBatteryStatus: '1.3.6.1.2.1.33.1.2.1.0',
|
||||
upsSecondsOnBattery: '1.3.6.1.2.1.33.1.2.2.0',
|
||||
upsEstimatedMinutesRemaining: '1.3.6.1.2.1.33.1.2.3.0',
|
||||
upsEstimatedChargeRemaining: '1.3.6.1.2.1.33.1.2.4.0',
|
||||
upsBatteryVoltage: '1.3.6.1.2.1.33.1.2.5.0',
|
||||
upsInputFrequency: '1.3.6.1.2.1.33.1.3.3.1.2',
|
||||
upsInputVoltage: '1.3.6.1.2.1.33.1.3.3.1.3',
|
||||
upsOutputSource: '1.3.6.1.2.1.33.1.4.1.0',
|
||||
upsOutputFrequency: '1.3.6.1.2.1.33.1.4.2.0',
|
||||
upsOutputVoltage: '1.3.6.1.2.1.33.1.4.4.1.2',
|
||||
upsOutputCurrent: '1.3.6.1.2.1.33.1.4.4.1.3',
|
||||
upsOutputPower: '1.3.6.1.2.1.33.1.4.4.1.4',
|
||||
upsOutputPercentLoad: '1.3.6.1.2.1.33.1.4.4.1.5',
|
||||
|
||||
// Printer MIB
|
||||
prtGeneralPrinterName: '1.3.6.1.2.1.43.5.1.1.16.1',
|
||||
prtMarkerSuppliesLevel: '1.3.6.1.2.1.43.11.1.1.9',
|
||||
prtMarkerSuppliesMaxCapacity: '1.3.6.1.2.1.43.11.1.1.8',
|
||||
};
|
||||
|
||||
/**
|
||||
* SNMP value types
|
||||
*/
|
||||
export type TSnmpValueType =
|
||||
| 'OctetString'
|
||||
| 'Integer'
|
||||
| 'Counter'
|
||||
| 'Counter32'
|
||||
| 'Counter64'
|
||||
| 'Gauge'
|
||||
| 'Gauge32'
|
||||
| 'TimeTicks'
|
||||
| 'IpAddress'
|
||||
| 'ObjectIdentifier'
|
||||
| 'Null'
|
||||
| 'Opaque';
|
||||
|
||||
/**
|
||||
* SNMP varbind (variable binding)
|
||||
*/
|
||||
export interface ISnmpVarbind {
|
||||
oid: string;
|
||||
type: TSnmpValueType;
|
||||
value: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* SNMP session options
|
||||
*/
|
||||
export interface ISnmpOptions {
|
||||
/** Community string (v1/v2c) or username (v3) */
|
||||
community?: string;
|
||||
/** SNMP version: 1, 2 (v2c), or 3 */
|
||||
version?: 1 | 2 | 3;
|
||||
/** Request timeout in milliseconds */
|
||||
timeout?: number;
|
||||
/** Number of retries */
|
||||
retries?: number;
|
||||
/** Port (default: 161) */
|
||||
port?: number;
|
||||
}
|
||||
|
||||
const DEFAULT_OPTIONS: Required<ISnmpOptions> = {
|
||||
community: 'public',
|
||||
version: 2,
|
||||
timeout: 5000,
|
||||
retries: 1,
|
||||
port: 161,
|
||||
};
|
||||
|
||||
/**
|
||||
* SNMP Protocol handler using net-snmp
|
||||
*/
|
||||
export class SnmpProtocol {
|
||||
private session: ReturnType<typeof plugins.netSnmp.createSession> | null = null;
|
||||
private address: string;
|
||||
private options: Required<ISnmpOptions>;
|
||||
|
||||
constructor(address: string, options?: ISnmpOptions) {
|
||||
this.address = address;
|
||||
this.options = { ...DEFAULT_OPTIONS, ...options };
|
||||
}
|
||||
|
||||
/**
|
||||
* Create SNMP session
|
||||
*/
|
||||
private getSession(): ReturnType<typeof plugins.netSnmp.createSession> {
|
||||
if (!this.session) {
|
||||
const snmpVersion =
|
||||
this.options.version === 1
|
||||
? plugins.netSnmp.Version1
|
||||
: plugins.netSnmp.Version2c;
|
||||
|
||||
this.session = plugins.netSnmp.createSession(this.address, this.options.community, {
|
||||
port: this.options.port,
|
||||
retries: this.options.retries,
|
||||
timeout: this.options.timeout,
|
||||
version: snmpVersion,
|
||||
});
|
||||
}
|
||||
return this.session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close SNMP session
|
||||
*/
|
||||
public close(): void {
|
||||
if (this.session) {
|
||||
this.session.close();
|
||||
this.session = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET operation - retrieve a single OID value
|
||||
*/
|
||||
public async get(oid: string): Promise<ISnmpVarbind> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const session = this.getSession();
|
||||
|
||||
session.get([oid], (error: Error | null, varbinds: unknown[]) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (varbinds.length === 0) {
|
||||
reject(new Error(`No response for OID ${oid}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const vb = varbinds[0] as { oid: string; type: number; value: unknown };
|
||||
|
||||
if (plugins.netSnmp.isVarbindError(vb)) {
|
||||
reject(new Error(`SNMP error for ${oid}: ${plugins.netSnmp.varbindError(vb)}`));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(this.parseVarbind(vb));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET operation - retrieve multiple OID values
|
||||
*/
|
||||
public async getMultiple(oids: string[]): Promise<ISnmpVarbind[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const session = this.getSession();
|
||||
|
||||
session.get(oids, (error: Error | null, varbinds: unknown[]) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
const results: ISnmpVarbind[] = [];
|
||||
for (const vb of varbinds) {
|
||||
const varbind = vb as { oid: string; type: number; value: unknown };
|
||||
if (!plugins.netSnmp.isVarbindError(varbind)) {
|
||||
results.push(this.parseVarbind(varbind));
|
||||
}
|
||||
}
|
||||
|
||||
resolve(results);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GETNEXT operation - get the next OID in the MIB tree
|
||||
*/
|
||||
public async getNext(oid: string): Promise<ISnmpVarbind> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const session = this.getSession();
|
||||
|
||||
session.getNext([oid], (error: Error | null, varbinds: unknown[]) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (varbinds.length === 0) {
|
||||
reject(new Error(`No response for GETNEXT ${oid}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const vb = varbinds[0] as { oid: string; type: number; value: unknown };
|
||||
|
||||
if (plugins.netSnmp.isVarbindError(vb)) {
|
||||
reject(new Error(`SNMP error for ${oid}: ${plugins.netSnmp.varbindError(vb)}`));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(this.parseVarbind(vb));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GETBULK operation (v2c/v3 only) - efficient retrieval of table rows
|
||||
*/
|
||||
public async getBulk(
|
||||
oids: string[],
|
||||
nonRepeaters: number = 0,
|
||||
maxRepetitions: number = 20
|
||||
): Promise<ISnmpVarbind[]> {
|
||||
if (this.options.version === 1) {
|
||||
throw new Error('GETBULK is not supported in SNMPv1');
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const session = this.getSession();
|
||||
|
||||
session.getBulk(oids, nonRepeaters, maxRepetitions, (error: Error | null, varbinds: unknown[]) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
const results: ISnmpVarbind[] = [];
|
||||
for (const vb of varbinds) {
|
||||
const varbind = vb as { oid: string; type: number; value: unknown };
|
||||
if (!plugins.netSnmp.isVarbindError(varbind)) {
|
||||
results.push(this.parseVarbind(varbind));
|
||||
}
|
||||
}
|
||||
|
||||
resolve(results);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk operation - retrieve all OIDs under a tree
|
||||
*/
|
||||
public async walk(baseOid: string): Promise<ISnmpVarbind[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const session = this.getSession();
|
||||
const results: ISnmpVarbind[] = [];
|
||||
|
||||
session.walk(
|
||||
baseOid,
|
||||
20, // maxRepetitions
|
||||
(varbinds: unknown[]) => {
|
||||
for (const vb of varbinds) {
|
||||
const varbind = vb as { oid: string; type: number; value: unknown };
|
||||
if (!plugins.netSnmp.isVarbindError(varbind)) {
|
||||
results.push(this.parseVarbind(varbind));
|
||||
}
|
||||
}
|
||||
},
|
||||
(error: Error | null) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve(results);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* SET operation - set an OID value
|
||||
*/
|
||||
public async set(oid: string, type: TSnmpValueType, value: unknown): Promise<ISnmpVarbind> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const session = this.getSession();
|
||||
|
||||
const snmpType = this.getSnmpType(type);
|
||||
const varbind = {
|
||||
oid,
|
||||
type: snmpType,
|
||||
value,
|
||||
};
|
||||
|
||||
session.set([varbind], (error: Error | null, varbinds: unknown[]) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (varbinds.length === 0) {
|
||||
reject(new Error(`No response for SET ${oid}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const vb = varbinds[0] as { oid: string; type: number; value: unknown };
|
||||
|
||||
if (plugins.netSnmp.isVarbindError(vb)) {
|
||||
reject(new Error(`SNMP error for ${oid}: ${plugins.netSnmp.varbindError(vb)}`));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(this.parseVarbind(vb));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get system information
|
||||
*/
|
||||
public async getSystemInfo(): Promise<{
|
||||
sysDescr: string;
|
||||
sysObjectID: string;
|
||||
sysUpTime: number;
|
||||
sysContact: string;
|
||||
sysName: string;
|
||||
sysLocation: string;
|
||||
}> {
|
||||
const oids = [
|
||||
SNMP_OIDS.sysDescr,
|
||||
SNMP_OIDS.sysObjectID,
|
||||
SNMP_OIDS.sysUpTime,
|
||||
SNMP_OIDS.sysContact,
|
||||
SNMP_OIDS.sysName,
|
||||
SNMP_OIDS.sysLocation,
|
||||
];
|
||||
|
||||
const varbinds = await this.getMultiple(oids);
|
||||
|
||||
const getValue = (oid: string): unknown => {
|
||||
const vb = varbinds.find((v) => v.oid === oid);
|
||||
return vb?.value;
|
||||
};
|
||||
|
||||
return {
|
||||
sysDescr: String(getValue(SNMP_OIDS.sysDescr) || ''),
|
||||
sysObjectID: String(getValue(SNMP_OIDS.sysObjectID) || ''),
|
||||
sysUpTime: Number(getValue(SNMP_OIDS.sysUpTime) || 0),
|
||||
sysContact: String(getValue(SNMP_OIDS.sysContact) || ''),
|
||||
sysName: String(getValue(SNMP_OIDS.sysName) || ''),
|
||||
sysLocation: String(getValue(SNMP_OIDS.sysLocation) || ''),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if device is reachable via SNMP
|
||||
*/
|
||||
public async isReachable(): Promise<boolean> {
|
||||
try {
|
||||
await this.get(SNMP_OIDS.sysDescr);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse varbind to our format
|
||||
*/
|
||||
private parseVarbind(vb: { oid: string; type: number; value: unknown }): ISnmpVarbind {
|
||||
return {
|
||||
oid: vb.oid,
|
||||
type: this.getTypeName(vb.type),
|
||||
value: this.parseValue(vb.type, vb.value),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get type name from SNMP type number
|
||||
*/
|
||||
private getTypeName(type: number): TSnmpValueType {
|
||||
const typeMap: Record<number, TSnmpValueType> = {
|
||||
[plugins.netSnmp.ObjectType.OctetString]: 'OctetString',
|
||||
[plugins.netSnmp.ObjectType.Integer]: 'Integer',
|
||||
[plugins.netSnmp.ObjectType.Counter]: 'Counter',
|
||||
[plugins.netSnmp.ObjectType.Counter32]: 'Counter32',
|
||||
[plugins.netSnmp.ObjectType.Counter64]: 'Counter64',
|
||||
[plugins.netSnmp.ObjectType.Gauge]: 'Gauge',
|
||||
[plugins.netSnmp.ObjectType.Gauge32]: 'Gauge32',
|
||||
[plugins.netSnmp.ObjectType.TimeTicks]: 'TimeTicks',
|
||||
[plugins.netSnmp.ObjectType.IpAddress]: 'IpAddress',
|
||||
[plugins.netSnmp.ObjectType.OID]: 'ObjectIdentifier',
|
||||
[plugins.netSnmp.ObjectType.Null]: 'Null',
|
||||
[plugins.netSnmp.ObjectType.Opaque]: 'Opaque',
|
||||
};
|
||||
return typeMap[type] || 'OctetString';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SNMP type number from type name
|
||||
*/
|
||||
private getSnmpType(type: TSnmpValueType): number {
|
||||
const typeMap: Record<TSnmpValueType, number> = {
|
||||
OctetString: plugins.netSnmp.ObjectType.OctetString,
|
||||
Integer: plugins.netSnmp.ObjectType.Integer,
|
||||
Counter: plugins.netSnmp.ObjectType.Counter,
|
||||
Counter32: plugins.netSnmp.ObjectType.Counter32,
|
||||
Counter64: plugins.netSnmp.ObjectType.Counter64,
|
||||
Gauge: plugins.netSnmp.ObjectType.Gauge,
|
||||
Gauge32: plugins.netSnmp.ObjectType.Gauge32,
|
||||
TimeTicks: plugins.netSnmp.ObjectType.TimeTicks,
|
||||
IpAddress: plugins.netSnmp.ObjectType.IpAddress,
|
||||
ObjectIdentifier: plugins.netSnmp.ObjectType.OID,
|
||||
Null: plugins.netSnmp.ObjectType.Null,
|
||||
Opaque: plugins.netSnmp.ObjectType.Opaque,
|
||||
};
|
||||
return typeMap[type];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse value based on type
|
||||
*/
|
||||
private parseValue(type: number, value: unknown): unknown {
|
||||
// OctetString - convert Buffer to string
|
||||
if (type === plugins.netSnmp.ObjectType.OctetString && Buffer.isBuffer(value)) {
|
||||
return value.toString('utf8');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user