python_收集服务器基本信息

之前都是一直自己写模块收集服务器基本信息,后来看到facter收集的信息很全面,所以就拿来用用,嘿嘿

1、facter安装

facter是基于puppet的,所以安装puppet即可。

yum -y install puppet

2、facter收集的数据:

architecture => x86_64
augeasversion => 0.9.0
blockdevice_xvda_size => 8589934592
blockdevice_xvdd_size => 119197696
blockdevices => xvda,xvdd
facterversion => 1.7.2
filesystems => ext4,iso9660
hardwareisa => x86_64
hardwaremodel => x86_64
hostname => client155
id => root
interfaces => eth0,lo
ipaddress => 192.168.70.155
ipaddress_eth0 => 192.168.70.155
ipaddress_lo => 127.0.0.1
is_virtual => true
kernel => Linux
kernelmajversion => 2.6
kernelrelease => 2.6.32-358.14.1.el6.x86_64
kernelversion => 2.6.32
macaddress => DA:F4:FB:4A:B1:4C
macaddress_eth0 => DA:F4:FB:4A:B1:4C
memoryfree => 679.69 MB
memoryfree_mb => 679.69
memorysize => 992.65 MB
memorysize_mb => 992.65
memorytotal => 992.65 MB
mtu_eth0 => 1500
mtu_lo => 16436
netmask => 255.255.255.0
netmask_eth0 => 255.255.255.0
netmask_lo => 255.0.0.0
network_eth0 => 192.168.70.0
network_lo => 127.0.0.0
operatingsystem => CentOS
operatingsystemmajrelease => 6
operatingsystemrelease => 6.4
osfamily => RedHat
path => /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
physicalprocessorcount => 1
processor0 => Intel(R) Xeon(R) CPU E5-2609 0 @ 2.40GHz
processorcount => 1
ps => ps -ef
puppetversion => 3.2.3
rubysitedir => /usr/lib/ruby/site_ruby/1.8
rubyversion => 1.8.7
selinux => false
sshdsakey => AAAAB3NzaC1kc3MAAACBANuHscOK1ru99yB6ZjLVXJYGc2tyydQMnpz3oMo954fkmaOITiPSdTFMdgzNx3nNtTLoSwTP4rtxOfr00ZP56nRm2xgBbRwXMFNU67p3pc3+qcL4RpXPMhoJ0CpQI2QAX5pfBrpSypYNXGdp9ra4NHtSymDmKLiKQJJzoUmTcIXTAAAAFQCj7pN+CXbx6mHlfOe+fuTMfdgsiQAAAIAPicJQzwRVLG16sTFTfiY4515qy2OvUDERD0YHmq9oFQmodS8B90mImbyh6NTHN742DQWDK2W3gpdeSBqDuT1aGDH2wvwsnopZzir9Qk2ZhZV69gLCpWKUrrpZ/qwNQmiJAEnx1S2PaWjwsQGRea06XnIiqs1aYj8+TvlKwSf9jQAAAIBp0ysDa58epQOW/7qM2/YllTpt9vxKCqBQrwwuicSc7ITgVZGZ2Vmx/6kaAAYyrizeUQ5VD/E/gLPAbkxrldReQzrKFbug1X1UngDhY30h7n2Vaq85fkprhPnY+TTXLjFQ2QflnO/3pL3rgGJ3MKhbcDsh3Xc1MiFKf1fxv88Ucg==
sshfp_dsa => SSHFP 2 1 b59363f3c105a2a13138cc682814493f540ac7a8
SSHFP 2 2 6470614ed0054e961af57ac29863b6850e9c910549a833a1aa5523c983680130
sshfp_rsa => SSHFP 1 1 51811ef597cc552c8adf5d8362d9709283a051f1
SSHFP 1 2 8e6876cbc3a8da53b63498231bcb2cec72c7e9dfce9224146d43757aeffb1e32
sshrsakey => AAAAB3NzaC1yc2EAAAABIwAAAQEAtsF2BtHxbAXTUuzkLyyFHhdLkw2nY4bQEk3Gx/SoTEA3I+12fOwNbHFUFptj1PPbowsAagb2VHfqJnEd/2imhY76T6K07a1Hry9k+9GpQzRnNjaAEiVI3sY01x7Gkrvg8lghdZBi7p5y6rqKVjfP9nY6DAQlgx9XU9FpJ3lT2+eUtt7WirBBafaijwJkHtKeEluEJJ009WwLM6U0gaJjF5cUtqSdQ1lFUUFNmvMQz3qgQV3gcJSskvQSwW4dimGKTYUtxhdEeBeb7uNYqiLc/wP/cJ0grtvtPtBDlUTmdugeq7OrZUc0NxAZqmqHqSfO0Aw4XaOuFJrs/w7nNM+2UQ==
swapfree => 1.97 GB
swapfree_mb => 2015.99
swapsize => 1.97 GB
swapsize_mb => 2015.99
timezone => CST
uniqueid => 00000000
uptime => 2 days
uptime_days => 2
uptime_hours => 53
uptime_seconds => 192700
virtual => xenu

3、脚本代码:

收集到的数据转换为json格式,以api接口形式提交给数据收集器。

#!/usr/bin/python

from subprocess import PIPE,Popen
import urllib,urllib2,json

def facter():
    cmd = 'facter'
    subf = Popen(cmd,stdout=PIPE)
    return subf.stdout.readlines()

def parser_facter(string):
    facter_list = {}
    for line in string:
        if '=>' not in line:
            continue
        l = line.split('=>')
        facter_list['%s'%l[0].strip()] = '%s'%l[1].strip()
    return facter_list
def tranjson(d):
    js = json.dumps(d)
    print js

def sendInfo(d):
    urllib2.urlopen('http://192.168.70.127:8000/api/collectjson',json.dumps(d))

if __name__ == '__main__':
    info_list = parser_facter(facter())
    sendInfo(info_list)

你可能感兴趣的:(python)