Package Products :: Package ZenModel :: Module IpService
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenModel.IpService

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, 2009 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  __doc__="""IpService 
 15   
 16  IpService is a function provided by computer (like a server).  it 
 17  is defined by a protocol type (udp/tcp) and a port number. 
 18   
 19  """ 
 20   
 21  from Globals import DTMLFile, InitializeClass 
 22  from AccessControl import ClassSecurityInfo 
 23  from Products.ZenModel.ZenossSecurity import * 
 24   
 25  from Products.ZenRelations.RelSchema import * 
 26   
 27  from Products.ZenModel.Service import Service 
 28  from Products.ZenModel.IpServiceClass import IpServiceClass 
 29  from Products.ZenUtils.IpUtil import isip 
 30   
31 -def manage_addIpService(context, id, protocol, port, userCreated=None, REQUEST=None):
32 """ 33 Make an IP service entry 34 """ 35 s = IpService(id) 36 s.protocol = protocol 37 s.port = int(port) 38 args = {'protocol':protocol, 'port':int(port)} 39 # Indexing is subscribed to ObjectAddedEvent, which fires 40 # on _setObject, so we want to set service class first. 41 s.__of__(context).setServiceClass(args) 42 context._setObject(id, s) 43 s = context._getOb(id) 44 if userCreated: s.setUserCreateFlag() 45 if REQUEST is not None: 46 REQUEST['RESPONSE'].redirect(context.absolute_url() 47 +'/manage_main') 48 return s
49 50 addIpService = DTMLFile('dtml/addIpService',globals()) 51 52
53 -def getIpServiceKey(protocol, port):
54 return "%s_%05d" % (protocol, port)
55 56
57 -class IpService(Service):
58 """ 59 IpService object 60 """ 61 62 __pychecker__='no-override' 63 64 portal_type = meta_type = 'IpService' 65 66 protocols = ('tcp', 'udp') 67 68 ipaddresses = [] 69 discoveryAgent = "" 70 port = 0 71 protocol = "" 72 manageIp = "" 73 74 collectors = ('zenstatus',) 75 76 _properties = ( 77 {'id':'port', 'type':'int', 'mode':'', 'setter': 'setPort', 78 'description':"TCP port to check for this service."}, 79 {'id':'protocol', 'type':'string', 'mode':'', 'setter': 'setProtocol', 80 'description':"Protocol (TCP or UPD) used by this service."}, 81 {'id':'ipaddresses', 'type':'lines', 'mode':'', 82 'description':"IP addresses that this service is listening on."}, 83 {'id':'discoveryAgent', 'type':'string', 'mode':'', 84 'description':"What process was used to discover this service."}, 85 {'id':'manageIp', 'type':'string', 'mode':'', 86 'description':"The IP address to check for this service."}, 87 ) 88 _relations = Service._relations + ( 89 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","ipservices")), 90 ) 91 92 factory_type_information = ( 93 { 94 'immediate_view' : 'ipServiceDetail', 95 'actions' : 96 ( 97 { 'id' : 'status' 98 , 'name' : 'Status' 99 , 'action' : 'ipServiceDetail' 100 , 'permissions' : (ZEN_VIEW, ) 101 }, 102 { 'id' : 'events' 103 , 'name' : 'Events' 104 , 'action' : 'viewEvents' 105 , 'permissions' : (ZEN_VIEW, ) 106 }, 107 { 'id' : 'manage' 108 , 'name' : 'Administration' 109 , 'action' : 'ipServiceManage' 110 , 'permissions' : ("Manage DMD",) 111 }, 112 { 'id' : 'viewHistory' 113 , 'name' : 'Modifications' 114 , 'action' : 'viewHistory' 115 , 'permissions' : (ZEN_VIEW_MODIFICATIONS,) 116 }, 117 ) 118 }, 119 ) 120 121 security = ClassSecurityInfo() 122 123
124 - def monitored(self):
125 """ 126 Return monitored state of ipservice. 127 If service only listens on 127.0.0.1 return false. 128 """ 129 if self.cantMonitor(): return False 130 return super(IpService, self).monitored()
131 132
133 - def cantMonitor(self):
134 """ 135 Return true if IpService only listens on 127.0.0.1, or if it is a UDP 136 service. 137 """ 138 return self.protocol == 'udp' \ 139 or ( len(self.ipaddresses) == 1 140 and "127.0.0.1" in self.ipaddresses )
141 142 143
144 - def getInstDescription(self):
145 """ 146 Return some text that describes this component. Default is name. 147 """ 148 return "%s-%d ips:%s" % (self.protocol, self.port, 149 ", ".join(self.ipaddresses))
150 151
152 - def setServiceClass(self, kwargs):
153 """ 154 Set the service class based on a dict describing the service. 155 Dict keys are be protocol and port 156 """ 157 protocol = kwargs['protocol'] 158 port = kwargs['port'] 159 name = getIpServiceKey(protocol, port) 160 path = "/IpService/" 161 srvs = self.dmd.getDmdRoot("Services") 162 srvclass = srvs.createServiceClass(name=name, path=path, 163 factory=IpServiceClass, port=port) 164 self.serviceclass.addRelation(srvclass)
165 166
167 - def getSendString(self):
168 return self.getAqProperty("sendString")
169 170
171 - def getExpectRegex(self):
172 return self.getAqProperty("expectRegex")
173 174
175 - def getServiceClass(self):
176 """ 177 Return a dict like one set by IpServiceMap for services. 178 """ 179 svc = self.serviceclass() 180 if svc: 181 return {'protocol': self.protocol, 'port': self.port } 182 return {}
183 184
185 - def primarySortKey(self):
186 return "%s-%05d" % (self.protocol, self.port)
187
188 - def getManageIp(self):
189 """ 190 A service can listen on multiple interfaces on a device, 191 and the interface it listens on may not be the same one 192 that is the manageIp for the device. 193 194 @return: IP address to contact the service on 195 @rtype: string 196 """ 197 if self.manageIp: 198 # List of IP address 199 interfaces = self.getNonLoopbackIpAddresses() 200 if self.manageIp in interfaces: 201 if self.manageIp in self.ipaddresses or \ 202 '0.0.0.0' in self.ipaddresses: 203 return self.manageIp 204 # Oops! Our management IP is no longer here 205 206 return self._getManageIp()
207
208 - def _getManageIp(self):
209 """ 210 Pick an IP out of available choices. 211 212 @return: IP address to contact the service on 213 @rtype: string 214 """ 215 manage_ip = Service.getManageIp(self) 216 bare_ip = manage_ip.split('/',1)[0] 217 if bare_ip in self.ipaddresses: 218 return bare_ip 219 220 for ip in self.ipaddresses: 221 if ip != '0.0.0.0' and ip != '127.0.0.1': 222 return ip 223 return bare_ip
224
225 - def setManageIp(self, manageIp):
226 """ 227 Manually set the management IP address to check the 228 service status. 229 230 @parameter manageIp: IP address to check the service health 231 @type manageIp: string 232 """ 233 if not manageIp: 234 return 235 236 bare_ip = manageIp.split('/',1)[0] 237 if not isip(bare_ip): 238 return 239 240 ips = self.getIpAddresses() 241 if '0.0.0.0' in self.ipaddresses and bare_ip in ips: 242 self.manageIp = bare_ip 243 244 if bare_ip in self.ipaddresses: 245 self.manageIp = bare_ip
246
247 - def unsetManageIp(self):
248 """ 249 Remove a prevously set management IP address to check the 250 service status. 251 """ 252 self.manageIp = ''
253
254 - def getIpAddresses(self):
255 """ 256 List the IP addresses to which we can contact the service. 257 258 @return: list of IP addresses 259 @rtype: array of strings 260 """ 261 ips = [ ip for ip in self.ipaddresses \ 262 if ip != '0.0.0.0' and ip != '127.0.0.1' ] 263 if not ips: 264 ips = Service.getNonLoopbackIpAddresses(self) 265 ips = [ x.split('/',1)[0] for x in ips ] 266 return ips
267 268
269 - def getProtocol(self):
270 return self.protocol
271
272 - def getPort(self):
273 return self.port
274
275 - def getKeyword(self):
276 sc = self.serviceclass() 277 if sc: return sc.name
278
279 - def getDescription(self):
280 sc = self.serviceclass() 281 if sc: return sc.description
282
283 - def ipServiceClassUrl(self):
284 sc = self.serviceclass() 285 if sc: return sc.getPrimaryUrlPath()
286 287 288 security.declareProtected('Manage DMD', 'manage_editService')
289 - def manage_editService(self, id=None, 290 status=None, ipaddresses=None, 291 manageIp=None, 292 protocol=None, port=None, 293 description=None, 294 monitor=False, severity=5, sendString="", 295 expectRegex="", REQUEST=None):
296 """ 297 Edit a Service from a web page. 298 """ 299 if id: 300 self.rename(id) 301 if status: self.status = status 302 self.ipaddresses = ipaddresses 303 self.description = description 304 self.protocol = protocol 305 self._updateProperty('port', port) 306 307 308 if protocol != self.protocol or port != self.port: 309 self.setServiceClass({'protocol':protocol, 'port':int(port)}) 310 311 self.setManageIp(manageIp) 312 313 msg = [] 314 msg.append(self.setAqProperty("sendString", sendString, "string")) 315 msg.append(self.setAqProperty("expectRegex", expectRegex, "string")) 316 self.index_object() 317 318 return super(IpService, self).manage_editService(monitor, severity, 319 msg=msg,REQUEST=REQUEST)
320 321 322 InitializeClass(IpService) 323