示例#1
0
文件: stats.py 项目: pwgen/alignak
class Stats(object):
    """Stats class to export data into a statsd format

    """
    def __init__(self):
        self.name = ''
        self.type = ''
        self.app = None
        self.stats = {}
        # There are two modes that are not exclusive
        # first the kernel mode
        self.api_key = ''
        self.secret = ''
        self.http_proxy = ''
        self.con = HTTPClient(uri='http://kernel.alignak.io')
        # then the statsd one
        self.statsd_sock = None
        self.statsd_addr = None

    def launch_reaper_thread(self):
        """Launch thread that collects data

        :return: None
        """
        self.reaper_thread = threading.Thread(None,
                                              target=self.reaper,
                                              name='stats-reaper')
        self.reaper_thread.daemon = True
        self.reaper_thread.start()

    def register(self,
                 app,
                 name,
                 _type,
                 api_key='',
                 secret='',
                 http_proxy='',
                 statsd_host='localhost',
                 statsd_port=8125,
                 statsd_prefix='alignak',
                 statsd_enabled=False):
        """Init statsd instance with real values

        :param app: application (arbiter, scheduler..)
        :type app: alignak.daemon.Daemon
        :param name: daemon name
        :type name: str
        :param _type: daemon type
        :type _type:
        :param api_key: api_key to post data
        :type api_key: str
        :param secret: secret to post data
        :type secret: str
        :param http_proxy: proxy http if necessary
        :type http_proxy: str
        :param statsd_host: host to post data
        :type statsd_host: str
        :param statsd_port: port to post data
        :type statsd_port: int
        :param statsd_prefix: prefix to add to metric
        :type statsd_prefix: str
        :param statsd_enabled: bool to enable statsd
        :type statsd_enabled: bool
        :return: None
        """
        self.app = app
        self.name = name
        self.type = _type
        # kernel.io part
        self.api_key = api_key
        self.secret = secret
        self.http_proxy = http_proxy
        # local statsd part
        self.statsd_host = statsd_host
        self.statsd_port = statsd_port
        self.statsd_prefix = statsd_prefix
        self.statsd_enabled = statsd_enabled

        if self.statsd_enabled:
            logger.debug('Loading statsd communication with %s:%s.%s',
                         self.statsd_host, self.statsd_port,
                         self.statsd_prefix)
            self.load_statsd()

        # Also load the proxy if need
        self.con.set_proxy(self.http_proxy)

    def load_statsd(self):
        """Create socket connection to statsd host

        :return: None
        """
        try:
            self.statsd_addr = (socket.gethostbyname(self.statsd_host),
                                self.statsd_port)
            self.statsd_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        except (socket.error, socket.gaierror), exp:
            logger.error('Cannot create statsd socket: %s', exp)
            return
示例#2
0
文件: stats.py 项目: hvad/alignak
class Stats(object):
    """Stats class to export data into a statsd format

    """

    def __init__(self):
        self.name = ""
        self.type = ""
        self.app = None
        self.stats = {}
        # There are two modes that are not exclusive
        # first the kernel mode
        self.api_key = ""
        self.secret = ""
        self.http_proxy = ""
        self.con = HTTPClient(uri="http://kernel.alignak.io")
        # then the statsd one
        self.statsd_sock = None
        self.statsd_addr = None

    def launch_reaper_thread(self):
        """Launch thread that collects data

        :return: None
        """
        self.reaper_thread = threading.Thread(None, target=self.reaper, name="stats-reaper")
        self.reaper_thread.daemon = True
        self.reaper_thread.start()

    def register(
        self,
        app,
        name,
        _type,
        api_key="",
        secret="",
        http_proxy="",
        statsd_host="localhost",
        statsd_port=8125,
        statsd_prefix="alignak",
        statsd_enabled=False,
    ):
        """Init statsd instance with real values

        :param app: application (arbiter, scheduler..)
        :type app: alignak.daemon.Daemon
        :param name: daemon name
        :type name: str
        :param _type: daemon type
        :type _type:
        :param api_key: api_key to post data
        :type api_key: str
        :param secret: secret to post data
        :type secret: str
        :param http_proxy: proxy http if necessary
        :type http_proxy: str
        :param statsd_host: host to post data
        :type statsd_host: str
        :param statsd_port: port to post data
        :type statsd_port: int
        :param statsd_prefix: prefix to add to metric
        :type statsd_prefix: str
        :param statsd_enabled: bool to enable statsd
        :type statsd_enabled: bool
        :return: None
        """
        self.app = app
        self.name = name
        self.type = _type
        # kernel.io part
        self.api_key = api_key
        self.secret = secret
        self.http_proxy = http_proxy
        # local statsd part
        self.statsd_host = statsd_host
        self.statsd_port = statsd_port
        self.statsd_prefix = statsd_prefix
        self.statsd_enabled = statsd_enabled

        if self.statsd_enabled:
            logger.debug(
                "Loading statsd communication with %s:%s.%s", self.statsd_host, self.statsd_port, self.statsd_prefix
            )
            self.load_statsd()

        # Also load the proxy if need
        self.con.set_proxy(self.http_proxy)

    def load_statsd(self):
        """Create socket connection to statsd host

        :return: None
        """
        try:
            self.statsd_addr = (socket.gethostbyname(self.statsd_host), self.statsd_port)
            self.statsd_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        except (socket.error, socket.gaierror), exp:
            logger.error("Cannot create statsd socket: %s", exp)
            return