Пример #1
0
    process: function(service, callback) {
        this.prepare(service);

        if(service.snmp_oids.length === 0) {
            // no OIDs found for this service

            if(netdata.options.DEBUG === true)
                service.error('no OIDs to process.');

            callback(null);
            return;
        }

        if(typeof service.snmp_session === 'undefined' || service.snmp_session === null) {
            // no SNMP session has been created for this service
            // the SNMP session is just the initialization of NET-SNMP

            if(netdata.options.DEBUG === true)
                netdata.debug(service.module.name + ': ' + service.name + ': opening ' + this.name + ' session on ' + service.request.hostname + ' community ' + service.request.community + ' options ' + netdata.stringify(service.request.options));

            // create the SNMP session
            service.snmp_session = net_snmp.createSession (service.request.hostname, service.request.community, service.request.options);

            if(netdata.options.DEBUG === true)
                netdata.debug(service.module.name + ': ' + service.name + ': got ' + this.name + ' session: ' + netdata.stringify(service.snmp_session));

            // if we later need traps, this is how to do it:
            //service.snmp_session.trap(net_snmp.TrapType.LinkDown, function(error) {
            //  if(error) console.error('trap error: ' + netdata.stringify(error));
            //});
        }

        // do it, get the SNMP values for the sessions we need
        this.getdata(service, 0, 0, 0, callback);
    }
Пример #2
0
 function getSession(host, community, version) {
     var sessionKey = host + ":" + community + ":" + version;
     if (!(sessionKey in sessions)) {
         sessions[sessionKey] = snmp.createSession(host, community, { version: version });
     }
     return sessions[sessionKey];
 }
Пример #3
0
function SNMP(data) {
  this.host = data.host;
  this.traps = data.traps;
  this.session = snmp.createSession ("127.0.0.1", "public");


  var oids = ["1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.1.6.0"];

  this.session.get (oids, function (error, varbinds) {
    if (error) {
        console.error (error);
    } else {
      for (var i = 0; i < varbinds.length; i++) {
        if (snmp.isVarbindError(varbinds[i])) {
          console.error (snmp.varbindError (varbinds[i]));         
        } else {
          console.log (varbinds[i].oid + " = " + varbinds[i].value);          
        }
      }
    }
  });


  this.session.trap (snmp.TrapType.LinkDown, function (error) {
    if (error)
      console.error ("!  SNMP ", error);
  });
};
Пример #4
0
    function SnmpTNode(n) {
        RED.nodes.createNode(this,n);
        this.community = n.community || "public";
        this.host = n.host || "127.0.0.1";
        this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
        this.oids = n.oids.replace(/\s/g,"");
        this.session = snmp.createSession (this.host, this.community, {version: this.version});
        var node = this;
        var maxRepetitions = 20;

        function sortInt (a, b) {
            if (a > b) { return 1; }
            else if (b > a) { return -1; }
            else { return 0; }
        }

        function responseCb (error, table) {
            if (error) {
                console.error (error.toString ());
            } else {
                var indexes = [];
                for (var index in table) {
                    if (table.hasOwnProperty(index)) {
                        indexes.push (parseInt (index));
                    }
                }
                indexes.sort (sortInt);
                for (var i = 0; i < indexes.length; i++) {
                    var columns = [];
                    for (var column in table[indexes[i]]) {
                        if (table[indexes[i]].hasOwnProperty(column)) {
                            columns.push(parseInt (column));
                        }
                    }
                    columns.sort(sortInt);
                    console.log ("row index = " + indexes[i]);
                    for (var j = 0; j < columns.length; j++) {
                        console.log ("  column " + columns[j] + " = " + table[indexes[i]][columns[j]]);
                    }
                }
                msg.payload = table;
                node.send(msg);
            }
        }
        this.on("input",function(msg) {
            var oids = node.oids || msg.oid;
            if (oids) {
                msg.oid = oids;
                node.session.table(oids, maxRepetitions, responseCb);
            }
            else {
                node.warn("No oid to search for");
            }
        });
    }
