CMDB Autoclient思路分析
1.start.py里的script.run():执行run函数-->
2.script.py run方法-->
3.判断模式MODE(Agent/SSHSALT)-->
4.执行client.py里的Agent/SSHSALT-->
5.分别执行/src/plugins/__init__.py里的exec_plugin对应的
方法(__agent/__salt/__ssh),__init__.py调用settings.py里的配置文件,exec_plugin()方法拆分settings.py里的模块和类,并分别执行
plugins/目录下的basic,board,cpu,disk,memory,nic对应的py文件,这些py文件都要导入相对应的settings.py配置文件
可插拔式的插件
/bin目录
执行流程如下:
start.py 1.config.settings加入环境变量 2.整个目录加入环境变量 3.执行 script.py 1.文件导入 { 1.默认配置 2.自定义配置 } 2.Agent { 1.实例化类 2.执行execute函数-------->{1.取数据;2.发送给API} client } -->取数据{1.实例化(插件)2.执行exec.plugin函数}---------->取数据 { 1.实例化插件 2.执行process函数(执行命令方式传参数)-->结果调用parse格式化数据 3.获取结果 } 3.数据data{ basic{hostname....} cpu memory Nic disk } 获取到数据 1.获取主机名 2.判断主机文件{ 1.有没有写入 2.如果主机名不同,以文件里的主机名为主 }
sudo MegaCli -PDList -aALL
dmidecode
/bin目录:编写启动文件
import os import sys # print(os.environ)#存放当前的环境变量,这就是一个公共的环境变量,在程序的内存里放这么一个字典 os.environ['USER_SETTINGS'] = "config.settings"#往字典里添加一个值 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#获取项目所在目录的路径 sys.path.append(BASE_DIR)#添加到系统路径sys.path print(sys.path) from src import script if __name__ == '__main__': script.run() from src.plugins import PluginManager # 导入__init__.py文件 # server_info = PluginManager.exec_plugin() # for k,v in server_info.items(): # print(k,v) # server_info = PluginManager().exec_plugin() # """ # { # "cpu": {'status':True,'data': xxxxx}, # "cpu": {'status':True,'data': xxxxx}, # "cpu": {'status':True,'data': xxxxx}, # } # """ # for k,v in server_info.items(): # print(k,v) # server_info = PluginManager('c1.com').exec_plugin() # server_info = PluginManager('c1.com').exec_plugin()
/config目录,存放配置文件
# from django.conf import global_settings #Django默认配置文件 #替换全局的配置文件 """ 用户自定义配置文件 """ import os BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#BASEDIR当前路径#F:\project\auto_client USER = 'root' PWD = 'pwd' MODE = "AGENT"#SALT,SSH DEBUG = True SSH_USER = "root" SSH_PWD = "root" SSH_KEY = "/xxxx/xxxx/xxxx/"#也可以用公钥和私钥 SSH_PORT = 22 PLUGINS_DICT = { 'basic':"src.plugins.basic.Basic",#基本信息 'board':"src.plugins.board.Board",#主板 'cpu':"src.plugins.cpu.Cpu",#CPU信息 'disk':"src.plugins.disk.Disk",#硬盘信息 'memory':"src.plugins.memory.Memory",#内存信息 'nic':"src.plugins.nic.Nic",#网卡信息 }#这是关于模块路径和类名相关的配置文件 #这里跟Django的中间件很相似,但是这里是字典(字典无序),而Django的中间件是列表(列表有序,顺序执行) #根据这个配置文件找到它自己的类,并执行对应的方法 #如果导入plugins目录,会执行__init__.py文件 # API = "http://www.oldboyedu.com"#API入库的路径 API = "http://127.0.0.1:8000/api/asset.html" #API入库的路径 CERT_PATH = os.path.join(BASEDIR,'config','cert')
c2.com
/files目录:
SMBIOS 2.7 present. Handle 0x0001, DMI type 1, 27 bytes System Information Manufacturer: Parallels Software International Inc. Product Name: Parallels Virtual Platform Version: None Serial Number: Parallels-1A 1B CB 3B 64 66 4B 13 86 B0 86 FF 7E 2B 20 30 UUID: 3BCB1B1A-6664-134B-86B0-86FF7E2B2030 Wake-up Type: Power Switch SKU Number: Undefined Family: Parallels VM
processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 0 cpu cores : 6 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 0 cpu cores : 6 apicid : 32 initial apicid : 32 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 2 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 1 cpu cores : 6 apicid : 2 initial apicid : 2 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 3 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 1 cpu cores : 6 apicid : 34 initial apicid : 34 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 4 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 2 cpu cores : 6 apicid : 4 initial apicid : 4 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 5 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 2 cpu cores : 6 apicid : 36 initial apicid : 36 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 6 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 3 cpu cores : 6 apicid : 6 initial apicid : 6 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 7 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 3 cpu cores : 6 apicid : 38 initial apicid : 38 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 8 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 4 cpu cores : 6 apicid : 8 initial apicid : 8 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 9 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 4 cpu cores : 6 apicid : 40 initial apicid : 40 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 10 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 5 cpu cores : 6 apicid : 10 initial apicid : 10 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 11 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 5 cpu cores : 6 apicid : 42 initial apicid : 42 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 12 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 0 cpu cores : 6 apicid : 1 initial apicid : 1 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 13 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 0 cpu cores : 6 apicid : 33 initial apicid : 33 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 14 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 1 cpu cores : 6 apicid : 3 initial apicid : 3 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 15 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 1 cpu cores : 6 apicid : 35 initial apicid : 35 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 16 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 2 cpu cores : 6 apicid : 5 initial apicid : 5 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 17 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 2 cpu cores : 6 apicid : 37 initial apicid : 37 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 18 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 3 cpu cores : 6 apicid : 7 initial apicid : 7 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 19 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 3 cpu cores : 6 apicid : 39 initial apicid : 39 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 20 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 4 cpu cores : 6 apicid : 9 initial apicid : 9 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 21 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 4 cpu cores : 6 apicid : 41 initial apicid : 41 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 22 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 0 siblings : 12 core id : 5 cpu cores : 6 apicid : 11 initial apicid : 11 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.84 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 23 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz stepping : 4 cpu MHz : 2099.921 cache size : 15360 KB physical id : 1 siblings : 12 core id : 5 cpu cores : 6 apicid : 43 initial apicid : 43 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 4199.42 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management:
Adapter #0 Enclosure Device ID: 32 Slot Number: 0 Drive's postion: DiskGroup: 0, Span: 0, Arm: 0 Enclosure position: 0 Device Id: 0 WWN: 5000C5007272C288 Sequence Number: 2 Media Error Count: 0 Other Error Count: 0 Predictive Failure Count: 0 Last Predictive Failure Event Seq Number: 0 PD Type: SAS Raw Size: 279.396 GB [0x22ecb25c Sectors] Non Coerced Size: 278.896 GB [0x22dcb25c Sectors] Coerced Size: 278.875 GB [0x22dc0000 Sectors] Firmware state: Online, Spun Up Device Firmware Level: LS08 Shield Counter: 0 Successful diagnostics completion on : N/A SAS Address(0): 0x5000c5007272c289 SAS Address(1): 0x0 Connected Port Number: 0(path0) Inquiry Data: SEAGATE ST300MM0006 LS08S0K2B5NV FDE Enable: Disable Secured: Unsecured Locked: Unlocked Needs EKM Attention: No Foreign State: None Device Speed: 6.0Gb/s Link Speed: 6.0Gb/s Media Type: Hard Disk Device Drive Temperature :29C (84.20 F) PI Eligibility: No Drive is formatted for PI information: No PI: No PI Drive's write cache : Disabled Port-0 : Port status: Active Port's Linkspeed: 6.0Gb/s Port-1 : Port status: Active Port's Linkspeed: Unknown Drive has flagged a S.M.A.R.T alert : No Enclosure Device ID: 32 Slot Number: 1 Drive's postion: DiskGroup: 0, Span: 0, Arm: 1 Enclosure position: 0 Device Id: 1 WWN: 5000C5007272DE74 Sequence Number: 2 Media Error Count: 0 Other Error Count: 0 Predictive Failure Count: 0 Last Predictive Failure Event Seq Number: 0 PD Type: SAS Raw Size: 279.396 GB [0x22ecb25c Sectors] Non Coerced Size: 278.896 GB [0x22dcb25c Sectors] Coerced Size: 278.875 GB [0x22dc0000 Sectors] Firmware state: Online, Spun Up Device Firmware Level: LS08 Shield Counter: 0 Successful diagnostics completion on : N/A SAS Address(0): 0x5000c5007272de75 SAS Address(1): 0x0 Connected Port Number: 0(path0) Inquiry Data: SEAGATE ST300MM0006 LS08S0K2B5AH FDE Enable: Disable Secured: Unsecured Locked: Unlocked Needs EKM Attention: No Foreign State: None Device Speed: 6.0Gb/s Link Speed: 6.0Gb/s Media Type: Hard Disk Device Drive Temperature :29C (84.20 F) PI Eligibility: No Drive is formatted for PI information: No PI: No PI Drive's write cache : Disabled Port-0 : Port status: Active Port's Linkspeed: 6.0Gb/s Port-1 : Port status: Active Port's Linkspeed: Unknown Drive has flagged a S.M.A.R.T alert : No Enclosure Device ID: 32 Slot Number: 2 Drive's postion: DiskGroup: 1, Span: 0, Arm: 0 Enclosure position: 0 Device Id: 2 WWN: 50025388A075B731 Sequence Number: 2 Media Error Count: 0 Other Error Count: 1158 Predictive Failure Count: 0 Last Predictive Failure Event Seq Number: 0 PD Type: SATA Raw Size: 476.939 GB [0x3b9e12b0 Sectors] Non Coerced Size: 476.439 GB [0x3b8e12b0 Sectors] Coerced Size: 476.375 GB [0x3b8c0000 Sectors] Firmware state: Online, Spun Up Device Firmware Level: 1B6Q Shield Counter: 0 Successful diagnostics completion on : N/A SAS Address(0): 0x500056b37789abee Connected Port Number: 0(path0) Inquiry Data: S1SZNSAFA01085L Samsung SSD 850 PRO 512GB EXM01B6Q FDE Enable: Disable Secured: Unsecured Locked: Unlocked Needs EKM Attention: No Foreign State: None Device Speed: 6.0Gb/s Link Speed: 6.0Gb/s Media Type: Solid State Device Drive: Not Certified Drive Temperature :25C (77.00 F) PI Eligibility: No Drive is formatted for PI information: No PI: No PI Drive's write cache : Disabled Drive's NCQ setting : Disabled Port-0 : Port status: Active Port's Linkspeed: 6.0Gb/s Drive has flagged a S.M.A.R.T alert : No Enclosure Device ID: 32 Slot Number: 3 Drive's postion: DiskGroup: 1, Span: 0, Arm: 1 Enclosure position: 0 Device Id: 3 WWN: 50025385A02A074F Sequence Number: 2 Media Error Count: 0 Other Error Count: 0 Predictive Failure Count: 0 Last Predictive Failure Event Seq Number: 0 PD Type: SATA Raw Size: 476.939 GB [0x3b9e12b0 Sectors] Non Coerced Size: 476.439 GB [0x3b8e12b0 Sectors] Coerced Size: 476.375 GB [0x3b8c0000 Sectors] Firmware state: Online, Spun Up Device Firmware Level: 6B0Q Shield Counter: 0 Successful diagnostics completion on : N/A SAS Address(0): 0x500056b37789abef Connected Port Number: 0(path0) Inquiry Data: S1AXNSAF912433K Samsung SSD 840 PRO Series DXM06B0Q FDE Enable: Disable Secured: Unsecured Locked: Unlocked Needs EKM Attention: No Foreign State: None Device Speed: 6.0Gb/s Link Speed: 6.0Gb/s Media Type: Solid State Device Drive: Not Certified Drive Temperature :28C (82.40 F) PI Eligibility: No Drive is formatted for PI information: No PI: No PI Drive's write cache : Disabled Drive's NCQ setting : Disabled Port-0 : Port status: Active Port's Linkspeed: 6.0Gb/s Drive has flagged a S.M.A.R.T alert : No Enclosure Device ID: 32 Slot Number: 4 Drive's postion: DiskGroup: 1, Span: 1, Arm: 0 Enclosure position: 0 Device Id: 4 WWN: 50025385A01FD838 Sequence Number: 2 Media Error Count: 0 Other Error Count: 0 Predictive Failure Count: 0 Last Predictive Failure Event Seq Number: 0 PD Type: SATA Raw Size: 476.939 GB [0x3b9e12b0 Sectors] Non Coerced Size: 476.439 GB [0x3b8e12b0 Sectors] Coerced Size: 476.375 GB [0x3b8c0000 Sectors] Firmware state: Online, Spun Up Device Firmware Level: 5B0Q Shield Counter: 0 Successful diagnostics completion on : N/A SAS Address(0): 0x500056b37789abf0 Connected Port Number: 0(path0) Inquiry Data: S1AXNSAF303909M Samsung SSD 840 PRO Series DXM05B0Q FDE Enable: Disable Secured: Unsecured Locked: Unlocked Needs EKM Attention: No Foreign State: None Device Speed: 6.0Gb/s Link Speed: 6.0Gb/s Media Type: Solid State Device Drive: Not Certified Drive Temperature :27C (80.60 F) PI Eligibility: No Drive is formatted for PI information: No PI: No PI Drive's write cache : Disabled Drive's NCQ setting : Disabled Port-0 : Port status: Active Port's Linkspeed: 6.0Gb/s Drive has flagged a S.M.A.R.T alert : No Enclosure Device ID: 32 Slot Number: 5 Drive's postion: DiskGroup: 1, Span: 1, Arm: 1 Enclosure position: 0 Device Id: 5 WWN: 50025385A02AB5C9 Sequence Number: 2 Media Error Count: 0 Other Error Count: 0 Predictive Failure Count: 0 Last Predictive Failure Event Seq Number: 0 PD Type: SATA Raw Size: 476.939 GB [0x3b9e12b0 Sectors] Non Coerced Size: 476.439 GB [0x3b8e12b0 Sectors] Coerced Size: 476.375 GB [0x3b8c0000 Sectors] Firmware state: Online, Spun Up Device Firmware Level: 6B0Q Shield Counter: 0 Successful diagnostics completion on : N/A SAS Address(0): 0x500056b37789abf1 Connected Port Number: 0(path0) Inquiry Data: S1AXNSAFB00549A Samsung SSD 840 PRO Series DXM06B0Q FDE Enable: Disable Secured: Unsecured Locked: Unlocked Needs EKM Attention: No Foreign State: None Device Speed: 6.0Gb/s Link Speed: 6.0Gb/s Media Type: Solid State Device Drive: Not Certified Drive Temperature :28C (82.40 F) PI Eligibility: No Drive is formatted for PI information: No PI: No PI Drive's write cache : Disabled Drive's NCQ setting : Disabled Port-0 : Port status: Active Port's Linkspeed: 6.0Gb/s Drive has flagged a S.M.A.R.T alert : No Exit Code: 0x00
Memory Device Total Width: 32 bits Data Width: 32 bits Size: 1024 MB Form Factor: DIMM Set: None Locator: DIMM #0 Bank Locator: BANK #0 Type: DRAM Type Detail: EDO Speed: 667 MHz Manufacturer: Not Specified Serial Number: Not Specified Asset Tag: Not Specified Part Number: Not Specified Rank: Unknown Memory Device Total Width: 32 bits Data Width: 32 bits Size: No Module Installed Form Factor: DIMM Set: None Locator: DIMM #1 Bank Locator: BANK #1 Type: DRAM Type Detail: EDO Speed: 667 MHz Manufacturer: Not Specified Serial Number: Not Specified Asset Tag: Not Specified Part Number: Not Specified Rank: Unknown Memory Device Total Width: 32 bits Data Width: 32 bits Size: No Module Installed Form Factor: DIMM Set: None Locator: DIMM #2 Bank Locator: BANK #2 Type: DRAM Type Detail: EDO Speed: 667 MHz Manufacturer: Not Specified Serial Number: Not Specified Asset Tag: Not Specified Part Number: Not Specified Rank: Unknown Memory Device Total Width: 32 bits Data Width: 32 bits Size: No Module Installed Form Factor: DIMM Set: None Locator: DIMM #3 Bank Locator: BANK #3 Type: DRAM Type Detail: EDO Speed: 667 MHz Manufacturer: Not Specified Serial Number: Not Specified Asset Tag: Not Specified Part Number: Not Specified Rank: Unknown Memory Device Total Width: 32 bits Data Width: 32 bits Size: No Module Installed Form Factor: DIMM Set: None Locator: DIMM #4 Bank Locator: BANK #4 Type: DRAM Type Detail: EDO Speed: 667 MHz Manufacturer: Not Specified Serial Number: Not Specified Asset Tag: Not Specified Part Number: Not Specified Rank: Unknown Memory Device Total Width: 32 bits Data Width: 32 bits Size: No Module Installed Form Factor: DIMM Set: None Locator: DIMM #5 Bank Locator: BANK #5 Type: DRAM Type Detail: EDO Speed: 667 MHz Manufacturer: Not Specified Serial Number: Not Specified Asset Tag: Not Specified Part Number: Not Specified Rank: Unknown Memory Device Total Width: 32 bits Data Width: 32 bits Size: No Module Installed Form Factor: DIMM Set: None Locator: DIMM #6 Bank Locator: BANK #6 Type: DRAM Type Detail: EDO Speed: 667 MHz Manufacturer: Not Specified Serial Number: Not Specified Asset Tag: Not Specified Part Number: Not Specified Rank: Unknown Memory Device Total Width: 32 bits Data Width: 32 bits Size: No Module Installed Form Factor: DIMM Set: None Locator: DIMM #7 Bank Locator: BANK #7 Type: DRAM Type Detail: EDO Speed: 667 MHz Manufacturer: Not Specified Serial Number: Not Specified Asset Tag: Not Specified Part Number: Not Specified Rank: Unknown
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:1c:42:a5:57:7a brd ff:ff:ff:ff:ff:ff 3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether 52:54:00:a3:74:29 brd ff:ff:ff:ff:ff:ff 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500 link/ether 52:54:00:a3:74:29 brd ff:ff:ff:ff:ff:ff 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:1c:42:a5:57:7a brd ff:ff:ff:ff:ff:ff inet 10.211.55.4/24 brd 10.211.55.255 scope global eth0 inet6 fdb2:2c26:f4e4:0:21c:42ff:fea5:577a/64 scope global dynamic valid_lft 2591752sec preferred_lft 604552sec inet6 fe80::21c:42ff:fea5:577a/64 scope link valid_lft forever preferred_lft forever 3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether 52:54:00:a3:74:29 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500 link/ether 52:54:00:a3:74:29 brd ff:ff:ff:ff:ff:ff
/lib目录:
#转换函数 def convert_to_int(value,default=0): try: result = int(value) except Exception as e: result = default return result def convert_mb_to_gb(value,default=0): try: value = value.strip('MB') result = int(value) except Exception as e: result = default return result
/conf目录:
""" 内置配置文件 """ EMAIL = "bing@qq.com" # print(dir())
from config import settings import os import importlib from . import global_settings#导入当前的settings class Settings(object): def __init__(self): ###################找到默认配置################## for name in dir(global_settings): if name.isupper(): value = getattr(global_settings,name) setattr(self,name,value) settings_module = os.environ.get('USER_SETTINGS') #####################找到自定义配置,自定义的优先,默认的次之###################### #根据字符串导入模块(反射) if not settings_module: return m = importlib.import_module(settings_module) for name in dir(m): if name.isupper(): value = getattr(m,name) setattr(self,name,value) setings = Settings()#创建一个对象
/src目录:
from lib.conf.config import settings from .client import Agent from .client import SSHSALT def run():#从/bin目录里的start.py,start.py的run方法 if settings.MODE == "AGENT":#模式为Agent方式采集 obj = Agent()#执行Agent方法 else: obj = SSHSALT() obj.execute()
import requests import json from src.plugins import PluginManager from lib.conf.config import settings from concurrent.futures import ThreadPoolExecutor class Base(object): def post_asset(self,server_info): requests.post(settings.API,json=server_info)#在内部会把字典,转成json,json.dumps(dict)#发送json数据给API # body: json.dumps(server_info) # headers= {'content-type':'application/json'} # request.body # json.loads(request.body) class Agent(Base):#继承Base类 def execute(self): server_info = PluginManager().exec_plugin() # print(server_info) # for k,v in server_info.items(): # print(k,v) hostname = server_info['basic']['data']['hostname']#动态获取当前主机名 certname = open(settings.CERT_PATH,'r',encoding='utf8').read().strip() if not certname: with open(settings.CERT_PATH,'w',encoding='utf8') as f: f.write(hostname) else: server_info['basic']['data']['hostname'] = certname#以certname为准(主机名) self.post_asset(server_info) # self.post_asset(server_info) #body:json.dumps(server_info):字典序列化之后发到后台 #headers = {'content-type':'application/json'} #request.body:请求体里的 #在Django后台要执行json.loads(request.body),再转化成字典 #只有content_type=application/x-www-form-urlencoded时,request.POST才有值的 但是这里request.POST是没有值的 """ { k:json.dumps(server_info), } """ """ elif self.content_type == 'application/x-www-form-urlencoded': self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict() """ class SSHSALT(Base): def get_host(self): # 获取未采集的主机列表: response = requests.get(settings.API) result = json.loads(response.text)## "{status:'True',data: ['c1.com','c2.com']}" if not result['status']:#如果返回False,什么都不做 return return result['data']#返回True时,返回主机列表 def run(self,host): server_info = PluginManager(host).exec_plugin() self.post_asset(server_info) def execute(self):#要做成并发的模式(线程池) host_list = self.get_host() pool = ThreadPoolExecutor(10)#一次采集10个资产信息 for host in host_list: pool.submit(self.run,host) #拿到数据发送到API,还要对API加密 #高内聚,低耦合 #该自己实现的全部实现:高内聚, 不跟别人关联:低耦合 #自己完成所有的功能:高内聚
/plugins目录:
import importlib import traceback from lib.conf.config import settings#settings里有PLUGINS_DICT class PluginManager(object): def __init__(self,hostname=None):#settings.PLUGINS_DICT放在构造方法里更好 self.hostname = hostname#SSH和SALT采集时要用到 self.plugin_dict = settings.PLUGINS_DICT self.mode = settings.MODE self.debug = settings.DEBUG if self.mode == "SSH": self.ssh_user = settings.SSH_USER self.ssh_port = settings.SSH_PORT self.ssh_pwd = settings.SSH_PWD self.ssh_key = settings.SSH_KEY #SSH和SALT方式采集需要提供主机名,而AGENT则不需要提供主机名 def exec_plugin(self): """ 获取所有的插件,并执行获取插件返回值 :return: """ response = {} for k,v in self.plugin_dict.items(): # 'basic':"src.plugins.basic.Basic", ret = {'status':True,'data':None} try:#做一下异常处理,因为采集资产信息有可能出错,正确的可以提交,错误信息应该把错误日志提交给日志表 module_path, class_name = v.rsplit(".", 1) # module_path是模块,class_name:类名,rsplit是字符串的方法,从右边往前切片,1是切一次 m = importlib.import_module(module_path) cls = getattr(m, class_name) # 获取模块下的类名 # obj = cls().process() #类名加括号创建对象再执行一个process方法 result = "根据v获取类,并执行其方法采集资产" if hasattr(cls, 'initial'): # process_request:中间件里的方法 obj = cls.initial() # 如果cls有initial方法,则执行initial方法 else: obj = cls() result = obj.process(self.command, self.debug)#self.command传入的参数 # result = "根据v获取类,并执行其方法采集资产" # self.command一加括号就可以执行command方法,在其他插件里的def process(self,command_func,debug)函数有个command_func参数,这个参数就是函数名,加括号可以传参数,command_func("ifconfig") ret['data'] = result except Exception as e: ret['status'] = False #ret['data'] = "c1.com class_name :e" 哪台主机的哪个插件 ret['data'] = "[%s][%s] 采集数据出现错误:%s" %(self.hostname if self.hostname else "AGENT",k,traceback.format_exc())#错误堆栈信息添加进ret字典里 response[k] = ret return response #self.hostname有可能是None,也就是AGENT方式 #中间件时:request.process request.response #同中间件类似,定义的插件一定要加到配置文件里的PLUGINS_DICT字典里 def command(self,cmd): if self.mode == "AGENT": return self.__agent(cmd) elif self.mode == "SSH": return self.__ssh(cmd) elif self.mode == "SALT": return self.__salt(cmd) else: raise Exception("模式只能是AGENT,SSH,SALT") def __agent(self,cmd): import subprocess output = subprocess.getoutput(cmd) return output def __salt(self,cmd): # import salt.client # local = salt.client.LocalClient() # result = local.cmd(self.hostname, 'cmd.run', [cmd]) # return result[self.hostname] salt_cmd = "salt '%s' cmd.run '%s'" %(self.hostname,cmd,)#saltstack对Python2的支持更好一点,在Python3使用得用subprocess执行命令 import subprocess output = subprocess.getoutput(salt_cmd) return output def __ssh(self,cmd):#加双下划线代表私有的,只能自己调用,外部访问不到,只需知道command函数就行了,主机名,用户名,密码 import paramiko #公钥私钥的方式连接服务器 # private_key = paramiko.RSAKey.from_private_key_file(self.ssh_key) # ssh = paramiko.SSHClient() # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # ssh.connect(hostname=self.hostname, port=self.ssh_port, username=self.ssh_user, pkey=private_key) # stdin, stdout, stderr = ssh.exec_command(cmd) # result = stdout.read() # ssh.close() #用户名密码连接服务器 ssh = paramiko.SSHClient()#创建一个ssh对象 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#创建连接不在know_hosts文件中的主机 ssh.connect(hostname=self.hostname,port=self.ssh_port,username=self.ssh_user,password=self.ssh_pwd)#通过主机名,端口,用户名,密码 stdin,stdout,stderr = ssh.exec_command(cmd)#执行命令 result = stdout.read()#获取命令结果 ssh.close()#关闭连接 return result#返回结果
#baisc.py用于找到Linux系统基本信息,如操作系统平台.系统版本,主机名 from .base import BasePlugin import os from lib.conf.config import settings#导入配置文件模块,从而导入执行三种采集方式的模块 class Basic(object): def __init__(self): pass @classmethod#类方法用于做扩展,自定制操作 def initial(cls): #预留的钩子 return cls()#cls()是对象 def process(self,command_func,debug): if debug: output = { 'os_platform':"linux", 'os_version':"CentOS relese 6.6 (Final)\nKernel \r on an \m", 'hostname':'c1.com' } else: output = { 'os_platform':command_func("uname").strip(),#查到系统Linux,并去空格 'os_version':command_func("cat /etc/issue").strip().split('\n')[0],#查看系统版本 'hostname':command_func("hostname").strip() } return output """ #cat /etc/issue CentOS release 6.6 (Final) Kernel \r on an \m(\r 和 \m都是转义字符) """
import os from lib.conf.config import setings class Board(object): #Linux命令:dmesg def __init__(self): pass @classmethod def initial(cls): return cls() def process(self,command_func,debug): if debug: output = open(os.path.join(setings.BASEDIR,'files/board.out'),'r',encoding='utf8').read() else: output = command_func("sudo dmidecode -t1") return self.parse(output) def parse(self,content): result = {} key_map = { 'Manufacturer':'manufacturer',#制造商 'Product Name':'model',#产品名称 'Serial Number':'sn',#sn号,也就是序列号 } for item in content.split('\n'): row_data = item.strip().split(':') if len(row_data) == 2:#如果列表里有两个元素,代表是一个key,一个value if row_data[0] in key_map: result[key_map[row_data[0]]] = row_data[1].strip() if row_data[1] else row_data[1] return result """ result字典: { 'manufacturer': 'Parallels Software International Inc.', 'model': 'Parallels Virtual Platform', 'sn': 'Parallels-1A 1B CB 3B 64 66 4B 13 86 B0 86 FF 7E 2B 20 30' } """ """ sudo dmidecode -t1的执行结果 # dmidecode 3.0 Scanning /dev/mem for entry point. SMBIOS 2.7 present. Handle 0x0001, DMI type 1, 27 bytes System Information Manufacturer: VMware, Inc. Product Name: VMware Virtual Platform Version: None Serial Number: VMware-56 4d f9 b6 c0 30 5c 72-7c cb b4 33 b5 ec 81 4e UUID: B6F94D56-30C0-725C-7CCB-B433B5EC814E Wake-up Type: Power Switch SKU Number: Not Specified Family: Not Specified """
#linux命令:cat /proc/cpuinfo import os from lib.conf.config import setings class Cpu(object): def __init__(self): pass @classmethod def initial(cls): return cls() def process(self,command_func,debug): if debug: output = open(os.path.join(setings.BASEDIR,'files/cpuinfo.out'),'r',encoding='utf8').read() else: output = command_func('cat /proc/cpuinfo') return self.parse(output) def parse(self,content): """ #根据split或者正则表达式 解析shell命令返回的结果 :param content: shell命令结果 :return: 解析后的结果 """ response = {'cpu_count':0,'cpu_physical_count':0,'cpu_model':''} cpu_physical_set = set()#设置一个集合 content = content.strip() for item in content.split('\n\n'): for row_line in item.split('\n'): key, value = row_line.split(':') key = key.strip() if key == 'processor': response['cpu_count'] += 1 elif key == 'physical id': cpu_physical_set.add(value) elif key == 'model name': if not response['cpu_model']: response['cpu_model'] = value response['cpu_physical_count'] = len(cpu_physical_set) return response
#df -h import re import os from lib.conf.config import setings class Disk(object): def __init__(self): pass @classmethod def initial(cls): return cls()#cls代指Disk类 def process(self,command_func,debug): if debug: output = open(os.path.join(setings.BASEDIR,'files/disk.out'),'r',encoding='utf8').read() else: output = command_func("sudo MegaCli -PDList -aALL") return self.parse(output) 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)#mega_patter_match是匹配函数 if name: if key == "Raw Size": raw_size = re.search('(\d+\.\d+)',value.strip())#硬盘大小,如:Raw Size: 279.396 GB [0x22ecb25c Sectors] 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
#free -m import os from lib import convert from lib.conf.config import settings class Memory(object): def __init__(self): pass @classmethod def initial(cls): return cls() def process(self,command_func,debug): if debug: output = open(os.path.join(settings.BASEDIR,'files/memory.out'),'r',encoding='utf8').read() else: output = command_func("sudo dmidecode -q -t 17 2>/dev/null") return self.parse(output) def parse(self,content): """ 解析shell命令返回的结果 :param content: shell命令结果 :return: 解析后的结果 """ ram_dict = {} key_map = { 'Size':'capacity', 'Locator':'slot', 'Type':'model', 'Speed':'speed', 'Manufacturer':'manufacturer', 'Serial Number':'sn', } devices = content.split('Memory Device')#列表里的元素都是字符串,有些字符串是套着键值对 for item in devices: item = item.strip() if not item: continue if item.startswith('#'): continue segment = {} lines = item.split('\n\t') for line in lines: if not line.strip(): continue if len(line.split(':')): # print(len(line.split(':')))#按照':'分割之后长度是2,因为分割之前都是键值对 key,value = line.split(':') else: key = line.split(':') value = "" if key in key_map: if key == 'size': segment[key_map[key_map['Size']]] = convert.convert_mb_to_gb(value,0)# else: segment[key_map[key_map[key.strip()]]] = value.strip() ram_dict[segment['slot']] = segment#往空字典里添加值 return ram_dict """ ram_dict输出: {'DIMM #0': {'capacity': 1024, 'slot': 'DIMM #0', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #1': {'capacity': 0, 'slot': 'DIMM #1', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #2': {'capacity': 0, 'slot': 'DIMM #2', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #3': {'capacity': 0, 'slot': 'DIMM #3', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #4': {'capacity': 0, 'slot': 'DIMM #4', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #5': {'capacity': 0, 'slot': 'DIMM #5', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #6': {'capacity': 0, 'slot': 'DIMM #6', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #7': {'capacity': 0, 'slot': 'DIMM #7', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}} """
#free -m import os from lib import convert from lib.conf.config import settings class Memory(object): def __init__(self): pass @classmethod def initial(cls): return cls() def process(self,command_func,debug): if debug: output = open(os.path.join(settings.BASEDIR,'files/memory.out'),'r',encoding='utf8').read() else: output = command_func("sudo dmidecode -q -t 17 2>/dev/null") return self.parse(output) def parse(self,content): """ 解析shell命令返回的结果 :param content: shell命令结果 :return: 解析后的结果 """ ram_dict = {} key_map = { 'Size':'capacity', 'Locator':'slot', 'Type':'model', 'Speed':'speed', 'Manufacturer':'manufacturer', 'Serial Number':'sn', } devices = content.split('Memory Device')#列表里的元素都是字符串,有些字符串是套着键值对 for item in devices: item = item.strip() if not item: continue if item.startswith('#'): continue segment = {} lines = item.split('\n\t') for line in lines: if not line.strip(): continue if len(line.split(':')): # print(len(line.split(':')))#按照':'分割之后长度是2,因为分割之前都是键值对 key,value = line.split(':') else: key = line.split(':') value = "" if key in key_map: if key == 'size': segment[key_map[key_map['Size']]] = convert.convert_mb_to_gb(value,0)# else: segment[key_map[key_map[key.strip()]]] = value.strip() ram_dict[segment['slot']] = segment#往空字典里添加值 return ram_dict """ ram_dict输出: {'DIMM #0': {'capacity': 1024, 'slot': 'DIMM #0', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #1': {'capacity': 0, 'slot': 'DIMM #1', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #2': {'capacity': 0, 'slot': 'DIMM #2', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #3': {'capacity': 0, 'slot': 'DIMM #3', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #4': {'capacity': 0, 'slot': 'DIMM #4', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #5': {'capacity': 0, 'slot': 'DIMM #5', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #6': {'capacity': 0, 'slot': 'DIMM #6', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}, 'DIMM #7': {'capacity': 0, 'slot': 'DIMM #7', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}} """
#!/usr/bin/env python # -*- coding:utf-8 -*- import os import re from lib.conf.config import settings class Nic(object): def __init__(self): pass @classmethod def initial(cls): return cls() def process(self, command_func, debug): if debug: output = open(os.path.join(settings.BASEDIR, 'files/nic.out'), 'r', encoding='utf-8').read() interfaces_info = self._interfaces_ip(output) else: interfaces_info = self.linux_interfaces(command_func) self.standard(interfaces_info) return interfaces_info def linux_interfaces(self, command_func): ''' Obtain interface information for *NIX/BSD variants ''' ifaces = dict() ip_path = 'ip' if ip_path: cmd1 = command_func('sudo {0} link show'.format(ip_path)) cmd2 = command_func('sudo {0} addr show'.format(ip_path)) ifaces = self._interfaces_ip(cmd1 + '\n' + cmd2) return ifaces def which(self, exe): def _is_executable_file_or_link(exe): # check for os.X_OK doesn't suffice because directory may executable return (os.access(exe, os.X_OK) and (os.path.isfile(exe) or os.path.islink(exe))) if exe: if _is_executable_file_or_link(exe): # executable in cwd or fullpath return exe # default path based on busybox's default default_path = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin' search_path = os.environ.get('PATH', default_path) path_ext = os.environ.get('PATHEXT', '.EXE') ext_list = path_ext.split(';') search_path = search_path.split(os.pathsep) if True: # Add any dirs in the default_path which are not in search_path. If # there was no PATH variable found in os.environ, then this will be # a no-op. This ensures that all dirs in the default_path are # searched, which lets salt.utils.which() work well when invoked by # salt-call running from cron (which, depending on platform, may # have a severely limited PATH). search_path.extend( [ x for x in default_path.split(os.pathsep) if x not in search_path ] ) for path in search_path: full_path = os.path.join(path, exe) if _is_executable_file_or_link(full_path): return full_path return None def _number_of_set_bits_to_ipv4_netmask(self, set_bits): # pylint: disable=C0103 ''' Returns an IPv4 netmask from the integer representation of that mask. Ex. 0xffffff00 -> '255.255.255.0' ''' return self.cidr_to_ipv4_netmask(self._number_of_set_bits(set_bits)) def cidr_to_ipv4_netmask(self, cidr_bits): ''' Returns an IPv4 netmask ''' try: cidr_bits = int(cidr_bits) if not 1 <= cidr_bits <= 32: return '' except ValueError: return '' netmask = '' for idx in range(4): if idx: netmask += '.' if cidr_bits >= 8: netmask += '255' cidr_bits -= 8 else: netmask += '{0:d}'.format(256 - (2 ** (8 - cidr_bits))) cidr_bits = 0 return netmask def _number_of_set_bits(self, x): ''' Returns the number of bits that are set in a 32bit int ''' # Taken from http://stackoverflow.com/a/4912729. Many thanks! x -= (x >> 1) & 0x55555555 x = ((x >> 2) & 0x33333333) + (x & 0x33333333) x = ((x >> 4) + x) & 0x0f0f0f0f x += x >> 8 x += x >> 16 return x & 0x0000003f def _interfaces_ip(self, out): ''' Uses ip to return a dictionary of interfaces with various information about each (up/down state, ip address, netmask, and hwaddr) ''' ret = dict() right_keys = ['name', 'hwaddr', 'up', 'netmask', 'ipaddrs'] def parse_network(value, cols): ''' Return a tuple of ip, netmask, broadcast based on the current set of cols ''' brd = None if '/' in value: # we have a CIDR in this address ip, cidr = value.split('/') # pylint: disable=C0103 else: ip = value # pylint: disable=C0103 cidr = 32 if type_ == 'inet': mask = self.cidr_to_ipv4_netmask(int(cidr)) if 'brd' in cols: brd = cols[cols.index('brd') + 1] return (ip, mask, brd) groups = re.compile('\r?\n\\d').split(out) for group in groups: iface = None data = dict() for line in group.splitlines(): if ' ' not in line: continue match = re.match(r'^\d*:\s+([\w.\-]+)(?:@)?([\w.\-]+)?:\s+<(.+)>', line) if match: iface, parent, attrs = match.groups() if 'UP' in attrs.split(','): data['up'] = True else: data['up'] = False if parent and parent in right_keys: data[parent] = parent continue cols = line.split() if len(cols) >= 2: type_, value = tuple(cols[0:2]) iflabel = cols[-1:][0] if type_ in ('inet',): if 'secondary' not in cols: ipaddr, netmask, broadcast = parse_network(value, cols) if type_ == 'inet': if 'inet' not in data: data['inet'] = list() addr_obj = dict() addr_obj['address'] = ipaddr addr_obj['netmask'] = netmask addr_obj['broadcast'] = broadcast data['inet'].append(addr_obj) else: if 'secondary' not in data: data['secondary'] = list() ip_, mask, brd = parse_network(value, cols) data['secondary'].append({ 'type': type_, 'address': ip_, 'netmask': mask, 'broadcast': brd, }) del ip_, mask, brd elif type_.startswith('link'): data['hwaddr'] = value if iface: if iface.startswith('pan') or iface.startswith('lo') or iface.startswith('v'): del iface, data else: ret[iface] = data del iface, data return ret def standard(self, interfaces_info): for key, value in interfaces_info.items(): ipaddrs = set() netmask = set() if not 'inet' in value: value['ipaddrs'] = '' value['netmask'] = '' else: for item in value['inet']: ipaddrs.add(item['address']) netmask.add(item['netmask']) value['ipaddrs'] = '/'.join(ipaddrs) value['netmask'] = '/'.join(netmask) del value['inet']
你现在所遭遇的每一个不幸,都来自一个不肯努力的曾经。大数据SRE(运维开发)学习交流群239963844
posted on 2017-07-29 23:33 bigdata_devops 阅读(531) 评论(0) 编辑 收藏 举报