def get_item(self, beanstr, key, port): """ java -jar cmdline-jmxclient-0.10.3.jar - localhost:12345 java.lang:type=Memory NonHeapMemoryUsage 参数: """ pre_key = key sub_key = None if "." in key: pos = str(key).rfind(".") pre_key = key[0:pos] sub_key = key[pos+1:] lst_beanstr = self._parse_beanstr(beanstr) cmdstr = "%s -jar %s - localhost:%s '%s' '%s'" % (self._java_path, self._cmdclient_jar, port, lst_beanstr, pre_key) c2 = cmds(cmdstr, timeout=3) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() (stdo_list, stde_list) = (re.split("\n", stdo), re.split("\n", stde)) logdict = { "beanstr": beanstr, "lst_beanstr": lst_beanstr, "key": key, "cmdstr": cmdstr, "stdo": stdo, "stde": stde, "retcode": retcode, "orders": ["beanstr", "lst_beanstr", "key", "cmdstr", "stdo", "stde", "retcode"], } if retcode !=0: self._logger.dictlog(width=8, level="error", **logdict) return #else: # self._logger.dictlog(width=8, level="info", **logdict) if stde_list: stdo_list.extend(stde_list) ## without sub attr if not sub_key and stdo_list: line = stdo_list[-1] ln_ary = re.split(" ", line) if ln_ary and len(ln_ary) >= 2: if pre_key in ln_ary[-2]: return ln_ary[-1] #print stdo_list,"###" ## with sub attr for line in stdo_list: line = str(line).strip() ln_ary = re.split(":", line) if ln_ary and len(ln_ary) != 2:continue dst_key = str(ln_ary[0]).strip() dst_val = str(ln_ary[1]).strip() if sub_key == dst_key: return dst_val return None
def get_port_list(self): cmdstr = "ps -ef | grep 'jmxremote.port='| grep -v grep 2>/dev/null" plst = [] c2 = cmds(cmdstr, timeout=3) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() (stdo_list, stde_list) = (re.split("\n", stdo), re.split("\n", stde)) logdict = { "cmdstr": cmdstr, "stdo": stdo, "stde": stde, "retcode": retcode, "orders": ["cmdstr", "stdo", "stde", "retcode"], } if retcode != 0: self._logger.dictlog(width=8, level="error", **logdict) return #else: # self._logger.dictlog(width=8, level="info", **logdict) data = list() for line in stdo_list: if not line or str(line).strip() == "": continue line = str(line).strip() ln_ary = re.split("[\s]+", line) runuser = ln_ary[0] pid = ln_ary[1] jmxport = None for field in ln_ary: if "jmxremote.port=" in field: jmxport = str(field).split("jmxremote.port=")[1] if "-Dcatalina.home=" in field: appname = str(field).split("/")[-1] elif "io.mycat.MycatStartup" in field: appname = "Mycat-server" elif "-Dapp=" in field: appname = str(field).split("-Dapp=")[1] confitem = { "{#PID}": int(pid), "{#RUNUSER}": runuser, } if jmxport: confitem["{#JVMPORT}"] = int(jmxport) if appname: confitem["{#APPNAME}"] = str(appname) if confitem: data.append(confitem) return json.dumps({'data': data}, sort_keys=True, indent=7, separators=(",", ":"))
def _send_data(self, tmpfile): '''Send the queue data to Zabbix.''' #cmdstr = '/data/svr/zabbix-agentd/bin/zabbix_sender -vv -c {0} -i {1}' cmdstr = '/usr/local/zabbix-agent/bin/zabbix_sender -vv -c {0} -i {1}' if self.senderhostname: cmdstr = cmdstr + " -s " + self.senderhostname c2 = cmds(cmdstr.format(self.conf, tmpfile)) stdo, stde, return_code = c2.stdo(), c2.stde(), c2.code() logging.debug("Finished sending data") logging.info("Found return code of " + str(return_code)) (stdo_list, stde_list) = (re.split("\n", stdo.strip()), re.split("\n", stde.strip())) logdict = { "cmdstr": cmdstr, "stdo": stdo, "stde": stde, "retcode": return_code, "orders": ["cmdstr", "stdo", "stde", "return_code"], } if return_code == 1: logging.error(logdict) else: logging.debug(logdict) return return_code
def call_jmx(self, port, beanstr, key=None): """ java -jar cmdline-jmxclient-0.10.3.jar - localhost:12345 java.lang:type=Memory NonHeapMemoryUsage 参数: """ if key: cmdstr = "java -jar %s - localhost:%s '%s' '%s'" % ( self._cmdclient_jar, port, beanstr, key) else: cmdstr = "java -jar %s - localhost:%s '%s' " % ( self._cmdclient_jar, port, beanstr) c2 = cmds(cmdstr, timeout=3) logging.debug("Call jmx get key " + beanstr + ":" + key + " on port " + str(port)) stdo, stde, return_code = c2.stdo(), c2.stde(), c2.code() logging.info("Found return code of " + str(return_code)) logdict = { "cmdstr": cmdstr, "stdo": stdo, "stde": stde, "retcode": return_code, "orders": ["cmdstr", "stdo", "stde", "return_code"] } if return_code == 1: logging.error(logdict) return else: logging.debug(logdict) return stde if stde else stdo
def get_port_list(self): cmdstr = "ps -ef|grep java|grep -oP 'jmxremote.port=[0-9]{1,}'|grep -oP '\d+' 2>/dev/null" c2 = cmds(cmdstr, timeout=3) stdo, stde, return_code = c2.stdo(), c2.stde(), c2.code() logging.info("Found return code of " + str(return_code)) (stdo_list, stde_list) = (re.split("\n", stdo.strip()), re.split("\n", stde.strip())) logdict = { "cmdstr": cmdstr, "stdo": stdo, "stde": stde, "retcode": return_code, "orders": ["cmdstr", "stdo", "stde", "return_code"], } if return_code == 1: logging.error(logdict) return else: logging.debug(logdict) return stdo_list
def get_port_list(self): cmdstr = "ps -ef | grep tomcat | grep 'jmxremote.port='| grep -v grep 2>/dev/null" plst = [] c2 = cmds(cmdstr, timeout=3) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() (stdo_list, stde_list) = (re.split("\n", stdo), re.split("\n", stde)) logdict = { "cmdstr": cmdstr, "stdo": stdo, "stde": stde, "retcode": retcode, "orders": ["cmdstr", "stdo", "stde", "retcode"], } if retcode !=0: self._logger.dictlog(width=8, level="error", **logdict) return #else: # self._logger.dictlog(width=8, level="info", **logdict) data = list() for line in stdo_list: if not line or str(line).strip() == "": continue line = str(line).strip() ln_ary = re.split("[\s]+", line) pid = ln_ary[1] runuser = ln_ary[0] (jmxport, catalina_home, tc_dirname, buz_port) = (None, None, None, None) for field in ln_ary: if "jmxremote.port=" in field: jmxport = str(field).split("jmxremote.port=")[1] if "catalina.home=" in field: catalina_home = str(field).split("catalina.home=")[1] tc_dirname = str(catalina_home).split("/")[-1] confitem = { "{#RUNUSER}": runuser, "{#PID}": int(pid), } if jmxport: confitem["{#JVMPORT}"] = int(jmxport) if catalina_home: confitem["{#CTLN_HOME}"] = catalina_home server_xml = "%s/conf/server.xml" % (catalina_home) if os.path.exists(server_xml): parser = xml.sax.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, 0) handler = TCSerHandler() parser.setContentHandler( handler ) parser.parse(server_xml) biz_port = handler.get_biz_port() if biz_port: confitem["{#BIZPORT}"] = biz_port if tc_dirname: confitem["{#DIRNAME}"] = tc_dirname if confitem: data.append(confitem) import json return json.dumps({'data': data}, sort_keys=True, indent=7, separators=(",",":"))