Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / other / hookbox @ 7da1b039

Historique | Voir | Annoter | Télécharger (1,52 ko)

1
#!/usr/bin/env python
2

    
3
import os
4
import json
5
import urllib
6
import urllib2
7

    
8
from munin import MuninPlugin
9

    
10

    
11

    
12
class HookboxPlugin(MuninPlugin):
13
    title = 'hookbox'
14
    args = "--base 1000"
15
    vlabel = "Y"
16
    info = "Subscibed users"
17
    scale = False
18

    
19
    def get_channels(self):
20
        return os.environ.get('HOOKBOX_CHANNELS', '').split(',')
21

    
22
    def get_url(self):
23
        return os.environ.get('HOOKBOX_URL', 'http://localhost:8001/rest')
24

    
25
    def get_secret(self):
26
        return os.environ.get('HOOKBOX_SECRET', '')
27

    
28

    
29
    @property
30
    def fields(self):
31
        return (
32
            (channel, dict(
33
                label=channel,
34
                info="%s - users" % channel,
35
                type="GAUGE",
36
            ))
37
            for channel in self.get_channels()
38
        )
39

    
40
    def get_channel_info(self, channel_name):
41
        values = {
42
            'channel_name': channel_name,
43
            'secret': self.get_secret(),
44
        }
45
        req = urllib2.Request("%s/get_channel_info?%s" % (self.get_url(), urllib.urlencode(values)))
46
        resp = urllib2.urlopen(req)
47
        return json.loads(resp.read())
48

    
49
    def get_subscribers(self, channel_name):
50
        try:
51
            return len(self.get_channel_info(channel_name)[1]['subscribers'])
52
        except (urllib2.URLError, KeyError), e:
53
            return 'U'
54

    
55
    def execute(self):
56
        return dict(
57
            (channel_name, self.get_subscribers(channel_name))
58
            for channel_name in self.get_channels()
59
        )
60

    
61
if __name__ == "__main__":
62
    HookboxPlugin().run()