1
2
3
4
5
6
7
8
9
10
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
49
50 addIpService = DTMLFile('dtml/addIpService',globals())
51
52
55
56
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 )
113 },
114 )
115
116 security = ClassSecurityInfo()
117
118
120 """
121 Return monitored state of ipservice.
122 If service only listens on 127.0.0.1 return false.
123 """
124 if self.cantMonitor(): return False
125 return super(IpService, self).monitored()
126
127
129 """
130 Return true if IpService only listens on 127.0.0.1, or if it is a UDP
131 service.
132 """
133 return self.protocol == 'udp' \
134 or ( len(self.ipaddresses) == 1
135 and ("127.0.0.1" in self.ipaddresses or "::1" in self.ipaddresses))
136
137
138
140 """
141 Return some text that describes this component. Default is name.
142 """
143 return "%s-%d ips:%s" % (self.protocol, self.port,
144 ", ".join(self.ipaddresses))
145
146
160
161
164
165
168
169
171 """
172 Return a dict like one set by IpServiceMap for services.
173 """
174 svc = self.serviceclass()
175 if svc:
176 return {'protocol': self.protocol, 'port': self.port }
177 return {}
178
179
182
202
204 """
205 Pick an IP out of available choices.
206
207 @return: IP address to contact the service on
208 @rtype: string
209 """
210 manage_ip = Service.getManageIp(self)
211 bare_ip = manage_ip.split('/',1)[0]
212 if bare_ip in self.ipaddresses:
213 return bare_ip
214
215 for ip in self.ipaddresses:
216 if ip != '0.0.0.0' and ip != '127.0.0.1' and ip != '::1':
217 return ip
218 return bare_ip
219
221 """
222 Manually set the management IP address to check the
223 service status.
224
225 @parameter manageIp: IP address to check the service health
226 @type manageIp: string
227 """
228 if not manageIp:
229 return
230
231 bare_ip = manageIp.split('/',1)[0]
232 if not isip(bare_ip):
233 return
234
235 ips = self.getIpAddresses()
236 if '0.0.0.0' in self.ipaddresses and bare_ip in ips:
237 self.manageIp = bare_ip
238
239 if bare_ip in self.ipaddresses:
240 self.manageIp = bare_ip
241
243 """
244 Remove a prevously set management IP address to check the
245 service status.
246 """
247 self.manageIp = ''
248
250 """
251 List the IP addresses to which we can contact the service.
252
253 @return: list of IP addresses
254 @rtype: array of strings
255 """
256 ips = [ ip for ip in self.ipaddresses \
257 if ip != '0.0.0.0' and ip != '127.0.0.1' and ip != '::1']
258 if not ips:
259 ips = Service.getNonLoopbackIpAddresses(self)
260 ips = [ x.split('/',1)[0] for x in ips ]
261 return ips
262
263
266
269
271 sc = self.serviceclass()
272 if sc: return sc.name
273
275 sc = self.serviceclass()
276 if sc: return sc.description
277
281
282
283 security.declareProtected('Manage DMD', 'manage_editService')
284 - def manage_editService(self, id=None,
285 status=None, ipaddresses=None,
286 manageIp=None,
287 protocol=None, port=None,
288 description=None,
289 monitor=False, severity=5, sendString="",
290 expectRegex="", REQUEST=None):
291 """
292 Edit a Service from a web page.
293 """
294 if id:
295 self.rename(id)
296 if status: self.status = status
297 self.ipaddresses = ipaddresses
298 self.description = description
299 self.protocol = protocol
300 self._updateProperty('port', port)
301
302
303 if protocol != self.protocol or port != self.port:
304 self.setServiceClass({'protocol':protocol, 'port':int(port)})
305
306 self.setManageIp(manageIp)
307
308 msg = []
309 msg.append(self.setAqProperty("sendString", sendString, "string"))
310 msg.append(self.setAqProperty("expectRegex", expectRegex, "string"))
311 self.index_object()
312
313 return super(IpService, self).manage_editService(monitor, severity,
314 msg=msg,REQUEST=REQUEST)
315
316
317 InitializeClass(IpService)
318