|
1 | | -#!/usr/bin/env python |
2 | | -# Copyright (c) 2014, Paessler AG <support@paessler.com> |
3 | | -# All rights reserved. |
4 | | -# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
5 | | -# following conditions are met: |
6 | | -# 1. Redistributions of source code must retain the above copyright notice, this list of conditions |
7 | | -# and the following disclaimer. |
8 | | -# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions |
9 | | -# and the following disclaimer in the documentation and/or other materials provided with the distribution. |
10 | | -# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse |
11 | | -# or promote products derived from this software without specific prior written permission. |
12 | | - |
13 | | -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
14 | | -# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
15 | | -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
16 | | -# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
17 | | -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
18 | | -# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
19 | | -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
20 | | -# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
21 | | - |
22 | | -import sys |
23 | | -import gc |
24 | | -import logging |
25 | | - |
26 | | -try: |
27 | | - from pysnmp.entity.rfc3413.oneliner import cmdgen |
28 | | - snmp = True |
29 | | -except Exception as e: |
30 | | - logging.error("PySNMP could not be imported. SNMP Sensors won't work.Error: %s" % e) |
31 | | - snmp = False |
32 | | - pass |
33 | | - |
34 | | - |
35 | | -class SNMPCustomString(object): |
36 | | - |
37 | | - def __init__(self): |
38 | | - gc.enable() |
39 | | - |
40 | | - @staticmethod |
41 | | - def get_kind(): |
42 | | - """ |
43 | | - return sensor kind |
44 | | - """ |
45 | | - return "mpsnmpcustomstring" |
46 | | - |
47 | | - @staticmethod |
48 | | - def get_sensordef(): |
49 | | - """ |
50 | | - Definition of the sensor and data to be shown in the PRTG WebGUI |
51 | | - """ |
52 | | - sensordefinition = { |
53 | | - "kind": SNMPCustomString.get_kind(), |
54 | | - "name": "SNMP Custom String", |
55 | | - "description": "Monitors a string value returned by a specific OID using SNMP", |
56 | | - "help": "Monitors a string value returned by a specific OID using SNMP", |
57 | | - "tag": "mpsnmpcustomstringsensor", |
58 | | - "groups": [ |
59 | | - { |
60 | | - "name": "OID values", |
61 | | - "caption": "OID values", |
62 | | - "fields": [ |
63 | | - { |
64 | | - "type": "edit", |
65 | | - "name": "oid", |
66 | | - "caption": "OID Value", |
67 | | - "required": "1", |
68 | | - "help": "Please enter the OID value." |
69 | | - }, |
70 | | - { |
71 | | - "type": "edit", |
72 | | - "name": "unit", |
73 | | - "caption": "Unit String", |
74 | | - "default": "#", |
75 | | - "help": "Enter a 'unit' string, e.g. 'ms', 'Kbyte' (for display purposes only)." |
76 | | - }, |
77 | | - { |
78 | | - "type": "radio", |
79 | | - "name": "snmp_version", |
80 | | - "caption": "SNMP Version", |
81 | | - "required": "1", |
82 | | - "help": "Choose your SNMP Version", |
83 | | - "options": { |
84 | | - "1": "V1", |
85 | | - "2": "V2c", |
86 | | - "3": "V3" |
87 | | - }, |
88 | | - "default": 2 |
89 | | - }, |
90 | | - { |
91 | | - "type": "edit", |
92 | | - "name": "community", |
93 | | - "caption": "Community String", |
94 | | - "required": "1", |
95 | | - "help": "Please enter the community string." |
96 | | - }, |
97 | | - { |
98 | | - "type": "integer", |
99 | | - "name": "port", |
100 | | - "caption": "Port", |
101 | | - "required": "1", |
102 | | - "default": 161, |
103 | | - "help": "Provide the SNMP port" |
104 | | - } |
105 | | - ] |
106 | | - } |
107 | | - ] |
108 | | - } |
109 | | - if not snmp: |
110 | | - sensordefinition = "" |
111 | | - return sensordefinition |
112 | | - |
113 | | - def snmp_get(self, oid, target, snmp_type, community, port, unit): |
114 | | - try: |
115 | | - sys.path.append('./') |
116 | | - from pysnmp.entity.rfc3413.oneliner import cmdgen |
117 | | - snmpget = cmdgen.CommandGenerator() |
118 | | - error_indication, error_status, error_index, var_binding = snmpget.getCmd( |
119 | | - cmdgen.CommunityData(community), cmdgen.UdpTransportTarget((target, port)), oid) |
120 | | - except Exception as import_error: |
121 | | - logging.error(import_error) |
122 | | - raise |
123 | | - |
124 | | - if snmp_type == "1": |
125 | | - channellist = [ |
126 | | - { |
127 | | - "name": "Value", |
128 | | - "mode": "integer", |
129 | | - "kind": "custom", |
130 | | - "customunit": "%s" % unit, |
131 | | - "value": str(var_binding[0][1]) |
132 | | - } |
133 | | - ] |
134 | | - else: |
135 | | - channellist = [ |
136 | | - { |
137 | | - "name": "Value", |
138 | | - "mode": "counter", |
139 | | - "kind": "custom", |
140 | | - "customunit": "%s" % unit, |
141 | | - "value": str(var_binding[0][1]) |
142 | | - } |
143 | | - ] |
144 | | - return channellist |
145 | | - |
146 | | - @staticmethod |
147 | | - def get_data(data, out_queue): |
148 | | - snmpcustom = SNMPCustomString() |
149 | | - try: |
150 | | - snmp_data = snmpcustom.snmp_get(str(data['oid']), data['host'], data['value_type'], |
151 | | - data['community'], int(data['port']), data['unit']) |
152 | | - logging.debug("Running sensor: %s" % snmpcustom.get_kind()) |
153 | | - except Exception as get_data_error: |
154 | | - logging.error("Ooops Something went wrong with '%s' sensor %s. Error: %s" % (snmpcustom.get_kind(), |
155 | | - data['sensorid'], |
156 | | - get_data_error)) |
157 | | - data = { |
158 | | - "sensorid": int(data['sensorid']), |
159 | | - "error": "Exception", |
160 | | - "code": 1, |
161 | | - "message": "SNMP Request failed. See log for details" |
162 | | - } |
163 | | - out_queue.put(data) |
164 | | - return 1 |
165 | | - |
166 | | - data = { |
167 | | - "sensorid": int(data['sensorid']), |
168 | | - "message": "OK", |
169 | | - "channel": snmp_data |
170 | | - } |
171 | | - del snmpcustom |
172 | | - gc.collect() |
173 | | - out_queue.put(data) |
174 | | - return 0 |
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2014, Paessler AG <support@paessler.com> |
| 3 | +# All rights reserved. |
| 4 | +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
| 5 | +# following conditions are met: |
| 6 | +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions |
| 7 | +# and the following disclaimer. |
| 8 | +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions |
| 9 | +# and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 10 | +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse |
| 11 | +# or promote products derived from this software without specific prior written permission. |
| 12 | + |
| 13 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 14 | +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 15 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 16 | +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 17 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 19 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 20 | +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 21 | + |
| 22 | +import sys |
| 23 | +import gc |
| 24 | +import logging |
| 25 | +import time |
| 26 | + |
| 27 | +try: |
| 28 | + from pysnmp.entity.rfc3413.oneliner import cmdgen |
| 29 | + snmp = True |
| 30 | +except Exception as e: |
| 31 | + logging.error("PySNMP could not be imported. SNMP Sensors won't work.Error: %s" % e) |
| 32 | + snmp = False |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +class SNMPCustomString(object): |
| 37 | + |
| 38 | + def __init__(self): |
| 39 | + gc.enable() |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def get_kind(): |
| 43 | + """ |
| 44 | + return sensor kind |
| 45 | + """ |
| 46 | + return "mpsnmpcustomstring" |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def get_sensordef(): |
| 50 | + """ |
| 51 | + Definition of the sensor and data to be shown in the PRTG WebGUI |
| 52 | + """ |
| 53 | + sensordefinition = { |
| 54 | + "kind": SNMPCustomString.get_kind(), |
| 55 | + "name": "SNMP Custom String", |
| 56 | + "description": "Monitors a string value returned by a specific OID using SNMP", |
| 57 | + "help": "Monitors a string value returned by a specific OID using SNMP", |
| 58 | + "tag": "mpsnmpcustomstringsensor", |
| 59 | + "groups": [ |
| 60 | + { |
| 61 | + "name": "OID values", |
| 62 | + "caption": "OID values", |
| 63 | + "fields": [ |
| 64 | + { |
| 65 | + "type": "edit", |
| 66 | + "name": "oid", |
| 67 | + "caption": "OID Value", |
| 68 | + "required": "1", |
| 69 | + "help": "Please enter the OID value." |
| 70 | + }, |
| 71 | + { |
| 72 | + "type": "radio", |
| 73 | + "name": "snmp_version", |
| 74 | + "caption": "SNMP Version", |
| 75 | + "required": "1", |
| 76 | + "help": "Choose your SNMP Version", |
| 77 | + "options": { |
| 78 | + "1": "V1", |
| 79 | + "2": "V2c", |
| 80 | + "3": "V3" |
| 81 | + }, |
| 82 | + "default": 2 |
| 83 | + }, |
| 84 | + { |
| 85 | + "type": "edit", |
| 86 | + "name": "community", |
| 87 | + "caption": "Community String", |
| 88 | + "required": "1", |
| 89 | + "help": "Please enter the community string." |
| 90 | + }, |
| 91 | + { |
| 92 | + "type": "integer", |
| 93 | + "name": "port", |
| 94 | + "caption": "Port", |
| 95 | + "required": "1", |
| 96 | + "default": 161, |
| 97 | + "help": "Provide the SNMP port" |
| 98 | + } |
| 99 | + ] |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + if not snmp: |
| 104 | + sensordefinition = "" |
| 105 | + return sensordefinition |
| 106 | + |
| 107 | + def snmp_get(self, oid, target, snmp_type, community, port, unit): |
| 108 | + try: |
| 109 | + sys.path.append('./') |
| 110 | + from pysnmp.entity.rfc3413.oneliner import cmdgen |
| 111 | + start = time.clock() |
| 112 | + snmpget = cmdgen.CommandGenerator() |
| 113 | + error_indication, error_status, error_index, var_binding = snmpget.getCmd( |
| 114 | + cmdgen.CommunityData(community), cmdgen.UdpTransportTarget((target, port)), oid) |
| 115 | + end = time.clock() |
| 116 | + delta = (end - start) * 1000 |
| 117 | + except Exception as import_error: |
| 118 | + logging.error(import_error) |
| 119 | + raise |
| 120 | + |
| 121 | + channel_list = [ |
| 122 | + { |
| 123 | + "name": "Response Time", |
| 124 | + "mode": "float", |
| 125 | + "kind": "TimeResponse", |
| 126 | + "value": float(delta) |
| 127 | + } |
| 128 | + ] |
| 129 | + return ( |
| 130 | + str(var_binding[0][1]), |
| 131 | + channel_list |
| 132 | + ) |
| 133 | + |
| 134 | + @staticmethod |
| 135 | + def get_data(data, out_queue): |
| 136 | + snmpcustom = SNMPCustomString() |
| 137 | + try: |
| 138 | + snmp_data, channel = snmpcustom.snmp_get(str(data['oid']), data['host'], 'string', |
| 139 | + data['community'], int(data['port']), '') |
| 140 | + logging.debug("Running sensor: %s" % snmpcustom.get_kind()) |
| 141 | + except Exception as get_data_error: |
| 142 | + logging.error("Ooops Something went wrong with '%s' sensor %s. Error: %s" % (snmpcustom.get_kind(), |
| 143 | + data['sensorid'], |
| 144 | + get_data_error)) |
| 145 | + data = { |
| 146 | + "sensorid": int(data['sensorid']), |
| 147 | + "error": "Exception", |
| 148 | + "code": 1, |
| 149 | + "message": "SNMP Request failed. See log for details" |
| 150 | + } |
| 151 | + out_queue.put(data) |
| 152 | + return 1 |
| 153 | + |
| 154 | + data = { |
| 155 | + "sensorid": int(data['sensorid']), |
| 156 | + "message": snmp_data, |
| 157 | + "channel": channel |
| 158 | + } |
| 159 | + del snmpcustom |
| 160 | + gc.collect() |
| 161 | + out_queue.put(data) |
| 162 | + return 0 |
0 commit comments