zoukankan      html  css  js  c++  java
  • 获取centos6.5系统信息脚本

    最近想尝试做两件比较重要的事情,第一是用python写个cmdb,第二还是用python写个小型监控系统,下面是获取系统信息的脚本:

    #!/usr/bin/env python
    # coding:utf-8
    
    
    import json
    import subprocess
    import psutil
    import socket
    import time
    import re
    import platform
    import requests
    
    
    device_white = ['eth0','eth1', 'eth2', 'eth3', 'em1']
    
    headers = {"Content-Type": "application/json"}
    
    
    def get_hostname():
        return socket.gethostname()
    
    def get_device_info():
        ret = []
        for device, info in psutil.net_if_addrs().iteritems():
            if device in device_white:
                device_info = {'device': device}
                for snic in info:
                    if snic.family == 2:
                        device_info['ip'] = snic.address
                    elif snic.family == 17:
                        device_info['mac'] = snic.address
                ret.append(device_info)
        return ret
    
    def get_cpuinfo():
        ret = {"cpu": '', 'num': 0}
        with open('/proc/cpuinfo') as f:
            for line in f:
                line_list = line.strip().split(':')
                key = line_list[0].rstrip()
                if key == "model name":
                    ret['cpu'] = line_list[1].lstrip()
                if key == "processor":
                    ret['num'] += 1
        return ret
    
    def get_disk():
        cmd = """/sbin/fdisk -l|grep Disk|egrep -v 'identifier|mapper|Disklabel'"""
        disk_data = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        partition_size = []
        for dev in disk_data.stdout.readlines():
            try:
                size = int(dev.strip().split(', ')[1].split()[0]) / 1024 / 1024 / 1024
                partition_size.append(str(size))
            except:
                pass
        return " + ".join(partition_size)
    
    def get_Manufacturer():
        cmd = """/usr/sbin/dmidecode | grep -A6 'System Information'"""
        ret = {}
        manufacturer_data = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        for line in manufacturer_data.stdout.readlines():
            if "Manufacturer" in line:
                ret['manufacturers'] = line.split(': ')[1].strip()
            elif "Product Name" in line:
                ret['server_type'] = line.split(': ')[1].strip()
            elif "Serial Number" in line:
                ret['st'] = line.split(': ')[1].strip().replace(' ','')
            elif "UUID" in line:
                ret['uuid'] = line.split(': ')[1].strip()
        return ret
        #return manufacturer_data.stdout.readline().split(': ')[1].strip()
    
    # 出厂日期
    def get_rel_date():
        cmd = """/usr/sbin/dmidecode | grep -i release"""
        data = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        date = data.stdout.readline().split(': ')[1].strip()
        return re.sub(r'(d+)/(d+)/(d+)',r'3-1-2',date)  
    
    def get_os_version():
        return " ".join(platform.linux_distribution())
    
    def get_innerIp(ipinfo):
        inner_device = ["eth0", "bond0"]
        ret = {}
        for info in ipinfo:
            if info.has_key('ip') and info.get('device', None) in inner_device:
                ret['ip'] = info['ip']
                ret['mac_address'] = info['mac']
                return  ret
        return {}
    
    def get_Memtotal():
        with open('/proc/meminfo') as mem_open:
            a = int(mem_open.readline().split()[1])
            return a / 1024 
        
    
    def run(): 
        data = {}
        res = {}
        data['hostname'] = get_hostname()
        data.update(get_innerIp(get_device_info()))
        cpuinfo = get_cpuinfo()
        data['server_cpu'] = "{cpu} {num}".format(**cpuinfo)
        data['server_disk'] = get_disk()
        data.update( get_Manufacturer())
        data['manufacture_date'] = get_rel_date()
        data['os'] = get_os_version()
        data['server_mem'] = get_Memtotal()
        if "VMware" in data['manufacturers']:
            data['vm_status'] = 0
        else:
            data['vm_status'] = 1
        res['params']=data
        res['jsonrpc'] = "2.0"
        res["id"] = 1
        res["method"]= "server.radd"
     #   print res 
     #   for k,v in data.iteritems():
     #       print k, v
        send(res)
    
    def send(data):
        url = "http://192.168.63.182:2000/api"
        r = requests.post(url, headers=headers,json=data)
        print r.status_code
        print r.content
    
    
    if __name__ == "__main__":
       run()
    

      

  • 相关阅读:
    Angular $http解析通过接口获得的json数据
    Python基础(十四)—装饰器 wrapper
    Python基础(十二)—面向对象拾遗(__slots__、@property、枚举类、元类)
    Python基础(十三)—切片、yield、生成器、序列化JSON
    Python基础(十一)—魔法方法(构造析构、属性魔法、算数魔法、类型魔法等)
    Python基础(十)—面向对象的深入讲解(继承、Mixin编程机制等)
    Python基础(九)—异常except、else&with、异常的名称层次
    Python基础(八)—编码详解(ASCII、GBK、Unicode、UTF-8等)、decode&encode
    Python基础(七)—输入输出(open、os、pickle)
    Python基础(六)—函数式编程(内部函数、闭包、lambda、filter/map/reduce/sorce、偏函数)
  • 原文地址:https://www.cnblogs.com/uglyliu/p/6217892.html
Copyright © 2011-2022 走看看