root / plugins / rethinkdb / rethinkdb_node_io @ 05d8c7c8
Historique | Voir | Annoter | Télécharger (4,04 ko)
| 1 |
#!/usr/bin/env python3 |
|---|---|
| 2 |
""" |
| 3 |
rethinkdb_node_io - A munin plugin for Linux to monitor the io count |
| 4 |
per second on the local node |
| 5 |
|
| 6 |
This plugin is licensed under the AGPL 3.0 license |
| 7 |
|
| 8 |
AGPL 3.0 RubenKelevra |
| 9 |
Author: @RubenKelevra - <ruben@vfn-nrw.de> |
| 10 |
|
| 11 |
This plugin is written with the known limitation to a single instance per |
| 12 |
host. Patches which remove this limitation are very welcome. |
| 13 |
|
| 14 |
If your port / host is somewhat else than the default |
| 15 |
localhost:28015, and/or your database-server differes in name from |
| 16 |
`hostname` (short hostname), you can add rethinkdb-node-io config vars |
| 17 |
like: |
| 18 |
[rethinkdb_*] |
| 19 |
env.rethinkdb_port 12345 |
| 20 |
env.rethinkdb_host localhost.com |
| 21 |
env.rethinkdb_servername localhost.com |
| 22 |
|
| 23 |
The following munin configuration parameters are supported: |
| 24 |
#%# family=auto contrib |
| 25 |
#%# capabilities=autoconf |
| 26 |
|
| 27 |
""" |
| 28 |
|
| 29 |
from importlib.util import find_spec |
| 30 |
from multiprocessing import Process |
| 31 |
from os import environ as env |
| 32 |
from shutil import which |
| 33 |
from socket import gethostname |
| 34 |
from sys import argv |
| 35 |
from sys import exit as fatal_ |
| 36 |
from sys import stderr |
| 37 |
|
| 38 |
|
| 39 |
# functions |
| 40 |
def fatal(status): |
| 41 |
fatal_("ERROR: " + status)
|
| 42 |
|
| 43 |
|
| 44 |
def rclose_async(): |
| 45 |
conn.close() |
| 46 |
|
| 47 |
|
| 48 |
def check_load_rethinkdb() -> bool: |
| 49 |
try: |
| 50 |
rdb_spec = find_spec("rethinkdb")
|
| 51 |
if rdb_spec is None: |
| 52 |
return False |
| 53 |
return True |
| 54 |
except: |
| 55 |
fatal("Unknown error while try to load RethinkDB-Driver")
|
| 56 |
|
| 57 |
|
| 58 |
def eprint(*args, **kwargs): |
| 59 |
print(*args, file=stderr, **kwargs) |
| 60 |
|
| 61 |
|
| 62 |
def getFirstLine(respond): |
| 63 |
assert isinstance(respond, net.DefaultCursor) |
| 64 |
for e in respond: |
| 65 |
return e |
| 66 |
|
| 67 |
|
| 68 |
def print_config(servername): |
| 69 |
print("graph_title RethinkDB on '%s'- Local Database IOPS and Queries" % servername)
|
| 70 |
print("graph_args --base 1000 -l 0")
|
| 71 |
print("graph_vlabel Operations / second")
|
| 72 |
print("graph_category rethinkdb")
|
| 73 |
print("total_qps.label queries per sec")
|
| 74 |
print("total_qps.type COUNTER")
|
| 75 |
print("total_rdps.label read docs per sec")
|
| 76 |
print("total_rdps.type COUNTER")
|
| 77 |
print("total_wdps.label written docs per sec")
|
| 78 |
print("total_wdps.type COUNTER")
|
| 79 |
exit(0) |
| 80 |
|
| 81 |
|
| 82 |
def check_autoconf() -> bool: |
| 83 |
# this might be too easy, but gonna try. |
| 84 |
if which("rethinkdb"):
|
| 85 |
return True |
| 86 |
return False |
| 87 |
|
| 88 |
|
| 89 |
if __name__ == '__main__': |
| 90 |
try: |
| 91 |
RETHINKDB_SERVERNAME = env['rethinkdb_servername'] |
| 92 |
except: |
| 93 |
RETHINKDB_SERVERNAME = gethostname() |
| 94 |
|
| 95 |
if len(argv) > 2: |
| 96 |
fatal("unsupported argument count")
|
| 97 |
elif len(argv) == 2: |
| 98 |
if str(argv[1]) == "config": |
| 99 |
print_config(RETHINKDB_SERVERNAME) |
| 100 |
elif str(argv[1]) == "autoconf": |
| 101 |
if check_autoconf(): |
| 102 |
print("yes")
|
| 103 |
else: |
| 104 |
print("no")
|
| 105 |
if not check_load_rethinkdb(): |
| 106 |
# FIXME: Correct display of error message when driver is missing should be checked |
| 107 |
fatal("RethinkDB-Driver not available!")
|
| 108 |
exit(0) |
| 109 |
else: |
| 110 |
fatal("unsupported argument")
|
| 111 |
|
| 112 |
if not check_load_rethinkdb(): |
| 113 |
fatal("RethinkDB-Driver not available!")
|
| 114 |
from rethinkdb import net, connect, db |
| 115 |
|
| 116 |
# load environment |
| 117 |
try: |
| 118 |
RETHINKDB_PORT = env['rethinkdb_port'] |
| 119 |
except: |
| 120 |
RETHINKDB_PORT = "28015" |
| 121 |
|
| 122 |
try: |
| 123 |
RETHINKDB_HOST = env['rethinkdb_host'] |
| 124 |
except: |
| 125 |
RETHINKDB_HOST = "localhost" |
| 126 |
|
| 127 |
try: |
| 128 |
conn = connect(RETHINKDB_HOST, RETHINKDB_PORT) |
| 129 |
except: |
| 130 |
fatal("connection attempt to the rethinkdb-host \"%s\" via port \"%s\" failed" % (
|
| 131 |
str(RETHINKDB_HOST), str(RETHINKDB_PORT))) |
| 132 |
|
| 133 |
query_engine_info = getFirstLine( |
| 134 |
db('rethinkdb').table("stats").filter({"server": RETHINKDB_SERVERNAME}).pluck('query_engine').limit(1).run(
|
| 135 |
conn))['query_engine'] |
| 136 |
|
| 137 |
rclose = Process(target=rclose_async()) |
| 138 |
rclose.start() |
| 139 |
|
| 140 |
print("total_qps.value %s" % (query_engine_info["queries_total"]))
|
| 141 |
print("total_rdps.value %s" % (query_engine_info["read_docs_total"]))
|
| 142 |
print("total_wdps.value %s" % (query_engine_info["written_docs_total"]))
|
| 143 |
|
| 144 |
# wait for connection termination |
| 145 |
rclose.join() |
