Projet

Général

Profil

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

root / plugins / mongodb / mongo_mem @ 4168c164

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

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

    
5
mongo_mem - MongoDB memory 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
    [mongo_mem]
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_mem]
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
def ok(s):
60
    return s == "resident" or s == "virtual" or s == "mapped"
61

    
62
def doData():
63
    for k,v in getServerStatus()["mem"].items():
64
        if ok(k):
65
            print( str(k) + ".value " + str(v * 1024 * 1024) )
66

    
67

    
68
def doConfig():
69
    print("""
70
graph_title MongoDB memory usage
71
graph_args --base 1024 -l 0 --vertical-label Bytes
72
graph_category %s
73
""" % os.getenv('graph_category', 'mongodb'))
74

    
75
    for k in getServerStatus()["mem"]:
76
        if ok( k ):
77
            print(k + ".label " + k)
78
            print(k + ".draw LINE1")
79

    
80

    
81
if __name__ == "__main__":
82

    
83
    if len(sys.argv) > 1 and sys.argv[1] == "config":
84
        doConfig()
85
    else:
86
        doData()