root / plugins / git / gitlab_statistics @ 852aa41a
Historique | Voir | Annoter | Télécharger (1,96 ko)
| 1 | 7761514c | pcy | #!/usr/bin/env python3 |
|---|---|---|---|
| 2 | # -*- python -*- |
||
| 3 | |||
| 4 | """ |
||
| 5 | =head1 INTRODUCTION |
||
| 6 | |||
| 7 | Plugin to monitor Gitlab status |
||
| 8 | |||
| 9 | =head1 INSTALLATION |
||
| 10 | |||
| 11 | Usage: 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 HISTORY |
||
| 27 | |||
| 28 | 2019-10-02: v 1.0 pcy <pcy.ulyssis.org>: created |
||
| 29 | |||
| 30 | =head1 USAGE |
||
| 31 | |||
| 32 | Parameters understood: |
||
| 33 | |||
| 34 | config (required) |
||
| 35 | autoconf (optional - used by munin-config) |
||
| 36 | |||
| 37 | =head1 MAGIC MARKERS |
||
| 38 | |||
| 39 | #%# family=auto |
||
| 40 | #%# capabilities=autoconf |
||
| 41 | """ |
||
| 42 | |||
| 43 | |||
| 44 | import os |
||
| 45 | import json |
||
| 46 | import urllib |
||
| 47 | import sys |
||
| 48 | |||
| 49 | |||
| 50 | def weakbool(x): |
||
| 51 | return x.lower().strip() in {"true", "yes", "y", "1"}
|
||
| 52 | |||
| 53 | |||
| 54 | url = None |
||
| 55 | if 'hostname' in os.environ and 'token' in os.environ: |
||
| 56 | url = "https://" + os.getenv('hostname') \
|
||
| 57 | + "/api/v4/application/statistics?private_token=" \ |
||
| 58 | + os.getenv('token')
|
||
| 59 | |||
| 60 | logarithmic = weakbool(os.getenv('logarithmic', 'N'))
|
||
| 61 | |||
| 62 | |||
| 63 | def reqjson(): |
||
| 64 | try: |
||
| 65 | raw_data = urllib.request.urlopen(url) |
||
| 66 | return json.loads(raw_data) |
||
| 67 | 852aa41a | Lars Kruse | except IOError: |
| 68 | 7761514c | pcy | print("Cannot reach the GitLab API endpoint.", file=sys.stderr)
|
| 69 | exit(1) |
||
| 70 | |||
| 71 | |||
| 72 | def autoconf(): |
||
| 73 | if 'hostname' not in os.environ: |
||
| 74 | print("no ('hostname' envvar not set)")
|
||
| 75 | elif 'token' not in os.environ: |
||
| 76 | print("no ('token' envvar not set)")
|
||
| 77 | else: |
||
| 78 | print("yes")
|
||
| 79 | |||
| 80 | |||
| 81 | def config(): |
||
| 82 | print("""\
|
||
| 83 | graph_title GitLab statistics |
||
| 84 | graph_vlabel amount |
||
| 85 | graph_category devel""") |
||
| 86 | if logarithmic: |
||
| 87 | print("graph_args --logarithmic")
|
||
| 88 | |||
| 89 | for x in reqjson().keys(): |
||
| 90 | print(x + ".label " + x) |
||
| 91 | |||
| 92 | |||
| 93 | def fetch(): |
||
| 94 | rj = reqjson() |
||
| 95 | for (x, y) in rj.items(): |
||
| 96 | print("%s.value %d" % (x, int(y.replace(',', ''))))
|
||
| 97 | |||
| 98 | |||
| 99 | if len(sys.argv) >= 2: |
||
| 100 | if sys.argv[1] == 'autoconf': |
||
| 101 | autoconf() |
||
| 102 | elif sys.argv[1] == 'config': |
||
| 103 | config() |
||
| 104 | else: |
||
| 105 | fetch() |
||
| 106 | else: |
||
| 107 | fetch() |
