UNPKG

domotz-remote-pawn

Version:

Domotz Agent

145 lines (124 loc) 5.21 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz UK LLP * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ /** * * Created by Massimiliano Cuzzoli <massi@domotz.com> on 05/10/16. * */ const PROTOCOL_NAME = 'snmp'; const DISCOVERY_SEMAPHORE = 'SNMP_SEMAPHORE'; const OIDS_TO_DISCOVER = { '1.3.6.1.2.1.1.1.0': 'string', // sysDescr '1.3.6.1.2.1.1.2.0': 'string', // sysOID // IGNORED --- '1.3.6.1.2.1.1.3': 'int', // sysUpTimeInstance '1.3.6.1.2.1.1.4.0': 'string', // sysContact '1.3.6.1.2.1.1.5.0': 'string', // sysName '1.3.6.1.2.1.1.6.0': 'string', // sysLocation '1.3.6.1.2.1.1.7.0': 'int', // sysServices '1.3.6.1.2.1.2.1.0': 'int' // ifNumber }; const DEFAULT_READ_COMMUNITY = 'public'; var snmpDiscoveryFactory = function (resourceLocator, commandBuilder, responseRendering, snmpGetterFactory) { "use strict"; var mutex = resourceLocator.utils.mutex; var q = resourceLocator.q; var discoveryUtils = resourceLocator.discovery.utils; var snmpDiscoverySnapshotData = {}; var deferredSnmpBulkGet = function (snmpCommand, host) { var deferred = q.defer(); snmpGetterFactory(snmpCommand, resourceLocator).execute( function (result) { console.debug('SNMP DISCOVERY:: Received result for %s -> %s', host, JSON.stringify(result)); result['host'] = host; deferred.resolve(result); } ); return deferred.promise; }; var lock_discovery = function () { return mutex.lock(DISCOVERY_SEMAPHORE, 1); }; var unlock_discovery = function () { mutex.unlock(DISCOVERY_SEMAPHORE, 1); }; var renderSnmpResults = function (results) { for (var i = 0; i < results.length; i++) { var host = results[i].host; var data = responseRendering(resourceLocator, results[i]); console.debug('SNMP DISCOVERY:: Parsed Result: %s - |%s|', host, JSON.stringify(data)); if (!data.error) { snmpDiscoverySnapshotData[host] = data; } } }; var discover = function (callback, err_callback) { if (!lock_discovery()) { console.warn("WARNING: SNMP Discovery already in progress, ignoring the call"); return; } var ipList = discoveryUtils.getKnownIpAddresses(); var snmpCommunityByIp = discoveryUtils.getDetailsFieldsForDevices(['snmp_read_community']); var deferred_result_list = []; snmpDiscoverySnapshotData = {}; for (var i = 0; i < ipList.length; i++) { var address = ipList[i]; var snmpCommand = commandBuilder.buildBulkGetCommand(OIDS_TO_DISCOVER, address, snmpCommunityByIp[address].snmp_read_community || DEFAULT_READ_COMMUNITY); console.debug("Starting SNMP discovery for %s", address); deferred_result_list.push(deferredSnmpBulkGet(snmpCommand, address)); } q.all(deferred_result_list) .then(renderSnmpResults) .then(function () { unlock_discovery(); if (callback) callback(snmpDiscoverySnapshotData); }) .catch(function (err) { unlock_discovery(); console.error("SNMP DISCOVERY:: Error on snmp information processing %s", err); if (err_callback) err_callback(err); }); }; var applyFunctionToSnapshotEntries = function (functionToApply) { for (var address in snmpDiscoverySnapshotData) { if (snmpDiscoverySnapshotData.hasOwnProperty(address)) { functionToApply(address, snmpDiscoverySnapshotData[address], PROTOCOL_NAME); } } }; var searchEntryIntoSnapshot = function (device_address) { return snmpDiscoverySnapshotData[device_address]; }; return { discover: discover, applyFunctionToSnapshotEntries: applyFunctionToSnapshotEntries, searchEntryIntoSnapshot: searchEntryIntoSnapshot, isDiscoveryInProgress: function () { return mutex.isLocked(DISCOVERY_SEMAPHORE); }, getProtocolName: function () { return PROTOCOL_NAME }, getProtocolNameUpperCase: function () { return PROTOCOL_NAME.toUpperCase(); }, getOidForInterfaceNumber: function () { return '1.3.6.1.2.1.2.1.0'; } }; }; module.exports.factory = snmpDiscoveryFactory;