机器信息采集脚本.doc

上传人:牧羊曲112 文档编号:4046110 上传时间:2023-04-02 格式:DOC 页数:12 大小:250KB
返回 下载 相关 举报
机器信息采集脚本.doc_第1页
第1页 / 共12页
机器信息采集脚本.doc_第2页
第2页 / 共12页
机器信息采集脚本.doc_第3页
第3页 / 共12页
机器信息采集脚本.doc_第4页
第4页 / 共12页
机器信息采集脚本.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《机器信息采集脚本.doc》由会员分享,可在线阅读,更多相关《机器信息采集脚本.doc(12页珍藏版)》请在三一办公上搜索。

1、机器信息采集脚本(python)目录一、做什么的?2二、怎么做?4(1)、依赖的库4(2)、命令行解析模块 optparse4(3)windows系统信息4(4)Linux系统信息74.1 cpu信息74.2 network信息94.3 存储信息10(5)数据库信息11(6)已经测试环境117.SunOS sunv440 不支持12标签:脚本,python,系统管理员,命令行,解析器,操作系统源代码位置: (希望大家能多多star收藏和fork改进程序)一、做什么的?(1)主要用于采集服务器的相关信息,提供两种交互方式,一种是将服务器信息汇总至xml文件中,便于软件开发者使用(图1);另一种方

2、式通过命令行交互的方式,通过输入参数信息查询相关信息(图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/psutilwindows系统需要安装WMI库 下载地址:

3、https:/pypi.python.org/pypi/WMI/#downloads 这个库在安装之前要安装pywin32(2)、命令行解析模块 optparse功能强大,且易于使用,可以方便地生成标准的、符合Unix/Posix规范的命令行说明。文档说明中文翻译:(3)windows系统信息WMI 最初于1998年作为一个附加组件与 Windows NT 4.0 Service Pack 4 一起发行,是内置在Windows 2000、 Windows XP和Windows Server 2003 系列操作系统中核心的管理支持技术。基于由 Distributed Management Tas

4、k Force (DMTF) 所监督的业界标准,WMI是一种规范和基础结构,通过它可以访问、配置、管理和监视几乎所有的Windows资源。大多用户习惯于使用众多的图形化管理工 具来管理Windows资源,在WMI之前这些工具都是通过 Win32应用程序编程接口(Application ProgrammingInterfaces,API)来访问和管理Windows资源的。只要你熟悉系统编程你就知道API有多么重要。但是大多数脚本 语言都不能直接调用Win32 API,WMI的出现使得系统管理员可以通过一种简便的方法即利用常见的脚本语言实现常用的系统管理任务。利用WMI需要和脚本如WSH和VBSc

5、ript结合起来,可以实现的功能大家可以看微软的MSDN文档。因此基本上windows系统能表现出来,你都可以通过这个模块获取,具体的可参考下面代码:#!/usr/bin/env python# -*- coding: utf-8 -*-try:import wmiimport win32apiexcept ImportError:wmi = Noneimport sys,platformimport subprocessimport osdef get_system_info(c,sys):if sys = Windows:for sys in c.Win32_OperatingSystem

6、():print Version :t%s % sys.Caption.encode( GBK)print Vernum :t%s % sys.BuildNumberdef get_memory_info(c,sys):if sys = Windows:for mem in c.Win32_MemoryArray():print t + str(mem.Caption) + t + str(mem.Name)cs = c.Win32_ComputerSystem()pfu = c.Win32_PageFileUsage()MemTotal = int(cs 0.TotalPhysicalMem

7、ory)/ 1024/1024print TotalPhysicalMemory : + t + str(MemTotal) + M#tmpdict MemFree = int( sys0.FreePhysicalMemory)/1024SwapTotal = int(pfu 0.AllocatedBaseSize)print SwapTotal : + t + str(SwapTotal) + M#tmpdict SwapFree = int( pfu0.AllocatedBaseSize - pfu0.CurrentUsage)def get_disk_info(c,sys,infolis

8、t):if sys = Windows:tmpdict = dict()tmplist=list()for physical_disk in c.Win32_DiskDrive():if physical_disk.Size:tmpdict disk=str(physical_disk.Caption)tmpdictsize =str(long(physical_disk.Size)/1024/1024/1024)+Gtmpdict dev = str(physical_disk.MediaType)tmplist.append(tmpdict)print tmpdictdev +:t+tmp

9、dictdisk + :t + tmpdictsize infolist.append(tmplist) def get_cpu_info(c,sys,infolist):if sys = Windows:tmplist=list()tmpdict=dict()tmpdict CpuCores = 0for cpu in c.Win32_Processor(): tmpdict model name = cpu.Nametry:tmpdict CpuCores = cpu.NumberOfCoresexcept:tmpdict CpuCores += 1tmpdict CpuClock = c

10、pu.MaxClockSpeed print CpuType :t + str(tmpdictmodel name)print CpuCores :t + str(tmpdictCpuCores)tmplist.append(tmpdict)#infolist.append(tmplist)return tmplistdef get_network_info(c,sys,infolist):if sys = Windows:tmplist=list()for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):tmpdi

11、ct=dict()tmpdict Description = interface.Descriptiontmpdict IPAddress = interface.IPAddress0 tmpdict IPSubnet = interface.IPSubnet0 tmpdict MAC = interface.MACAddresstmplist.append(tmpdict)for i in tmplist:print iDescriptionprint t + MAC : + t + iMAC print t + IPAddress : + t + iIPAddress print t +

