CMDB学习之三数据采集

判断系统因为是公用的方法,所有要写基类方法使用,首先在插件中创建一个基类

将插件文件继承基类

 思路是创建基类使用handler.cmd ,命令去获取系统信息,然后进行判断,然后去执行 磁盘 ,cpu,网卡,内存等信息的收集;

基类代码:

class BasePlugin:

    def get_os(self,handler,hostname):
        os = handler.cmd("查询操作系统的命令",hostname)
        # return os
        return 'win32'

    def process(self,handler,hostname):
        os = self.get_os(handler,hostname)
        if os == 'win32': #测试判断执行win32
            return self.win(handler,hostname)
        else:
            return self.linux(handler,hostname)

    def win(self,handler,hostname):
        #约束派生类必须实现win方法
        raise NotImplementedError('handler() must Implemented.')

    def linux(self,handler,hostname):
        #约束派生类必须实现Linux方法
        raise NotImplementedError('handler() must Implemented.')

disk.py ,cpu.py,memory.py,network.py 代码;

from .base import BasePlugin

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
from .base import BasePlugin

class Memory(BasePlugin):
    def win(self,handler,hostname):
        '''
        执行命令拿到结果-内存
        :return:
        '''
        print("执行win方法")
        ret = handler.cmd('wmic memphysical list brief',hostname)[0:10]
        return ret
    def linux(self,handler,hostname):
        '''
        执行命令拿到结果-内存
        :return:
        '''
        print("执行Linux方法")
        ret = handler.cmd('free',hostname)[0:10]
        return ret
from .base import BasePlugin
class CPU(BasePlugin):
    def win(self,handler,hostname):
        '''
        执行命令拿到结果-cpu
        :return:
        '''
        print("执行win方法")
        ret = handler.cmd('wmic cpu',hostname)[0:10]
        return ret
    def linux(self,handler,hostname):
        '''
        执行命令拿到结果-cpu
        :return:
        '''
        print("执行Linux方法")
        ret = handler.cmd('wmic cpu',hostname)[0:10]
        return ret
from .base import BasePlugin
class Network(BasePlugin):
    def win(self,handler,hostname):
        '''
        执行命令拿到结果-网卡
        :return:
        '''
        print("执行win方法")
        ret = handler.cmd('ipconfig',hostname)[0:10]
        return ret
    def linux(self,handler,hostname):
        '''
        执行命令拿到结果-网卡
        :return:
        '''
        print("执行Linux方法")
        ret = handler.cmd('ifconfig',hostname)[0:10]
        return ret

 

最后测试执行结果

 

posted @ 2019-02-22 17:52  痒乐多多  阅读(170)  评论(0编辑  收藏  举报