def linux(self, handler, hostname): """ 获取linux网卡信息 1. 如果是debug模式,则读取本地文件处理交给self._interfaces_ip进行处理 2. 如果是不是debug模式,则调用self.linux_interface方法传递handler进行处理 3. 把处理交过交给self.standard解析返回 :param handler: :param hostname: :return: """ result = BaseResponse() try: if self.debug: output = open(os.path.join(self.base_dir, 'files/nic.out'), 'r').read() interfaces_info = self._interfaces_ip(output) else: interfaces_info = self.linux_interfaces(handler, hostname) result.data = interfaces_info self.standard(interfaces_info) except Exception as e: msg = traceback.format_exc() result.status = False result.error = msg log.error(msg) return result.dict
def linux(self, handler, hostname): """ 获取linux内存信息 1. 如果是debug模式,则读取本地文件处理 2. 如果是不是debug模式,则执行对象的命令 1. 拿到采集结果交给parse进行格式化处理 :param handler: 采集器引擎 :param hostname: 需要采集的主机 :return: self.parse(output) """ result = BaseResponse() try: if self.debug: output = open(os.path.join(self.base_dir, 'files/memory.out'), 'r').read() else: shell_command = "dmidecode -q -t 17 2>/dev/null" output = handler.cmd(shell_command, hostname) result.data = self.parse(output) except Exception as e: msg = traceback.format_exc() result.status = False result.error = msg log.error(msg) return result.dict
def linux(self): response = BaseResponse() try: if self.test_mode: from config.settings import BASEDIR output = open(os.path.join(BASEDIR, 'files/nic.out'), 'r').read() interfaces_info = self._interfaces_ip(output) else: interfaces_info = self.linux_interfaces() self.standard(interfaces_info) response.data = interfaces_info except Exception as e: msg = "%s linux nic plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def linux(self): response = BaseResponse() try: uptime = int(time.time()) - int(psutil.boot_time()) nicflow = psutil.net_io_counters() nicinfo = { "kb_sent": round(Bytes_KB(nicflow.bytes_sent) / uptime, 2), "kb_recv": round(Bytes_KB(nicflow.bytes_recv) / uptime, 2), "packets_sent": nicflow.packets_sent // uptime, "packets_recv": nicflow.packets_recv // uptime, "device": {} } for k, v in psutil.net_if_addrs().items(): if v[0].address and v[0].netmask and v[0].broadcast: nicinfo['device'].setdefault( k, { "name": k, "address": v[0].address, "netmask": v[0].netmask, "broadcast": v[0].broadcast }) response.data = nicinfo except Exception as e: msg = "%s linux NicPlugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def linux(self): response = BaseResponse() try: # 物理个数 2 cpu_physical_count = psutil.cpu_count(logical=False) # 逻辑个数 4 cpu_core_count = psutil.cpu_count() # 负载 (0.0, 0.0, 0.0) cpu_load_tuple = psutil.getloadavg() #cpu 总的cpu使用率 0.8 cpu_total_util = psutil.cpu_percent(interval=1) response.data = {"cpu_physical_count":cpu_physical_count, "cpu_core_count":cpu_core_count, "cpu_load_tuple":cpu_load_tuple, "cpu_total_util":cpu_total_util} except Exception as e: msg = "%s linux CpuPlugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def run(self): response = BaseResponse() try: ret = {} temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.4.1.674.10892.5.4.1100.90.1.2.1' % (settings.community_name, self.manager_ip)) for line in temp.split('\n'): line = line.strip('"') snmp_num = line.split()[0].split('.')[-1] solt = line.split()[-1].split(':')[-1] ret[solt] = {'macaddress': None} tmp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.4.1.674.10892.5.4.1100.90.1.15.1.%s' % (settings.community_name, self.manager_ip, snmp_num)) macaddress = tmp.split(':')[-1].strip() ret[solt]['macaddress'] = macaddress response.data = ret except Exception as e: msg = "%s dell nic plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def get_serverinfo(self): response = BaseResponse() try: #内存总大小 memory_total_size = round( psutil.virtual_memory().total / 1024 / 1024 / 1024, 2) #交换内存总大小 swap_total_size = round( psutil.swap_memory().total / 1024 / 1024 / 1024, 2) #cpu核数 cpu_cores = psutil.cpu_count() #网卡信息 net_list = [] for name, v in psutil.net_if_addrs().items(): ip = v[0].address netmask = v[0].netmask broadcast = v[0].broadcast if all((ip, netmask, broadcast)): net_list.append({ "name": name, "ip": ip, "netmask": netmask, "broadcast": broadcast }) #磁盘信息 disk_list = [] for disk in psutil.disk_partitions(): disk_list.append({ "name": disk.device, "total_size": round( psutil.disk_usage(disk.mountpoint).total / 1024 / 1024 / 1024, 2) }) #主机运行状态 status = 'running' ##目前是写死的 后期得考虑如何检测主机是否在运行中 serverinfo_dict = { "flag": "0", "hostname": self.hostname, "version": self.version, "cpu_cores": cpu_cores, "memory_total_size": memory_total_size, "swap_total_size": swap_total_size, "net_list": net_list, "disk_list": disk_list, "status": status } response.data = serverinfo_dict except Exception as e: msg = "主机-%s-%s抓取系统信息失败,class ServerInfo出错,%s" self.logger.log( msg % (self.hostname, self.ip, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, self.ip, traceback.format_exc()) return response
def execute(self,host): resp = BaseResponse() try: resp.data = getattr(self,self.mode)(host) except Exception as e: resp.status = False resp.error = e return resp
def response_msg(msg): resp = BaseResponse(msg) if msg["Content"].strip().startswith("#"): msg_data = {"openid": msg["FromUserName"], "msg": msg["Content"]} requests.post(settings.COMPANY_URL["customer"]["POST_TO_ADMIN"], data=msg_data) context = {"Content": "客服消息已经发送,请稍后"} context = {"Content": "如果在消息前面加上#,那么我就可以回复客服消息"} return resp.get_response(context)
def linux(self): response = BaseResponse() try: per_part_info_dict = {} for partinfo in psutil.disk_partitions(): k = str(partinfo.device) per_part_info_dict.setdefault( k, { 'device': partinfo.device, 'mountpoint': partinfo.mountpoint, 'fstype': partinfo.fstype }) for k, v in per_part_info_dict.items(): total_GB = Bytes_GB( psutil.disk_usage(v.get('mountpoint')).total) used_GB = Bytes_GB(psutil.disk_usage(v.get('mountpoint')).used) percent = psutil.disk_usage(v.get('mountpoint')).percent per_part_info_dict[k].setdefault('total_GB', total_GB) per_part_info_dict[k].setdefault('used_GB', used_GB) per_part_info_dict[k].setdefault('percent', percent) dic = {} for k, v in psutil.disk_io_counters(perdisk=True).items(): per_disk_read_count = v.read_count per_disk_write_count = v.write_count per_disk_read_kb = Bytes_KB(v.read_bytes) per_disk_write_kb = Bytes_KB(v.write_bytes) per_disk_read_time_s = ms_s(v.read_time) per_disk_write_time_s = ms_s(v.write_time) try: dic.setdefault( k, { 'r/s': per_disk_read_count // per_disk_read_time_s, 'w/s': per_disk_write_count // per_disk_write_time_s, 'rKB/s': per_disk_read_kb // per_disk_read_time_s, 'wKB/s': per_disk_write_kb // per_disk_write_time_s, }) except Exception as e: pass for k, v in per_part_info_dict.items(): new_k = ''.join(re.findall('[a-z]{3}\d', k)) per_part_info_dict[k].update(dic[new_k]) response.data = per_part_info_dict except Exception as e: msg = "%s linux DiskPlugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def get_servermoni(self): response=BaseResponse() try: # 系统负载 load_5m = round(psutil.getloadavg()[0], 3) load_10m = round(psutil.getloadavg()[1], 3) load_15m = round(psutil.getloadavg()[2], 3) # cpu利用率 cpu_util = psutil.cpu_percent(interval=1) # 内存利用率 memory_util = psutil.virtual_memory().percent # swap利用率 swap_util = psutil.swap_memory().percent # 磁盘流量 import re disk_list=[] for disk in psutil.disk_partitions(): name = re.findall('\w+', disk.device)[1] space_util = psutil.disk_usage(disk.mountpoint).percent obj = psutil.disk_io_counters(perdisk=True).get(name) if obj: rmbs = round(obj.read_bytes / 1024 / 1024 / obj.read_time, 3) wmbs = round(obj.write_bytes / 1024 / 1024 / obj.write_time, 3) else: rmbs = 0 wmbs = 0 disk_list.append({"name":name,"space_util":space_util,"rmbs":rmbs,"wmbs":wmbs}) # 网卡流量 uptime = int(psutil.boot_time()) #系统运行时间 sentmbs = round(psutil.net_io_counters().bytes_sent / 1024 / 1024 / uptime, 3) recvmbs = round(psutil.net_io_counters().bytes_recv / 1024 / 1024 / uptime, 3) servermoni_dict={ "flag":"1", "load":{ "load_5m":load_5m, "load_10m":load_10m, "load_15m":load_15m }, "cpu_util":cpu_util, "memory_util":memory_util, "swap_util":swap_util, "disk_list":disk_list, "net_flow":{ "sentmbs":sentmbs, "recvmbs":recvmbs }, "uptime":uptime, } response.data=servermoni_dict except Exception as e: msg = "主机-%s抓取系统监控信息失败,class ServerMoni出错,%s" self.logger.log(msg % (self.hostname,self.ip, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname,self.ip, traceback.format_exc()) return response
def response_msg(msg): resp = BaseResponse(msg) if msg['Content'].strip().startswith('#'): msg_data = {'openid': msg['FromUserName'], 'msg': msg['Content']} requests.post(settings.COMPANY_URL['customer']['POST_TO_ADMIN'], data=msg_data) context = {'Content': '客服消息已经发送,请稍后'} context = {'Content': '如果在消息前面加上#,那么我就可以回复客服消息'} return resp.get_response(context)
def linux(self, handler, hostname): response = BaseResponse() try: output = handler.cmd(command="sudo dmidecode -t1", hostname=None) response.data = self.parse(output) except Exception as e: msg = traceback.format_exc() response.status = False response.error = msg logger.error(msg) return response.dict
def linux(self, handler, hostname): response = BaseResponse() try: output = handler.cmd(command="cat /proc/cpuinfo", hostname=None) response.data = self.parse(output) except: msg = traceback.format_exc() response.status = False response.error = msg logger.error(msg) return response.dict
def run(self): response = BaseResponse() try: shell_command = 'free -m|grep Mem' output = self.exec_shell_cmd(shell_command) response.data = self.parse(output) # Mem: 988 825 163 3 124 312 except Exception as e: msg = "%s dell memory plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def run(self): response = BaseResponse() try: shell_command = 'df -Pkl|grep -v Filesystem|grep -v "文件系统"' output = self.exec_shell_cmd(shell_command) response.data = self.parse(output) except Exception as e: msg = "%s dell disk plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def linux(self): response = BaseResponse() try: stdin, stdout, stderr = self.ssh.exec_command('df -hT') output = stdout.read().decode('gbk').strip() response.data = self.parse(output) except Exception as e: msg = "%s linux cpu plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def linux(self, handler, hostname): response = BaseResponse() try: output = handler.cmd( command="/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aALL", hostname=None) response.data = self.parse(output) except: msg = traceback.format_exc() response.status = False response.error = msg logger.error(msg) return response.dict
def run(self): response = BaseResponse() try: shell_command = 'cat /proc/cpuinfo' output = self.exec_shell_cmd(shell_command) response.data = self.parse(output) except Exception as e: msg = "%s dell cpu plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def get_server_info(): """获取服务器各种信息""" response = BaseResponse() try: server_info = {} disk = DiskPlugin() mem = MemoryPlugin() board = MotherboardPlugin() nic = NicPlugin() cpu = CpuPlugin() hostname = board.os_hostname() sn = board.sn() server_info['os_platform'] = board.os_platform() server_info['os_version'] = board.os_version() # 获取主板信息 board_info = board.execute() server_info.update(board_info) # 获取CPU信息 cpu_info = cpu.execute() server_info.update(cpu_info) # 获取磁盘信息 server_info['disk'] = disk.execute() if not server_info['disk'].get('status'): log.write_error_log('[%s][disk],%s' % (hostname, server_info['disk'].get('error',"数据获取失败"))) if server_info['disk'].get('error'): del server_info['disk']['error'] # 获取内存信息 server_info['memory'] = mem.execute() if not server_info['memory'].get('status'): log.write_error_log('[%s][memory],%s' % (hostname, server_info['memory'].get('error',"内存数据获取失败"))) if server_info['memory'].get('error'): del server_info['memory']['error'] # 获取网卡信息 server_info['nic'] = nic.execute() if not server_info['nic'].get('status'): log.write_error_log('[%s][nic],%s' % (hostname, server_info['nic'].get('error',"网卡数据获取失败"))) if server_info['nic'].get('error'): del server_info['nic']['error'] response.data = server_info response.status = True except Exception as e: response.message = str(e) return response
def order(msg): post_data = {"openid": msg['FromUserName']} is_binding = requests.post(settings.COMPANY_URL['bind']['IS_BINDING'], data=post_data) is_binding = json_loads(is_binding.text) if is_binding['success']: order_json = json_loads( requests.post(settings.COMPANY_URL['bind']['ORDER_LIST'], data=post_data)) context = {"Content": order_json} return BaseResponse(msg).get_response(context) else: context = {"openid": msg['FromUserName']} return BaseResponse(msg, "login_notify.xml").get_response(context)
def linux(self, handler, hostname=None): response = BaseResponse() try: if self.debug: output = open(os.path.join(self.base_dir, 'files', 'cpuinfo.out'), 'r').read() else: shell_command = "cat /proc/cpuinfo" output = handler.cmd(shell_command, hostname).decode('utf-8') response.data = self.parse(output) except Exception as e: msg = traceback.format_exc() response.status = False response.error = msg return response.dict
def linux(self): response = BaseResponse() try: interfaces_info = self.linux_interfaces() self.standard(interfaces_info) response.data = interfaces_info except Exception as e: msg = "%s linux nic plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def run(self): response = BaseResponse() try: response.data = { 'sn': None, 'fast_server_number': None, 'model': None } temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.4.1.674.10892.2.1.1.11.0' % (settings.community_name, self.manager_ip)) sn = temp.split('"')[-2] response.data['sn'] = sn temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.4.1.674.10892.5.1.3.3.0' % (settings.community_name, self.manager_ip)) fast_server_number = temp.split('"')[-2] response.data['fast_server_number'] = fast_server_number temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.4.1.674.10892.5.1.3.12.0' % (settings.community_name, self.manager_ip)) model = temp.split('"')[-2] response.data['model'] = model except Exception as e: msg = "%s dell mainboard plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def pack(host): package = {} for plugin, v in settings.plugins.items(): try: path, cls = v.rsplit(".", 1) p = importlib.import_module(path) data = getattr(p, cls)().execute(host) package[plugin] = data except Exception as e: resp = BaseResponse() resp.status = False resp.error = e.__str__() package[plugin] = resp return package
def run(self): response = BaseResponse() try: response.data = { 'cpu_model': None, 'cpu_physical_count': 0, 'cpu_count': 0 } temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.4.1.674.10892.5.4.1100.30.1.23.1' % (settings.community_name, self.manager_ip)) cpu_model = temp.split('"')[1] response.data['cpu_model'] = cpu_model temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.4.1.674.10892.5.4.1100.30.1.23.1|wc -l' % (settings.community_name, self.manager_ip)) cpu_physical_count = int(temp) response.data['cpu_physical_count'] = cpu_physical_count temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.4.1.674.10892.5.4.1100.30.1.18.1' % (settings.community_name, self.manager_ip)) cpu_count = 0 for line in temp.split('\n'): cpu_count += int(line.split(':')[-1]) response.data['cpu_count'] = cpu_count except Exception as e: msg = "%s dell cpu plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def get_server_info(): response = BaseResponse() try: server_info = {} diskObj = DiskPlugin() memObj = MemoryPlugin() boardObj = MotherboardPlugin() nicObj = NicPlugin() cpuObj = CpuPlugin() collect_hostname = boardObj.os_hostname() hostname = collect_hostname # if hostname != collect_hostname: # raise Exception( # 'hostname inconformity :task hostname is %s,collect hostname is %s' % (hostname, collect_hostname)) server_info['os_platform'] = boardObj.os_platform() server_info['os_version'] = boardObj.os_version() board_info = boardObj.execute() server_info.update(board_info) cpu_info = cpuObj.execute() server_info.update(cpu_info) server_info['disk'] = diskObj.execute() if not server_info['disk']['status']: log.write_error_log('[%s][disk],%s' % (hostname, server_info['disk']['error'])) del server_info['disk']['error'] server_info['memory'] = memObj.execute() if not server_info['memory']['status']: log.write_error_log('[%s][memory],%s' % (hostname, server_info['memory']['error'])) del server_info['memory']['error'] server_info['nic'] = nicObj.execute() if not server_info['nic']['status']: log.write_error_log('[%s][nic],%s' % (hostname, server_info['nic']['error'])) del server_info['nic']['error'] response.data = server_info response.status = True except Exception as e: response.message = str(e) return response
def run(self): response = BaseResponse() try: response.data = { 'sn': None, 'port_number': 0, 'device_name': None, 'model': None, 'basic_info': None } temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.2.1.47.1.1.1.1.11' % (settings.community_name, self.manager_ip)) temp_list = temp.split('\n') for item in temp_list: if item.split('"')[1]: sn = item.split('"')[1].strip() response.data['sn'] = sn port_number = response.data['port_number'] temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.2.1.2.2.1.2' % (settings.community_name, self.manager_ip)) temp_list = temp.split('\n') for item in temp_list: if re.match('GigabitEthernet', item.split(':')[-1].strip()): port_number += 1 response.data['port_number'] = port_number temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.2.1.1.5.0' % (settings.community_name, self.manager_ip)) device_name = temp.split(':')[-1].strip() response.data['device_name'] = device_name temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.2.1.47.1.1.1.1.2.2' % (settings.community_name, self.manager_ip)) model = temp.split('"')[1].split()[1].strip() response.data['model'] = model temp = self.exec_shell_cmd( 'snmpwalk -v 2c -c %s %s .1.3.6.1.2.1.1.1.0' % (settings.community_name, self.manager_ip)) basic_info = temp.split(':')[-1].strip() response.data['basic_info'] = basic_info except Exception as e: msg = "%s h3c switch plugin error: %s" self.logger.log(msg % (self.hostname, traceback.format_exc()), False) response.status = False response.error = msg % (self.hostname, traceback.format_exc()) return response
def linux(self, handler, hostname): response = BaseResponse() try: output = handler.cmd( command="sudo dmidecode -q -t 17 2>/dev/null", hostname=None) response.data = self.parse(output) except: msg = traceback.format_exc() response.status = False response.error = msg logger.error(msg) for k, v in response.data.items(): if v['capacity'] == "No": del response.data[k] return response.dict
def linux(self, handler, hostname): response = BaseResponse() try: ifaces = dict() cmd1 = handler.cmd(command='ip link show',hostname=None) cmd2 = handler.cmd(command='ip addr show',hostname=None) interfaces_info = self._interfaces_ip(cmd1 + '\n' + cmd2) self.standard(interfaces_info) response.data = interfaces_info except Exception as e: msg = traceback.format_exc() response.status = False response.error = msg logger.error(msg) return response.dict
def linux(self, handler, hostname): response = BaseResponse() try: if self.debug: output = open(os.path.join(self.base_dir, 'files', 'memory.out'), 'r').read() else: shell_command = "sudo dmidecode -q -t 17 2>/dev/null" output = handler.cmd(shell_command, hostname) response.data = self.parse(output) except Exception as e: msg = traceback.format_exc() response.status = False response.error = msg logger.error(msg) return response.dict
def about(msg): resp = BaseResponse(msg, "about.xml") return resp.get_response()
def contact(msg): resp = BaseResponse(msg, "contact.xml") return resp.get_response()