Пример #5
0
/*------------------------------------------------------------------------------------------------------*/
/*----------------------------------------- SNMP Code --------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------*/

/*
* Create session according to user parameters
*/
function createStartSession() {
  var options = {
    port: PORT,
    retries: 1,
    timeout: 5000,
    transport: "udp",
    trapPort: 162,
    version: snmp.Version1
  };
  session = snmp.createSession(IP_ADDRESS, "public");
  updateData();
}
Пример #6
0
    socket.on('register-oid-to-machine', function(data) {

        console.log("Adicionando oid para máquina: " + data.id);

        // Fecha sessões antigas
        if (socket._session) {
            socket._session.close();
            socket._session = null;
        }

        var machine = db('machines').find({ id: data.id });
        if (!machine) {
            socket.emit('whoops', 'Jovem padawan, não encontramos sua máquina.');
            return;
        }

        db('machines')
            .chain()
            .find({id: data.id})
            .assign({ oids: data.oids })
            .value();

        var missing = false;
        var fields = [];

        //Re-fetching oids
        for (var index in data.oids) {
            var oid = db('oids').find({
                value: data.oids[index]
            });
            if (!oid) {
                missing = true;
                continue;
            }
            fields.push(oid);
        }

        // Envia lista de fields atualziada pro cliente
        socket.emit('message', 'OID inserida para ' + machine.address + ' com sucesso.');
        socket.emit('message', 'Monitoração de ' + machine.address + ' atualizada.');
        socket.emit('fields', fields);

        socket._send = true;
        socket._machine = data.id;
        socket._query = fields.map((field) => field.value);
        socket._session = snmp.createSession(machine.address, machine.community, {
            version: machine.version == '2c' ? snmp.Version2c : snmp.Version1
        });

    });
Пример #7
0
function FutureSensor(sensorInfo, options) {
  Sensor.call(this, sensorInfo, options);
  if (sensorInfo.model) {
    this.model = sensorInfo.model;
  } 

  //future = sensorDriver.getNetwork('future');
  logger.debug('FutureSensor', sensorInfo);

  var ip = sensorInfo.device.address;
  this.sensorIdx = Number(this.id.slice(-2)); // FIXME: last 2 disits from id
  this.session = snmp.createSession(ip, 'public', SNMP_OPTIONS); 
  // FIXME: share device handle or close after using(probably with timer)
  this.dataTypes = _.first(FutureSensor.properties.dataTypes[this.model]);
}
Пример #8
0
    this.getInfoDiskFromOids = function (oids, eventName, disk) {
        var self = this;
        var session = snmp.createSession(this.ip, this.community);

        session.get(oids, function (err, varbinds) {
            if (err)
                console.log('Erreur' + err);
            else {
                for (var i = 0; i < varbinds.length; i++) {
                    if (snmp.isVarbindError(varbinds[i]))
                        return null;
                    else {
                        eventEmitter.emit(eventName, varbinds[i].value.toString(), self, disk);
                    }
                }
            }
        });
    }
