Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / mongodb / mongodb_conn @ c6f88968

Historique | Voir | Annoter | Télécharger (1,84 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
=head1 AUTHOR
22

    
23
Alban Espie-Guillon <alban.espie@alterway.fr>
24

    
25
based on Stefan Andersen <stefan@stefanandersen.dk> work.
26

    
27
=head1 LICENSE
28
The Beer Ware License (Revision 42)
29
<alban.espie@alterway.fr> wrote this file. As long
30
as you retain this notice you can do whatever you want
31
with this stuff. If we meet some day, and you think
32
this stuff is worth it, you can buy me a beer in return.
33

    
34
SPDX-License-Identifier: Beerware
35

    
36
=cut
37
"""
38

    
39
import os
40
import sys
41
import pymongo
42

    
43

    
44
def _get_connections():
45
    host = os.environ.get('host', '127.0.0.1')
46
    port = os.environ.get('port', 27017)
47
    username = os.environ.get('username', '')
48
    password = os.environ.get('password', '')
49
    conn = pymongo.MongoClient(host, int(port))
50
    if username:
51
        connAuth = conn['admin']
52
        connAuth.authenticate(username, password)
53

    
54
    """ cli : db.serverStatus().connections """
55
    conn_status = conn.admin.command("serverStatus")['connections']
56
    return conn_status
57

    
58

    
59
def run():
60
    connections = _get_connections()
61
    for c, v in connections.items():
62
        print(str(c) + ".value " + str(v))
63

    
64

    
65
def config():
66
    print("""
67
graph_title MongoDB Connections Count
68
graph_vlabel Connections count
69
graph_category db
70
graph_args --base 1000 -l 0
71
current.label current
72
current.draw AREASTACK
73
available.label available
74
available.draw AREASTACK
75
active.label active
76
active.draw AREASTACK
77
""")
78

    
79

    
80
if __name__ == "__main__":
81
    if len(sys.argv) > 1 and sys.argv[1] == "config":
82
        config()
83
    else:
84
        run()