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

# !/usr/bin/env python
# -*- coding:utf-8 -*-
from .base import BasePlugin
import os,re
import traceback
from lib.response import BaseReponse
class 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 = ''
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 logging
from 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 BasePlugin
import os,re
import traceback
from lib.response import BaseReponse
from lib.log import logger class 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 = ''
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

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

最新文章

  1. 马虎将classname加到了id属性中,造成报错
  2. C++STL - 函数模板
  3. Windows Azure Web Site (8) 设置Web Site时区
  4. HTML5 Web 客户端五种离线存储方式汇总
  5. SQL Server SA 密码丢失无法连接数据库怎么办?
  6. 微信浏览器里location.reload问题
  7. PHP获取本周开始时间
  8. Table of Contents - Nginx
  9. 使用mp4v2将H264+AAC合成mp4文件
  10. opengl雾开启
  11. 设计模式之观察者(Observer)模式 代码详解
  12. extjs6.0点击grid一行数据显示在一端的form中
  13. (转) c# ExecuteNonQuery() 返回值 -1
  14. 《SDN核心技术剖析和实战指南》2.1交换机核心技术小结
  15. 5.6.3.4 trim()方法
  16. js--面向对象继承
  17. java 解析excel
  18. Java数组练习题小结
  19. bzoj 3331: [BeiJing2013]压力
  20. 在DataFrame数据表里面提取需要的行

热门文章

  1. Java设置Client Socket链接Server超时时间
  2. js中编码的处理
  3. String slices
  4. 安卓开发--AsyncTask
  5. BZOJ 3240 构造矩阵+矩阵快速幂
  6. BZOJ 1024 SCOI2009 生日快乐 暴搜
  7. zookeeper图形界面工具zooinspector
  8. nodejs 通过 get获取数据修改redis数据
  9. error_reporting()函数
  10. 洛谷3964 [TJOI2013]松鼠聚会