Created
May 16, 2011 21:28
-
-
Save hoffrocket/975414 to your computer and use it in GitHub Desktop.
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/python | |
from subprocess import Popen | |
import sys, os, urllib2 | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
GMETRIC = "/usr/bin/gmetric --name=\"%s\" --value=\"%s\" --type=\"int32\" --units=\"%s\"" | |
class ServerStatus: | |
ops_tmp_file = os.path.join("/", "tmp", "mongo-prevops") | |
def __init__(self, port=None): | |
if (port == None): | |
port = 28017 | |
self.port = port | |
self.status = self.getServerStatus() | |
# call individual metrics | |
for f in ["conns", "btree", "repl", "ops", "lock", "mem"]: | |
getattr(self,f)() | |
def getServerStatus(self): | |
raw = urllib2.urlopen( "http://127.0.0.1:%s/_status" % self.port ).read() | |
return json.loads( raw )["serverStatus"] | |
def callGmetric(self, d): | |
for k,v in d.iteritems(): | |
cmd = GMETRIC % ("mongodb_%s_%s" % (k, self.port), v[0], v[1]) | |
Popen(cmd, shell=True) | |
def conns(self): | |
ss = self.status | |
self.callGmetric({ | |
"connections" : (ss["connections"]["current"], "connections") | |
}) | |
def btree(self): | |
try: | |
b = self.status["indexCounters"]["btree"] | |
self.callGmetric({ | |
"btree_accesses" : (b["accesses"], "count"), | |
"btree_hits" : (b["hits"], "count"), | |
"btree_misses" : (b["misses"], "count"), | |
"btree_resets" : (b["resets"], "count"), | |
"btree_miss_ratio" : (b["missRatio"], "ratio"), | |
}) | |
except: | |
pass | |
def mem(self): | |
try: | |
m = self.status["mem"] | |
self.callGmetric({ | |
"mem_resident" : (m["resident"], "MB"), | |
"mem_virtual" : (m["virtual"], "MB"), | |
"mem_mapped" : (m["mapped"], "MB"), | |
}) | |
except: | |
pass | |
def ops(self): | |
out = {} | |
cur_ops = self.status["opcounters"] | |
try: | |
f = open(self.ops_tmp_file, "r") | |
content = f.read() | |
prev_ops = json.loads(content) | |
f.close() | |
except (ValueError, IOError): | |
prev_ops = {} | |
for k,v in cur_ops.iteritems(): | |
if k in prev_ops: | |
name = k + "s_per_second" | |
if k == "query": | |
name = "queries_per_second" | |
out[name] = ((float(v) - float(prev_ops[k]) ) / 60, "ops/s") | |
f = open(self.ops_tmp_file, 'w') | |
try: | |
f.write(json.dumps(cur_ops)) | |
finally: | |
f.close() | |
self.callGmetric(out) | |
def repl(self): | |
try: | |
self.callGmetric({ | |
"is_master" : (self.status["repl"]["ismaster"], "boolean") | |
}) | |
except: | |
pass | |
def lock(self): | |
try: | |
self.callGmetric({ | |
"lock_ratio" : (self.status["globalLock"]["ratio"], "ratio") | |
}) | |
except: | |
pass | |
if __name__ == "__main__": | |
port = None | |
if (len(sys.argv) == 2): | |
port = sys.argv[1] | |
ServerStatus(port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment