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   
 28  from socket import getfqdn 
 29  hostname = getfqdn() 
 30   
 31  from Products.ZenHub.services.StatusConfig import ServiceConfig 
 32   
33 -class ZenTcpTest(protocol.Protocol):
34 35 defer = None 36 data = "" 37
38 - def connectionMade(self):
39 log.debug("connect to: %s" % self.transport.getPeer().host) 40 self.factory.msg = "pass" 41 self.cfg = self.factory.cfg 42 if self.cfg.sendString: 43 log.debug("sending: %s" % self.cfg.sendString) 44 self.transport.write(self.cfg.sendString) 45 if self.cfg.expectRegex: 46 self.defer = reactor.callLater(self.cfg.timeout, self.expectTimeout) 47 else: 48 self.loseConnection()
49 50
51 - def dataReceived(self, data):
52 log.debug("data: %s", data) 53 self.data += data 54 if self.cfg.expectRegex and re.search(self.cfg.expectRegex, data): 55 self.loseConnection()
56 57
58 - def expectTimeout(self):
59 msg = "IP Service %s TIMEOUT waiting for '%s'" % ( 60 self.cfg.component, self.cfg.expectRegex) 61 self.factory.msg = msg 62 self.loseConnection()
63 64
65 - def loseConnection(self):
66 log.debug("close: %s port: %s" % self.transport.addr) 67 self.data = "" 68 try: 69 self.defer.cancel() 70 except: 71 self.defer = None 72 self.transport.loseConnection()
73 74 75
76 -class ZenTcpClient(protocol.ClientFactory):
77 protocol = ZenTcpTest 78 msg = "pass" 79 deferred = None 80
81 - def __init__(self, svc, status):
82 self.cfg = svc 83 self.status = status
84
85 - def clientConnectionLost(self, connector, reason):
86 log.debug("lost: %s", reason.getErrorMessage()) 87 if self.deferred: 88 self.deferred.callback(self) 89 self.deferred = None
90 91
92 - def clientConnectionFailed(self, connector, reason):
93 log.debug("failed: %s", reason.getErrorMessage()) 94 log.debug(reason.type) 95 self.msg = "IP Service %s is down" % self.cfg.component 96 if self.deferred: 97 self.deferred.callback(self) 98 self.deferred = None
99 100
101 - def getEvent(self):
102 log.debug("status:%s msg:%s", self.status, self.msg) 103 if self.msg == "pass" and self.status > 0: 104 self.status = sev = 0 105 self.msg = "IP Service %s back up" % self.cfg.component 106 log.info(self.msg) 107 elif self.msg != "pass": 108 self.status += 1 109 sev = self.cfg.failSeverity 110 log.warn(self.msg) 111 else: 112 return None 113 return dict(device=self.cfg.device, 114 component=self.cfg.component, 115 ipAddress=self.cfg.ip, 116 summary=self.msg, 117 severity=sev, 118 eventClass=Status_IpService, 119 eventGroup="TCPTest", 120 agent="ZenStatus", 121 manager=hostname)
122
123 - def start(self):
124 d = self.deferred = defer.Deferred() 125 reactor.connectTCP(self.cfg.ip, self.cfg.port, self, self.cfg.timeout) 126 return d
127