1
2
3
4
5
6
7
8
9
10
11 __doc__ = """ZenTcpClient
12 Connect to the remote service and (optionally) test the output from
13 the service against what we expect.
14
15 Error types:
16
17 1. timeout (no connection)
18 2. connection refused - port not available on remote end
19 3. bad value - value returned did not match expectRegex
20
21 """
22 import re
23 import logging
24 log = logging.getLogger("zen.ZenTcpClient")
25 from socket import getfqdn
26 hostname = getfqdn()
27
28 from twisted.internet import reactor, protocol, defer
29 from Products.ZenEvents.ZenEventClasses import Status_IpService
30 from Products.ZenUtils.Utils import unused
31
33 """
34 Twisted class to make a TCP/IP connection to a remote IP service
35 and report back the result.
36 """
37 defer = None
38 data = ""
39
60
80
82 """
83 Called if we timeout waiting for the service to connect or for
84 receiving a response from the service that matches our regex.
85 """
86 msg = "IP Service %s TIMEOUT waiting for '%s'" % (
87 self.cfg.component, self.cfg.expectRegex)
88 log.debug("%s %s", self.cfg.ip, msg)
89 self.factory.msg = msg
90 self.loseConnection()
91
93 """
94 Shut down the connection and cleanup.
95 """
96 ip, port = self.transport.addr
97 log.debug("Closed connection to %s on port %s for %s",
98 ip, port, self.cfg.component)
99 self.data = ""
100 try:
101 self.defer.cancel()
102 except:
103 self.defer = None
104 self.transport.loseConnection()
105
106
108 """
109 Client class to run TCP tests.
110 """
111 protocol = ZenTcpTest
112 msg = "pass"
113 deferred = None
114
118
120 """
121 Record why the connection to the remote device was dropped.
122
123 @parameter connector: Twisted protocol object
124 @type connector: Twisted protocol object
125 @parameter reason: explanation for the connection loss
126 @type reason: Twisted error object
127 """
128 unused(connector)
129 errorMsg = reason.getErrorMessage()
130 if errorMsg != 'Connection was closed cleanly.':
131 log.debug("Lost connection to %s (%s) port %s: %s",
132 self.cfg.device, self.cfg.ip, self.cfg.port,
133 reason.getErrorMessage() )
134 if self.deferred:
135 self.deferred.callback(self)
136 self.deferred = None
137
139 """
140 Record why the connection to the remote device failed.
141
142 @parameter connector: Twisted protocol object
143 @type connector: Twisted protocol object
144 @parameter reason: explanation for the connection loss
145 @type reason: Twisted error object
146 """
147 log.debug("Connection to %s (%s) port %s failed: %s",
148 self.cfg.device, connector.host, self.cfg.port,
149 reason.getErrorMessage() )
150 self.msg = "IP Service %s is down" % self.cfg.component
151 if self.deferred:
152 self.deferred.callback(self)
153 self.deferred = None
154
188
189 - def start(self, ip_address):
190 """
191 Called by zenstatus to make a connection attempt to the service.
192
193 @return: Twisted deferred
194 @rtype: Twisted deferred
195 """
196 d = self.deferred = defer.Deferred()
197 reactor.connectTCP(ip_address, self.cfg.port, self, self.cfg.timeout)
198 return d
199