1
2
3
4
5
6
7
8
9
10
11
12
13
14 import socket
15 import logging
16 log = logging.getLogger("zen.SnmpClient")
17
18 from twisted.internet import reactor, error, defer
19 from twisted.python import failure
20 try:
21 from pynetsnmp.twistedsnmp import snmpprotocol, AgentProxy
22 except ImportError:
23 from twistedsnmp import snmpprotocol
24 from twistedsnmp.agentproxy import AgentProxy
25
26 import Globals
27
28 from Products.ZenUtils.IpUtil import isip
29 from Products.ZenUtils.Driver import drive
30
31 global defaultTries, defaultTimeout
32 defaultTries = 2
33 defaultTimeout = 1
34 defaultSnmpCommunity = 'public'
35
36 DEFAULT_MAX_OIDS_BACK = 40
37
39
42 global defaultTries, defaultTimeout
43 self.hostname = hostname
44 self.device = device
45 self.options = options
46 self.datacollector = datacollector
47 self.plugins = plugins
48
49 self._getdata = {}
50 self._tabledata = {}
51
52 community = getattr(device, 'zSnmpCommunity', "public")
53 port = int(getattr(device, 'zSnmpPort', 161))
54 snmpver = getattr(device, 'zSnmpVer', "v1")
55 self.tries = int(getattr(device,'zSnmpTries', defaultTries))
56 self.timeout = float(getattr(device,'zSnmpTimeout', defaultTimeout))
57
58 srcport = snmpprotocol.port()
59 self.proxy = AgentProxy(ipaddr, port, community, snmpver,
60 protocol=srcport.protocol)
61 if not hasattr(self.proxy, 'open'):
62 def doNothing(): pass
63 self.proxy.open = doNothing
64 self.proxy.close = doNothing
65
66
73
74
96
97
118
119
121 for plugin in self.plugins:
122 try:
123 log.debug('running %s', plugin)
124 pname = plugin.name()
125 self._tabledata[pname] = {}
126 log.debug("sending queries for plugin %s", pname)
127 if plugin.snmpGetMap:
128 yield self.proxy.get(plugin.snmpGetMap.getoids(),
129 timeout=self.timeout,
130 retryCount=self.tries)
131 self._getdata[pname] = driver.next()
132 for tmap in plugin.snmpGetTableMaps:
133 rowSize = len(tmap.getoids())
134 maxRepetitions = max(DEFAULT_MAX_OIDS_BACK / rowSize, 1)
135 yield self.proxy.getTable(tmap.getoids(),
136 timeout=self.timeout,
137 retryCount=self.tries,
138 maxRepetitions=maxRepetitions)
139 self._tabledata[pname][tmap] = driver.next()
140 except Exception, ex:
141 if not isinstance( ex, error.TimeoutError ):
142 log.exception("device %s plugin %s unexpected error",
143 self.hostname, pname)
144
145
147 """Return data for this client in the form
148 ((pname, (getdata, tabledata),)
149 getdata = {'.1.2.4.5':"value",}
150 tabledata = {tableMap : {'.1.2.3.4' : {'.1.2.3.4.1': "value",...}}}
151 """
152 data = []
153 for plugin in self.plugins:
154 pname = plugin.name()
155 getdata = self._getdata.get(pname,{})
156 tabledata = self._tabledata.get(pname,{})
157 if getdata or tabledata:
158 data.append((pname, (getdata, tabledata)))
159 return data
160
176
177
178
180 "build options list that both telnet and ssh use"
181
182 if not usage:
183 usage = "%prog [options] hostname[:port] oids"
184
185 if not parser:
186 from optparse import OptionParser
187 parser = OptionParser(usage=usage)
188
189 parser.add_option('--snmpCommunity',
190 dest='snmpCommunity',
191 default=defaultSnmpCommunity,
192 help='Snmp Community string')
193
194
195 if __name__ == "__main__":
196 import pprint
197 logging.basicConfig()
198 log = logging.getLogger()
199 log.setLevel(20)
200 import sys
201 sys.path.append("plugins")
202 from plugins.zenoss.snmp.InterfaceMap import InterfaceMap
203 ifmap = InterfaceMap()
204 sc = SnmpClient("gate.confmon.loc", community="zentinel", plugins=[ifmap,])
205 reactor.run()
206 pprint.pprint(sc.getResults())
207