Sigar是System Information Gatherer And Reporter的缩写。
Sigar API提供了一系列可移植的接口用来收集操作系统信息,诸如, 1 2 3 4 5 6 | 1. System memory, swap, cpu, load average, uptime, logins 2. Per-process memory, cpu, credential info, state, arguments, environment, open files 3. File system detection and metrics 4. Network interface detection, configuration info and metrics 5. TCP and UDP connection tables 6. Network route table |
好了,就介绍这么多吧。接下来看看如何在CentOS6.5上完成安装并使用呢?
安装过程很简单,需要安装epel的yum源。
1 2 3 4 5 6 7 8 9 10 11 | wget http: //dl .fedoraproject.org /pub/epel/6/x86_64/epel-release-6-8 .noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm yum install gcc yum install python-devel yum install sigar sigar-devel git clone git: //github .com /hyperic/sigar .git sigar.git cd sigar.git/ cd bindings /python/ python setup.py install |
安装完毕,进行简单的验证,如
1 2 3 4 5 6 | [root@localhost examples] # python Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2 Type "help" , "copyright" , "credits" or "license" for more information. >>> import sigar >>> |
由上面的输出,可以看到sigar已经安装完毕。
接下来,写个简单的脚本看看,sigar模块在python的基本使用,脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | # cat ifconfig.py #!/usr/bin/env python # -*- coding: utf-8 -*- # ldczz2008@163.com import os import sigar sg = sigar. open () def format_size(size): return sigar.format_size(size) # iflist是一个元组,是本机的网卡列表 iflist = sg.net_interface_list() # 循环得到每个网卡的参数信息 for ifname in iflist: ifconfig = sg.net_interface_config(ifname) flags = ifconfig .flags() encap = ifconfig . type () hwaddr = ifconfig .hwaddr() bcast = ifconfig .broadcast() addr4 = ifconfig .address() nmask = ifconfig .netmask() mtu = ifconfig .mtu() metric = ifconfig .metric() if hwaddr == "00:00:00:00:00:00" : hwaddr = "" else : hwaddr = "HWaddr " + hwaddr # 打印网卡基本信息 print "[ %s ], Link encap: %s, %s" % (ifname, encap, hwaddr) print "inet addr:" , addr4 print "Bcast: " , bcast print "Mask: " , nmask print "MTU: " , mtu print "Metric: " , metric # 获得网卡的状态值 # RX信息 # TX信息 ifstat = sg.net_interface_stat(ifname) print "以下是网卡[ %s ]的RX统计信息" % ifname print "\tRX packets:" , ifstat.rx_packets() print "\tErrors: " , ifstat.rx_errors() print "\tDropped: " , ifstat.rx_dropped() print "\tOverruns: " , ifstat.rx_overruns() print "\tFrame: " , ifstat.rx_frame() print "以下是网卡[ %s ]的TX统计信息" % ifname print "\tTX packets:" , ifstat.tx_packets() print "\tErrors: " , ifstat.tx_errors() print "\tDropped: " , ifstat.tx_dropped() print "\tOverruns: " , ifstat.tx_overruns() print "\tCarrier: " , ifstat.tx_carrier() print "\tCollisions:" , ifstat.tx_collisions() print "\tRx bytes: " , format_size(ifstat.rx_bytes()) print "\tTx bytes: " , format_size(ifstat.tx_bytes()) print sg.close() |
运行效果为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | # python ifconfig.py [ lo ], Link encap: Local Loopback, inet addr: 127.0.0.1 Bcast: 0.0.0.0 Mask: 255.0.0.0 MTU: 16436 Metric: 1 以下是网卡[ lo ]的RX统计信息 RX packets: 2780 Errors: 0 Dropped: 0 Overruns: 0 Frame: 0 以下是网卡[ lo ]的TX统计信息 TX packets: 2780 Errors: 0 Dropped: 0 Overruns: 0 Carrier: 0 Collisions: 0 Rx bytes: 1.5M Tx bytes: 1.5M [ eth0 ], Link encap: Ethernet, HWaddr 00:00:00:AA:00:CC inet addr: 192.168.0.122 Bcast: 192.168.0.255 Mask: 255.255.255.0 MTU: 1500 Metric: 1 以下是网卡[ eth0 ]的RX统计信息 RX packets: 224886 Errors: 0 Dropped: 0 Overruns: 0 Frame: 0 以下是网卡[ eth0 ]的TX统计信息 TX packets: 168626 Errors: 0 Dropped: 0 Overruns: 0 Carrier: 0 Collisions: 0 Rx bytes: 279M Tx bytes: 14M |
怎么样?不是很难?
:-)