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 { 'id' : 'viewHistory'
113 , 'name' : 'Modifications'
114 , 'action' : 'viewHistory'
115 , 'permissions' : (ZEN_VIEW_MODIFICATIONS,)
116 },
117 )
118 },
119 )
120
121 security = ClassSecurityInfo()
122
123
125 """
126 Return monitored state of ipservice.
127 If service only listens on 127.0.0.1 return false.
128 """
129 if self.cantMonitor(): return False
130 return super(IpService, self).monitored()
131
132
134 """
135 Return true if IpService only listens on 127.0.0.1, or if it is a UDP
136 service.
137 """
138 return self.protocol == 'udp' \
139 or ( len(self.ipaddresses) == 1
140 and "127.0.0.1" in self.ipaddresses )
141
142
143
145 """
146 Return some text that describes this component. Default is name.
147 """
148 return "%s-%d ips:%s" % (self.protocol, self.port,
149 ", ".join(self.ipaddresses))
150
151
165
166
169
170
173
174
176 """
177 Return a dict like one set by IpServiceMap for services.
178 """
179 svc = self.serviceclass()
180 if svc:
181 return {'protocol': self.protocol, 'port': self.port }
182 return {}
183
184
187
207
209 """
210 Pick an IP out of available choices.
211
212 @return: IP address to contact the service on
213 @rtype: string
214 """
215 manage_ip = Service.getManageIp(self)
216 bare_ip = manage_ip.split('/',1)[0]
217 if bare_ip in self.ipaddresses:
218 return bare_ip
219
220 for ip in self.ipaddresses:
221 if ip != '0.0.0.0' and ip != '127.0.0.1':
222 return ip
223 return bare_ip
224
226 """
227 Manually set the management IP address to check the
228 service status.
229
230 @parameter manageIp: IP address to check the service health
231 @type manageIp: string
232 """
233 if not manageIp:
234 return
235
236 bare_ip = manageIp.split('/',1)[0]
237 if not isip(bare_ip):
238 return
239
240 ips = self.getIpAddresses()
241 if '0.0.0.0' in self.ipaddresses and bare_ip in ips:
242 self.manageIp = bare_ip
243
244 if bare_ip in self.ipaddresses:
245 self.manageIp = bare_ip
246
248 """
249 Remove a prevously set management IP address to check the
250 service status.
251 """
252 self.manageIp = ''
253
255 """
256 List the IP addresses to which we can contact the service.
257
258 @return: list of IP addresses
259 @rtype: array of strings
260 """
261 ips = [ ip for ip in self.ipaddresses \
262 if ip != '0.0.0.0' and ip != '127.0.0.1' ]
263 if not ips:
264 ips = Service.getNonLoopbackIpAddresses(self)
265 ips = [ x.split('/',1)[0] for x in ips ]
266 return ips
267
268
271
274
276 sc = self.serviceclass()
277 if sc: return sc.name
278
280 sc = self.serviceclass()
281 if sc: return sc.description
282
286
287
288 security.declareProtected('Manage DMD', 'manage_editService')
289 - def manage_editService(self, id=None,
290 status=None, ipaddresses=None,
291 manageIp=None,
292 protocol=None, port=None,
293 description=None,
294 monitor=False, severity=5, sendString="",
295 expectRegex="", REQUEST=None):
296 """
297 Edit a Service from a web page.
298 """
299 if id:
300 self.rename(id)
301 if status: self.status = status
302 self.ipaddresses = ipaddresses
303 self.description = description
304 self.protocol = protocol
305 self._updateProperty('port', port)
306
307
308 if protocol != self.protocol or port != self.port:
309 self.setServiceClass({'protocol':protocol, 'port':int(port)})
310
311 self.setManageIp(manageIp)
312
313 msg = []
314 msg.append(self.setAqProperty("sendString", sendString, "string"))
315 msg.append(self.setAqProperty("expectRegex", expectRegex, "string"))
316 self.index_object()
317
318 return super(IpService, self).manage_editService(monitor, severity,
319 msg=msg,REQUEST=REQUEST)
320
321
322 InitializeClass(IpService)
323