Projet

Général

Profil

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

root / plugins / twemproxy / nutcracker_requests_ @ 51864405

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

1
#!/usr/bin/env python3
2
"""
3
=head1 NAME
4

    
5
nutcracker_requests_ - monitor twemproxy (aka: nutcracker)
6

    
7

    
8
=head1 CONFIGURATION
9

    
10
Config in /etc/munin/plugin-conf.d/nutcracker.conf:
11

    
12
 [nutcracker_*]
13
 env.NUTCRACKER_STATS_HOST 127.0.0.1
14
 env.NUTCRACKER_STATS_PORT 22222
15

    
16

    
17
=head1 AUTHORS
18

    
19
Copyright 2013 Edgar Veiga <edgarmveiga@gmail.com>
20

    
21
=cut
22
"""
23

    
24

    
25
import json
26
import socket
27
import os
28
import sys
29

    
30

    
31
def get_stats():
32
    data = ''
33

    
34
    HOST = os.environ.get('NUTCRACKER_STATS_HOST', '127.0.0.1')
35
    PORT = int(os.environ.get('NUTCRACKER_STATS_PORT', 22222))
36

    
37
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
38
    s.connect((HOST, PORT))
39

    
40
    file = s.makefile('r')
41
    data = file.readline()
42
    s.close()
43

    
44
    return json.loads(data)
45

    
46

    
47
def process_data():
48
    data = get_stats()
49
    # get pools
50
    for key, value in data.items():
51
        if isinstance(value, dict):
52
            total = 0
53
            # get server requests
54
            for pool_key, pool_value in value.items():
55
                if isinstance(pool_value, dict):
56
                    total += pool_value["requests"]
57
            print("requests_" + key + ".value" + " " + str(total))
58

    
59

    
60
def process_config():
61
    print("graph_title Nutcracker requests/s")
62
    print("graph_category other")
63
    print("graph_vlabel requests/s")
64

    
65
    data = get_stats()
66
    for key, value in data.items():
67
        if isinstance(value, dict):
68
            print("requests_" + key + ".label " + key)
69
            print("requests_" + key + ".type COUNTER")
70
            print("requests_" + key + ".min 0")
71

    
72

    
73
if __name__ == "__main__":
74
    if len(sys.argv) > 1 and sys.argv[1] == "config":
75
        process_config()
76
    else:
77
        process_data()