1
2
3
4
5
6
7
8
9
10
11 __doc__="""IpService
12
13 IpService is a function provided by computer (like a server). it
14 is defined by a protocol type (udp/tcp) and a port number.
15
16 """
17
18 from Globals import DTMLFile, InitializeClass
19 from AccessControl import ClassSecurityInfo
20 from Products.ZenModel.ZenossSecurity import *
21
22 from Products.ZenRelations.RelSchema import *
23
24 from Products.ZenModel.Service import Service
25 from Products.ZenModel.IpServiceClass import IpServiceClass
26 from Products.ZenUtils.IpUtil import isip
27
46
47 addIpService = DTMLFile('dtml/addIpService',globals())
48
49
52
53
55 """
56 IpService object
57 """
58
59 __pychecker__='no-override'
60
61 portal_type = meta_type = 'IpService'
62
63 protocols = ('tcp', 'udp')
64
65 ipaddresses = []
66 discoveryAgent = ""
67 port = 0
68 protocol = ""
69 manageIp = ""
70
71 collectors = ('zenstatus',)
72
73 _properties = (
74 {'id':'port', 'type':'int', 'mode':'', 'setter': 'setPort',
75 'description':"TCP port to check for this service."},
76 {'id':'protocol', 'type':'string', 'mode':'', 'setter': 'setProtocol',
77 'description':"Protocol (TCP or UPD) used by this service."},
78 {'id':'ipaddresses', 'type':'lines', 'mode':'',
79 'description':"IP addresses that this service is listening on."},
80 {'id':'discoveryAgent', 'type':'string', 'mode':'',
81 'description':"What process was used to discover this service."},
82 {'id':'manageIp', 'type':'string', 'mode':'',
83 'description':"The IP address to check for this service."},
84 )
85 _relations = Service._relations + (
86 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","ipservices")),
87 )
88
89 factory_type_information = (
90 {
91 'immediate_view' : 'ipServiceDetail',
92 'actions' :
93 (
94 { 'id' : 'status'
95 , 'name' : 'Status'
96 , 'action' : 'ipServiceDetail'
97 , 'permissions' : (ZEN_VIEW, )
98 },
99 { 'id' : 'events'
100 , 'name' : 'Events'
101 , 'action' : 'viewEvents'
102 , 'permissions' : (ZEN_VIEW, )
103 },
104 { 'id' : 'manage'
105 , 'name' : 'Administration'
106 , 'action' : 'ipServiceManage'
107 , 'permissions' : ("Manage DMD",)
108 },
109 )
110 },
111 )
112
113 security = ClassSecurityInfo()
114
115
117 """
118 Return monitored state of ipservice.
119 If service only listens on 127.0.0.1 return false.
120 """
121 if self.cantMonitor(): return False
122 return super(IpService, self).monitored()
123
124
126 """
127 Return true if IpService only listens on 127.0.0.1, or if it is a UDP
128 service.
129 """
130 return self.protocol == 'udp' \
131 or ( len(self.ipaddresses) == 1
132 and ("127.0.0.1" in self.ipaddresses or "::1" in self.ipaddresses))
133
134
135
137 """
138 Return some text that describes this component. Default is name.
139 """
140 return "%s-%d ips:%s" % (self.protocol, self.port,
141 ", ".join(self.ipaddresses))
142
143
157
158
161
162
165
166
168 """
169 Return a dict like one set by IpServiceMap for services.
170 """
171 svc = self.serviceclass()
172 if svc:
173 return {'protocol': self.protocol, 'port': self.port }
174 return {}
175
176
179
199
201 """
202 Pick an IP out of available choices.
203
204 @return: IP address to contact the service on
205 @rtype: string
206 """
207 manage_ip = Service.getManageIp(self)
208 bare_ip = manage_ip.split('/',1)[0]
209 if bare_ip in self.ipaddresses:
210 return bare_ip
211
212 for ip in self.ipaddresses:
213 if ip != '0.0.0.0' and ip != '127.0.0.1' and ip != '::1':
214 return ip
215 return bare_ip
216
218 """
219 Manually set the management IP address to check the
220 service status.
221
222 @parameter manageIp: IP address to check the service health
223 @type manageIp: string
224 """
225 if not manageIp:
226 return
227
228 bare_ip = manageIp.split('/',1)[0]
229 if not isip(bare_ip):
230 return
231
232 ips = self.getIpAddresses()
233 if '0.0.0.0' in self.ipaddresses and bare_ip in ips:
234 self.manageIp = bare_ip
235
236 if bare_ip in self.ipaddresses:
237 self.manageIp = bare_ip
238
240 """
241 Remove a prevously set management IP address to check the
242 service status.
243 """
244 self.manageIp = ''
245
247 """
248 List the IP addresses to which we can contact the service.
249
250 @return: list of IP addresses
251 @rtype: array of strings
252 """
253 ips = [ ip for ip in self.ipaddresses \
254 if ip != '0.0.0.0' and ip != '127.0.0.1' and ip != '::1']
255 if not ips:
256 ips = Service.getNonLoopbackIpAddresses(self)
257 ips = [ x.split('/',1)[0] for x in ips ]
258 return ips
259
260
263
266
268 sc = self.serviceclass()
269 if sc: return sc.name
270
272 sc = self.serviceclass()
273 if sc: return sc.description
274
278
279
280 security.declareProtected('Manage DMD', 'manage_editService')
281 - def manage_editService(self, id=None,
282 status=None, ipaddresses=None,
283 manageIp=None,
284 protocol=None, port=None,
285 description=None,
286 monitor=False, severity=5, sendString="",
287 expectRegex="", REQUEST=None):
288 """
289 Edit a Service from a web page.
290 """
291 if id:
292 self.rename(id)
293 if status: self.status = status
294 self.ipaddresses = ipaddresses
295 self.description = description
296 self.protocol = protocol
297 self._updateProperty('port', port)
298
299
300 if protocol != self.protocol or port != self.port:
301 self.setServiceClass({'protocol':protocol, 'port':int(port)})
302
303 self.setManageIp(manageIp)
304
305 msg = []
306 msg.append(self.setAqProperty("sendString", sendString, "string"))
307 msg.append(self.setAqProperty("expectRegex", expectRegex, "string"))
308 self.index_object()
309
310 return super(IpService, self).manage_editService(monitor, severity,
311 msg=msg,REQUEST=REQUEST)
312
313
314 InitializeClass(IpService)
315