root / plugins / mongodb / mongodb_conn @ bc55ce69
Historique | Voir | Annoter | Télécharger (2,06 ko)
| 1 |
#!/usr/bin/env python3 |
|---|---|
| 2 |
""" |
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
mongodb_conn - MongoDB Connection Count Plugin |
| 6 |
|
| 7 |
=head1 APPLICABLE SYSTEMS |
| 8 |
|
| 9 |
MongoDB 3.X and 4.X with pymongo installed. |
| 10 |
|
| 11 |
=head1 CONFIGURATION |
| 12 |
|
| 13 |
Default for host is 127.0.0.1 and port 27017 and will work without being defined: |
| 14 |
|
| 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 |
or |
| 22 |
|
| 23 |
[mongodb_conn] |
| 24 |
env.MONGO_DB_URI mongodb://user:passwd@127.0.0.1:27017 |
| 25 |
|
| 26 |
=head1 AUTHOR |
| 27 |
|
| 28 |
Alban Espie-Guillon <alban.espie@alterway.fr> |
| 29 |
|
| 30 |
based on Stefan Andersen <stefan@stefanandersen.dk> work. |
| 31 |
|
| 32 |
=head1 LICENSE |
| 33 |
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 |
""" |
| 43 |
|
| 44 |
import os |
| 45 |
import sys |
| 46 |
import pymongo |
| 47 |
|
| 48 |
|
| 49 |
def _get_connections(): |
| 50 |
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 |
|
| 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 |
for c, v in connections.items(): |
| 70 |
print(str(c) + ".value " + str(v)) |
| 71 |
|
| 72 |
|
| 73 |
def config(): |
| 74 |
print("""
|
| 75 |
graph_title MongoDB Connections Count |
| 76 |
graph_vlabel Connections count |
| 77 |
graph_category db |
| 78 |
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 |
""") |
| 86 |
|
| 87 |
|
| 88 |
if __name__ == "__main__": |
| 89 |
if len(sys.argv) > 1 and sys.argv[1] == "config": |
| 90 |
config() |
| 91 |
else: |
| 92 |
run() |
