-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_haproxy.py
More file actions
executable file
·68 lines (55 loc) · 2.19 KB
/
Copy pathcheck_haproxy.py
File metadata and controls
executable file
·68 lines (55 loc) · 2.19 KB
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
# -*- coding: utf-8 -*-
import csv
import requests
from requests.auth import HTTPBasicAuth
import argparse
import codecs
def build_parser():
"""
define param command line
:return: parser config
"""
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--url", required=False, dest="url", help="url haproxy stat default 127.0.0.1",
default="http://127.0.0.1")
parser.add_argument("-U", "--user", required=True, dest="user", help="user to login in")
parser.add_argument("-P", '--pass', required=True, dest="password", help="haproxy password")
parser.add_argument("-p", dest="port", default="8000", help="port stats default 8000")
return parser
class CheckHaproxy(object):
def __init__(self, url, user, password, port):
self.haproxy_csv = ":" + port + "/?stats;csv"
self.url = url + self.haproxy_csv
self.user = user
self.password = password
self.status = []
def get_status(self):
try:
res = requests.get(self.url, 'r', auth=HTTPBasicAuth(self.user, self.password))
if res.status_code == 401:
print("CRITICAL: login or pasword is wrong")
exit(2)
else:
text_csv = res.iter_lines()
csv_reader = csv.reader(codecs.iterdecode(text_csv, 'utf-8'), delimiter=',')
for rows in csv_reader:
# print(rows)
if rows[1] != "FRONTEND" and rows[1] != "svname" and rows[0] != 'statistics' \
and rows[17] != "UP" and rows[17] !='no check':
self.status.append("backend: {} serveur: {} is OFF line".format(rows[0], rows[1]))
except Exception as e:
print("CRITICAL: {} ".format(e))
exit(2)
def nagios(self):
if self.status:
print("CRITICAL: {}".format(self.status))
exit(2)
else:
print("OK - ALL backend")
exit(0)
if __name__ == "__main__":
pars = build_parser()
args = pars.parse_args()
check = CheckHaproxy(args.url, args.user, args.password, args.port)
check.get_status()
check.nagios()