Package ZenStatus :: Module ZenTcpClient
[hide private]
[frames] | no frames]

Source Code for Module ZenStatus.ZenTcpClient

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13  """ 
 14  Error types: 
 15   
 16      1. timeout (no connection) 
 17      2. connection refused - port not available on remote end 
 18      3. bad value - value returned did not match expectRegex 
 19   
 20  """ 
 21  import re 
 22  import logging 
 23  log = logging.getLogger("zen.ZenTcpClient") 
 24   
 25  from twisted.internet import reactor, protocol, defer 
 26  from Products.ZenEvents.ZenEventClasses import Status_IpService 
 27  from Products.ZenUtils.Utils import unused 
 28   
 29  from socket import getfqdn 
 30  hostname = getfqdn() 
 31   
 32  # needed for pb/jelly 
 33  from Products.ZenHub.services.StatusConfig import ServiceConfig 
 34  unused(ServiceConfig) 
 35   
36 -class ZenTcpTest(protocol.Protocol):
37 """ 38 Twisted class to make a TCP/IP connection to a remote IP service 39 and report back the result. 40 """ 41 defer = None 42 data = "" 43
44 - def connectionMade(self):
45 log.debug("Connected to %s" % self.transport.getPeer().host) 46 self.factory.msg = "pass" 47 self.cfg = self.factory.cfg 48 49 if self.cfg.sendString: 50 for line in self.cfg.sendString.split('\n'): 51 log.debug("Sending: %s" % line) 52 self.transport.write(line + '\n') 53 54 if self.cfg.expectRegex: 55 log.debug("Waiting for results to check against regex '%s'" % ( 56 self.cfg.expectRegex )) 57 self.defer = reactor.callLater(self.cfg.timeout, self.expectTimeout) 58 else: 59 self.loseConnection()
60 61
62 - def dataReceived(self, data):
63 log.debug("%s %s received data: %s" % (self.cfg.device, 64 self.cfg.component, data)) 65 self.data += data 66 if self.cfg.expectRegex: 67 if re.search(self.cfg.expectRegex, data): 68 log.debug("Found %s in '%s' -- closing connection" % ( 69 self.cfg.expectRegex, data)) 70 self.loseConnection() 71 else: 72 log.debug("No match for %s in '%s' -- looking for more data" % ( 73 self.cfg.expectRegex, data))
74 75
76 - def expectTimeout(self):
77 msg = "IP Service %s TIMEOUT waiting for '%s'" % ( 78 self.cfg.component, self.cfg.expectRegex) 79 log.debug( "%s %s" % (self.cfg.ip, msg) ) 80 self.factory.msg = msg 81 self.loseConnection()
82 83
84 - def loseConnection(self):
85 ip, port = self.transport.addr 86 log.debug("Closed connection to %s on port %s for %s" % ( 87 ip, port, self.cfg.component)) 88 self.data = "" 89 try: 90 self.defer.cancel() 91 except: 92 self.defer = None 93 self.transport.loseConnection()
94 95 96
97 -class ZenTcpClient(protocol.ClientFactory):
98 protocol = ZenTcpTest 99 msg = "pass" 100 deferred = None 101
102 - def __init__(self, svc, status):
103 self.cfg = svc 104 self.status = status
105
106 - def clientConnectionLost(self, connector, reason):
107 unused(connector) 108 log.debug("Lost connection to %s (%s) port %s : %s" % ( 109 self.cfg.device, self.cfg.ip, self.cfg.port, 110 reason.getErrorMessage() )) 111 if self.deferred: 112 self.deferred.callback(self) 113 self.deferred = None
114 115
116 - def clientConnectionFailed(self, connector, reason):
117 unused(connector) 118 log.debug("Connection to %s (%s) port %s failed: %s" % ( 119 self.cfg.device, self.cfg.ip, self.cfg.port, 120 reason.getErrorMessage() )) 121 log.debug(reason.type) 122 self.msg = "IP Service %s is down" % self.cfg.component 123 if self.deferred: 124 self.deferred.callback(self) 125 self.deferred = None
126 127
128 - def getEvent(self):
129 log.debug("status:%s msg:%s", self.status, self.msg) 130 if self.msg == "pass" and self.status > 0: 131 self.status = sev = 0 132 self.msg = "IP Service %s back up" % self.cfg.component 133 log.info(self.msg) 134 135 elif self.msg != "pass": 136 self.status += 1 137 sev = self.cfg.failSeverity 138 log.warn(self.msg) 139 140 else: 141 # Don't send an event as there's no problem and 142 # nothing to clear. 143 return None 144 145 return dict(device=self.cfg.device, 146 component=self.cfg.component, 147 ipAddress=self.cfg.ip, 148 summary=self.msg, 149 severity=sev, 150 eventClass=Status_IpService, 151 eventGroup="TCPTest", 152 agent="ZenStatus", 153 manager=hostname)
154
155 - def start(self):
156 d = self.deferred = defer.Deferred() 157 reactor.connectTCP(self.cfg.ip, self.cfg.port, self, self.cfg.timeout) 158 return d
159