Пример #9
0
function ReadDeviceInfo(device,callback)
{
	try {
		//var session = snmp.createSession ("172.20.66.75", "public");
		var session = snmp.createSession (device.ip, "public");
		console.log("Local:" + device.local + " IP:" + device.ip);
		var oids = ["1.3.6.1.2.1.47.1.1.1.1.10.1","1.3.6.1.2.1.1.1.0","1.3.6.1.4.1.3715.17.1.10.0"];
		
		session.get(oids, function (error, varbinds) {
			if (error) {
				console.log(error);
				device.running = "false";
				result.push(device);
				callback();
			} else {
				if(!snmp.isVarbindError(varbinds[0]))
				{
					device.version = varbinds[0].value.toString();
				}
				if(!snmp.isVarbindError(varbinds[1]))
				{
					device.versionname = varbinds[1].value.toString();
				}
				if(!snmp.isVarbindError(varbinds[2]))
				{
					device.serialnumber = varbinds[2].value.toString();
				}
				device.running = "true";
				result.push(device);
				callback();
			}
		});
		
		session.on ("error", function (error) {
    console.log (error.toString ());
    session.close ();
});
		
	} catch (ex) {
		device.running = "false";
		result.push(device);
		callback(ex);
  }
}
Пример #10
0
    socket.on('machine', function(id) {

        console.log('Máquina selecionada: ' + id);

        // Fecha sessões antigas
        if (socket._session) {
            socket._session.close();
            socket._session = null;
        }

        var machine = db('machines').find({ id: id });
        if (!machine) {
            socket.emit('whoops', 'Máquina não encontrada.');
            return;
        }

        var missing = false;
        var fields = [];
        for (var index in machine.oids) {
            var oid = db('oids').find({
                value: machine.oids[index]
            });
            if (!oid) {
                missing = true;
                continue;
            }
            fields.push(oid);
        }

        if (missing) {
            socket.emit('whoops', 'Um ou mais OIDs não foram encontrados.');
        }

        socket.emit('message', 'Monitoração de ' + machine.address + ' iniciada.');
        socket.emit('fields', fields);

        socket._send = true;
        socket._machine = id;
        socket._query = fields.map((field) => field.value);
        socket._session = snmp.createSession(machine.address, machine.community, {
            version: machine.version == '2c' ? snmp.Version2c : snmp.Version1
        });

    });
Пример #11
0
    function SnmpNode(n) {
        RED.nodes.createNode(this,n);
        this.community = n.community || "public";
        this.host = n.host || "127.0.0.1";
        this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
        this.oids = n.oids.replace(/\s/g,"");
        this.session = snmp.createSession (this.host, this.community, {version: this.version});
        var node = this;

        this.on("input",function(msg) {
            var oids = node.oids || msg.oid;
            if (oids) {
                node.session.get(oids.split(","), function(error, varbinds) {
                    if (error) {
                        node.error(error.toString());
                    } else {
                        for (var i = 0; i < varbinds.length; i++) {
                            if (snmp.isVarbindError (varbinds[i])) {
                                node.error(snmp.varbindError (varbinds[i]));
                            }
                            else {
                                if (varbinds[i].type == 4) { varbinds[i].value = varbinds[i].value.toString(); }
                                varbinds[i].tstr = snmp.ObjectType[varbinds[i].type];
                                //node.log(varbinds[i].oid + "|" + varbinds[i].tstr + "|" + varbinds[i].value);
                            }
                        }
                        msg.oid = oids;
                        msg.payload = varbinds;
                        node.send(msg);
                    }
                });
            }
            else {
                node.warn("No oid(s) to search for");
            }
        });
    }
Пример #12
0
    this.walkUsersFromOid = function () {
        // 1.3.6.1.4.1.77.1.2.25.1.1.6.74.117.108.105.101.110
        // 1.3.6.1.4.1.77.1.2.25.1.1.6.77.97.120.105.109.101
        var self = this;
        var oid = "1.3.6.1.4.1.77.1.2.25.1.1.6";
        var session = snmp.createSession(this.ip, this.community);

        var maxRepetitions = 1;

        session.walk(oid, maxRepetitions,
            function feedCb(varbinds) {
                for (var i = 0; i < varbinds.length; i++) {
                    if (snmp.isVarbindError(varbinds[i]))
                        console.error(snmp.varbindError(varbinds[i]));
                    else
                        eventEmitter.emit('feedCb_event', varbinds[i].value, self);
                } 
            }
            ,
            function doneCb(error) {
                if (error)
                    console.error(error.toString());
            });
    }
Пример #13
0
var snmp = require ("net-snmp");

var config = require('../config');
var serverInfo = config.varnishServer.connection;
var session = snmp.createSession ("localhost", config.server.communityName);

