Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / snmp / snmp__juniper @ 81db94e2

Historique | Voir | Annoter | Télécharger (5,28 ko)

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3

    
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
has been configured correctly.
29

    
30
=head1 MAGIC MARKERS
31

    
32
	#%# family=snmpauto
33
	#%# capabilities=snmpconf
34

    
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
import logging
58

    
59
from pysnmp.entity.rfc3413.oneliner import cmdgen
60

    
61
host = None
62
port = os.getenv('port', 161)
63
community = os.getenv('community', None)
64

    
65
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
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
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
		self.hostname = host
90
		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
			jnxOperatingTable)
100
#			ignoreNonIncreasingOids=True) # only available with pysnmp >= 4.2.4 (?)
101

    
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
				# TODO: Find a better way to get the routing engines in a cluster
113
				if str(value).endswith(' Routing Engine'):
114
					devices[str(name)[len(jnxOperatingTable):]] = re.sub("[^\w]", '_', str(value).replace(' Routing Engine', ''))
115
		return devices
116

    
117
	def get_one(self, prefix_oid, suffix):
118
		oid = prefix_oid + suffix
119
		errorIndication, errorStatus, errorIndex, varBindTable = self.gen.getCmd(
120
			self.auth,
121
			self.transport,
122
			oid)
123

    
124
		if errorIndication:
125
			logging.error("SNMP getCmd for %s failed: %s, %s, %s" % (oid, errorIndication, errorStatus, errorIndex))
126
			return None
127

    
128
		return int(varBindTable[0][1])
129

    
130
		
131

    
132
	def get_data(self):
133
		devs = self.get_devices()
134

    
135
		return {
136
			'temp': dict([(name, self.get_one(jnxOperatingTemp, suffix)) for suffix, name in devs.iteritems()]),
137
			'cpu': dict([(name, self.get_one(jnxOperatingCPU, suffix)) for suffix, name in devs.iteritems()]),
138
			'buffer': dict([(name, self.get_one(jnxOperatingBuffer, suffix)) for suffix, name in devs.iteritems()]),
139
		}
140

    
141
	def print_config(self):
142
		devices = self.get_devices()
143

    
144
		data_def = [
145
			('temp', self.hostname, 'System temperature', '--base 1000', 'System temperature in C', 'system'),
146
			('cpu', self.hostname, 'CPU usage', '--base 1000 -l 0 --upper-limit 100', 'CPU usage in %', 'system'),
147
			('buffer', self.hostname, 'Buffer usage', '--base 1000 -l 0 --upper-limit 100', 'Buffer usage in %', 'system'),
148
		]
149

    
150
		for datarow, hostname, title, args, vlabel, category in data_def:
151
			print """multigraph juniper_{datarow}
152
host_name {hostname}
153
graph_title {title}
154
graph_vlabel {vlabel}
155
graph_args {args}
156
graph_category {category}
157
graph_info {title}""".format(datarow=datarow, hostname=hostname, title=title, args=args, vlabel=vlabel, category=category)
158

    
159
			for suffix, node in devices.iteritems():
160
				ident = "%s_%s" % (datarow, node)
161
				print """{label}.info {title} on {node}
162
{label}.label {node}
163
{label}.type GAUGE
164
{label}.min 0""".format(title=title, label=ident, node=node)
165

    
166
	def execute(self):
167
		data = self.get_data()
168

    
169
		for pre, values in data.iteritems():
170
			print "multigraph juniper_%s" % pre
171
			for node, value in values.iteritems():
172
				print "%s_%s.value %s" % (pre, node, value)
173

    
174
c = JunOSSnmpClient(host, port, community)
175

    
176
if "snmpconf" in sys.argv[1:]:
177
	print "require 1.3.6.1.4.1.2636.3.1.13.1.5"
178
	sys.exit(0)
179
else:
180
	if not (host and port and community):
181
		print "# Bad configuration. Cannot run with Host=%s, port=%s and community=%s" % (host, port, community)
182
		sys.exit(1)
183
	
184
	if "config" in sys.argv[1:]:
185
		c.print_config()
186
	else:
187
		c.execute()