]> git.uio.no Git - uio-zabbix.git/blame - zabbix_haproxy_autodiscovery.py
Lagt inn zabbix_gfs2_autodiscovery.pl
[uio-zabbix.git] / zabbix_haproxy_autodiscovery.py
CommitLineData
0e4eed3a
RM
1#!/usr/bin/env python
2#
3
4#
5# Authors:
6# rafael@E-MC2.NET / https://e-mc2.net/
7#
8# Copyright (c) 2018 USIT-University of Oslo
9#
10# zabbix_haproxy_autodiscovery.py is free software: you can
11# redistribute it and/or modify it under the terms of the GNU General
12# Public License as published by the Free Software Foundation, either
13# version 3 of the License, or (at your option) any later version.
14#
15# zabbix_haproxy_autodiscovery.py is distributed in the hope that it
16# will be useful, but WITHOUT ANY WARRANTY; without even the implied
17# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18# See the GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with sms_send. If not, see <http://www.gnu.org/licenses/>.
22#
23
24import os
25import sys
26import json
27import argparse
28
29from io import StringIO
30import errno
31import socket
32
33
34SOCKET_BUFFER_SIZE = 1024
35SOCKET_PATH = "/var/lib/haproxy/stats"
36
37parameters = "pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime".split(",")
38
39
40# ############################################
41# get_haproxy_stats_data()
42# ############################################
43
44def get_haproxy_stats_data(command):
45
46 try:
47
48 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
49 sock.connect(SOCKET_PATH)
50
51 sock.sendall((command + "\n").encode())
52 buff = StringIO()
53
54 while True:
55 chunk = sock.recv(SOCKET_BUFFER_SIZE)
56
57 if chunk:
58 buff.write(chunk.decode("ascii"))
59 else:
60 break
61
62 sock.close()
63
64 response = buff.getvalue()
65 buff.close()
66
67 return response.rstrip("\n")
68
69 except Exception as e:
70 raise Exception("[ERROR]: %s\n" % e)
71
72
73# ############################################
74# generate_zabbix_autodiscovery()
75# ############################################
76
77def generate_zabbix_autodiscovery(stats,type):
78
79 try:
80 output_list = []
81
82 lines = stats.split("\n")
83 fields = lines.pop(0).split(",")
84
85 for line in lines:
86 values = line.split(",")
87
88 if values[1] == 'FRONTEND' and type == 'FRONTEND':
89
90 frontends = {"{#FRONTEND}": values[0]}
91 output_list.append(frontends)
92
93 if values[1] == 'BACKEND' and type == 'BACKEND':
94
95 backends = {"{#BACKEND}": values[0]}
96 output_list.append(backends)
97
98 if values[1] not in ['FRONTEND','BACKEND'] and type == 'SERVER':
99
100 servers = {"{#SERVER}": values[0] + "/" + values[1]}
101 output_list.append(servers)
102
103 result = {"data":output_list}
104 print json.dumps(result,sort_keys=True,indent=2)
105
106 except Exception as e:
107 raise Exception("[ERROR]: %s\n" % e)
108
109
110if __name__ == '__main__':
111
112 try:
113
114 #
115 # Type = FRONTEND | BACKEND | SERVER
116 #
117 type = sys.argv[1].upper()
118
119 stats = get_haproxy_stats_data("show stat")
120 generate_zabbix_autodiscovery(stats,type)
121
122 except Exception, e:
123 print e
124 sys.exit(1)