CMDB学习之三数据采集
生活随笔
收集整理的這篇文章主要介紹了
CMDB学习之三数据采集
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
判斷系統(tǒng)因?yàn)槭枪玫姆椒?#xff0c;所有要寫(xiě)基類方法使用,首先在插件中創(chuàng)建一個(gè)基類
將插件文件繼承基類
?思路是創(chuàng)建基類使用handler.cmd ,命令去獲取系統(tǒng)信息,然后進(jìn)行判斷,然后去執(zhí)行 磁盤(pán) ,cpu,網(wǎng)卡,內(nèi)存等信息的收集;
基類代碼:
class BasePlugin:def get_os(self,handler,hostname):os = handler.cmd("查詢操作系統(tǒng)的命令",hostname)# return osreturn 'win32'def process(self,handler,hostname):os = self.get_os(handler,hostname)if os == 'win32': #測(cè)試判斷執(zhí)行win32return self.win(handler,hostname)else:return self.linux(handler,hostname)def win(self,handler,hostname):#約束派生類必須實(shí)現(xiàn)win方法raise NotImplementedError('handler() must Implemented.')def linux(self,handler,hostname):#約束派生類必須實(shí)現(xiàn)Linux方法raise NotImplementedError('handler() must Implemented.')disk.py ,cpu.py,memory.py,network.py 代碼;
from .base import BasePluginclass Disk(BasePlugin):def win(self,handler,hostname):'''執(zhí)行命令拿到結(jié)果磁盤(pán):return:'''print("執(zhí)行win方法")ret = handler.cmd('wmic diskdrive',hostname)[0:10]return retdef linux(self,handler,hostname):'''執(zhí)行命令拿到結(jié)果磁盤(pán):return:'''print("執(zhí)行Linux方法")ret = handler.cmd('df -h',hostname)[0:10]return ret from .base import BasePluginclass Memory(BasePlugin):def win(self,handler,hostname):'''執(zhí)行命令拿到結(jié)果-內(nèi)存:return:'''print("執(zhí)行win方法")ret = handler.cmd('wmic memphysical list brief',hostname)[0:10]return retdef linux(self,handler,hostname):'''執(zhí)行命令拿到結(jié)果-內(nèi)存:return:'''print("執(zhí)行Linux方法")ret = handler.cmd('free',hostname)[0:10]return ret from .base import BasePlugin class CPU(BasePlugin):def win(self,handler,hostname):'''執(zhí)行命令拿到結(jié)果-cpu:return:'''print("執(zhí)行win方法")ret = handler.cmd('wmic cpu',hostname)[0:10]return retdef linux(self,handler,hostname):'''執(zhí)行命令拿到結(jié)果-cpu:return:'''print("執(zhí)行Linux方法")ret = handler.cmd('wmic cpu',hostname)[0:10]return ret from .base import BasePlugin class Network(BasePlugin):def win(self,handler,hostname):'''執(zhí)行命令拿到結(jié)果-網(wǎng)卡:return:'''print("執(zhí)行win方法")ret = handler.cmd('ipconfig',hostname)[0:10]return retdef linux(self,handler,hostname):'''執(zhí)行命令拿到結(jié)果-網(wǎng)卡:return:'''print("執(zhí)行Linux方法")ret = handler.cmd('ifconfig',hostname)[0:10]return ret?
最后測(cè)試執(zhí)行結(jié)果
?
轉(zhuǎn)載于:https://www.cnblogs.com/michael2018/p/10419637.html
總結(jié)
以上是生活随笔為你收集整理的CMDB学习之三数据采集的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 你会不会购买掉漆的iPhone 12
- 下一篇: nplayer怎么和电脑共享(nplay