1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 14:16:00 +00:00

Plugin snmp__juniper: migrate to Python3

This commit is contained in:
Lars Kruse 2020-11-26 02:13:49 +01:00
parent 8152186b9a
commit 7cf2dda94a
2 changed files with 119 additions and 110 deletions

View file

@ -1,22 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Johann Schmitz <johann@j-schmitz.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; version 2 only
#
# This program 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
""" """
=head1 NAME =head1 NAME
@ -27,11 +9,6 @@ snmp__juniper - Health monitoring plugin for Juniper firewalls.
Make sure your Juniper device is accessible via SNMP (e.g. via snmpwalk) and the munin-node Make sure your Juniper device is accessible via SNMP (e.g. via snmpwalk) and the munin-node
has been configured correctly. has been configured correctly.
=head1 MAGIC MARKERS
#%# family=snmpauto
#%# capabilities=snmpconf
=head1 VERSION =head1 VERSION
0.0.1 0.0.1
@ -42,11 +19,31 @@ Open a ticket at https://github.com/ercpe/contrib if you find one.
=head1 AUTHOR =head1 AUTHOR
Johann Schmitz <johann@j-schmitz.net> Copyright (C) 2014 Johann Schmitz <johann@j-schmitz.net>
=head1 LICENSE =head1 LICENSE
GPLv2 This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; version 2 only
This program 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
SPDX-License-Identifier: GPL-2.0-only
=head1 MAGIC MARKERS
#%# family=snmpauto
#%# capabilities=snmpconf
=cut =cut
""" """
@ -68,14 +65,14 @@ if debug:
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-7s %(message)s') logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-7s %(message)s')
try: try:
match = re.search("^(?:|.*\/)snmp_([^_]+)_juniper$", sys.argv[0]) match = re.search(r"^(?:|.*/)snmp_([^_]+)_juniper$", sys.argv[0])
host = match.group(1) host = match.group(1)
request = match.group(2) request = match.group(2)
match = re.search("^([^:]+):(\d+)$", host) match = re.search(r"^([^:]+):(\d+)$", host)
if match is not None: if match is not None:
host = match.group(1) host = match.group(1)
port = match.group(2) port = match.group(2)
except: except Exception:
pass pass
jnxOperatingTable = '1.3.6.1.4.1.2636.3.1.13.1.5' jnxOperatingTable = '1.3.6.1.4.1.2636.3.1.13.1.5'
@ -84,6 +81,7 @@ jnxOperatingTemp = '1.3.6.1.4.1.2636.3.1.13.1.7'
jnxOperatingCPU = '1.3.6.1.4.1.2636.3.1.13.1.8' jnxOperatingCPU = '1.3.6.1.4.1.2636.3.1.13.1.8'
jnxOperatingBuffer = '1.3.6.1.4.1.2636.3.1.13.1.11' jnxOperatingBuffer = '1.3.6.1.4.1.2636.3.1.13.1.11'
class JunOSSnmpClient(object): class JunOSSnmpClient(object):
def __init__(self, host, port, community): def __init__(self, host, port, community):
self.hostname = host self.hostname = host
@ -97,10 +95,12 @@ class JunOSSnmpClient(object):
self.transport, self.transport,
0, 20, 0, 20,
jnxOperatingTable) jnxOperatingTable)
# ignoreNonIncreasingOids=True) # only available with pysnmp >= 4.2.4 (?) # the following line is only available with pysnmp >= 4.2.4 (?)
# ignoreNonIncreasingOids=True)
if errorIndication: if errorIndication:
logging.error("SNMP bulkCmd for devices failed: %s, %s, %s" % (errorIndication, errorStatus, errorIndex)) logging.error("SNMP bulkCmd for devices failed: %s, %s, %s"
% (errorIndication, errorStatus, errorIndex))
return {} return {}
devices = {} devices = {}
@ -110,7 +110,8 @@ class JunOSSnmpClient(object):
continue continue
if 'Routing Engine' in str(value): if 'Routing Engine' in str(value):
devices[str(name)[len(jnxOperatingTable):]] = re.sub("[^\w]", '_', str(value).replace(' Routing Engine', '')) devices[str(name)[len(jnxOperatingTable):]] = re.sub(
r"[^\w]", '_', str(value).replace(' Routing Engine', ''))
return devices return devices
def get_one(self, prefix_oid, suffix): def get_one(self, prefix_oid, suffix):
@ -121,63 +122,72 @@ class JunOSSnmpClient(object):
oid) oid)
if errorIndication: if errorIndication:
logging.error("SNMP getCmd for %s failed: %s, %s, %s" % (oid, errorIndication, errorStatus, errorIndex)) logging.error("SNMP getCmd for %s failed: %s, %s, %s"
% (oid, errorIndication, errorStatus, errorIndex))
return None return None
return int(varBindTable[0][1]) return int(varBindTable[0][1])
def get_data(self): def get_data(self):
devs = self.get_devices() devs = self.get_devices()
return { return {
'temp': dict([(name, self.get_one(jnxOperatingTemp, suffix)) for suffix, name in devs.iteritems()]), 'temp': dict([(name, self.get_one(jnxOperatingTemp, suffix))
'cpu': dict([(name, self.get_one(jnxOperatingCPU, suffix)) for suffix, name in devs.iteritems()]), for suffix, name in devs.items()]),
'buffer': dict([(name, self.get_one(jnxOperatingBuffer, suffix)) for suffix, name in devs.iteritems()]), 'cpu': dict([(name, self.get_one(jnxOperatingCPU, suffix))
for suffix, name in devs.items()]),
'buffer': dict([(name, self.get_one(jnxOperatingBuffer, suffix))
for suffix, name in devs.items()]),
} }
def print_config(self): def print_config(self):
devices = self.get_devices() devices = self.get_devices()
data_def = [ data_def = [
('temp', self.hostname, 'System temperature', '--base 1000', 'System temperature in C'), ('temp', self.hostname, 'System temperature', '--base 1000',
('cpu', self.hostname, 'CPU usage', '--base 1000 -l 0 --upper-limit 100', 'CPU usage in %'), 'System temperature in C'),
('buffer', self.hostname, 'Buffer usage', '--base 1000 -l 0 --upper-limit 100', 'Buffer usage in %'), ('cpu', self.hostname, 'CPU usage', '--base 1000 -l 0 --upper-limit 100',
'CPU usage in %'),
('buffer', self.hostname, 'Buffer usage', '--base 1000 -l 0 --upper-limit 100',
'Buffer usage in %'),
] ]
for datarow, hostname, title, args, vlabel in data_def: for datarow, hostname, title, args, vlabel in data_def:
print """multigraph juniper_{datarow} print("""multigraph juniper_{datarow}
host_name {hostname} host_name {hostname}
graph_title {title} graph_title {title}
graph_vlabel {vlabel} graph_vlabel {vlabel}
graph_args {args} graph_args {args}
graph_category fw graph_category fw
graph_info {title}""".format(datarow=datarow, hostname=hostname, title=title, args=args, vlabel=vlabel) graph_info {title}""".format(datarow=datarow, hostname=hostname, title=title, args=args,
vlabel=vlabel))
for suffix, node in devices.iteritems(): for suffix, node in devices.items():
ident = "%s_%s" % (datarow, node) ident = "%s_%s" % (datarow, node)
print """{label}.info {title} on {node} print("""{label}.info {title} on {node}
{label}.label {node} {label}.label {node}
{label}.type GAUGE {label}.type GAUGE
{label}.min 0""".format(title=title, label=ident, node=node) {label}.min 0""".format(title=title, label=ident, node=node))
def execute(self): def execute(self):
data = self.get_data() data = self.get_data()
for pre, values in data.iteritems(): for pre, values in data.items():
print "multigraph juniper_%s" % pre print("multigraph juniper_%s" % pre)
for node, value in values.iteritems(): for node, value in values.items():
print "%s_%s.value %s" % (pre, node, value) print("%s_%s.value %s" % (pre, node, value))
c = JunOSSnmpClient(host, port, community) c = JunOSSnmpClient(host, port, community)
if "snmpconf" in sys.argv[1:]: if "snmpconf" in sys.argv[1:]:
print "require 1.3.6.1.4.1.2636.3.1.13.1.5" print("require 1.3.6.1.4.1.2636.3.1.13.1.5")
sys.exit(0) sys.exit(0)
else: else:
if not (host and port and community): if not (host and port and community):
print "# Bad configuration. Cannot run with Host=%s, port=%s and community=%s" % (host, port, community) print("# Bad configuration. Cannot run with Host=%s, port=%s and community=%s"
% (host, port, community), file=sys.stderr)
sys.exit(1) sys.exit(1)
if "config" in sys.argv[1:]: if "config" in sys.argv[1:]:

View file

@ -400,7 +400,6 @@ plugins/router/dsl-connection-speed
plugins/router/dsl-stats plugins/router/dsl-stats
plugins/router/freeboxuptime plugins/router/freeboxuptime
plugins/router/motorola_sb6141 plugins/router/motorola_sb6141
plugins/router/snmp__juniper
plugins/router/snmp__linksys_poe plugins/router/snmp__linksys_poe
plugins/router/speedport_300 plugins/router/speedport_300
plugins/rsync/rsyncd_bytes plugins/rsync/rsyncd_bytes