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 276169a6 Alban
#!/usr/bin/env python3
2
"""
3
=head1 NAME
4 c6f88968 Lars Kruse
5
mongo_ops - MongoDB ops Plugin
6 1f23e26d Eliot Horowitz
7 276169a6 Alban
=head1 APPLICABLE SYSTEMS
8 1f23e26d Eliot Horowitz
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_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 c6f88968 Lars Kruse
or
23 1f23e26d Eliot Horowitz
24 276169a6 Alban
    [mongodb_ops]
25
    env.MONGO_DB_URI mongodb://user:password@host:port/dbname
26 1f23e26d Eliot Horowitz
27 276169a6 Alban
=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 1f23e26d Eliot Horowitz
40
def getServerStatus():
41 276169a6 Alban
    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 1f23e26d Eliot Horowitz
59
60
def doData():
61
    ss = getServerStatus()
62 276169a6 Alban
    for k,v in ss["opcounters"].items():
63 1f23e26d Eliot Horowitz
        print( str(k) + ".value " + str(v) )
64
65
66 276169a6 Alban
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 1f23e26d Eliot Horowitz
75
    for k in getServerStatus()["opcounters"]:
76 276169a6 Alban
        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 1f23e26d Eliot Horowitz
83
if __name__ == "__main__":
84 276169a6 Alban
85 1f23e26d Eliot Horowitz
    if len(sys.argv) > 1 and sys.argv[1] == "config":
86
        doConfig()
87
    else:
88
        doData()