Blame view

slapos/promise/plugin/check_port_listening.py 1.68 KB
Thomas Gambier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
from zope import interface as zope_interface
from slapos.grid.promise import interface
from slapos.grid.promise.generic import GenericPromise

import socket
import sys

class RunPromise(GenericPromise):

  zope_interface.implements(interface.IPromise)

  def __init__(self, config):
    GenericPromise.__init__(self, config)
    # check port is listening at least every 2 minutes
    self.setPeriodicity(minute=2)

  def sense(self):
    """
      Simply test if we can connect to specified host:port.
    """
    hostname = self.getConfig('hostname')
Alain Takoudjou committed
22 23
    # if type of port should be int or str, unicode is not accepted.
    port = int(self.getConfig('port'))
Thomas Gambier committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    addr = (hostname , port)
    # in case of any error, we call "logger.error"
    # note that we could simply let the function raise an error, it would have the same effect
    # in this case the following code would be enough:
    #    socket.create_connection(addr).close()
    #    self.logger.info("port connection OK")
    try:
      socket.create_connection(addr).close()
    except (socket.herror, socket.gaierror), e:
      self.logger.error("ERROR hostname/port ({}) is not correct: {}".format(addr, e))
    except (socket.error, socket.timeout), e:
      self.logger.error("ERROR while connecting to {}: {}".format(addr, e))
    else:
      self.logger.info("port connection OK ({})".format(addr))

  # no need to define Test as it is the default implementation
  #def test(self):
  #  """
  #    Test is failing if last sense was bad.
  #  """
  #  return self._test(result_count=1, failure_amount=1)

  def anomaly(self):
    """
      There is an anomaly if last 3 senses were bad.
    """
    return self._anomaly(result_count=3, failure_amount=3)