Projet

Général

Profil

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

root / plugins / git / gitlab_statistics @ 87280ed7

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

1
#!/usr/bin/env python3
2
# -*- python -*-
3

    
4
"""
5
=head1 INTRODUCTION
6

    
7
Plugin to monitor Gitlab status
8

    
9
=head1 INSTALLATION
10

    
11
Place in /etc/munin/plugins/ (or link it there using ln -s)
12

    
13
=head1 CONFIGURATION
14

    
15
Add this to your /etc/munin/plugin-conf.d/munin-node:
16

    
17
=over 2
18

    
19
    [gitlab_statistics]
20
    env.logarithmic 1
21
    env.hostname gitlab.example.com # required
22
    env.token YourPrivateTokenHere # required
23

    
24
=back
25

    
26
=head1 AUTHORS
27

    
28
Copyright (C) 2019 pcy <pcy.ulyssis.org>
29

    
30
=head1 MAGIC MARKERS
31

    
32
 #%# family=auto
33
 #%# capabilities=autoconf
34

    
35
=cut
36
"""
37

    
38
import os
39
import json
40
import urllib.request
41
import sys
42

    
43

    
44
def weakbool(x):
45
    return x.lower().strip() in {"true", "yes", "y", "1"}
46

    
47

    
48
url = None
49
if 'hostname' in os.environ and 'token' in os.environ:
50
    url = "https://" + os.getenv('hostname') \
51
        + "/api/v4/application/statistics?private_token=" \
52
        + os.getenv('token')
53

    
54
logarithmic = weakbool(os.getenv('logarithmic', 'N'))
55

    
56

    
57
def reqjson():
58
    try:
59
        raw_data = urllib.request.urlopen(url)
60
        return json.loads(raw_data.read().decode())
61
    except IOError:
62
        print("Cannot reach the GitLab API endpoint.", file=sys.stderr)
63
        exit(1)
64

    
65

    
66
def autoconf():
67
    if 'hostname' not in os.environ:
68
        print("no ('hostname' envvar not set)")
69
    elif 'token' not in os.environ:
70
        print("no ('token' envvar not set)")
71
    else:
72
        print("yes")
73

    
74

    
75
def config():
76
    print("""\
77
graph_title GitLab statistics
78
graph_vlabel amount
79
graph_category devel""")
80
    if logarithmic:
81
        print("graph_args --logarithmic")
82

    
83
    for x in reqjson().keys():
84
        print(x + ".label " + x)
85

    
86

    
87
def fetch():
88
    rj = reqjson()
89
    for (x, y) in rj.items():
90
        print("%s.value %d" % (x, int(y.replace(',', ''))))
91

    
92

    
93
if len(sys.argv) >= 2:
94
    if sys.argv[1] == 'autoconf':
95
        autoconf()
96
    elif sys.argv[1] == 'config':
97
        config()
98
    else:
99
        fetch()
100
else:
101
    fetch()