12、IPSubnet : + t + iIPSubnet infolist.append(tmplist)for interfacePerfTCP in c.Win32_PerfRawData_Tcpip_TCPv4():print TCP Connect :t + str(interfacePerfTCP.ConnectionsEstablished)def get_Proceess_cmd(c,process_name):cmd = for process in c.Win32_Process():temp = unicode(process.CommandLine)name = proces

13、s.Nameif name.find(process_name) = 0:cmd = tempreturn cmddef get_info(cmd):p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell= False)return municate() 0.split( n)0 if _name_ = _main_:sys = platform.system()infolist = list()c = wmi.WMI ()(4)Linux系统信息4.1 cpu信息在linux系统中,提供了proc文件系统显示系统的软硬件信息.CPU的信

14、息在启动的过程中被装载到虚拟目录/proc下的cpuinfo文件中,我们可以通过cat /proc/cpuinfo查看一下:这样我们只需要通过分析这个文件来获取cpu的相关信息,代码如下:ef get_cpu_info(sys,infolist):cpuinfo=dict()procinfo=dict()tempdict = dict()templist = list()if sys = Linux: Return the information in /proc/cpuinfoas a dictionary in the following format:cpu_infoproc0=.cpu

15、_infoproc1=.nprocs = 0f = file( /proc/cpuinfo,r)while True :line = f.readline()if len(line) = 0:breakif not line.strip():# end of one processorcpuinfo proc %s % nprocs = procinfonprocs=nprocs+ 1# Resetprocinfo=dict()else:if len(line.split( :) = 2 :temp1 = line.split( :)0 .strip()temp2 = line.split(

16、:)1 .strip()tempdicttemp1 = temp2print temp1+ : +temp2else:procinfoline.split( :)0 .strip() = templist.append(tempdict)# infolist.append(tempdict)return templist4.2 network信息想实现一个类似ifconfig命令,查了一下资料没有找到好的方法,所以干脆就直接直接调用ifconfig。def get_network_info(sys1,infolist):if sys1 = Linux:tmplist=list()ethlist

17、 = get_info( ifconfig -s|grep -v Iface|grep -v lo|awk print $1).split(n)ethInfsys = get_info( lspci | grep Ethernet).split(n)i = 0for ethi in ethlist:if ethi != :tmpdict = dict()tmpdict Description = ethInfsysi.split(:)2tmpdict IPAddress = get_info(ifconfig %s | awk /inet addr:/ print $2 %(ethi).spl

18、it( :)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 + 1for i in tmplist:print iDescriptionprint t + MAC : + t + iMAC print t + IPAddress : + t + iIPAddress print t + IPSu

19、bnet : + t + iIPSubnet infolist.append(tmplist) 4.3 存储信息其实我首先想到的是fdisk这个命令,但是它有一个权限:无法支持超过2TB的硬盘分区,这个在当今情况下几乎是不可能。那么就用第二种分区命令了parted,由于我只想知道分区和大小,所以就在命令后面加了个过滤。如下后面再通过hdparm获取挂载盘的一些其他信息,代码如下:def get_disk_info(sys,infolist):if sys = Linux:tmplist=list()devlist = get_info( parted -l |grep Disk).split(

20、 n)for dev in devlist:if dev != :tmpdict = dict()tmpdict dev = dev.split() 1.split( :)0 tmpdict size = dev.split()2 temp = get_info( hdparm -I %s |awk /Model Number:/%(tmpdictdev)if temp != :tmpdict disk = temp.split(:)1else:tmpdict disk = tmplist.append(tmpdict)print tmpdictdev +:t+tmpdictdisk + :t

21、 + tmpdictsize infolist.append(tmplist)(5)数据库信息原理其实很简单,每个数据库启动后都会有后台进程,只要能获取到进程,就能查到它的启动参数信息,然后根据参数信息就能查到数据库的安装位置,然后通过数据库的bin目录中的程序获取该数据库的一些配置信息,比如版本号等,所以这个方法有时候不准。数据库支持系统获取方式Mysql windows通过查找mysqld进程,获取安装路径和数据库的配置信息Mysqllinux通过ps -ef |grep mysqld |grep basedir来定位进程oraclewindows通过查找oracle进程,获取安装路径,然

22、后在运行sqlplus就能获取版本信息了oraclelinux通过ps -ef |grep oracle |awk /LISTENER/print $8来定位置进程,在通过sqlplus获取版本信息sqlserverwindows通过查找sqlserver进程,获取相关信息syabsewindwos通过sqlsrc进程,获取相关信息(6)已经测试环境1.win7 mysql5.1 正常 (mysql是通过mysqld程序获取信息的)2.readhat5.6 mysql5.0.77 正常3.windwos server 2008 oracle11.2.0.1.0 正常 (在windows系统下是通过查询oracl.exe程序来获取信息的)4.centos6.4 oracle11.2.0.1.0 正常 (在linux系统下是通过查询oracle的监听进程来做获取信息的 )5.windows server 2003 sqlserver2005 正常(通过检测sqlservr.exe程序来获取安装路径)6.windows server 2003 sybase ase1254 正常(通过检测sqlsrvr.exe程序来获取安装路径)7.SunOS sunv440 不支持

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号