Last active
February 22, 2026 07:43
-
-
Save scr34m/9545330 to your computer and use it in GitHub Desktop.
MongoDB munin plugin for operations, memory and connections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import os | |
| try: | |
| import json | |
| except ImportError: | |
| import simplejson as json | |
| from urllib import request, error | |
| from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPDigestAuthHandler, build_opener, install_opener | |
| def getServerStatus(): | |
| host = os.environ.get("host", "127.0.0.1") | |
| port = int(os.environ.get("port", "28017")) | |
| url = "http://%s:%d/_status" % (host, port) | |
| req = request.Request(url) | |
| user = os.environ.get("user") | |
| password = os.environ.get("password") | |
| if user and password: | |
| passwdmngr = HTTPPasswordMgrWithDefaultRealm() | |
| passwdmngr.add_password(None, 'http://%s:%d' % (host, port), user, password) | |
| authhandler = HTTPDigestAuthHandler(passwdmngr) | |
| opener = build_opener(authhandler) | |
| install_opener(opener) | |
| try: | |
| with request.urlopen(req) as response: | |
| raw = response.read() | |
| return json.loads(raw)["serverStatus"] | |
| except error.HTTPError as e: | |
| print("HTTP error:", e.code) | |
| sys.exit(1) | |
| except error.URLError as e: | |
| print("URL error:", e.reason) | |
| sys.exit(1) | |
| name = "connections" | |
| def doData(): | |
| print(name + ".value " + str(getServerStatus()["connections"]["current"])) | |
| def doConfig(): | |
| print("graph_title MongoDB current connections") | |
| print("graph_args --base 1000 -l 0") | |
| print("graph_vlabel connections") | |
| print("graph_category MongoDB") | |
| print(name + ".label " + name) | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1 and sys.argv[1] == "config": | |
| doConfig() | |
| else: | |
| doData() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import os | |
| try: | |
| import json | |
| except ImportError: | |
| import simplejson as json | |
| from urllib import request, error | |
| from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPDigestAuthHandler, build_opener, install_opener | |
| def getServerStatus(): | |
| host = os.environ.get("host", "127.0.0.1") | |
| port = int(os.environ.get("port", "28017")) | |
| url = "http://%s:%d/_status" % (host, port) | |
| req = request.Request(url) | |
| user = os.environ.get("user") | |
| password = os.environ.get("password") | |
| if user and password: | |
| passwdmngr = HTTPPasswordMgrWithDefaultRealm() | |
| passwdmngr.add_password(None, 'http://%s:%d' % (host, port), user, password) | |
| authhandler = HTTPDigestAuthHandler(passwdmngr) | |
| opener = build_opener(authhandler) | |
| install_opener(opener) | |
| try: | |
| with request.urlopen(req) as response: | |
| raw = response.read() | |
| return json.loads(raw)["serverStatus"] | |
| except error.HTTPError as e: | |
| print("HTTP error:", e.code) | |
| sys.exit(1) | |
| except error.URLError as e: | |
| print("URL error:", e.reason) | |
| sys.exit(1) | |
| def ok(s): | |
| return s in ("resident", "virtual", "mapped") | |
| def doData(): | |
| for k, v in getServerStatus()["mem"].items(): # iteritems() -> items() Python 3-ban | |
| if ok(k): | |
| print(f"{k}.value {v * 1024 * 1024}") | |
| def doConfig(): | |
| print("graph_title MongoDB memory usage") | |
| print("graph_args --base 1024 -l 0 --vertical-label Bytes") | |
| print("graph_category MongoDB") | |
| for k in getServerStatus()["mem"]: | |
| if ok(k): | |
| print(f"{k}.label {k}") | |
| print(f"{k}.draw LINE1") | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1 and sys.argv[1] == "config": | |
| doConfig() | |
| else: | |
| doData() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import os | |
| try: | |
| import json | |
| except ImportError: | |
| import simplejson as json | |
| from urllib import request, error | |
| from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPDigestAuthHandler, build_opener, install_opener | |
| def getServerStatus(): | |
| host = os.environ.get("host", "127.0.0.1") | |
| port = int(os.environ.get("port", "28017")) | |
| url = "http://%s:%d/_status" % (host, port) | |
| req = request.Request(url) | |
| user = os.environ.get("user") | |
| password = os.environ.get("password") | |
| if user and password: | |
| passwdmngr = HTTPPasswordMgrWithDefaultRealm() | |
| passwdmngr.add_password(None, 'http://%s:%d' % (host, port), user, password) | |
| authhandler = HTTPDigestAuthHandler(passwdmngr) | |
| opener = build_opener(authhandler) | |
| install_opener(opener) | |
| try: | |
| with request.urlopen(req) as response: | |
| raw = response.read() | |
| return json.loads(raw)["serverStatus"] | |
| except error.HTTPError as e: | |
| print("HTTP error:", e.code) | |
| sys.exit(1) | |
| except error.URLError as e: | |
| print("URL error:", e.reason) | |
| sys.exit(1) | |
| def doData(): | |
| ss = getServerStatus() | |
| for k, v in ss["opcounters"].items(): | |
| print(f"{k}.value {v}") | |
| def doConfig(): | |
| print("graph_title MongoDB ops") | |
| print("graph_args --base 1000 -l 0") | |
| print("graph_vlabel ops / ${graph_period}") | |
| print("graph_category MongoDB") | |
| print("graph_total total") | |
| for k in getServerStatus()["opcounters"]: | |
| print(f"{k}.label {k}") | |
| print(f"{k}.min 0") | |
| print(f"{k}.type COUNTER") | |
| print(f"{k}.max 500000") | |
| print(f"{k}.draw LINE1") | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1 and sys.argv[1] == "config": | |
| doConfig() | |
| else: | |
| doData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment