Skip to content

Instantly share code, notes, and snippets.

@tamirko
Last active July 19, 2016 12:52
Show Gist options
  • Save tamirko/f282be3811de65c724b7f99ee73a0efe to your computer and use it in GitHub Desktop.
Save tamirko/f282be3811de65c724b7f99ee73a0efe to your computer and use it in GitHub Desktop.
Write to the snmp log file
import time
from snmpraw import SNMPRawCollector
from diamond.metric import Metric
class SNMPProxyCollector(SNMPRawCollector):
def collect_snmp(self, device, host, port, community):
"""
Collect SNMP interface data from device
"""
with open('/tmp/snmp.log', 'a') as f:
current_line = "Collecting raw SNMP statistics from device '{0}'".format(device)
f.write(current_line)
f.close()
dev_config = self.config['devices'][device]
if 'oids' in dev_config:
for oid, metricName in dev_config['oids'].items():
if (device, oid) in self.skip_list:
self.log.debug(
'Skipping OID '{0}' ({1}) on device '{2}''.format(
oid, metricName, device))
with open('/tmp/snmp.log', 'a') as f:
current_line = "Skipping OID '{0}' ({1}) on device '{2}'".format(
oid, metricName, device)
f.write(current_line)
f.close()
continue
timestamp = time.time()
value = self._get_value(device, oid, host, port, community)
if value is None:
continue
self.log.debug(
'\'{0}\' ({1}) on device \'{2}\' - value=[{3}]'.format(
oid, metricName, device, value))
with open('/tmp/snmp.log', 'a') as f:
current_line = "'{0}' ({1}) on device '{2}' - value=[{3}]".format(
oid, metricName, device, value)
f.write(current_line)
f.close()
device_path = '{}.{}.{}'.format(
dev_config['node_id'],
device,
dev_config['node_instance_id']
)
path = '.'.join([self.config['path_prefix'], device_path,
self.config['path_suffix'], metricName])
metric = Metric(path=path, value=value, timestamp=timestamp,
precision=self._precision(value),
host=device_path, metric_type='GAUGE')
self.publish_metric(metric)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment