Projet

Général

Profil

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

root / plugins / mongodb / mongo_ops @ 4387edfa

Historique | Voir | Annoter | Télécharger (1,83 ko)

1
#!/usr/bin/env python3
2
"""
3
=head1 NAME
4

    
5
mongo_ops - MongoDB ops 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_ops]
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
or
23

    
24
    [mongodb_ops]
25
    env.MONGO_DB_URI mongodb://user:password@host:port/dbname
26

    
27
=head1 AUTHOR
28

    
29
Original script there : https://github.com/comerford/mongo-munin
30

    
31
Updated by Alban Espie-Guillon <alban.espie@alterway.fr>
32

    
33
=cut
34
"""
35

    
36
import sys
37
import os
38
import pymongo
39

    
40
def getServerStatus():
41
    if 'MONGO_DB_URI' in os.environ:
42
      c = pymongo.MongoClient(os.environ['MONGO_DB_URI'])
43

    
44
    elif 'username' and 'password' in os.environ:
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
      c = pymongo.MongoClient(host, int(port))
50
      if username:
51
        cAuth = c['admin']
52
        cAuth.authenticate(username, password)
53

    
54
    else:
55
      c = pymongo.MongoClient()
56

    
57
    return c.admin.command('serverStatus', workingSet=True)
58

    
59

    
60
def doData():
61
    ss = getServerStatus()
62
    for k,v in ss["opcounters"].items():
63
        print( str(k) + ".value " + str(v) )
64

    
65

    
66
def doConfig():
67
    print("""
68
graph_title MongoDB ops
69
graph_args --base 1000 -l 0
70
graph_vlabel ops / ${graph_period}
71
graph_category db
72
graph_total total
73
""")
74

    
75
    for k in getServerStatus()["opcounters"]:
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()