机器信息采集脚本python.docx

上传人:b****8 文档编号:29574280 上传时间:2023-07-24 格式:DOCX 页数:13 大小:182.70KB
下载 相关 举报
机器信息采集脚本python.docx_第1页
第1页 / 共13页
机器信息采集脚本python.docx_第2页
第2页 / 共13页
机器信息采集脚本python.docx_第3页
第3页 / 共13页
机器信息采集脚本python.docx_第4页
第4页 / 共13页
机器信息采集脚本python.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

机器信息采集脚本python.docx

《机器信息采集脚本python.docx》由会员分享,可在线阅读,更多相关《机器信息采集脚本python.docx(13页珍藏版)》请在冰豆网上搜索。

机器信息采集脚本python.docx

机器信息采集脚本python

机器信息采集脚本(python)

 

目录

一、做什么的?

2

二、怎么做?

4

(1)、依赖的库4

(2)、命令行解析模块optparse4

(3)windows系统信息4

(4)Linux系统信息7

4.1cpu信息7

4.2network信息9

4.3存储信息10

(5)数据库信息11

(6)已经测试环境11

7.SunOSsunv440不支持12

标签:

脚本,python,系统管理员,命令行,解析器,操作系统

源代码位置:

(希望大家能多多star收藏和fork改进程序)

一、做什么的?

(1)主要用于采集服务器的相关信息,提供两种交互方式,一种是将服务器信息汇总至xml文件中,便于软件开发者使用(图1);另一种方式通过命令行交互的方式,通过输入参数信息查询相关信息(图2)。

图1

图2

(2)主要功能

∙uname:

操作系统版本、型号、处理器、机器名等

∙CPU:

型号、数量、使用率等

∙network:

网卡信息、MAC、IP、子网掩码、网络I/O等

∙memory:

内存大小、使用率、可用大小等

∙存储:

类型、大小、使用情况、磁盘I/O等

∙数据库版本和安装路径:

Mysql、oracle、sqlserver、sybase、db2

二、怎么做?

(1)、依赖的库

psutil库下载地址:

https:

//pypi.python.org/pypi/psutil

windows系统需要安装WMI库下载地址:

https:

//pypi.python.org/pypi/WMI/#downloads这个库在安装之前要安装pywin32

(2)、命令行解析模块optparse

功能强大,且易于使用,可以方便地生成标准的、符合Unix/Posix规范的命令行说明。

文档说明中文翻译:

(3)windows系统信息

WMI最初于1998年作为一个附加组件与WindowsNT4.0ServicePack4一起发行,是内置在Windows2000、WindowsXP和WindowsServer2003系列操作系统中核心的管理支持技术。

基于由DistributedManagementTaskForce(DMTF)所监督的业界标准,WMI是一种规范和基础结构,通过它可以访问、配置、管理和监视几乎所有的Windows资源。

大多用户习惯于使用众多的图形化管理工具来管理Windows资源,在WMI之前这些工具都是通过Win32应用程序编程接口(ApplicationProgrammingInterfaces,API)来访问和管理Windows资源的。

只要你熟悉系统编程你就知道API有多么重要。

但是大多数脚本语言都不能直接调用Win32API,WMI的出现使得系统管理员可以通过一种简便的方法即利用常见的脚本语言实现常用的系统管理任务。

利用WMI需要和脚本如WSH和VBScript结合起来,可以实现的功能大家可以看微软的MSDN文档。

因此基本上windows系统能表现出来,你都可以通过这个模块获取,具体的可参考下面代码:

#!

/usr/bin/envpython

#-*-coding:

utf-8-*-

try:

importwmi

importwin32api

exceptImportError:

wmi=None

importsys,platform

importsubprocess

importos

 

defget_system_info(c,sys):

ifsys=="Windows":

forsysinc.Win32_OperatingSystem():

print"Version:

\t%s"%sys.Caption.encode("GBK")

print"Vernum:

\t%s"%sys.BuildNumber

defget_memory_info(c,sys):

ifsys=="Windows":

formeminc.Win32_MemoryArray():

print'\t'+str(mem.Caption)+'\t'+str(mem.Name)

cs=c.Win32_ComputerSystem()

pfu=c.Win32_PageFileUsage()

MemTotal=int(cs[0].TotalPhysicalMemory)/1024/1024

