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

Source Code for Module ZenModel.IpService

  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  __doc__="""IpService.py 
 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  $Id: IpService.py,v 1.10 2004/04/22 22:04:14 edahl Exp $""" 
 20   
 21  __version__ = "$Revision: 1.10 $"[11:-2] 
 22   
 23  from Globals import DTMLFile 
 24  from Globals import InitializeClass 
 25  from AccessControl import ClassSecurityInfo 
 26  from Products.ZenModel.ZenossSecurity import * 
 27   
 28  from Products.ZenRelations.RelSchema import * 
 29   
 30  from Service import Service 
 31  from Products.ZenModel.IpServiceClass import IpServiceClass 
 32   
33 -def manage_addIpService(context, id, protocol, port, userCreated=None, REQUEST=None):
34 """make a device""" 35 s = IpService(id) 36 context._setObject(id, s) 37 s = context._getOb(id) 38 s.protocol = protocol 39 s.port = int(port) 40 args = {'protocol':protocol, 'port':int(port)} 41 s.setServiceClass(args) 42 if userCreated: s.setUserCreateFlag() 43 if REQUEST is not None: 44 REQUEST['RESPONSE'].redirect(context.absolute_url() 45 +'/manage_main') 46 return s
47 48 addIpService = DTMLFile('dtml/addIpService',globals()) 49 50
51 -def getIpServiceKey(protocol, port):
52 return "%s_%05d" % (protocol, port)
53 54
55 -class IpService(Service):
56 """ 57 IpService object 58 """ 59 60 __pychecker__='no-override' 61 62 portal_type = meta_type = 'IpService' 63 64 protocols = ('tcp', 'udp') 65 66 ipaddresses = [] 67 discoveryAgent = "" 68 port = 0 69 protocol = "" 70 71 collectors = ('zenstatus',) 72 73 _properties = ( 74 {'id':'port', 'type':'int', 'mode':'', 'setter': 'setPort'}, 75 {'id':'protocol', 'type':'string', 'mode':'', 'setter': 'setProtocol'}, 76 {'id':'ipaddresses', 'type':'lines', 'mode':''}, 77 {'id':'discoveryAgent', 'type':'string', 'mode':''}, 78 ) 79 _relations = Service._relations + ( 80 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","ipservices")), 81 ) 82 83 factory_type_information = ( 84 { 85 'immediate_view' : 'ipServiceDetail', 86 'actions' : 87 ( 88 { 'id' : 'status' 89 , 'name' : 'Status' 90 , 'action' : 'ipServiceDetail' 91 , 'permissions' : (ZEN_VIEW, ) 92 }, 93 { 'id' : 'events' 94 , 'name' : 'Events' 95 , 'action' : 'viewEvents' 96 , 'permissions' : (ZEN_VIEW, ) 97 }, 98 { 'id' : 'manage' 99 , 'name' : 'Administration' 100 , 'action' : 'ipServiceManage' 101 , 'permissions' : ("Manage DMD",) 102 }, 103 { 'id' : 'viewHistory' 104 , 'name' : 'Modifications' 105 , 'action' : 'viewHistory' 106 , 'permissions' : (ZEN_VIEW_MODIFICATIONS,) 107 }, 108 ) 109 }, 110 ) 111 112 security = ClassSecurityInfo() 113 114
115 - def monitored(self):
116 """Return monitored state of ipservice. 117 If service only listens on 127.0.0.1 return false. 118 """ 119 if self.cantMonitor(): return False 120 return super(IpService, self).monitored()
121 122
123 - def cantMonitor(self):
124 """ 125 Return true if IpService only listens on 127.0.0.1, or if it is a UDP 126 service. 127 """ 128 return self.protocol == 'udp' \ 129 or ( len(self.ipaddresses) == 1 130 and "127.0.0.1" in self.ipaddresses )
131 132 133
134 - def getInstDescription(self):
135 """Return some text that describes this component. Default is name. 136 """ 137 return "%s-%d ips:%s" % (self.protocol, self.port, 138 ", ".join(self.ipaddresses))
139 140
141 - def setServiceClass(self, kwargs):
142 """Set the service class based on a dict describing the service. 143 Dict keys are be protocol and port 144 """ 145 protocol = kwargs['protocol'] 146 port = kwargs['port'] 147 name = getIpServiceKey(protocol, port) 148 path = "/IpService/" 149 srvs = self.dmd.getDmdRoot("Services") 150 srvclass = srvs.createServiceClass(name=name, path=path, 151 factory=IpServiceClass, port=port) 152 self.serviceclass.addRelation(srvclass)
153 154
155 - def getSendString(self):
156 return self.getAqProperty("sendString")
157 158
159 - def getExpectRegex(self):
160 return self.getAqProperty("expectRegex")
161 162
163 - def getServiceClass(self):
164 """Return a dict like one set by IpServiceMap for services. 165 """ 166 svc = self.serviceclass() 167 if svc: 168 return {'protocol': self.protocol, 'port': svc.port } 169 return {}
170 171
172 - def primarySortKey(self):
173 return "%s-%05d" % (self.protocol, self.port)
174
175 - def getManageIp(self):
176 manage_ip = Service.getManageIp(self) 177 for ip in self.ipaddresses: 178 if ip != '0.0.0.0' and ip != '127.0.0.1': 179 return ip 180 return manage_ip
181
182 - def getProtocol(self):
183 return self.protocol
184
185 - def getPort(self):
186 return self.port
187
188 - def getKeyword(self):
189 sc = self.serviceclass() 190 if sc: return sc.name
191
192 - def getDescription(self):
193 sc = self.serviceclass() 194 if sc: return sc.description
195
196 - def ipServiceClassUrl(self):
197 sc = self.serviceclass() 198 if sc: return sc.getPrimaryUrlPath()
199 200 201 security.declareProtected('Manage DMD', 'manage_editService')
202 - def manage_editService(self, id=None, 203 status=None, ipaddresses=None, 204 protocol=None, port=None, 205 description=None, 206 monitor=False, severity=5, sendString="", 207 expectRegex="", REQUEST=None):
208 """Edit a Service from a web page. 209 """ 210 if id: 211 self.rename(id) 212 if status: self.status = status 213 self.ipaddresses = ipaddresses 214 self.description = description 215 self.protocol = protocol 216 self._updateProperty('port', port) 217 if protocol != self.protocol or port != self.port: 218 self.setServiceClass({'protocol':protocol, 'port':int(port)}) 219 220 msg = [] 221 msg.append(self.setAqProperty("sendString", sendString, "string")) 222 msg.append(self.setAqProperty("expectRegex", expectRegex, "string")) 223 self.index_object() 224 225 return super(IpService, self).manage_editService(monitor, severity, 226 msg=msg,REQUEST=REQUEST)
227 228 229 InitializeClass(IpService) 230