forked from skyscrapers/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_nginx
executable file
·106 lines (97 loc) · 3.25 KB
/
check_nginx
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
# check_nginx is a Nagios to monitor nginx statu
# my blog: http://www.nginxs.com
# The version is 1.0.0
# MSN: [email protected]
import string
import urllib2
import getopt
import sys
def usage():
print """check_nginx is a Nagios to monitor nginx status
Usage:
check_nginx [-h|--help][-U|--url][-P|--path][-u|--user][-p|--passwd][-w|--warning][-c|--critical]
Options:
--help|-h)
print check_nginx help.
--url|-U)
Sets nginx status url.
--path|-P)
Sets nginx status url path. Default is: off
--user|-u)
Sets nginx status BasicAuth user. Default is: off
--passwd|-p)
Sets nginx status BasicAuth passwd. Default is: off
--warning|-w)
Sets a warning level for nginx Active connections. Default is: off
--critical|-c)
Sets a critical level for nginx Active connections. Default is: off
Example:
The url is www.nginxs.com/status
./check_nginx -U www.nginxs.com -P /status -u eric -p nginx -w 1000 -c 2000
if dont't have password:
./check_nginx -U www.nginxs.com -P /status -w 1000 -c 2000
if don't have path and password:
./check_nginx -U www.nginxs.com -w 1000 -c 2000"""
sys.exit(3)
try:
options,args = getopt.getopt(sys.argv[1:],"hU:P:u:p:w:c:",["help","url=","path=","user=","passwd=","warning=","critical="])
except getopt.GetoptError:
usage()
sys.exit(3)
for name,value in options:
if name in ("-h","--help"):
usage()
if name in ("-U","--url"):
url = "http://"+value
if name in ("-P","--path"):
path = value
if name in ("-u","--user"):
user = value
if name in ("-p","--passwd"):
passwd = value
if name in ("-w","--warning"):
warning = int(value)
if name in ("-c","--critical"):
critical = int(value)
try:
if 'path' in dir():
req = urllib2.Request(url+path)
else:
req = urllib2.Request(url)
if 'user' in dir() and 'passwd' in dir():
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url+path, user, passwd)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
response = urllib2.urlopen(req)
the_page = response.readline()
conn = the_page.split()
ActiveConn = int(conn[2])
the_page1 = response.readline()
the_page2 = response.readline()
the_page3 = response.readline()
response.close()
b = the_page3.split()
reading = b[1]
writing = b[3]
waiting = b[5]
output = 'ActiveConn:%s,reading:%s,writing:%s,waiting:%s' % (ActiveConn,reading,writing,waiting)
perfdata = 'ActiveConn:%s,reading:%s,writing:%s,waiting:%s' % (ActiveConn,reading,writing,waiting)
except Exception:
print "NGINX STATUS unknown: Error while getting Connection"
sys.exit(3)
if 'warning' in dir() and 'critical' in dir():
if ActiveConn >= critical:
print 'CRITICAL - %s' % (output)
sys.exit(2)
elif ActiveConn >= warning:
print 'WARNING - %s' % (output)
sys.exit(1)
else:
print 'OK - %s' % (output)
sys.exit(0)
else:
print 'OK - %s' % (output)
sys.exit(0)