forked from samuel/python-munin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhookbox
More file actions
executable file
·62 lines (48 loc) · 1.52 KB
/
hookbox
File metadata and controls
executable file
·62 lines (48 loc) · 1.52 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
#!/usr/bin/env python
import os
import json
import urllib
import urllib2
from munin import MuninPlugin
class HookboxPlugin(MuninPlugin):
title = 'hookbox'
args = "--base 1000"
vlabel = "Y"
info = "Subscibed users"
scale = False
def get_channels(self):
return os.environ.get('HOOKBOX_CHANNELS', '').split(',')
def get_url(self):
return os.environ.get('HOOKBOX_URL', 'http://localhost:8001/rest')
def get_secret(self):
return os.environ.get('HOOKBOX_SECRET', '')
@property
def fields(self):
return (
(channel, dict(
label=channel,
info="%s - users" % channel,
type="GAUGE",
))
for channel in self.get_channels()
)
def get_channel_info(self, channel_name):
values = {
'channel_name': channel_name,
'secret': self.get_secret(),
}
req = urllib2.Request("%s/get_channel_info?%s" % (self.get_url(), urllib.urlencode(values)))
resp = urllib2.urlopen(req)
return json.loads(resp.read())
def get_subscribers(self, channel_name):
try:
return len(self.get_channel_info(channel_name)[1]['subscribers'])
except (urllib2.URLError, KeyError), e:
return 'U'
def execute(self):
return dict(
(channel_name, self.get_subscribers(channel_name))
for channel_name in self.get_channels()
)
if __name__ == "__main__":
HookboxPlugin().run()