跨平台 获取系统信息的python库 http://support.hyperic.com/disp

[size=large]The Sigar API provides a portable interface for gathering system information such as:

* System memory, swap, cpu, load average, uptime, logins
* Per-process memory, cpu, credential info, state, arguments, environment, open files
* File system detection and metrics
* Network interface detection, configuration info and metrics
* Network route and connection tables

Linux Windows 都可以用


内存检测的库 http://support.hyperic.com/display/SIGAR/Home
git clone git://github.com/hyperic/sigar.git sigar.git
./autogen.sh
./configure
make
make install
cd bindings/python/
python setup.py install


xxx@xxxx~/hypertable/sigar.git/bindings/python/examples $ cat free.py
import os, sigar;

sg = sigar.open()
mem = sg.mem()
swap = sg.swap()
sg.close()

print "\tTotal\tUsed\tFree"

print "Mem:\t",\
(mem.total() / 1024), \
(mem.used() / 1024), \
(mem.free() / 1024)

print "Swap:\t", \
(swap.total() / 1024), \
(swap.used() / 1024), \
(swap.free() / 1024)

print "RAM:\t", mem.ram(), "MB"

xxx@xxxx~/hypertable/sigar.git/bindings/python/examples $ python free.py
Total Used Free
Mem: 33018784 20918484 12100300
Swap: 0 0 0
RAM: 32248 MB
xxx@xxxx~/hypertable/sigar.git/bindings/python/examples $ cat df.py
import os, sigar;

sg = sigar.open()
fslist = sg.file_system_list()

def format_size(size):
return sigar.format_size(size * 1024)

print 'Filesystem\tSize\tUsed\tAvail\tUse%\tMounted on\tType\n'

for fs in fslist:
dir_name = fs.dir_name()
usage = sg.file_system_usage(dir_name)

total = usage.total()
used = total - usage.free()
avail = usage.avail()
pct = usage.use_percent() * 100
if pct == 0.0:
pct = '-'

print fs.dev_name(), format_size(total), format_size(used), format_size(avail),\
pct, dir_name, fs.sys_type_name(), '/', fs.type_name()
xxx@xxxx~/hypertable/sigar.git/bindings/python/examples $ python df.py
Filesystem Size Used Avail Use% Mounted on Type

/dev/md/2 37G 26G 12G 69.0 / reiserfs / local
proc 0 0 0 - /proc proc / none
sysfs 0 0 0 - /sys sysfs / none
udev 10M 208K 9.8M - /dev tmpfs / none
devpts 0 0 0 - /dev/pts devpts / none
/dev/sda4 391G 321G 70G 83.0 /log2 reiserfs / local
/dev/sdd1 931G 281G 650G 31.0 /mp4 reiserfs / local
/dev/sde1 1.4T 1.3T 75G 95.0 /backup1 xfs / local
/dev/sdf1 1.4T 157G 1.2T 12.0 /backup2 xfs / local
shm 16G 0 16G - /dev/shm tmpfs / none
tmpfs 40M 0 40M - /tmplog tmpfs / none
usbfs 0 0 0 - /proc/bus/usb usbfs / none
/dev/sdb1 1.4T 566G 831G 41.0 /mp5 vfat / local
/dev/sdc1 200G 82G 118G 41.0 /mp3 reiserfs / local

[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigar 是一个跨平台系统信息收集,可以获取系统的 CPU、内存、磁盘、网络等信息。以下是获取系统信息的示例代码: ```java import org.hyperic.sigar.*; public class SigarDemo { public static void main(String[] args) throws SigarException { Sigar sigar = new Sigar(); System.out.println("CPU信息:"); CpuInfo[] cpuInfos = sigar.getCpuInfoList(); for (CpuInfo cpuInfo : cpuInfos) { System.out.println("CPU型号:" + cpuInfo.getModel()); System.out.println("CPU频率:" + cpuInfo.getMhz() + "MHz"); System.out.println("CPU核数:" + cpuInfo.getTotalCores()); } System.out.println("内存信息:"); Mem mem = sigar.getMem(); System.out.println("总内存:" + mem.getTotal() / 1024 / 1024 + "MB"); System.out.println("已用内存:" + mem.getUsed() / 1024 / 1024 + "MB"); System.out.println("剩余内存:" + mem.getFree() / 1024 / 1024 + "MB"); System.out.println("磁盘信息:"); FileSystem[] fileSystems = sigar.getFileSystemList(); for (FileSystem fileSystem : fileSystems) { System.out.println("盘符:" + fileSystem.getDirName()); System.out.println("盘符类型:" + fileSystem.getTypeName()); FileSystemUsage usage = sigar.getFileSystemUsage(fileSystem.getDirName()); System.out.println("总大小:" + usage.getTotal() / 1024 / 1024 + "MB"); System.out.println("已用大小:" + usage.getUsed() / 1024 / 1024 + "MB"); System.out.println("剩余大小:" + usage.getFree() / 1024 / 1024 + "MB"); } System.out.println("网络信息:"); NetInterfaceConfig[] netConfigs = sigar.getNetInterfaceConfigList(); for (NetInterfaceConfig netConfig : netConfigs) { System.out.println("网络设备名:" + netConfig.getName()); System.out.println("IP地址:" + netConfig.getAddress()); System.out.println("子网掩码:" + netConfig.getNetmask()); } } } ``` 注意:需要引入 sigar.jar 和 sigar-amd64-winnt.dll(或其它平台对应的文件)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值