Created
March 19, 2015 13:01
-
-
Save rsff/ab061406e7982abdc3c2 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 | |
#this script can NEVER FAIL! | |
''' | |
script to retrieve jmx related to metrics and send them to zabbix | |
''' | |
import subprocess | |
import re | |
import sys | |
import argparse | |
import json | |
def get_values(command, slurp): | |
try: | |
p = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
out, err = p.communicate(slurp) | |
subprocess.Popen(['reset']).wait() | |
if p.returncode: | |
print ':(' | |
except: | |
print 'meh' | |
return out | |
def parse_values(val): | |
ll = [] | |
jj = [] | |
valtemp = val.split("jmx_list metrics", 1)[1] | |
'''remove /n and %''' | |
valtemp = valtemp[:-2] | |
lista = valtemp.split(' ') | |
for x in lista: | |
ll.append(re.sub('[{}]', '', x)) | |
for y in ll: | |
kk = re.search('^metrics:name=(.*)', y, re.IGNORECASE) | |
if kk: | |
jj.append(kk.group(1)) | |
return jj | |
def main(): | |
parser = argparse.ArgumentParser(description='jmx extractor') | |
parser.add_argument('-srv', '--hostname', help='specify hostname') | |
parser.add_argument('-pt', '--port', help='specify port') | |
parser.add_argument('-bin', '--binary', help='jmxsh binary eg:/home/jmxsh.jar') | |
args = parser.parse_args() | |
if len(sys.argv) == 1: | |
parser.print_help() | |
sys.exit(6) | |
PORT = args.port | |
HOST = args.hostname | |
JMXSH = args.binary | |
sp = 'jmx_connect -h ' + HOST + ' -p ' + PORT + ' \n' | |
sp += 'jmx_list metrics \n' | |
cmd = 'java -jar ' + JMXSH + ' -q' | |
entries=[] | |
jj = parse_values(get_values(cmd, sp)) | |
for x in jj: | |
entries.append({'{#JMXOBJ}': x}) | |
print json.dumps({ "data": entries }) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment