root / plugins / router / snmp__juniper @ a7139bca
Historique | Voir | Annoter | Télécharger (5,14 ko)
| 1 | a7139bca | Lars Kruse | #!/usr/bin/env python |
|---|---|---|---|
| 2 | f78847fb | Johann Schmitz | # -*- coding: utf-8 -*- |
| 3 | c255203d | Johann Schmitz | |
| 4 | # Copyright (C) 2014 Johann Schmitz <johann@j-schmitz.net> |
||
| 5 | # |
||
| 6 | # This program is free software; you can redistribute it and/or modify |
||
| 7 | # it under the terms of the GNU Library General Public License as published by |
||
| 8 | # the Free Software Foundation; version 2 only |
||
| 9 | # |
||
| 10 | # This program is distributed in the hope that it will be useful, |
||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | # GNU Library General Public License for more details. |
||
| 14 | # |
||
| 15 | # You should have received a copy of the GNU Library General Public License |
||
| 16 | # along with this program; if not, write to the Free Software |
||
| 17 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
||
| 18 | # |
||
| 19 | |||
| 20 | """ |
||
| 21 | =head1 NAME |
||
| 22 | |||
| 23 | snmp__juniper - Health monitoring plugin for Juniper firewalls. |
||
| 24 | |||
| 25 | =head1 CONFIGURATION |
||
| 26 | |||
| 27 | Make sure your Juniper device is accessible via SNMP (e.g. via snmpwalk) and the munin-node |
||
| 28 | f78847fb | Johann Schmitz | has been configured correctly. |
| 29 | c255203d | Johann Schmitz | |
| 30 | =head1 MAGIC MARKERS |
||
| 31 | |||
| 32 | f78847fb | Johann Schmitz | #%# family=snmpauto |
| 33 | #%# capabilities=snmpconf |
||
| 34 | c255203d | Johann Schmitz | |
| 35 | =head1 VERSION |
||
| 36 | |||
| 37 | 0.0.1 |
||
| 38 | |||
| 39 | =head1 BUGS |
||
| 40 | |||
| 41 | Open a ticket at https://github.com/ercpe/contrib if you find one. |
||
| 42 | |||
| 43 | =head1 AUTHOR |
||
| 44 | |||
| 45 | Johann Schmitz <johann@j-schmitz.net> |
||
| 46 | |||
| 47 | =head1 LICENSE |
||
| 48 | |||
| 49 | GPLv2 |
||
| 50 | |||
| 51 | =cut |
||
| 52 | """ |
||
| 53 | |||
| 54 | import re |
||
| 55 | import sys |
||
| 56 | import os |
||
| 57 | 2fff9d6b | Johann Schmitz | import logging |
| 58 | |||
| 59 | from pysnmp.entity.rfc3413.oneliner import cmdgen |
||
| 60 | c255203d | Johann Schmitz | |
| 61 | host = None |
||
| 62 | f78847fb | Johann Schmitz | port = os.getenv('port', 161)
|
| 63 | c255203d | Johann Schmitz | community = os.getenv('community', None)
|
| 64 | |||
| 65 | f78847fb | Johann Schmitz | debug = bool(os.getenv('MUNIN_DEBUG', os.getenv('DEBUG', 0)))
|
| 66 | |||
| 67 | if debug: |
||
| 68 | logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-7s %(message)s') |
||
| 69 | |||
| 70 | c255203d | Johann Schmitz | try: |
| 71 | match = re.search("^(?:|.*\/)snmp_([^_]+)_juniper$", sys.argv[0])
|
||
| 72 | host = match.group(1) |
||
| 73 | request = match.group(2) |
||
| 74 | match = re.search("^([^:]+):(\d+)$", host)
|
||
| 75 | if match is not None: |
||
| 76 | host = match.group(1) |
||
| 77 | port = match.group(2) |
||
| 78 | except: |
||
| 79 | pass |
||
| 80 | |||
| 81 | 2fff9d6b | Johann Schmitz | jnxOperatingTable = '1.3.6.1.4.1.2636.3.1.13.1.5' |
| 82 | |||
| 83 | jnxOperatingTemp = '1.3.6.1.4.1.2636.3.1.13.1.7' |
||
| 84 | jnxOperatingCPU = '1.3.6.1.4.1.2636.3.1.13.1.8' |
||
| 85 | jnxOperatingBuffer = '1.3.6.1.4.1.2636.3.1.13.1.11' |
||
| 86 | |||
| 87 | class JunOSSnmpClient(object): |
||
| 88 | def __init__(self, host, port, community): |
||
| 89 | f78847fb | Johann Schmitz | self.hostname = host |
| 90 | 2fff9d6b | Johann Schmitz | self.transport = cmdgen.UdpTransportTarget((host, int(port))) |
| 91 | self.auth = cmdgen.CommunityData('test-agent', community)
|
||
| 92 | self.gen = cmdgen.CommandGenerator() |
||
| 93 | |||
| 94 | def get_devices(self): |
||
| 95 | errorIndication, errorStatus, errorIndex, varBindTable = self.gen.bulkCmd( |
||
| 96 | self.auth, |
||
| 97 | self.transport, |
||
| 98 | 0, 20, |
||
| 99 | ee0d7b38 | Johann Schmitz | jnxOperatingTable) |
| 100 | f78847fb | Johann Schmitz | # ignoreNonIncreasingOids=True) # only available with pysnmp >= 4.2.4 (?) |
| 101 | 2fff9d6b | Johann Schmitz | |
| 102 | if errorIndication: |
||
| 103 | logging.error("SNMP bulkCmd for devices failed: %s, %s, %s" % (errorIndication, errorStatus, errorIndex))
|
||
| 104 | return {}
|
||
| 105 | |||
| 106 | devices = {}
|
||
| 107 | for row in varBindTable: |
||
| 108 | for name, value in row: |
||
| 109 | if not str(name).startswith(jnxOperatingTable): |
||
| 110 | continue |
||
| 111 | |||
| 112 | e607ac77 | Lionel Sausin | if 'Routing Engine' in str(value): |
| 113 | 2fff9d6b | Johann Schmitz | devices[str(name)[len(jnxOperatingTable):]] = re.sub("[^\w]", '_', str(value).replace(' Routing Engine', ''))
|
| 114 | return devices |
||
| 115 | |||
| 116 | def get_one(self, prefix_oid, suffix): |
||
| 117 | oid = prefix_oid + suffix |
||
| 118 | errorIndication, errorStatus, errorIndex, varBindTable = self.gen.getCmd( |
||
| 119 | self.auth, |
||
| 120 | self.transport, |
||
| 121 | oid) |
||
| 122 | |||
| 123 | if errorIndication: |
||
| 124 | logging.error("SNMP getCmd for %s failed: %s, %s, %s" % (oid, errorIndication, errorStatus, errorIndex))
|
||
| 125 | return None |
||
| 126 | |||
| 127 | return int(varBindTable[0][1]) |
||
| 128 | |||
| 129 | 17f78427 | Lars Kruse | |
| 130 | 2fff9d6b | Johann Schmitz | |
| 131 | def get_data(self): |
||
| 132 | devs = self.get_devices() |
||
| 133 | |||
| 134 | return {
|
||
| 135 | 'temp': dict([(name, self.get_one(jnxOperatingTemp, suffix)) for suffix, name in devs.iteritems()]), |
||
| 136 | 'cpu': dict([(name, self.get_one(jnxOperatingCPU, suffix)) for suffix, name in devs.iteritems()]), |
||
| 137 | 'buffer': dict([(name, self.get_one(jnxOperatingBuffer, suffix)) for suffix, name in devs.iteritems()]), |
||
| 138 | } |
||
| 139 | |||
| 140 | def print_config(self): |
||
| 141 | devices = self.get_devices() |
||
| 142 | |||
| 143 | f78847fb | Johann Schmitz | data_def = [ |
| 144 | 6c59a897 | Lars Kruse | ('temp', self.hostname, 'System temperature', '--base 1000', 'System temperature in C'),
|
| 145 | ('cpu', self.hostname, 'CPU usage', '--base 1000 -l 0 --upper-limit 100', 'CPU usage in %'),
|
||
| 146 | ('buffer', self.hostname, 'Buffer usage', '--base 1000 -l 0 --upper-limit 100', 'Buffer usage in %'),
|
||
| 147 | f78847fb | Johann Schmitz | ] |
| 148 | |||
| 149 | 6c59a897 | Lars Kruse | for datarow, hostname, title, args, vlabel in data_def: |
| 150 | f78847fb | Johann Schmitz | print """multigraph juniper_{datarow}
|
| 151 | host_name {hostname}
|
||
| 152 | graph_title {title}
|
||
| 153 | graph_vlabel {vlabel}
|
||
| 154 | graph_args {args}
|
||
| 155 | 6c59a897 | Lars Kruse | graph_category fw |
| 156 | graph_info {title}""".format(datarow=datarow, hostname=hostname, title=title, args=args, vlabel=vlabel)
|
||
| 157 | f78847fb | Johann Schmitz | |
| 158 | for suffix, node in devices.iteritems(): |
||
| 159 | ident = "%s_%s" % (datarow, node) |
||
| 160 | print """{label}.info {title} on {node}
|
||
| 161 | {label}.label {node}
|
||
| 162 | ee0d7b38 | Johann Schmitz | {label}.type GAUGE
|
| 163 | f78847fb | Johann Schmitz | {label}.min 0""".format(title=title, label=ident, node=node)
|
| 164 | 2fff9d6b | Johann Schmitz | |
| 165 | def execute(self): |
||
| 166 | data = self.get_data() |
||
| 167 | |||
| 168 | for pre, values in data.iteritems(): |
||
| 169 | f78847fb | Johann Schmitz | print "multigraph juniper_%s" % pre |
| 170 | 2fff9d6b | Johann Schmitz | for node, value in values.iteritems(): |
| 171 | print "%s_%s.value %s" % (pre, node, value) |
||
| 172 | c255203d | Johann Schmitz | |
| 173 | 2fff9d6b | Johann Schmitz | c = JunOSSnmpClient(host, port, community) |
| 174 | f78847fb | Johann Schmitz | |
| 175 | if "snmpconf" in sys.argv[1:]: |
||
| 176 | print "require 1.3.6.1.4.1.2636.3.1.13.1.5" |
||
| 177 | sys.exit(0) |
||
| 178 | c255203d | Johann Schmitz | else: |
| 179 | f78847fb | Johann Schmitz | if not (host and port and community): |
| 180 | print "# Bad configuration. Cannot run with Host=%s, port=%s and community=%s" % (host, port, community) |
||
| 181 | sys.exit(1) |
||
| 182 | 17f78427 | Lars Kruse | |
| 183 | f78847fb | Johann Schmitz | if "config" in sys.argv[1:]: |
| 184 | c.print_config() |
||
| 185 | else: |
||
| 186 | c.execute() |
