root / plugins / mongodb / mongodb_conn @ dca09431
Historique | Voir | Annoter | Télécharger (2,09 ko)
| 1 | 0a4891fc | Alban | #!/usr/bin/env python3 |
|---|---|---|---|
| 2 | """ |
||
| 3 | =head1 NAME |
||
| 4 | c6f88968 | Lars Kruse | |
| 5 | mongodb_conn - MongoDB Connection Count Plugin |
||
| 6 | 0a4891fc | Alban | |
| 7 | =head1 APPLICABLE SYSTEMS |
||
| 8 | |||
| 9 | c6f88968 | Lars Kruse | MongoDB 3.X and 4.X with pymongo installed. |
| 10 | 0a4891fc | 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 | 0a4891fc | Alban | |
| 15 | [mongodb_conn] |
||
| 16 | env.host 127.0.0.1 |
||
| 17 | env.port 27017 |
||
| 18 | env.username user |
||
| 19 | env.password P@55w0rd |
||
| 20 | |||
| 21 | bc55ce69 | Jeremías | or |
| 22 | 6ee90f2e | Jeremías | |
| 23 | [mongodb_conn] |
||
| 24 | env.MONGO_DB_URI mongodb://user:passwd@127.0.0.1:27017 |
||
| 25 | |||
| 26 | 0a4891fc | Alban | =head1 AUTHOR |
| 27 | |||
| 28 | c6f88968 | Lars Kruse | Alban Espie-Guillon <alban.espie@alterway.fr> |
| 29 | |||
| 30 | based on Stefan Andersen <stefan@stefanandersen.dk> work. |
||
| 31 | 0a4891fc | Alban | |
| 32 | =head1 LICENSE |
||
| 33 | c6f88968 | Lars Kruse | The Beer Ware License (Revision 42) |
| 34 | <alban.espie@alterway.fr> wrote this file. As long |
||
| 35 | as you retain this notice you can do whatever you want |
||
| 36 | with this stuff. If we meet some day, and you think |
||
| 37 | this stuff is worth it, you can buy me a beer in return. |
||
| 38 | |||
| 39 | SPDX-License-Identifier: Beerware |
||
| 40 | |||
| 41 | =cut |
||
| 42 | 0a4891fc | Alban | """ |
| 43 | c6f88968 | Lars Kruse | |
| 44 | 0a4891fc | Alban | import os |
| 45 | import sys |
||
| 46 | import pymongo |
||
| 47 | |||
| 48 | |||
| 49 | def _get_connections(): |
||
| 50 | 6ee90f2e | Jeremías | if 'MONGO_DB_URI' in os.environ: |
| 51 | conn = pymongo.MongoClient(os.environ['MONGO_DB_URI']) |
||
| 52 | else: |
||
| 53 | host = os.environ.get('host', '127.0.0.1')
|
||
| 54 | port = os.environ.get('port', 27017)
|
||
| 55 | username = os.environ.get('username', '')
|
||
| 56 | password = os.environ.get('password', '')
|
||
| 57 | conn = pymongo.MongoClient(host, int(port)) |
||
| 58 | if username: |
||
| 59 | connAuth = conn['admin'] |
||
| 60 | connAuth.authenticate(username, password) |
||
| 61 | 0a4891fc | Alban | |
| 62 | """ cli : db.serverStatus().connections """ |
||
| 63 | conn_status = conn.admin.command("serverStatus")['connections']
|
||
| 64 | return conn_status |
||
| 65 | |||
| 66 | |||
| 67 | def run(): |
||
| 68 | connections = _get_connections() |
||
| 69 | 276169a6 | Alban | for c, v in connections.items(): |
| 70 | print(str(c) + ".value " + str(v)) |
||
| 71 | 0a4891fc | Alban | |
| 72 | |||
| 73 | def config(): |
||
| 74 | print("""
|
||
| 75 | graph_title MongoDB Connections Count |
||
| 76 | graph_vlabel Connections count |
||
| 77 | dca09431 | Jeremías | graph_category %s |
| 78 | 0a4891fc | Alban | graph_args --base 1000 -l 0 |
| 79 | current.label current |
||
| 80 | current.draw AREASTACK |
||
| 81 | available.label available |
||
| 82 | available.draw AREASTACK |
||
| 83 | active.label active |
||
| 84 | active.draw AREASTACK |
||
| 85 | dca09431 | Jeremías | """ % os.getenv('graph_category', 'db'))
|
| 86 | 0a4891fc | Alban | |
| 87 | |||
| 88 | if __name__ == "__main__": |
||
| 89 | if len(sys.argv) > 1 and sys.argv[1] == "config": |
||
| 90 | config() |
||
| 91 | else: |
||
| 92 | run() |
