博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CMDB学习之七-实现采集错误捕捉,日志信息处理
阅读量:5075 次
发布时间:2019-06-12

本文共 7218 字,大约阅读时间需要 24 分钟。

首先采集disk的具体实现方上代码:

# !/usr/bin/env python# -*- coding:utf-8 -*-from .base import BasePluginimport os,reimport tracebackfrom lib.response import BaseReponseclass Disk(BasePlugin):    def win(self,handler,hostname):        '''        执行命令拿到结果-磁盘        :return:        '''        print("执行win方法")        ret = handler.cmd('wmic diskdrive',hostname)[0:10]        return ret    # def linux(self,handler,hostname):    #     '''    #     执行命令拿到结果-磁盘    #     :return:    #     '''    #     print("执行Linux方法")    #    #     ret = handler.cmd('df -h',hostname)[0:10]    #     return ret    def linux(self, handler, hostname):        '''        采集数据        :param handler:        :param hostname:        :return:        '''        #实现错误信息记录,标记,post 提交到服务端,首先定义一个字典如下:        # result = {'status':True,'error':None,'data':None}        #字典的形式使用比较麻烦,所有这里在lib 文件中定义一个类来使用,看起比较高端些,使用方法是        '''        这里是在 lib 文件实现的,使用的时候记得导入模块        class BaseReponse():    def __init__(self):        self.status = True        self.error = None        self.data = None    #调用内部方法 静态方法属性    @property    def dict(self):        return self.__dict__        '''        reponse = BaseReponse()        try :            if self.debug:                output = open(os.path.join(self.base_dir, 'files', 'disk.out'), 'r').read()            else:                shell_command = "sudo MegaCli  -PDList -aALL" #根据执行的命令                output = handler.cmd(shell_command, hostname)            reponse.data = self.parse(output)        except Exception as e:            error_msg = traceback.format_exc()            reponse.status = False            reponse.error = error_msg        return reponse.dict    def parse(self, content):        """        解析shell命令返回结果        :param content: shell 命令结果        :return:解析后的结果        """        response = {}        result = []        for row_line in content.split("\n\n\n\n"):            result.append(row_line)        for item in result:            temp_dict = {}            for row in item.split('\n'):                if not row.strip():                    continue                if len(row.split(':')) != 2:                    continue                key, value = row.split(':')                name = self.mega_patter_match(key)                if name:                    if key == 'Raw Size':                        raw_size = re.search('(\d+\.\d+)', value.strip())                        if raw_size:                            temp_dict[name] = raw_size.group()                        else:                            raw_size = '0'                    else:                        temp_dict[name] = value.strip()            if temp_dict:                response[temp_dict['slot']] = temp_dict        return response    @staticmethod    def mega_patter_match(needle):        grep_pattern = {
'Slot': 'slot', 'Raw Size': 'capacity', 'Inquiry': 'model', 'PD Type': 'pd_type'} for key, value in grep_pattern.items(): if needle.startswith(key): return value return False

看见截图

按照上面采集磁盘的方法,在其它的 网卡,cpu 。内存 去实现下

#######################

下面是错误日志记录 ,是loging 模块实现,思路是让每次报错的是日志信息写入到文件中

按照原来思路,把功能写到lib 目录中进行掉用

配置文件中添加一个记录文件日志的路径配置信息

 

 导入loging 到日配置文件

import loggingfrom config import settings# file_handler = logging.FileHandler('xxxxxxxx.log', 'a', encoding='utf-8')# file_handler.setFormatter(logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s:  %(message)s"))## logger = logging.Logger('s1', level=logging.INFO)# logger.addHandler(file_handler)## logger.info('1111')# logger.error('2222')class Logger:    def __init__(self):        self.path = settings.LOG_FILE_PATH        file_handler = logging.FileHandler(self.path, 'a', encoding='utf-8')        fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s")        file_handler.setFormatter(fmt)        self.logger = logging.Logger('cmdb',level=logging.INFO)        self.logger.addHandler(file_handler)    def info(self,msg):        self.logger.info(msg)    def error(self,mag):        self.logger.error(mag)logger = Logger()

 disk 采集文件实现代码如下

# !/usr/bin/env python# -*- coding:utf-8 -*-from .base import BasePluginimport os,reimport tracebackfrom lib.response import BaseReponsefrom lib.log import loggerclass Disk(BasePlugin):    def win(self,handler,hostname):        '''        执行命令拿到结果-磁盘        :return:        '''        print("执行win方法")        ret = handler.cmd('wmic diskdrive',hostname)[0:10]        return ret    # def linux(self,handler,hostname):    #     '''    #     执行命令拿到结果-磁盘    #     :return:    #     '''    #     print("执行Linux方法")    #    #     ret = handler.cmd('df -h',hostname)[0:10]    #     return ret    def linux(self, handler, hostname):        '''        采集数据        :param handler:        :param hostname:        :return:        '''        #实现错误信息记录,标记,post 提交到服务端,首先定义一个字典如下:        # result = {'status':True,'error':None,'data':None}        #字典的形式使用比较麻烦,所有这里在lib 文件中定义一个类来使用,看起比较高端些,使用方法是        '''        这里是在 lib 文件实现的        class BaseReponse():    def __init__(self):        self.status = True        self.error = None        self.data = None    #调用内部方法 静态方法属性    @property    def dict(self):        return self.__dict__        '''        reponse = BaseReponse()        try :            if self.debug:                output = open(os.path.join(self.base_dir, 'files', 'disk.out'), 'r').read()            else:                shell_command = "sudo MegaCli  -PDList -aALL" #根据执行的命令                output = handler.cmd(shell_command, hostname)            reponse.data = self.parse(output)        except Exception as e:            error_msg = traceback.format_exc()            reponse.status = False            reponse.error = error_msg            # 记录错误日志            logger.error(error_msg)        return reponse.dict    def parse(self, content):        """        解析shell命令返回结果        :param content: shell 命令结果        :return:解析后的结果        """        response = {}        result = []        for row_line in content.split("\n\n\n\n"):            result.append(row_line)        for item in result:            temp_dict = {}            for row in item.split('\n'):                if not row.strip():                    continue                if len(row.split(':')) != 2:                    continue                key, value = row.split(':')                name = self.mega_patter_match(key)                if name:                    if key == 'Raw Size':                        raw_size = re.search('(\d+\.\d+)', value.strip())                        if raw_size:                            temp_dict[name] = raw_size.group()                        else:                            raw_size = '0'                    else:                        temp_dict[name] = value.strip()            if temp_dict:                response[temp_dict['slot']] = temp_dict        return response    @staticmethod    def mega_patter_match(needle):        grep_pattern = {
'Slot': 'slot', 'Raw Size': 'capacity', 'Inquiry': 'model', 'PD Type': 'pd_type'} for key, value in grep_pattern.items(): if needle.startswith(key): return value return False

其它的采集插件依次方法实现

 

转载于:https://www.cnblogs.com/michael2018/p/10505079.html

你可能感兴趣的文章