File: server.py

package info (click to toggle)
python-haproxyadmin 0.2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: python: 1,500; makefile: 165; sh: 1
file content (88 lines) | stat: -rw-r--r-- 2,611 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
#
# pylint: disable=superfluous-parens
#
"""
haproxyadmin.internal.server
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This module provides a class, which is used within haproxyadmin for creating a
object to work with a server. This object is associated only with a single
HAProxy process.

"""

class _Server:
    """Class for interacting with a server of a backend in one HAProxy.

    :param backend: a _Backend object in which server is part of.
    :param name: server name.
    :type name: ``string``
    :param sid: server id (unique inside a proxy).
    :type sid: ``string``
    """
    def __init__(self, backend, name, sid):
        self.backend = backend
        self._name = name
        self.process_nb = self.backend.process_nb
        self._sid = sid

    @property
    def name(self):
        """Return the name of the backend server."""
        return self._name

    @property
    def sid(self):
        """Return server id"""
        data = self.stats_data()
        self._sid = data.sid

        return self._sid

    def stats_data(self):
        """Return stats data

        Check documentation of ``stats_data`` method in :class:`_Frontend`.

        :rtype: ``utils.CSVLine`` object
        """
        # Fetch data using the last known sid
        try:
            data = self.backend.hap_process.servers_stats(
                self.backend.name, self.backend.iid, self._sid)[self.name]
        except KeyError:
            # A lookup on HAProxy with the current id doesn't return
            # an object with our name.
            # Most likely object got different id due to a reshuffle in conf.
            # Thus retrieve all objects to get latest data for the object.
            try:
                data = self.backend.hap_process.servers_stats(
                    self.backend.name)[self.name]
            except KeyError:
                # The object has gone from running configuration!
                # This occurs when object was removed from configuration
                # and haproxy was reloaded.We cant recover from this situation.
                raise

        return data

    def metric(self, name):
        data = self.stats_data()

        return getattr(data, name)

    def stats(self):
        """Build dictionary for all statistics reported by HAProxy.

        :return: A dictionary with statistics
        :rtype: ``dict``
        """
        data = self.stats_data()
        keys = data.heads
        values = data.parts

        return dict(zip(keys, values))

    def command(self, cmd):
        return self.backend.hap_process.command(cmd)