zoukankan      html  css  js  c++  java
  • 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

    最后测试执行结果

  • 相关阅读:
    [转]C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现
    C#设置System.Net.ServicePointManager.DefaultConnectionLimit,突破Http协议的并发连接数限制
    [转]WebBrowser控件禁用超链接转向、脚本错误提示、默认右键菜单和快捷键
    [转]C#打印DataGridView的例子源码
    c# TreeView 父节点选中/不选时子节点都同步选中/不选
    C#中PictureBox异步加载图片
    [转]FusionCharts 3.1 破解版 – 非常好用的Flash图表控件
    配合JavaScript拖动页面中控件
    在ThinkPad T400上安装win2003 所遇问题
    C# 抛弃MoveTo来实现文件重命名
  • 原文地址:https://www.cnblogs.com/michael2018/p/10419637.html
Copyright © 2011-2022 走看看