Package ZenHub :: Package services :: Module PerformanceConfig
[hide private]
[frames] | no frames]

Source Code for Module ZenHub.services.PerformanceConfig

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  import Globals 
 15  from Products.ZenEvents.ZenEventClasses import Status_Snmp 
 16   
 17  from Products.ZenHub.HubService import HubService 
 18  from Products.ZenHub.PBDaemon import translateError 
 19   
 20  from Products.ZenModel.Device import Device 
 21  from Acquisition import aq_parent 
 22   
 23  from twisted.internet import defer 
 24   
 25  from Procrastinator import Procrastinate 
 26  from ThresholdMixin import ThresholdMixin 
 27   
 28  ATTRIBUTES = ( 
 29      'id', 
 30      'manageIp', 
 31      'zMaxOIDPerRequest', 
 32      'zSnmpMonitorIgnore', 
 33      'zSnmpAuthPassword', 
 34      'zSnmpAuthType', 
 35      'zSnmpCommunity', 
 36      'zSnmpPort', 
 37      'zSnmpPrivPassword', 
 38      'zSnmpPrivType', 
 39      'zSnmpSecurityName', 
 40      'zSnmpTimeout', 
 41      'zSnmpTries', 
 42      'zSnmpVer', 
 43      ) 
 44   
 45  from twisted.spread import pb 