print"TotalPhysicalMemory:

"+'\t'+str(MemTotal)+"M"

#tmpdict["MemFree"]=int(sys[0].FreePhysicalMemory)/1024

SwapTotal=int(pfu[0].AllocatedBaseSize)

print"SwapTotal:

"+'\t'+str(SwapTotal)+"M"

#tmpdict["SwapFree"]=int(pfu[0].AllocatedBaseSize-pfu[0].CurrentUsage)

defget_disk_info(c,sys,infolist):

ifsys=="Windows":

tmpdict=dict()

tmplist=list()

forphysical_diskinc.Win32_DiskDrive():

ifphysical_disk.Size:

tmpdict["disk"]=str(physical_disk.Caption)

tmpdict["size"]=str(long(physical_disk.Size)/1024/1024/1024)+"G"

tmpdict["dev"]=str(physical_disk.MediaType)

tmplist.append(tmpdict)

printtmpdict["dev"]+":

\t"+tmpdict["disk"]+':

\t'+tmpdict["size"]

infolist.append(tmplist)

defget_cpu_info(c,sys,infolist):

ifsys=="Windows":

tmplist=list()

tmpdict=dict()

tmpdict["CpuCores"]=0

forcpuinc.Win32_Processor():

tmpdict["modelname"]=cpu.Name

try:

tmpdict["CpuCores"]=cpu.NumberOfCores

except:

tmpdict["CpuCores"]+=1

tmpdict["CpuClock"]=cpu.MaxClockSpeed

print'CpuType:

\t'+str(tmpdict["modelname"])

print'CpuCores:

\t'+str(tmpdict["CpuCores"])

tmplist.append(tmpdict)

#infolist.append(tmplist)

returntmplist

defget_network_info(c,sys,infolist):

ifsys=="Windows":

tmplist=list()

forinterfaceinc.Win32_NetworkAdapterConfiguration(IPEnabled=1):

tmpdict=dict()

tmpdict["Description"]=interface.Description

tmpdict["IPAddress"]=interface.IPAddress[0]

tmpdict["IPSubnet"]=interface.IPSubnet[0]

tmpdict["MAC"]=interface.MACAddress

tmplist.append(tmpdict)

foriintmplist:

printi["Description"]

print'\t'+"MAC:

"+'\t'+i["MAC"]

print'\t'+"IPAddress:

"+'\t'+i["IPAddress"]

print'\t'+"IPSubnet:

"+'\t'+i["IPSubnet"]

infolist.append(tmplist)

forinterfacePerfTCPinc.Win32_PerfRawData_Tcpip_TCPv4():

print'TCPConnect:

\t'+str(interfacePerfTCP.ConnectionsEstablished)

defget_Proceess_cmd(c,process_name):

cmd=""

forprocessinc.Win32_Process():

temp=unicode(process.CommandLine)

name=process.Name

ifname.find(process_name)>=0:

cmd=temp

returncmd

defget_info(cmd):

p=subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=False)

returnmunicate()[0].split("\n")[0]

if__name__=="__main__":

sys=platform.system()

infolist=list()

c=wmi.WMI()

(4)Linux系统信息

4.1cpu信息

在linux系统中,提供了proc文件系统显示系统的软硬件信息.CPU的信息在启动的过程中被装载到虚拟目录/proc下的cpuinfo文件中,我们可以通过cat/proc/cpuinfo查看一下:

这样我们只需要通过分析这个文件来获取cpu的相关信息,代码如下:

efget_cpu_info(sys,infolist):

cpuinfo=dict()

procinfo=dict()

tempdict=dict()

templist=list()

ifsys=="Linux":

'''Returntheinformationin/proc/cpuinfo

asadictionaryinthefollowingformat:

cpu_info['proc0']={...}

cpu_info['proc1']={...}

'''

nprocs=0

f=file("/proc/cpuinfo",'r')

whileTrue:

line=f.readline()

iflen(line)==0:

break

ifnotline.strip():

#endofoneprocessor

cpuinfo['proc%s'%nprocs]=procinfo

nprocs=nprocs+1

#Reset

procinfo=dict()

else:

