1
2
3
4
5
6
7
8
9
10
11
12
13
14 import sys
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 from twisted.internet.error import TimeoutError
21
22 from Products.ZenUtils.snmp import SnmpV1Config, SnmpV2cConfig
23 from Products.ZenUtils.snmp import SnmpAgentDiscoverer
24
25 from pynetsnmp.twistedsnmp import snmpprotocol, Snmpv3Error
26
27 import Globals
28
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
38 from BaseClient import BaseClient
39
41
42 - def __init__(self, hostname, ipaddr, options=None, device=None,
43 datacollector=None, plugins=[]):
58
64
71
72
73
94
95
136
138 def inner(driver):
139 """
140 Twisted driver class to iterate through devices
141
142 @param driver: Zenoss driver
143 @type driver: Zenoss driver
144 @return: successful result is a list of IPs that were added
145 @rtype: Twisted deferred
146 """
147 log.info("Rediscovering SNMP connection info for %s",
148 self.device.id)
149
150 communities = list(self.device.zSnmpCommunities)
151 communities.reverse()
152
153 configs = []
154 weight = 0
155 for community in communities:
156 for port in self.device.zSnmpPorts:
157 weight+=1
158 port = int(port)
159 configs.append(SnmpV1Config(
160 self.device.manageIp, weight=weight,
161 port=port,
162 timeout=self.connInfo.zSnmpTimeout,
163 retries=self.connInfo.zSnmpTries,
164 community=community))
165 configs.append(SnmpV2cConfig(
166 self.device.manageIp, weight=weight+1000, port=port,
167 timeout=self.connInfo.zSnmpTimeout,
168 retries=self.connInfo.zSnmpTries,
169 community=community))
170
171 yield SnmpAgentDiscoverer().findBestConfig(configs)
172 driver.next()
173 return drive(inner)
174
175
177 maxOidsPerRequest = getattr(self.device, 'zMaxOIDPerRequest', DEFAULT_MAX_OIDS_BACK)
178 log.debug("Using a max of %s OIDs per request", maxOidsPerRequest)
179 for plugin in self.plugins:
180 try:
181 log.debug('running %s', plugin)
182 pname = plugin.name()
183 self._tabledata[pname] = {}
184 log.debug("sending queries for plugin %s", pname)
185 if plugin.snmpGetMap:
186 results = {}
187 for oid in plugin.snmpGetMap.getoids():
188 yield self.proxy.get([oid])
189 results.update(driver.next())
190 self._getdata[pname] = results
191 for tmap in plugin.snmpGetTableMaps:
192 rowSize = len(tmap.getoids())
193 maxRepetitions = max(maxOidsPerRequest / rowSize, 1)
194 yield self.proxy.getTable(tmap.getoids(),
195 maxRepetitions=maxRepetitions,
196 limit=sys.maxint)
197 self._tabledata[pname][tmap] = driver.next()
198 except Exception, ex:
199 if not isinstance( ex, error.TimeoutError ):
200 log.exception("device %s plugin %s unexpected error",
201 self.hostname, pname)
202
203
205 """Return data for this client in the form
206 ((plugin, (getdata, tabledata),)
207 getdata = {'.1.2.4.5':"value",}
208 tabledata = {tableMap : {'.1.2.3.4' : {'.1.2.3.4.1': "value",...}}}
209 """
210 data = []
211 for plugin in self.plugins:
212 pname = plugin.name()
213 getdata = self._getdata.get(pname,{})
214 tabledata = self._tabledata.get(pname,{})
215 if getdata or tabledata:
216 data.append((plugin, (getdata, tabledata)))
217 return data
235
238
240 "build options list that both telnet and ssh use"
241 if not usage:
242 usage = "%prog [options] hostname[:port] oids"
243 if not parser:
244 from optparse import OptionParser
245 parser = OptionParser(usage=usage)
246
247 parser.add_option('--snmpCommunity',
248 dest='snmpCommunity',
249 default=defaultSnmpCommunity,
250 help='Snmp Community string')
251
252
253 if __name__ == "__main__":
254 import pprint
255 logging.basicConfig()
256 log = logging.getLogger()
257 log.setLevel(20)
258 import sys
259 sys.path.append("plugins")
260 from plugins.zenoss.snmp.InterfaceMap import InterfaceMap
261 ifmap = InterfaceMap()
262 sc = SnmpClient("gate.confmon.loc", community="zentinel", plugins=[ifmap,])
263 reactor.run()
264 pprint.pprint(sc.getResults())
265