Projet

Général

Profil

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

root / plugins / snmp / snmp__juniper @ ee0d7b38

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

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