def add_port(self, config_type, zone_name, port, protocol): """ Add port and protocol to a zone :param config_type: :param zone_name: :param port: :param protocol: :return: """ prot = ('udp', 'tcp') if self.port_isvalid(port) and protocol in prot: log.debug(port) port = self.change_type(port) cmd = '' if zone_name != '': if config_type == 'permanent': cmd = 'firewall-cmd --permanent --zone=' + zone_name + ' --add-port=' + port + '/' + protocol else: cmd = 'firewall-cmd --zone=' + zone_name + ' --add-port=' + port + '/' + protocol else: if config_type == 'permanent': cmd = 'firewall-cmd --permanent' + ' --add-port=' + port + '/' + protocol else: cmd = 'firewall-cmd' + ' --add-port=' + port + '/' + protocol result = os.system(cmd) if result == 0: return 'add port success', True else: log.error(const.ADD_PORT_FAILED+port+'&'+protocol) return 'add port failed', False else: log.error(const.PORT_OR_PROTOCOL_INVALID+port+'&'+protocol) return 'port or protocol is invalid ,please resume load', False
def get_socket_info(cls): """ get socket info :return: """ socket_info = {} try: logger.debug("socket begin:======>") rtinfo = functions.launchcmd('sar -n SOCK 1 1').readlines() logger.debug("commands finished! ") if rtinfo: continue_falg = True for item in rtinfo: if continue_falg: if "totsck" in item: continue_falg = False continue tmplist = item.split() socket_info["time"] = tmplist[0] socket_info["totsck"] = tmplist[1] socket_info["tcpsck"] = tmplist[2] socket_info["udpsck"] = tmplist[3] socket_info["rawsck"] = tmplist[4] socket_info["ip-frag"] = tmplist[5] socket_info["tcp-tw"] = tmplist[6] break except IOError, e: logger.error(e.message, e) return False, socket_info
def get(self, request, format=None): psMemCpuInfo = {} commands_MEM = "ps -aux|sort -k4nr|grep -v 'USER'|head -n 1" commands_CPU = "ps -aux|sort -k3nr|grep -v 'USER'|head -n 1" try: ps_MEM_info = functions.launchcmd(commands_MEM).readline() ps_CPU_info = functions.launchcmd(commands_CPU).readline() logger.debug("ps_MEM_info ===> " + ps_MEM_info) logger.debug("ps_CPU_info ===> " + ps_CPU_info) psMemCpuInfo["psmeminfo"] = format_process_info(ps_MEM_info) if psMemCpuInfo["psmeminfo"]["pid"].isdigit() and psutil.pid_exists(int(psMemCpuInfo["psmeminfo"]["pid"])): psMemCpuInfo["psmeminfo"]["name"] = psutil.Process(int(psMemCpuInfo["psmeminfo"]["pid"])).name() else: psMemCpuInfo["psmeminfo"]["name"] = "" psMemCpuInfo["pscpuinfo"] = format_process_info(ps_CPU_info) if psMemCpuInfo["pscpuinfo"]["pid"].isdigit() and psutil.pid_exists(int(psMemCpuInfo["pscpuinfo"]["pid"])): psMemCpuInfo["pscpuinfo"]["name"] = psutil.Process(int(psMemCpuInfo["pscpuinfo"]["pid"])).name() else: psMemCpuInfo["pscpuinfo"]["name"] = "" except Exception, exception: logger.error("PsCpuMemInfo error! ===> " + exception.message) return Response(psMemCpuInfo, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get_udp_info(cls): """ get udp info :return: """ udp_info = {} try: logger.debug("udp begin:======>") rtinfo = functions.launchcmd('sar -n UDP 1 1').readlines() logger.debug("commands finished! ") if rtinfo: continue_falg = True for item in rtinfo: if continue_falg: if "idgm" in item: continue_falg = False continue tmplist = item.split() udp_info["time"] = tmplist[0] udp_info["idgm/s"] = tmplist[1] udp_info["odgm/s"] = tmplist[2] udp_info["noport/s"] = tmplist[3] udp_info["idgmerr/s"] = tmplist[4] break except IOError, e: logger.error(e.message, e) return False, udp_info
def change_type(self, var): val_list = var.split('-') if len(val_list) == 1: log.debug(str(int(val_list[0]))) return str(int(val_list[0])) else: return str(int(val_list[0])) + '-' + str(int(val_list[1]))
def get_throughput_info(cls): """ get throughput info :return: """ throughput_list = {} try: logger.debug("throughput begin:======>") rtinfo = functions.launchcmd('sar -n ALL 1 1').readlines() logger.debug("commands finished! ") if rtinfo: continue_falg = True falg = True for item in rtinfo: throughput_info = {} if continue_falg: if "IFACE" in item: continue_falg = False continue if "rxerr" in item: falg = False continue if "call" in item: break tmplist = item.split() if tmplist is None or tmplist == []: continue if falg: throughput_info["time"] = tmplist[0] throughput_info["name"] = tmplist[1] throughput_info["rxpck/s"] = tmplist[2] throughput_info["txpck/s"] = tmplist[3] throughput_info["rxkB/s"] = tmplist[4] throughput_info["txkB/s"] = tmplist[5] throughput_info["rxcmp/s"] = tmplist[6] throughput_info["txcmp/s"] = tmplist[7] throughput_info["rxmcst/s"] = tmplist[8] throughput_list[tmplist[1]] = throughput_info else: throughput_info = throughput_list[tmplist[1]] throughput_info["rxerr/s"] = tmplist[2] throughput_info["txerr/s"] = tmplist[3] throughput_info["coll/s"] = tmplist[4] throughput_info["rxdrop/s"] = tmplist[5] throughput_info["txdrop/s"] = tmplist[6] throughput_info["txcarr/s"] = tmplist[7] throughput_info["rxfram/s"] = tmplist[8] throughput_info["rxfifo/s"] = tmplist[9] throughput_info["txfifo/s"] = tmplist[10] throughput_list[tmplist[1]] = throughput_info except IOError, e: logger.error(e.message, e) return False, throughput_list
def get(self, request, format=None): percent = [] rtn_info = mem_history.objects.order_by("id") for info in rtn_info: percent.append(info.percent) logger.debug(percent) return Response({"mem_percent": percent}, status=status.HTTP_200_OK)
def get(self, request, format=None): tps_list = [] rtn_info = disk_history.objects.order_by("id") for info in rtn_info: tps_list.append({"tpsnum": info.tpsnum}) logger.debug(tps_list) return Response(tps_list, status=status.HTTP_200_OK)
def get(self, request, format=None): total_history = [] rtn_info = net_histroy.objects.order_by("id") for info in rtn_info: total_history.append(info.total) logger.debug(total_history) return Response({"net_total": total_history}, status=status.HTTP_200_OK)
def get_iptrafic_info(cls): """ get iptrafic info :return: """ iptrafic_info = {} try: logger.debug("iptrafic begin:======>") rtinfo = functions.launchcmd('sar -n ALL 1 1').readlines() logger.debug("commands finished! ") if rtinfo: continue_falg = True falg = True for item in rtinfo: if continue_falg: if "irec" in item: continue_falg = False continue if "ihdrerr" in item: falg = False continue if "imsg" in item: break tmplist = item.split() if tmplist is None or tmplist == []: continue if falg: iptrafic_info["time"] = tmplist[0] iptrafic_info["irec/s"] = tmplist[1] iptrafic_info["fwddgm/s"] = tmplist[2] iptrafic_info["idel/s"] = tmplist[3] iptrafic_info["orq/s"] = tmplist[4] iptrafic_info["asmrq/s"] = tmplist[5] iptrafic_info["asmok/s"] = tmplist[6] iptrafic_info["fragok/s"] = tmplist[7] iptrafic_info["fragcrt/s"] = tmplist[8] else: iptrafic_info["ihdrerr/s"] = tmplist[1] iptrafic_info["iadrerr/s"] = tmplist[2] iptrafic_info["iukwnpr/s"] = tmplist[3] iptrafic_info["idisc/s"] = tmplist[4] iptrafic_info["odisc/s"] = tmplist[5] iptrafic_info["onort/s"] = tmplist[6] iptrafic_info["asmf/s"] = tmplist[7] iptrafic_info["fragf/s"] = tmplist[8] except IOError, e: logger.error(e.message, e) return False, iptrafic_info
def get(self, request, format=None): load_infos = [] rtn_info = cpu_history.objects.order_by("id") for info in rtn_info: dick_info = { "min1": info.oneminute, "min5": info.fiveminute, "min15": info.fifteenminute } load_infos.append(dick_info) logger.debug(load_infos) return Response(load_infos, status=status.HTTP_200_OK)
def agentHeartBeat(self): logger.debug("agent heart beat begin!") self.scheduler.add_interval_job(self.heartBeatWithServer, seconds=3) self.scheduler.start() print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try: while True: if not utils.httpdstate: sys.exit(0) except KeyboardInterrupt: self.scheduler.shutdown() finally: self.connection.close()
def get(self, request, format=None): keys = ["all"] if request.GET.get("keys"): keys = request.GET["keys"].replace("[", "").replace("]", "").replace( "\"", "").split(",") logger.debug(keys) rtn_flag, rtn_page_info = MemBusiness.get_page_info(keys) if rtn_flag: return Response({"pageinfo": rtn_page_info}, status=status.HTTP_200_OK) else: return Response(util.getResult(message.RESULT_TYPE_ERROR, message.RESULT_TYPE_ERROR), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get_tcp_info(cls): """ get tcp info :return: """ tcp_info = {} try: logger.debug("tcp begin:======>") rtinfo = functions.launchcmd('sar -n ALL 1 1').readlines() logger.debug("commands finished! ") if rtinfo: continue_falg = True falg = True for item in rtinfo: if continue_falg: if "active" in item: continue_falg = False continue if "atmptf" in item: falg = False continue if "idgm" in item: break tmplist = item.split() if tmplist is None or tmplist == []: continue if falg: tcp_info["time"] = tmplist[0] tcp_info["active/s"] = tmplist[1] tcp_info["passive/s"] = tmplist[2] tcp_info["iseg/s"] = tmplist[3] tcp_info["oseg/s"] = tmplist[4] else: tcp_info["atmptf/s"] = tmplist[1] tcp_info["estres/s"] = tmplist[2] tcp_info["retrans/s"] = tmplist[3] tcp_info["isegerr/s"] = tmplist[4] tcp_info["orsts/s"] = tmplist[5] except IOError, e: logger.error(e.message, e) return False, tcp_info
def heartBeatWithServer(self): agentInfo = {'type': constr.NODE_STATUS, 'token': constr.NODE_TOKEN} flag, agentid = utils.getAgentId() if not (flag and agentid): return agentInfo['id'] = agentid logger.debug("node hb info : {}".format(str(agentInfo))) try: self.channel.basic_publish(exchange='exchangeTest', routing_key='heartBeatKey', body=json.dumps(agentInfo)) except Exception, e: logger.error("heartbeat exception: {}".format(e.message)) credentials = pika.PlainCredentials('inspur', 'inspur') flag, rabbitmq_ip = utils.getRabbitmqIp() # 这里可以连接远程IP,请记得打开远程端口 parameters = pika.ConnectionParameters(rabbitmq_ip, 5672, '/', credentials) self.connection = pika.BlockingConnection(parameters) self.channel = self.connection.channel() pass
def get(self, request, format=None): logger.debug("net begin:") netinfo = {} try: commands_net = "sar -n DEV 1 1" logger.debug(commands_net) net_info_list = subprocess.Popen(commands_net, stdout=subprocess.PIPE, shell=True).stdout.readlines() logger.debug("commands finshed!") rx = 0 tx = 0 for net_info in net_info_list: if -1 != net_info.find(":") or -1 != net_info.find(":"): if -1 == net_info.find("rxkB"): rx = rx + float(net_info.split()[4]) tx = tx + float(net_info.split()[5]) netinfo["rx"] = rx netinfo["tx"] = tx except Exception, exception: logger.error("NetIOInfo error ===> " + exception.message) return Response(netinfo, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get_swap_info(cls, keys): """ get swap info :param keys: the items :return: """ swap_memory = {} try: rtinfo = psutil.swap_memory() if rtinfo: logger.debug("--------get swap memory info begin-------") rtinfo_list = str(rtinfo).replace("sswap(", "").replace(")", "").split(",") pg_in_out = functions.launchcmd('sar -W 1 1').readlines() logger.debug("cmd:sar -W 1 1 finished") for item in rtinfo_list: items = item.split("=") if ("all" in keys or [] == keys) and strip( items[0]) in ("total", "used", "free"): swap_memory[strip(items[0])] = strip(items[1]) elif strip(items[0]) in keys: swap_memory[strip(items[0])] = strip(items[1]) if "all" in keys or [] == keys: swap_memory["pswpin"] = pg_in_out[-1].split()[1] swap_memory["pswpout"] = pg_in_out[-1].split()[2] for key in keys: if key == "pswpin": swap_memory["pswpin"] = pg_in_out[-1].split()[1] elif key == "pswpout": swap_memory["pswpout"] = pg_in_out[-1].split()[2] except IOError: return False, swap_memory return True, swap_memory
def get_numastat_info(cls): """ get numastat info :return: """ numastat_list = [] numastat = [] try: rtninfo = functions.launchcmd('numastat').readlines() for item in rtninfo: items = item.split() numastat.append(items) logger.debug(numastat) logger.debug(len(numastat[0])) for i in range(0, len(numastat[0])): numastat_info = {} logger.debug(i) numastat_info["name"] = numastat[0][i] numastat_info["numa_hit"] = numastat[1][i + 1] numastat_info["numa_miss"] = numastat[2][i + 1] numastat_info["numa_foreign"] = numastat[3][i + 1] numastat_info["interleave_hit"] = numastat[4][i + 1] numastat_info["local_node"] = numastat[5][i + 1] numastat_info["other_node"] = numastat[6][i + 1] numastat_list.append(numastat_info) # numastat_info["numastat_list"] = numastat_list except IOError: return False, numastat_list return True, numastat_list
def get_slab_info(cls, name, keys): """ get slab info :param keys: the items :return: """ slab_list = [] try: logger.debug("slabtop begin:======>") rtinfo = functions.launchcmd('slabtop -o').readlines() logger.debug("commands finished! length=") logger.debug(len(rtinfo)) if rtinfo: continue_falg = True if "" == name: for item in rtinfo: slab_info = {} if continue_falg: if "OBJS" in item: continue_falg = False continue slab_info["name"] = item.split()[7] slab_info["objs"] = item.split()[0] slab_info["slabs"] = item.split()[4] slab_info["obj_slab"] = item.split()[5] slab_info["active"] = item.split()[1] slab_info["obj_size"] = item.split()[3] slab_info["use"] = item.split()[2] slab_list.append(slab_info) return True, slab_list else: slab_info = {} slab_info_temp = {} for item in rtinfo[7:]: if name == item.split()[7]: slab_info_temp["name"] = item.split()[7] slab_info_temp["objs"] = item.split()[0] slab_info_temp["slabs"] = item.split()[4] slab_info_temp["obj_slab"] = item.split()[5] slab_info_temp["active"] = item.split()[1] slab_info_temp["obj_size"] = item.split()[3] slab_info_temp["use"] = item.split()[2] break if "all" in keys: slab_list.append(slab_info_temp) return True, slab_info_temp for key in keys: slab_info[key] = slab_info_temp[key] return True, slab_info except IOError: return False, slab_list return True, slab_list
def view_log_detail(cls, limit, offset, desc, search): """ get file detail :param limit: page size :param offset: page start :param desc: :param search: saerch :return: file detail """ logger.debug("init view log detail") p = settings.LOG_ROOT_PATH + settings.KERNEL_LOG_NAME p = p.encode(settings.DEFAULT_CHARSET) f = open(p, 'rb') if desc: count, page_list = cls.log_kdesc_detail(f, limit, offset, search) else: count, page_list = cls.log_kdetail(f, limit, offset, search) result = {"total": count, "rows": page_list} if f: f.close() return result
def getinfo(to, api, domain, port, method='GET'): """ use httplib get api data :param to: :param api: :param domain: :param port: :param method :return: """ httpclient = None obj = None if not httpdstate(): # the server already down return obj try: httpclient = httplib.HTTPConnection(domain, port, timeout=to) httpclient.request(method, api) # response is a HTTPResponse object response = httpclient.getresponse() if response.status == 200: obj = json.loads(response.read()) else: logger.debug('method:getinfo -- {} {} {} {}'.format( method, api, str(response.status), response.reason)) except Exception: error_msg0 = str(sys.exc_info()[0]) error_msg1 = str(sys.exc_info()[1]) logger.error("method:getinfo -- {}:{}{} {}{}".format( domain, str(port), api, error_msg0, error_msg1)) finally: if httpclient: httpclient.close() return obj
def register_agent(self): if self.flag: api = "{}".format('/server/api/register/') # api = "{}".format('/api/resources/') # for test nodeinfo = { constr.ID: str(self.server_info["AGENTID"]), constr.ALIAS: self.server_info["ALIAS"], constr.PORT: 8000, constr.TOKEN: constr.NODE_TOKEN, constr.STATUS: constr.RUNNING } params = json.dumps(nodeinfo) obj = utils.postinfo(20, api, self.server_info["IP"], self.server_info["PORT"], params, method='POST') if str(obj) == '200' or str(obj) == '204': logger.debug("register_agent SUCESS!") hb = heartBeat.HeartBeat() hb.agentHeartBeat() return True else: logger.debug("register_agent failed! ==> {}".format(str(obj))) time.sleep(20) self.register_agent() else: logger.error("agent register fialed! : ".format( str(self.server_info))) return False
def get(self, request, format=None): disk_ps_info = {} try: logger.debug("diskinfo begin--------------") net_info_list = functions.launchcmd('iotop -P -b -n 1 | head -n 4').readlines() logger.debug("net_info_list -1 ====== " + net_info_list[-1]) disk_ps_info["psdiskinfo"] = net_info_list[-1].split()[0] logger.debug("psdiskinfo ===> " + disk_ps_info["psdiskinfo"]) if disk_ps_info["psdiskinfo"].isdigit() and psutil.pid_exists(int(disk_ps_info["psdiskinfo"])): disk_ps_info["name"] = psutil.Process(int(disk_ps_info["psdiskinfo"])).name() else: disk_ps_info["name"] = "" except Exception, exception: logger.error("DiskIOInfo error!===>" + exception.message) return Response(disk_ps_info, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def update_stats(self, request, itemid): """ update data :param request: :param itemid: :return: """ log.debug(constant.LOG_START_SYSFILE + itemid) if constant.SYSFILE_BASIC_URL == itemid: stats_sysfs = self._plugin_sysfs.update() log.debug(constant.LOG_END_SYSFILE) if stats_sysfs is not None: return True, stats_sysfs else: return False, "can not get data." elif "glances" == itemid: stats_fs = self._plugin_fs.update() log.debug(constant.LOG_END_SYSFILE) return True, stats_fs else: log.error(constant.EXCEPT_URL_ERROR + itemid) return False, constant.EXCEPT_URL_ERROR
def process_request(self, request): ''' process the request object from upper middleware the request with valid authcode will be allowed to access resource, if without valid authcode, but with valid username and password will be allowed, otherwise access will be denied. ''' request.isPermissioned = True session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) current_time = time.time() # delete expire auth deleteExpireAuth(session_key, current_time) # fetch new auth permission = KSMP_Permission() pms = permission.get_permission(session_key) authlist = permission.get_all_auth() data = {} urls = [ '/login/validatecode/', '/monitor/cpu/load/', '/monitor/mem/', '/resources/psnetinfo/', '/monitor/disk/io/' ] if pms in authlist: permission.save_to_auth(pms, current_time) elif letpass(urls, request.path): pass else: if request.method == "GET": data = request.GET.copy() elif request.method == "POST": data = request.POST try: username = data['username'] password = data['password'] except KeyError: request.session.flush() raise PermissionDenied else: username = functions.unicorn_decrypt(username) password = functions.unicorn_decrypt(password) if not isrootuser(username): request.isPermissioned = False raise PermissionDenied # try_choices = request.COOKIES.get(settings.TRY_TIMES # check whether is existed or not. logger.debug(username) if not userStatus.isUserExist(username): userStatus.initUserStatus(username) logger.debug(username + 'check exist') # check whether username is locked # check access_time is over age. if both yes, raise denied if userStatus.isUserLocked(username): # request.isLocked = True # check whether lock is over age. logger.debug(username + 'check locked') lastAccessTime = userStatus.getLastAccessTime(username) if lastAccessTime and (current_time - float(lastAccessTime) < settings.LOCKEDAGE): request.isPermissioned = False # request.isLocked = True raise PermissionDenied # check username and password. if right, get permissed and reset username's status if isvalidpassword(username, password): authcode = permission.set_permission(username) request.session._session_key = authcode userStatus.initUserStatus(username) # raise deny. update access_time and try_times++ else: request.isPermissioned = False logger.debug(username + 'update status') userStatus.updateUserStatus(username=username, access_time=current_time) raise PermissionDenied