summaryrefslogtreecommitdiffstats
path: root/roles/nagios_client/files/scripts/check_haproxy_conns.py
blob: e9e8c9f96815e5d968e67f8f47535beed1a66690 (plain)
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
#!/usr/bin/env python
""" Nagios check for haproxy over-subscription.

fedmsg-gateway is the primary concern as it can eat up a ton of simultaneous
connections.

:Author:  Ralph Bean <rbean@redhat.com>
"""

import socket
import sys


def _numeric(value):
    """ Type casting utility """
    try:
        return int(value)
    except ValueError:
        try:
            return float(value)
        except ValueError:
            return value


def query(sockname="/var/run/haproxy-stat"):
    """ Read stats from the haproxy socket and return a dict """
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.connect("/var/run/haproxy-stat")
    s.send('show info\n')
    try:
        response = s.recv(1024).strip()
        lines = response.split('\n')
        data = dict([map(str.strip, line.split(':')) for line in lines])
        data = dict([(k, _numeric(v)) for k, v in data.items()])
        return data
    except Exception, e:
        print str(e)
    finally:
        s.close()

    return None


def nagios_check(data):
    """ Print warnings and return nagios exit codes. """

    current = data['CurrConns']
    maxconn = data['Maxconn']
    percent = 100 * float(current) / float(maxconn)
    details = "%.2f%% subscribed.  %i current of %i maxconn." % (
        percent, current, maxconn,
    )

    if percent < 50:
        print "HAPROXY SUBS OK: " + details
        return 0

    if percent < 75:
        print "HAPROXY SUBS WARN: " + details
        return 1

    if percent <= 100:
        print "HAPROXY SUBS CRIT: " + details
        return 2

    print "HAPROXY SUBS UNKNOWN: " + details
    return 3


if __name__ == '__main__':
    try:
        data = query(sockname="/var/run/haproxy-stat")
    except Exception as e:
        print "HAPROXY SUBS UNKNOWN: " + str(e)
        sys.exit(3)
    sys.exit(nagios_check(data))