root / plugins / mongodb / mongodb_docs @ dca09431
Historique | Voir | Annoter | Télécharger (1,86 ko)
| 1 | 276169a6 | Alban | #!/usr/bin/env python3 |
|---|---|---|---|
| 2 | """ |
||
| 3 | =head1 NAME |
||
| 4 | c6f88968 | Lars Kruse | |
| 5 | mongodb_docs - MongoDB docs Plugin |
||
| 6 | 276169a6 | Alban | |
| 7 | =head1 APPLICABLE SYSTEMS |
||
| 8 | |||
| 9 | c6f88968 | Lars Kruse | MongoDB 3.X and 4.X with pymongo installed. |
| 10 | 276169a6 | Alban | |
| 11 | =head1 CONFIGURATION |
||
| 12 | |||
| 13 | c6f88968 | Lars Kruse | Default for host is 127.0.0.1 and port 27017 and will work without being defined: |
| 14 | 276169a6 | Alban | |
| 15 | [mongodb_docs] |
||
| 16 | env.host 127.0.0.1 |
||
| 17 | env.port 27017 |
||
| 18 | env.username user |
||
| 19 | env.password P@55w0rd |
||
| 20 | env.db dbname |
||
| 21 | |||
| 22 | c6f88968 | Lars Kruse | or |
| 23 | 276169a6 | Alban | |
| 24 | [mongodb_docs] |
||
| 25 | env.MONGO_DB_URI mongodb://user:password@host:port/dbname |
||
| 26 | |||
| 27 | =head1 AUTHOR |
||
| 28 | |||
| 29 | c6f88968 | Lars Kruse | Original script there : https://github.com/comerford/mongo-munin |
| 30 | |||
| 31 | Updated by Alban Espie-Guillon <alban.espie@alterway.fr> |
||
| 32 | |||
| 33 | =cut |
||
| 34 | 276169a6 | Alban | """ |
| 35 | c6f88968 | Lars Kruse | |
| 36 | 276169a6 | Alban | import sys |
| 37 | import os |
||
| 38 | import pymongo |
||
| 39 | |||
| 40 | |||
| 41 | def getServerStatus(): |
||
| 42 | if 'MONGO_DB_URI' in os.environ: |
||
| 43 | c = pymongo.MongoClient(os.environ['MONGO_DB_URI']) |
||
| 44 | |||
| 45 | elif 'username' and 'password' in os.environ: |
||
| 46 | host = os.environ.get('host', '127.0.0.1')
|
||
| 47 | port = os.environ.get('port', 27017)
|
||
| 48 | username = os.environ.get('username', '')
|
||
| 49 | password = os.environ.get('password', '')
|
||
| 50 | c = pymongo.MongoClient(host, int(port)) |
||
| 51 | if username: |
||
| 52 | cAuth = c['admin'] |
||
| 53 | cAuth.authenticate(username, password) |
||
| 54 | |||
| 55 | else: |
||
| 56 | c = pymongo.MongoClient() |
||
| 57 | |||
| 58 | return c.admin.command('serverStatus', workingSet=True)
|
||
| 59 | |||
| 60 | |||
| 61 | def doData(): |
||
| 62 | ss = getServerStatus() |
||
| 63 | for k, v in ss["metrics"]["document"].items(): |
||
| 64 | print(str(k) + ".value " + str(v)) |
||
| 65 | |||
| 66 | |||
| 67 | def doConfig(): |
||
| 68 | print("""
|
||
| 69 | graph_title MongoDB documents |
||
| 70 | graph_args --base 1000 -l 0 |
||
| 71 | graph_vlabel documents |
||
| 72 | graph_category db |
||
| 73 | """) |
||
| 74 | |||
| 75 | for k in getServerStatus()["metrics"]["document"]: |
||
| 76 | print(k + ".label " + k) |
||
| 77 | print(k + ".min 0") |
||
| 78 | print(k + ".type COUNTER") |
||
| 79 | print(k + ".max 500000") |
||
| 80 | print(k + ".draw LINE1") |
||
| 81 | |||
| 82 | |||
| 83 | if __name__ == "__main__": |
||
| 84 | |||
| 85 | if len(sys.argv) > 1 and sys.argv[1] == "config": |
||
| 86 | doConfig() |
||
| 87 | else: |
||
| 88 | doData() |
