Projet

Général

Profil

Révision f52a7ea9

IDf52a7ea9a76d867877e5534c83d3ad3591b60121
Parent f78847fb
Enfant ae30c597

Ajouté par Johann Schmitz il y a presque 12 ans

New plugin to monitor flow and cp sessions of Juniper firewalls via SNMP.

Voir les différences:

plugins/snmp/snmp__juniper_spu
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_spu - Network monitoring plugin for the SPU's in 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', "public")
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_spu$", 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
jnxJsSPUMonitoringObjectsTable = '1.3.6.1.4.1.2636.3.39.1.12.1.1'
82
#jnxJsSPUMonitoringIndex = jnxJsSPUMonitoringObjectsTable + '.1.1'
83
#jnxJsSPUMonitoringFPCIndex = jnxJsSPUMonitoringObjectsTable + '.1.2'
84
#jnxJsSPUMonitoringSPUIndex = jnxJsSPUMonitoringObjectsTable + '.1.3'
85
#jnxJsSPUMonitoringCPUUsage = jnxJsSPUMonitoringObjectsTable + '.1.4'
86
#jnxJsSPUMonitoringMemoryUsage = jnxJsSPUMonitoringObjectsTable + '.1.5'
87
jnxJsSPUMonitoringCurrentFlowSession = jnxJsSPUMonitoringObjectsTable + '.1.6'
88
jnxJsSPUMonitoringMaxFlowSession = jnxJsSPUMonitoringObjectsTable + '.1.7'
89
jnxJsSPUMonitoringCurrentCPSession = jnxJsSPUMonitoringObjectsTable + '.1.8'
90
jnxJsSPUMonitoringMaxCPSession = jnxJsSPUMonitoringObjectsTable + '.1.9'
91
#jnxJsSPUMonitoringNodeIndex = jnxJsSPUMonitoringObjectsTable + '.1.10'
92
jnxJsSPUMonitoringNodeDescr = jnxJsSPUMonitoringObjectsTable + '.1.11'
93
jnxJsSPUMonitoringFlowSessIPv4 = jnxJsSPUMonitoringObjectsTable + '.1.12'
94
jnxJsSPUMonitoringFlowSessIPv6 = jnxJsSPUMonitoringObjectsTable + '.1.13'
95
jnxJsSPUMonitoringCPSessIPv4 = jnxJsSPUMonitoringObjectsTable + '.1.14'
96
jnxJsSPUMonitoringCPSessIPv6 = jnxJsSPUMonitoringObjectsTable + '.1.15'
97

  
98
class JunOSSnmpClient(object):
99

  
100
	def __init__(self, host, port, community):
101
		self.hostname = host
102
		self.transport = cmdgen.UdpTransportTarget((host, int(port)))
103
		self.auth = cmdgen.CommunityData('test-agent', community)
104
		self.gen = cmdgen.CommandGenerator()
105

  
106
	def get_data(self):
107
		errorIndication, errorStatus, errorIndex, varBindTable = self.gen.bulkCmd(
108
			self.auth,
109
			self.transport,
110
			0, 10,
111
			jnxJsSPUMonitoringObjectsTable)
112
#			ignoreNonIncreasingOids=True) # only available with pysnmp >= 4.2.4 (?) and broken anyway
113

  
114
		if errorIndication:
115
			logging.error("SNMP bulkCmd for devices failed: %s, %s, %s" % (errorIndication, errorStatus, errorIndex))
116
			return {}
117

  
118
		devices = {}
119
		values = {}
120
		for row in varBindTable:
121
			for name, value in row:
122
				if not str(name).startswith(jnxJsSPUMonitoringObjectsTable):
123
					continue
124

  
125
				oid = str(name)
126

  
127
				nodeid = oid[oid.rindex('.'):]
128

  
129
				idx = oid[len(jnxJsSPUMonitoringObjectsTable)+3:]
130
				idx = idx[:idx.index('.')]
131

  
132
				if oid.startswith(jnxJsSPUMonitoringNodeDescr):
133
					devices[nodeid] = str(value)
134
					continue
135

  
136
				values[oid] = int(value)
137
		return devices, values
138

  
139
	def print_config(self):
140
		devices, data = self.get_data()
141

  
142
		data_def = [
143
			('flow', self.hostname, 'Flow sessions', '--base 1000', '# of sessions', jnxJsSPUMonitoringMaxFlowSession),
144
			('cp', self.hostname, 'Central point sessions', '--base 1000', '# of sessions', jnxJsSPUMonitoringMaxCPSession),
145
		]
