def __init__(self, logpath, debug=False): self._logpath = logpath self._debug = debug self._java_path = which("java") self._cmdclient_jar = get_realpath( ) + "/" + "cmdline-jmxclient-0.10.3.jar" self._logger = slog(self._logpath, debug=debug, size=5, count=5)
def get_mysql_cmd_output(self, cmdstr, hostname=None, username=None, password=None, port=None): try: hostname = hostname if hostname else self._iphost username = username if username else self._username passwd = password if password else self._password port = port if port else self._port bp = which("mysql") binpaths = [ bp, "/data0/mysql/product/bin/mysql", ] mysql_bp = None for p in binpaths: if os.path.isfile(p): mysql_path = p break if not mysql_path: return sql_cmdstr = '%s -h%s -P%s -u%s -p%s -e "%s"' % ( mysql_path, hostname, port, username, passwd, cmdstr) c2 = cmds(sql_cmdstr, timeout=2) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() logdict = { #"cmdstr": cmdstr, "cmdstr": sql_cmdstr, "stdo": stdo, "stde": stde, "retcode": retcode, "orders": ["cmdstr", "stdo", "stde", "retcode"], } if retcode != 0: self._logger.dictlog(width=8, level="error", **logdict) return None else: self._logger.dictlog(width=8, level="info", **logdict) output_list = re.split("[\n]+", str(stdo).strip()) content = dict() for line in output_list: line = str(line).strip().replace(" ", "").strip("|").lower() line_ary = re.split(r"[\s]+", line) if len(line_ary) < 2: continue if content.has_key(line_ary[0]): pass content[line_ary[0]] = line_ary[1] return content except Exception as expt: import traceback tb = traceback.format_exc() self._logger.error(tb)
def _get_result(self, iphost=None, port=None, username=None, password=None): try: hostname = iphost if iphost else self._iphost port = port if port else self._port username = username if username else self._username password = password if password else self._password resobj = None binpt = which("mongo") pbinpaths = [ binpt, "/usr/local/mongodb/bin/mongo", "/home/albert/mongodb/mongodb-3.0.0/bin/mongo", ] cmdstr = None for bp in pbinpaths: if not bp: continue if not os.path.exists(bp): continue cmdstr = "echo 'db.serverStatus()' | %s admin --host '%s' --port %s --quiet" % ( bp, hostname, port) if username and password: cmdstr = "echo 'db.serverStatus()' | %s admin --host '%s' --port %s -u '%s' -p '%s' --quiet" % ( bp, hostname, port, username, password) break if not cmdstr: return None c2 = cmds(cmdstr, timeout=3) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() 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) stdo_str = stdo stdo_str = stdo_str.replace("NumberLong(", "").replace( ")", "").replace("ISODate(", "").replace("ObjectId(", "") #print stdo_str resobj = json.loads(stdo_str) return resobj except Exception as expt: import traceback tb = traceback.format_exc() self._logger.error(tb)
def get_mysql_cmd_output(self, hostname=None, username=None, password=None, port=None): try: hostname = hostname if hostname else self._iphost username = username if username else self._username passwd = password if password else self._password port = port if port else self._port sys_binpath = which("mysqladmin") ## 适配编译安装,这里设置常用的路径 binpaths = [ sys_binpath, "/usr/bin/mysqladmin", "/data/mysql/product/bin/mysqladmin" ] cmdstr = None command = "extended-status" for p in binpaths: if not p or os.path.exists(p) == False: continue cmdstr = "%s -h%s -P%s -u'%s' -p'%s' %s" % ( p, hostname, port, username, passwd, command) if not cmdstr: return None c2 = cmds(cmdstr, timeout=3) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() 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 None else: self._logger.dictlog(width=8, level="info", **logdict) output_list = re.split("[\n]+", str(stdo).strip()) content = dict() for line in output_list: line = str(line).strip().replace(" ", "").lower().strip("|") line_ary = re.split("\|", line) if len(line_ary) < 2: continue content[line_ary[0]] = line_ary[1] return content except Exception as expt: import traceback tb = traceback.format_exc() self._logger.error(tb)
def get_mysql_cmd_output(self, cmdstr, hostname=None, username=None, password=None, port=None): try: hostname = hostname if hostname else self._iphost username = username if username else self._username passwd = password if password else self._password port = port if port else self._port bp = which("mysql") mysql_path = bp if bp else "/usr/local/bin/mysql" # 自编译mysql请修改此路径或自行添加PATH变量 if not (os.path.isfile(mysql_path) and os.access(mysql_path, os.X_OK)): exit("No mysql client found.") sql_cmdstr = '%s -h%s -P%s -u"%s" -p"%s" -e "%s"' % ( mysql_path, hostname, port, username, passwd, cmdstr) c2 = cmds(sql_cmdstr, timeout=2) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() logdict = { # "cmdstr": cmdstr, "cmdstr": sql_cmdstr, "stdo": stdo, "stde": stde, "retcode": retcode, "orders": ["cmdstr", "stdo", "stde", "retcode"], } if retcode != 0: self._logger.dictlog(width=8, level="error", **logdict) return None else: self._logger.dictlog(width=8, level="info", **logdict) output_list = re.split("[\n]+", str(stdo).strip()) content = dict() # 将命令行返回的内容解析成字典 for line in output_list: line = str(line).strip().replace(" ", "").strip("|").lower() # line_ary = re.split(r"[\s]+", line) line_ary = re.split(r"[\s|\||:|;|,]+", line) if len(line_ary) < 2: continue if line_ary[0] in content: pass content[line_ary[0]] = line_ary[1] return content except Exception as expt: import traceback tb = traceback.format_exc() self._logger.error(tb)
def get_mysql_cmd_output(self, cmdstr, hostname=None,username=None,password=None,port=None): try: hostname= hostname if hostname else self._iphost username = username if username else self._username passwd = password if password else self._password port = port if port else self._port bp = which("mysql") binpaths = [ bp, "/data0/mysql/product/bin/mysql", ] mysql_bp = None for p in binpaths: if os.path.isfile(p): mysql_path = p break if not mysql_path: return sql_cmdstr = '%s -h%s -P%s -u%s -p%s -e "%s"' % (mysql_path,hostname,port,username,passwd, cmdstr) c2 = cmds(sql_cmdstr, timeout=2) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() logdict = { #"cmdstr": cmdstr, "cmdstr": sql_cmdstr, "stdo": stdo, "stde": stde, "retcode": retcode, "orders": ["cmdstr", "stdo", "stde", "retcode"], } if retcode !=0: self._logger.dictlog(width=8, level="error", **logdict) return None else: self._logger.dictlog(width=8, level="info", **logdict) output_list = re.split("[\n]+", str(stdo).strip()) content = dict() for line in output_list: line = str(line).strip().replace(" ", "").strip("|").lower() #line_ary = re.split(r"[\s]+", line) line_ary = re.split(r"[\s|\||:|;|,]+", line) if len(line_ary) < 2:continue if content.has_key(line_ary[0]): pass content[line_ary[0]] = line_ary[1] return content except Exception as expt: import traceback tb = traceback.format_exc() self._logger.error(tb)
def get_res_set(self, key, port=None, password=None): port = port if port else self._port password = password if password else self._password rds_cli_path = which("redis-cli") ## 适配编译安装,这里设置常用的路径 rds_paths_def = [ "/usr/bin/redis-cli", "/bin/redis-cli", "/usr/local/redis-server/bin/redis-cli" ] cmdstr = None if rds_cli_path: cmdstr = "%s -h 127.0.0.1 -p %s info" % (rds_cli_path, port) if password: cmdstr = "%s -h 127.0.0.1 -a %s -p %s info" % (rds_cli_path, password, port) else: for p in rds_paths_def: if os.path.exists(p) == False: continue cmdstr = "%s -h 127.0.0.1 -p %s info" % (p, port) if password: cmdstr = "%s -h 127.0.0.1 -a %s -p %s info" % (p, password, port) break 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) pass resobj = {} 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() resobj[dst_key] = dst_val return resobj
def _get_result(self, iphost=None, port=None, username=None, password=None): try: hostname= iphost if iphost else self._iphost port = port if port else self._port username = username if username else self._username password = password if password else self._password resobj = None binpt = which("mongo") pbinpaths = [ binpt, "/usr/local/mongodb/bin/mongo", "/home/albert/mongodb/mongodb-3.0.0/bin/mongo", ] cmdstr = None for bp in pbinpaths: if not bp:continue if not os.path.exists(bp): continue cmdstr = "echo 'db.serverStatus()' | %s admin --host '%s' --port %s --quiet" % (bp, hostname, port) if username and password: cmdstr = "echo 'db.serverStatus()' | %s admin --host '%s' --port %s -u '%s' -p '%s' --quiet" % (bp, hostname, port, username, password) break if not cmdstr: return None c2 = cmds(cmdstr, timeout=3) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() 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) stdo_str = stdo stdo_str = stdo_str.replace("NumberLong(", "").replace(")", "").replace("ISODate(", "") #print stdo_str resobj = json.loads(stdo_str) return resobj except Exception as expt: import traceback tb = traceback.format_exc() self._logger.error(tb)
def get_mysql_cmd_output(self, hostname=None, username=None, password=None, port=None): try: hostname= hostname if hostname else self._iphost username = username if username else self._username passwd = password if password else self._password port = port if port else self._port sys_binpath = which("mysqladmin") ## 适配编译安装,这里设置常用的路径 binpaths = [sys_binpath, "/usr/bin/mysqladmin", "/data/mysql/product/bin/mysqladmin"] cmdstr = None command = "extended-status" for p in binpaths: if not p or os.path.exists(p) == False: continue cmdstr = "%s -h%s -P%s -u'%s' -p'%s' %s" % (p,hostname,port,username,passwd, command) if not cmdstr:return None c2 = cmds(cmdstr, timeout=3) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() 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 None else: self._logger.dictlog(width=8, level="info", **logdict) output_list = re.split("[\n]+", str(stdo).strip()) content = dict() for line in output_list: line = str(line).strip().replace(" ", "").lower().strip("|") line_ary = re.split("\|", line) if len(line_ary) < 2:continue content[line_ary[0]] = line_ary[1] return content except Exception as expt: import traceback tb = traceback.format_exc() self._logger.error(tb)
def get_res_set(self, key, port=None, password=None): port = port if port else self._port password = password if password else self._password rds_cli_path = which("redis-cli") ## 适配编译安装,这里设置常用的路径 rds_paths_def = ["/usr/bin/redis-cli", "/bin/redis-cli", "/usr/local/redis-server/bin/redis-cli"] cmdstr = None if rds_cli_path: cmdstr = "%s -h 127.0.0.1 -p %s info" % (rds_cli_path, port) if password: cmdstr = "%s -h 127.0.0.1 -a %s -p %s info" % (rds_cli_path, password, port) else: for p in rds_paths_def: if os.path.exists(p) == False: continue cmdstr = "%s -h 127.0.0.1 -p %s info" % (p, port) if password: cmdstr = "%s -h 127.0.0.1 -a %s -p %s info" % (p, password, port) break 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) resobj = {} 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() resobj[dst_key] = dst_val return resobj
def get_item(self, key, port=None, password=None, force=False): """ 参数: """ # cmdstr = "redis-cli -h 127.0.0.1 -p 6379 info | grep 'used_cpu_sys' " port = port if port else self._port password = password if password else self._password if force == False: value = self._file_cache.get_val_from_json(key) logdict = { "msg": "Try To Get From Cache File: %s" % self._file_cache_path, "key": key, "value": value, "orders": ["msg", "key", "value"], } self._logger.dictlog(width=8, level="info", **logdict) if value: return value rds_cli_path = which("redis-cli") ## 适配编译安装,这里设置常用的路径 rds_paths_def = ["/usr/bin/redis-cli", "/bin/redis-cli", "/usr/local/redis-server/bin/redis-cli"] cmdstr = None if rds_cli_path: cmdstr = "%s -h 127.0.0.1 -p %s info" % (rds_cli_path, port) if password: cmdstr = "%s -h 127.0.0.1 -a %s -p %s info" % (rds_cli_path, password, port) else: for p in rds_paths_def: if os.path.exists(p) == False: continue cmdstr = "%s -h 127.0.0.1 -p %s info" % (p, port) if password: cmdstr = "%s -h 127.0.0.1 -a %s -p %s info" % (p, password, port) break 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) resobj = {} 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() resobj[dst_key] = dst_val self._file_cache.save_to_cache_file(resobj) return resobj.get(key, "")
def get_item(self, key, port=None, password=None, force=False): """ 参数: """ # cmdstr = "redis-cli -h 127.0.0.1 -p 6379 info | grep 'used_cpu_sys' " port = port if port else self._port password = password if password else self._password if force == False: value = self._file_cache.get_val_from_json(key) logdict = { "msg": "Try To Get From Cache File: %s" % self._file_cache_path, "key": key, "value": value, "orders": ["msg", "key", "value"], } self._logger.dictlog(width=8, level="info", **logdict) if value: return value rds_cli_path = which("redis-cli") ## 适配编译安装,这里设置常用的路径 rds_paths_def = [ "/usr/local/bin/redis-cli", "/usr/bin/redis-cli", "/bin/redis-cli", "/usr/local/redis-server/bin/redis-cli" ] cmdstr = None if rds_cli_path: cmdstr = "%s -h 127.0.0.1 -p %s info" % (rds_cli_path, port) if password: cmdstr = "%s -h 127.0.0.1 -a %s -p %s info" % (rds_cli_path, password, port) else: for p in rds_paths_def: if os.path.exists(p) == False: continue cmdstr = "%s -h 127.0.0.1 -p %s info" % (p, port) if password: cmdstr = "%s -h 127.0.0.1 -a %s -p %s info" % (p, password, port) break 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) resobj = {} 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() resobj[dst_key] = dst_val self._file_cache.save_to_cache_file(resobj) return resobj.get(key, "")
def __init__(self, logpath, debug=False): self._logpath = logpath self._debug = debug self._java_path = which("java") self._cmdclient_jar = get_realpath()+"/" +"cmdline-jmxclient-0.10.3.jar" self._logger = slog(self._logpath, debug=debug, size=5, count=2)