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