146

  
147
		for datarow, hostname, title, args, vlabel, max_prefix in data_def:
148
			print """multigraph juniper_{datarow}
149
host_name {hostname}
150
graph_title {title}
151
graph_vlabel {vlabel}
152
graph_args {args}
153
graph_category network
154
graph_info {title}""".format(datarow=datarow, hostname=hostname, title=title, args=args, vlabel=vlabel)
155

  
156
			for suffix, node in devices.iteritems():
157
				ident = "%s_%s" % (datarow, node)
158
				print """{label}.info {title} on {node}
159
{label}.label {node}
160
{label}.type GAUGE
161
{label}.min 0
162
{label}.max {max}""".format(title=title, label=ident, node=node, max=data.get(max_prefix + suffix))
163

  
164
			for suffix, node in devices.iteritems():
165
				print """
166
multigraph juniper_{datarow}.{node}
167
host_name {hostname}
168
graph_title {title}
169
graph_vlabel {vlabel}
170
graph_args {args}
171
graph_category network
172
graph_info {title}
173
{datarow}V4.info Current IPv4 {datarow} sessions
174
{datarow}V4.label IPv4
175
{datarow}V4.draw AREASTACK
176
{datarow}V4.type GAUGE
177
{datarow}V6.info Current IPv6 {datarow} sessions
178
{datarow}V6.label IPv6
179
{datarow}V6.draw AREASTACK
180
{datarow}V6.type GAUGE
181
{datarow}Current.info Current total {datarow} sessions
182
{datarow}Current.label Total
183
{datarow}Current.draw LINE1
184
{datarow}Current.type GAUGE
185
{datarow}Current.colour 000000
186
{datarow}Max.info Max. {datarow} sessions supported by the device(s)
187
{datarow}Max.label Max
188
{datarow}Max.draw LINE0
189
{datarow}Max.type GAUGE
190
""".format(datarow=datarow, hostname=hostname, title=title, args=args, vlabel=vlabel, node=node)
191

  
192
	def execute(self):
193
		devices, data = self.get_data()
194

  
195
		print "multigraph juniper_flow"
196
		for suffix, node in devices.iteritems():
197
			print "flow_%s.value %s" % (node, data.get(jnxJsSPUMonitoringCurrentFlowSession + suffix, 0))
198

  
199
			print "multigraph juniper_flow.%s" % node
200
			print "flowV4.value %s" % data.get(jnxJsSPUMonitoringFlowSessIPv4 + suffix, 0)
201
			print "flowV6.value %s" % data.get(jnxJsSPUMonitoringFlowSessIPv6 + suffix, 0)
202
			print "flowCurrent.value %s" % data.get(jnxJsSPUMonitoringCurrentFlowSession + suffix, 0)
203
			print "flowMax.value %s" % data.get(jnxJsSPUMonitoringMaxFlowSession + suffix, 0)
204

  
205
		print "multigraph juniper_cp"
206
		for suffix, node in devices.iteritems():
207
			print "cp_%s.value %s" % (node, data.get(jnxJsSPUMonitoringCurrentCPSession + suffix, 0))
208

  
209
			print "multigraph juniper_cp.%s" % node
210
			print "cpV4.value %s" % data.get(jnxJsSPUMonitoringCPSessIPv4 + suffix, 0)
211
			print "cpV6.value %s" % data.get(jnxJsSPUMonitoringCPSessIPv6 + suffix, 0)
212
			print "cpCurrent.value %s" % data.get(jnxJsSPUMonitoringCurrentCPSession + suffix, 0)
213
			print "cpMax.value %s" % data.get(jnxJsSPUMonitoringMaxCPSession + suffix, 0)
214

  
215
c = JunOSSnmpClient(host, port, community)
216

  
217
if "snmpconf" in sys.argv[1:]:
218
	print "require %s" % (jnxJsSPUMonitoringObjectsTable, )
219
	sys.exit(0)
220
else:
221
	if not (host and port and community):
222
		print "# Bad configuration. Cannot run with Host=%s, port=%s and community=%s" % (host, port, community)
223
		sys.exit(1)
224
	
225
	if "config" in sys.argv[1:]:
226
		c.print_config()
227
	else:
228
		c.execute()

Formats disponibles : Unified diff