46 -class SnmpConnInfo(pb.Copyable, pb.RemoteCopy):
47 "A class to transfer the many SNMP values to clients" 48
49 - def __init__(self, device):
50 "Store the properties from the device" 51 for propertyName in ATTRIBUTES: 52 setattr(self, propertyName, getattr(device, propertyName, None)) 53 self.id = device.id
54
55 - def __cmp__(self, other):
56 for propertyName in ATTRIBUTES: 57 c = cmp(getattr(self, propertyName), getattr(other, propertyName)) 58 if c != 0: 59 return c 60 return 0
61
62 - def summary(self):
63 result = 'SNMP info for %s at %s:%s' % ( 64 self.id, self.manageIp, self.zSnmpPort) 65 result += ' timeout: %s tries: %d' % ( 66 self.zSnmpTimeout, self.zSnmpTries) 67 result += ' version: %s ' % (self.zSnmpVer) 68 if '3' not in self.zSnmpVer: 69 result += ' community: %s' % self.zSnmpCommunity 70 else: 71 result += ' securityName: %s' % self.zSnmpSecurityName 72 result += ' authType: %s' % self.zSnmpAuthType 73 result += ' privType: %s' % self.zSnmpPrivType 74 return result
75
76 - def createSession(self, protocol=None, allowCache=False):
77 "Create a session based on the properties" 78 from pynetsnmp.twistedsnmp import AgentProxy 79 cmdLineArgs=[] 80 if '3' in self.zSnmpVer: 81 if self.zSnmpPrivType: 82 cmdLineArgs += ['-l', 'authPriv'] 83 cmdLineArgs += ['-x', self.zSnmpPrivType] 84 cmdLineArgs += ['-X', self.zSnmpPrivPassword] 85 elif self.zSnmpAuthType: 86 cmdLineArgs += ['-l', 'authNoPriv'] 87 else: 88 cmdLineArgs += ['-l', 'noAuthNoPriv'] 89 if self.zSnmpAuthType: 90 cmdLineArgs += ['-a', self.zSnmpAuthType] 91 cmdLineArgs += ['-A', self.zSnmpAuthPassword] 92 cmdLineArgs += ['-u', self.zSnmpSecurityName] 93 p = AgentProxy(ip=self.manageIp, 94 port=self.zSnmpPort, 95 timeout=self.zSnmpTimeout, 96 snmpVersion=self.zSnmpVer, 97 community=self.zSnmpCommunity, 98 cmdLineArgs=cmdLineArgs, 99 protocol=protocol, 100 allowCache=allowCache) 101 p.snmpConnInfo = self 102 return p
103
104 - def __repr__(self):
105 return '<%s for %s>' % (self.__class__, self.id)
106 107 pb.setUnjellyableForClass(SnmpConnInfo, SnmpConnInfo) 108 109
110 -class PerformanceConfig(HubService, ThresholdMixin):
111
112 - def __init__(self, dmd, instance):
113 HubService.__init__(self, dmd, instance) 114 self.config = self.dmd.Monitors.Performance._getOb(self.instance) 115 self.procrastinator = Procrastinate(self.pushConfig)
116 117 118 @translateError
119 - def remote_propertyItems(self):
120 return self.config.propertyItems()
121 122 123 @translateError
124 - def remote_getSnmpStatus(self, devname=None):
125 "Return the failure counts for Snmp" 126 counts = {} 127 try: 128 # get all the events with /Status/Snmp 129 conn = self.zem.connect() 130 try: 131 curs = conn.cursor() 132 cmd = ('SELECT device, sum(count) ' + 133 ' FROM status ' + 134 ' WHERE eventClass = "%s"' % Status_Snmp) 135 if devname: 136 cmd += ' AND device = "%s"' % devname 137 cmd += ' GROUP BY device' 138 curs.execute(cmd); 139 counts = dict([(d, int(c)) for d, c in curs.fetchall()]) 140 finally: 141 self.zem.close(conn) 142 except Exception, ex: 143 self.log.exception('Unable to get Snmp Status') 144 raise 145 if devname: 146 return [(devname, counts.get(devname, 0))] 147 return [(dev.id, counts.get(dev.id, 0)) for dev in self.config.devices()]
148 149
150 - def remote_getDefaultRRDCreateCommand(self, *args, **kwargs):
151 return self.config.getDefaultRRDCreateCommand(*args, **kwargs)
152 153
154 - def notifyAll(self, device):
155 if device.perfServer.getRelatedId() == self.instance: 156 self.procrastinator.doLater(device)
157 158
159 - def pushConfig(self, device):
160 deferreds = [] 161 cfg = self.getDeviceConfig(device) 162 for listener in self.listeners: 163 if cfg is None: 164 deferreds.append(listener.callRemote('deleteDevice', device.id)) 165 else: 166 deferreds.append(self.sendDeviceConfig(listener, cfg)) 167 return defer.DeferredList(deferreds)
168 169
170 - def getDeviceConfig(self, device):
171 "How to get the config for a device" 172 return None
173 174
175 - def sendDeviceConfig(self, listener, config):
176 "How to send the config to a device, probably via callRemote" 177 pass
178 179
180 - def update(self, object):
181 if not self.listeners: 182 return 183 184 # the PerformanceConf changed 185 from Products.ZenModel.PerformanceConf import PerformanceConf 186 if isinstance(object, PerformanceConf) and object.id == self.instance: 187 for listener in self.listeners: 188 listener.callRemote('setPropertyItems', object.propertyItems()) 189 190 # a ZenPack is installed 191 from Products.ZenModel.ZenPack import ZenPack 192 if isinstance(object, ZenPack): 193 for listener in self.listeners: 194 try: 195 listener.callRemote('updateThresholdClasses', 196 self.remote_getThresholdClasses()) 197 except Exception, ex: 198 self.log.warning("Error notifying a listener of new classes") 199 200 # device has been changed: 201 if isinstance(object, Device): 202 self.notifyAll(object) 203 return 204 205 # somethinge else... mark the devices as out-of-date 206 from Products.ZenModel.DeviceClass import DeviceClass 207 208 while object: 209 # walk up until you hit an organizer or a device 210 if isinstance(object, DeviceClass): 211 for device in object.getSubDevices(): 212 self.notifyAll(device) 213 break 214 215 if isinstance(object, Device): 216 self.notifyAll(object) 217 break 218 219 object = aq_parent(object)
220 221
222 - def deleted(self, obj):
223 for listener in self.listeners: 224 if isinstance(obj, Device): 225 listener.callRemote('deleteDevice', obj.id)
226