iflen(line.split(':

'))==2:

temp1=line.split(':

')[0].strip()

temp2=line.split(':

')[1].strip()

tempdict[temp1]=temp2

printtemp1+":

"+temp2

else:

procinfo[line.split(':

')[0].strip()]=''

templist.append(tempdict)

#infolist.append(tempdict)

returntemplist

4.2network信息

想实现一个类似ifconfig命令,查了一下资料没有找到好的方法,所以干脆就直接直接调用ifconfig。

defget_network_info(sys1,infolist):

ifsys1=='Linux':

tmplist=list()

ethlist=get_info('ifconfig-s|grep-vIface|grep-vlo|awk\'{print$1}\'').split("\n")

ethInfsys=get_info("lspci|grepEthernet").split("\n")

i=0

forethiinethlist:

ifethi!

="":

tmpdict=dict()

tmpdict["Description"]=ethInfsys[i].split(":

")[2]

tmpdict["IPAddress"]=get_info('ifconfig%s|awk\'/inetaddr:

/{print$2}\''%(ethi)).split(":

")[1]

tmpdict["IPSubnet"]=get_info('ifconfig%s|awk\'/Mask/{print$4}\''%(ethi)).split(":

")[1]

tmpdict["MAC"]=get_info('ifconfig%s|awk\'/HWaddr/{print$5}\''%(ethi))

tmplist.append(tmpdict)

i=i+1

foriintmplist:

printi["Description"]

print'\t'+"MAC:

"+'\t'+i["MAC"]

print'\t'+"IPAddress:

"+'\t'+i["IPAddress"]

print'\t'+"IPSubnet:

"+'\t'+i["IPSubnet"]

infolist.append(tmplist)

4.3存储信息

其实我首先想到的是fdisk这个命令,但是它有一个权限:

无法支持超过2TB的硬盘分区,这个在当今情况下几乎是不可能。

那么就用第二种分区命令了parted,由于我只想知道分区和大小,所以就在命令后面加了个过滤。

如下

后面再通过hdparm获取挂载盘的一些其他信息,代码如下:

defget_disk_info(sys,infolist):

ifsys=="Linux":

tmplist=list()

devlist=get_info("parted-l|grepDisk").split("\n")

fordevindevlist:

ifdev!

="":

tmpdict=dict()

tmpdict["dev"]=dev.split()[1].split(":

")[0]

tmpdict["size"]=dev.split()[2]

temp=get_info("hdparm-I%s|awk\'/ModelNumber:

/\'"%(tmpdict["dev"]))

iftemp!

="":

tmpdict["disk"]=temp.split(":

")[1]

else:

tmpdict["disk"]=""

tmplist.append(tmpdict)

printtmpdict["dev"]+":

\t"+tmpdict["disk"]+':

\t'+tmpdict["size"]

infolist.append(tmplist)

(5)数据库信息

原理其实很简单,每个数据库启动后都会有后台进程,只要能获取到进程,就能查到它的启动参数信息,然后根据参数信息就能查到数据库的安装位置,然后通过数据库的bin目录中的程序获取该数据库的一些配置信息,比如版本号等,所以这个方法有时候不准。

数据库

支持系统

获取方式

Mysql

windows

通过查找mysqld进程,获取安装路径和数据库的配置信息

Mysql

linux

通过ps-ef|grepmysqld|grepbasedir来定位进程

oracle

windows

通过查找oracle进程,获取安装路径,然后在运行sqlplus就能获取版本信息了

oracle

linux

通过ps-ef|greporacle|awk\'/LISTENER/{print$8}\'来定位置进程,在通过sqlplus获取版本信息

sqlserver

windows

通过查找sqlserver进程,获取相关信息

syabse

windwos

通过sqlsrc进程,获取相关信息

(6)已经测试环境

1.win7mysql5.1正常(mysql是通过mysqld程序获取信息的)

2.readhat5.6mysql5.0.77正常

3.windwosserver2008oracle11.2.0.1.0正常(在windows系统下是通过查询oracl.exe程序来获取信息的)

4.centos6.4oracle11.2.0.1.0正常(在linux系统下是通过查询oracle的监听进程来做获取信息的)

5.windowsserver2003sqlserver2005正常(通过检测sqlservr.exe程序来获取安装路径)

6.windowsserver2003sybasease1254正常(通过检测sqlsrvr.exe程序来获取安装路径)

7.SunOSsunv440不支持

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1