root / plugins / git / gitlab_statistics @ 87280ed7
Historique | Voir | Annoter | Télécharger (1,87 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 | c6f88968 | Lars Kruse | Place in /etc/munin/plugins/ (or link it there using ln -s) |
| 12 | 7761514c | pcy | |
| 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 | c6f88968 | Lars Kruse | =head1 AUTHORS |
| 27 | 7761514c | pcy | |
| 28 | c6f88968 | Lars Kruse | Copyright (C) 2019 pcy <pcy.ulyssis.org> |
| 29 | 7761514c | pcy | |
| 30 | =head1 MAGIC MARKERS |
||
| 31 | |||
| 32 | c6f88968 | Lars Kruse | #%# family=auto |
| 33 | #%# capabilities=autoconf |
||
| 34 | 7761514c | pcy | |
| 35 | c6f88968 | Lars Kruse | =cut |
| 36 | """ |
||
| 37 | 7761514c | pcy | |
| 38 | import os |
||
| 39 | import json |
||
| 40 | 87280ed7 | Doctor | import urllib.request |
| 41 | 7761514c | pcy | 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 | 87280ed7 | Doctor | return json.loads(raw_data.read().decode()) |
| 61 | 852aa41a | Lars Kruse | except IOError: |
| 62 | 7761514c | pcy | 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() |
