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
47
48 addIpService = DTMLFile('dtml/addIpService',globals())
49
50
53
54
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 manageIp = ""
71
72 collectors = ('zenstatus',)
73
74 _properties = (
75 {'id':'port', 'type':'int', 'mode':'', 'setter': 'setPort',
76 'description':"TCP port to check for this service."},
77 {'id':'protocol', 'type':'string', 'mode':'', 'setter': 'setProtocol',
78 'description':"Protocol (TCP or UPD) used by this service."},
79 {'id':'ipaddresses', 'type':'lines', 'mode':'',
80 'description':"IP addresses that this service is listening on."},
81 {'id':'discoveryAgent', 'type':'string', 'mode':'',
82 'description':"What process was used to discover this service."},
83 {'id':'manageIp', 'type':'string', 'mode':'',
84 'description':"The IP address to check for this service."},
85 )
86 _relations = Service._relations + (
87 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","ipservices")),
88 )
89
90 factory_type_information = (
91 {
92 'immediate_view' : 'ipServiceDetail',
93 'actions' :
94 (
95 { 'id' : 'status'
96 , 'name' : 'Status'
97 , 'action' : 'ipServiceDetail'
98 , 'permissions' : (ZEN_VIEW, )
99 },
100 { 'id' : 'events'
101 , 'name' : 'Events'
102 , 'action' : 'viewEvents'
103 , 'permissions' : (ZEN_VIEW, )
104 },
105 { 'id' : 'manage'
106 , 'name' : 'Administration'
107 , 'action' : 'ipServiceManage'
108 , 'permissions' : ("Manage DMD",)
109 },
110 { 'id' : 'viewHistory'
111 , 'name' : 'Modifications'
112 , 'action' : 'viewHistory'
113 , 'permissions' : (ZEN_VIEW_MODIFICATIONS,)
114 },
115 )
116 },
117 )
118
119 security = ClassSecurityInfo()
120
121
123 """
124 Return monitored state of ipservice.
125 If service only listens on 127.0.0.1 return false.
126 """
127 if self.cantMonitor(): return False
128 return super(IpService, self).monitored()
129
130
132 """
133 Return true if IpService only listens on 127.0.0.1, or if it is a UDP
134 service.
135 """
136 return self.protocol == 'udp' \
137 or ( len(self.ipaddresses) == 1
138 and "127.0.0.1" in self.ipaddresses )
139
140
141
143 """
144 Return some text that describes this component. Default is name.
145 """
146 return "%s-%d ips:%s" % (self.protocol, self.port,
147 ", ".join(self.ipaddresses))
148
149
163
164
167
168
171
172
174 """
175 Return a dict like one set by IpServiceMap for services.
176 """
177 svc = self.serviceclass()
178 if svc:
179 return {'protocol': self.protocol, 'port': svc.port }
180 return {}
181
182
185
205
207 """
208 Pick an IP out of available choices.
209
210 @return: IP address to contact the service on
211 @rtype: string
212 """
213 manage_ip = Service.getManageIp(self)
214 bare_ip = manage_ip.split('/',1)[0]
215 if bare_ip in self.ipaddresses:
216 return bare_ip
217
218 for ip in self.ipaddresses:
219 if ip != '0.0.0.0' and ip != '127.0.0.1':
220 return ip
221 return bare_ip
222
224 """
225 Manually set the management IP address to check the
226 service status.
227
228 @parameter manageIp: IP address to check the service health
229 @type manageIp: string
230 """
231 if not manageIp:
232 return
233
234 bare_ip = manageIp.split('/',1)[0]
235 if not isip(bare_ip):
236 return
237
238 ips = self.getIpAddresses()
239 if '0.0.0.0' in self.ipaddresses and bare_ip in ips:
240 self.manageIp = bare_ip
241
242 if bare_ip in self.ipaddresses:
243 self.manageIp = bare_ip
244
246 """
247 Remove a prevously set management IP address to check the
248 service status.
249 """
250 self.manageIp = ''
251
253 """
254 List the IP addresses to which we can contact the service.
255
256 @return: list of IP addresses
257 @rtype: array of strings
258 """
259 ips = [ ip for ip in self.ipaddresses \
260 if ip != '0.0.0.0' and ip != '127.0.0.1' ]
261 if not ips:
262 ips = Service.getNonLoopbackIpAddresses(self)
263 ips = [ x.split('/',1)[0] for x in ips ]
264 return ips
265
266
269
272
274 sc = self.serviceclass()
275 if sc: return sc.name
276
278 sc = self.serviceclass()
279 if sc: return sc.description
280
284
285
286 security.declareProtected('Manage DMD', 'manage_editService')
287 - def manage_editService(self, id=None,
288 status=None, ipaddresses=None,
289 manageIp=None,
290 protocol=None, port=None,
291 description=None,
292 monitor=False, severity=5, sendString="",
293 expectRegex="", REQUEST=None):
294 """
295 Edit a Service from a web page.
296 """
297 if id:
298 self.rename(id)
299 if status: self.status = status
300 self.ipaddresses = ipaddresses
301 self.description = description
302 self.protocol = protocol
303 self._updateProperty('port', port)
304
305
306 if protocol != self.protocol or port != self.port:
307 self.setServiceClass({'protocol':protocol, 'port':int(port)})
308
309 self.setManageIp(manageIp)
310
311 msg = []
312 msg.append(self.setAqProperty("sendString", sendString, "string"))
313 msg.append(self.setAqProperty("expectRegex", expectRegex, "string"))
314 self.index_object()
315
316 return super(IpService, self).manage_editService(monitor, severity,
317 msg=msg,REQUEST=REQUEST)
318
319
320 InitializeClass(IpService)
321