forked from lambdaq/munin-uwsgi-stats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuwsgi_multi_
executable file
·130 lines (110 loc) · 3.53 KB
/
uwsgi_multi_
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- python -*-
#
# Define env.addr variable in your settings file, e.g.
#
#/etc/munin/plugin-conf.d/munin-node
#[uwsgi_*]
#env.addr 127.0.0.1:49000
#
# Magic markers
#%# capabilities=autoconf suggest
#%# family=auto
UWSGI_STATS = ''
import os
import os.path
import sys
import json
import socket
from pprint import pformat
UWSGI_STATS = os.environ.get('addr', '') or '127.0.0.1:49000'
NAME, MULTI, MODE = os.path.basename(__file__).split('_', 2)
modes = {
'listen_queue': ('Listen queue', 'listen queue slots used'),
'listen_queue_errors': ('Listen queue errors', 'listen queue errors'),
'requests': ('Requests', 'requests managed per ${graph_period}'),
'exceptions': ('Exceptions', 'exceptions per ${graph_period}'),
'vsz': ('Virtual memory', 'bytes'),
'tx': ('Transmitted data', 'bytes per ${graph_period}'),
'avg_rt': ('Average response time', 'time (ms)'),
'rss': ('Resident Set Size', 'bytes'),
}
def get_uwsgi_stats():
'''
Get the json stats from the uwsgi stats socket and return a dict
'''
if ':' in UWSGI_STATS:
host, port = UWSGI_STATS.split(':')
sfamily, addr = (socket.AF_INET, (host, int(port)))
else:
sfamily, addr = (socket.AF_UNIX, UWSGI_STATS)
raw_js = ''
try:
sock = socket.socket(sfamily, socket.SOCK_STREAM)
sock.settimeout(3)
sock.connect(addr)
while True:
data = sock.recv(4096)
if len(data) < 1:
break
raw_js += data
data = json.loads(raw_js or '{}')
return data
except:
print("no (unable to get uWSGI stats at %s)" % UWSGI_STATS)
def print_stats():
'''
Calculate values and print them out for munin to graph
'''
data = get_uwsgi_stats()
if MODE in ['listen_queue', 'listen_queue_errors']:
value = data.get(MODE, 0)
print '{0}.value {1}'.format(MODE, value)
elif MODE in ['avg_rt']:
for worker in data.get('workers', []):
value = worker.get(MODE, 0) / 1000
print '{0}_{1}.value {2}'.format(MODE, worker['id'], value)
else:
for worker in data.get('workers', []):
value = worker.get(MODE, 0)
print '{0}_{1}.value {2}'.format(MODE, worker['id'], value)
def print_config():
'''
Print the config for munin
'''
data = get_uwsgi_stats()
workers = data.get('workers', [])
graph = ('graph_title "{desc}"\n'
'graph_vlabel "{vlabel}"\n'
'graph_category uWSGI\n').format(desc=modes[MODE][0],
vlabel=modes[MODE][1])
out = ''
if MODE in ['tx', 'vsz', 'rss']:
graph += 'graph_args --base 1024\n'
if MODE in ['listen_queue', 'listen_queue_errors']:
out += '{name}.label {name}'
else:
for worker in workers:
if MODE in ['requests', 'exceptions', 'tx']:
out += '{name}_{worker}.type DERIVE\n'
out += '{name}_{worker}.min 0\n'
out += "{name}_{worker}.label {name} {worker}\n"
out = out.format(name=MODE, worker=worker['id'])
print graph + out
def main():
'''
Process args and do stuff.
'''
if len(sys.argv) > 1:
opt = sys.argv[1]
if opt == 'suggest':
print '\n'.join(modes.keys())
elif opt == 'autoconf':
if get_data():
print 'yes'
elif opt == 'config' and MODE in modes:
print_config()
else:
print_stats()
if __name__ == '__main__':
main()