exports.pushServerStats = function(io) {
		// console.log('server info');
		var totalMemory = 0;
		var freeMemory = 0;
		var idlCPU = 0;
		var freeDisk = 0;
		var cpuLoad = 0;

		var oids = ["1.3.6.1.4.1.2021.4.5.0", "1.3.6.1.4.1.2021.4.6.0",  "1.3.6.1.4.1.2021.9.1.9.1", "1.3.6.1.4.1.2021.10.1.3.2", "1.3.6.1.4.1.2021.11.11.0"];

		session.get (oids, function (error, varbinds) {
		    if (error) {
		        console.error (error.toString ());
		    } else {
		        for (var i = 0; i < varbinds.length; i++) {
		            if (snmp.isVarbindError (varbinds[i]))
		                console.error (snmp.varbindError (varbinds[i]));
		            else {
		                // console.log (varbinds[i].oid + "|" + varbinds[i].value);
		            	switch(varbinds[i].oid) {
		            		case "1.3.6.1.4.1.2021.4.5.0":
		            			totalMemory = varbinds[i].value;
		            			// console.log('Total memory: ' + varbinds[i].value);
		            			break;
		            		case "1.3.6.1.4.1.2021.4.6.0":
Пример #14
0
#!/usr/bin/node

var snmp = require('net-snmp');

var printerData = require('./printers-config');
var pd = new printerData();

var host = process.argv[1].replace(/.*\/printer_/, "");

if (!host.match(/\//)) {
	if (process.argv[2] && process.argv[2] == 'autoconf') {
		console.log('yes');
		process.exit();
	}
	var session = snmp.createSession(host, "public", {
		version: snmp.Version1
	});
	session.get([ pd.deviceID.join('.') ], function (error, varbind) {
		if (!error) {
			var device = varbind[0].value.toString('UTF-8');
			var oids = {};
			if (pd.select(device)) {
				session.subtree(pd.consumableTypes.base.join('.'), function (varbind) {
					for (var v in varbind) {
						oids[varbind[v].oid] = varbind[v].type == 4 ? varbind[v].value.toString('UTF-8') : varbind[v].value;
					}
				}, function done(result) {
					if (process.argv[2] && process.argv[2] == 'config') {
						console.log('graph_title ' + device + ' consumables on ' + host + '\ngraph_args --units-exponent 0 --upper-limit 100 --lower-limit 0\ngraph_vlabel percent\ngraph_category printers');
						for (c in pd.devices[device].consumables) {
							var unit = 'c' + pd.devices[device].consumables[c].join('') + '.';
Пример #15
0
app.post('/api/start', function (req, res) {
    //Conecta ao IP enviado Item 1
    session = snmp.createSession(req.body.hostAddress, "public", {version: snmp.Version2c});
    res.status(200).send('');
});
Пример #16
0
var snmp = require ("net-snmp"),
		fs = require('fs');

var target = process.argv[2],
		community = process.argv[3],
		version = (process.argv[4] == "2c") ? snmp.Version2c : snmp.Version1,
		session = snmp.createSession (target, community, {version: version}),
		oid = '1.3.6.1',
		maxRepetitions = 5,
		MIB = [];

function doneCb (error) {
	if (error) { console.error (error.toString()); }
	console.log('stringifying');
	fs.writeFileSync('MIBFROMBOX.json', JSON.stringify(MIB, null, 2));
	console.log('all done!');
}

function feedCb (varbinds) {
	for (var i = 0; i < varbinds.length; i++) {
		if (snmp.isVarbindError (varbinds[i])) {
			console.error (snmp.varbindError (varbinds[i]));
			process.exit(1);
		}
		else {
			var type = varbinds[i].type;
			if (type == 4 || type == 6 || type == 64) { varbinds[i].value = varbinds[i].value.toString(); }
			else { varbinds[i].value = Number(varbinds[i].value.toString()); }
			MIB.push(varbinds[i]);
		}
	}