root / plugins / mongodb / mongo_mem @ c6f88968
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_mem - MongoDB memory Plugin |
||
| 6 | 65a40bb6 | Eliot Horowitz | |
| 7 | 276169a6 | Alban | =head1 APPLICABLE SYSTEMS |
| 8 | 65a40bb6 | 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 | [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 | c6f88968 | Lars Kruse | or |
| 23 | 65a40bb6 | Eliot Horowitz | |
| 24 | 276169a6 | Alban | [mongodb_mem] |
| 25 | env.MONGO_DB_URI mongodb://user:password@host:port/dbname |
||
| 26 | 65a40bb6 | 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 | 65a40bb6 | 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 | 65a40bb6 | Eliot Horowitz | |
| 59 | def ok(s): |
||
| 60 | return s == "resident" or s == "virtual" or s == "mapped" |
||
| 61 | |||
| 62 | def doData(): |
||
| 63 | 276169a6 | Alban | for k,v in getServerStatus()["mem"].items(): |
| 64 | 65a40bb6 | Eliot Horowitz | if ok(k): |
| 65 | print( str(k) + ".value " + str(v * 1024 * 1024) ) |
||
| 66 | |||
| 67 | |||
| 68 | 276169a6 | Alban | def doConfig(): |
| 69 | print("""
|
||
| 70 | graph_title MongoDB memory usage |
||
| 71 | graph_args --base 1024 -l 0 --vertical-label Bytes |
||
| 72 | graph_category mongodb |
||
| 73 | """) |
||
| 74 | 65a40bb6 | Eliot Horowitz | |
| 75 | for k in getServerStatus()["mem"]: |
||
| 76 | if ok( k ): |
||
| 77 | 276169a6 | Alban | print(k + ".label " + k) |
| 78 | print(k + ".draw LINE1") |
||
| 79 | 65a40bb6 | Eliot Horowitz | |
| 80 | |||
| 81 | if __name__ == "__main__": |
||
| 82 | 276169a6 | Alban | |
| 83 | 65a40bb6 | Eliot Horowitz | if len(sys.argv) > 1 and sys.argv[1] == "config": |
| 84 | doConfig() |
||
| 85 | else: |
||
| 86 | doData() |
