This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnginx-stat.py
executable file
·91 lines (74 loc) · 1.82 KB
/
nginx-stat.py
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
#!/usr/bin/python
#
# Options:
#
# -a active
# -a accepted
# -a handled
# -a requests
# -a reading
# -a writing
# -a waiting
#
import sys
import getopt
import urllib2
import re
import ssl
def usage():
print "usage: nginx-stat.py -h 127.0.0.1 -p 80 -a [active|accepted|handled|request|reading|writing|waiting]"
sys.exit(2)
def main():
# Default values
host = "localhost"
port = "80"
getInfo = "None"
proto = "http"
_headers = {}
gcontext = ""
if len(sys.argv) < 2:
usage()
try:
opts, _ = getopt.getopt(sys.argv[1:], "h:p:a:")
except getopt.GetoptError:
usage()
# Assign parameters as variables
for opt, arg in opts:
if opt == "-h":
host = arg
if opt == "-p":
port = arg
if opt == "-a":
getInfo = arg
if port == "443":
proto = "https"
_headers = {'X-Mashape-Key': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'}
gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
url = proto + "://" + host + ":" + port + "/nginx_status/"
request = urllib2.Request(url, headers=_headers)
result = urllib2.urlopen(request, context=gcontext)
buffer = re.findall(r'\d{1,8}', result.read())
## Format:
## Active connections: 196
## server accepts handled requests
## 272900 272900 328835
## Reading: 0 Writing: 6 Waiting: 190
if getInfo == "active":
print buffer[0]
elif getInfo == "accepted":
print buffer[1]
elif getInfo == "handled":
print buffer[2]
elif getInfo == "requests":
print buffer[3]
elif getInfo == "reading":
print buffer[4]
elif getInfo == "writing":
print buffer[5]
elif getInfo == "waiting":
print buffer[6]
else:
print "unknown"
sys.exit(1)
if __name__ == "__main__":
main()