最近想尝试做两件比较重要的事情,第一是用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()

  

获取centos6.5系统信息脚本的更多相关文章

  1. 运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程,把获取的信息存入数据库

    运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程 有关前两篇的链接: 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 运 ...

  2. 运用Python语言编写获取Linux基本系统信息(二):文件系统使用情况获取

    本文跟着上一篇文章继续写,上一篇文章的链接 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 一.随便说说 获取文件系统使用情况的思路和上一篇获取主要系统是 ...

  3. 重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息

    原文:重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息 [源码下载] 重新想象 Windows 8 Store ...

  4. java web 获取客户端操作系统信息

    package com.java.basic.pattern; import java.util.regex.Matcher; import java.util.regex.Pattern; /** ...

  5. sql 获取批处理信息的脚本(优化器在处理批处理时所发生的优化器事件)

    --获取批处理信息的脚本(优化器在处理批处理时所发生的优化器事件) SET NOCOUNT ON; DBCC FREEPROCCACHE; --清空过程缓存 GO --使用tempdb..Optsta ...

  6. 获取ios设备系统信息的方法 之 [UIDevice currentDevice]

    获取iphone的系统信息使用[UIDevice currentDevice],信息如下: [[UIDevice currentDevice] systemName]:系统名称,如iPhone OS ...

  7. centos6服务启动脚本及开机启动过程

    centos6服务启动脚本 centos6的服务启动脚本都放在/etc/rc.d/init.d/下,/etc/init.d/是/etc/rc.d/init.d/的软链接: centos6的服务启动脚本 ...

  8. 获取网站title的脚本

    脚本在此 公司的商城需要添加一个脚本,这个脚本就是观察首页页面是否正常,虽然已经配置了zabbix监控网站是否200,但是有一些特殊的情况,比如网页可以打开但是页面是"file not fo ...

  9. DOS windows 使用bat脚本获取 IP MAC 系统信息

    @echo select disk 0 >dpjs.txt @echo detail disk >>dpjs.txt diskpart /s dpjs.txt@echo ------ ...

随机推荐

  1. Unity贴图锯齿

    之前在做一个Unity视频插件,发现渲染上去的贴图锯齿十分明显,开了抗锯齿也没用.最后在一次偶然的机会,发现了原来是贴图FilterMode设置有问题 之前用的是FilterMode.Point,后来 ...

  2. Shelve Instance 操作详解 - 每天5分钟玩转 OpenStack(38)

    Instance 被 Suspend 后虽然处于 Shut Down 状态,但 Hypervisor 依然在宿主机上为其预留了资源,以便在以后能够成功 Resume. 如果希望释放这些预留资源,可以使 ...

  3. openstack想说爱你不容易

    网上一牛人的博客专门写的是关于openstack的,看晕了.先收藏下.猛击下面的地址 http://www.cnblogs.com/popsuper1982/

  4. 推荐一款 chrome SSH 插件 - Secure Shell

    http://blog.csdn.net/jizhongchun/article/details/8519413 导读:我就是一个Chrome Fans.最近发现一款google官方出的SSH插件 - ...

  5. linux下的一些操作(持续更新)

    文件操作 创建文件夹: mkdir 文件夹名称 查看当前目录的文件夹及文件:ls 参看当前文件夹下的所有文件及信息: ls -l 删除空文件夹:rmdir 文件夹名称 删除非空文件夹:rm rf 文件 ...

  6. Postgresql扩展及UUID

    切换数据库 \connect $DBNAME 查看Postgresql的可用扩展 SELECT * FROM pg_available_extensions; 安装所需扩展 CREATE EXTENS ...

  7. ARM嵌入式开发板

    iTOP-4412 ARM嵌入式开发板----主要特点 iTOP-4412开发平台是北京迅为电子研发设计的嵌入式开发板平台,核心板配备64位双通道2GB DDR3,16GBEMMC存储,三星原厂S5M ...

  8. EF6 DataMigration 从入门到进阶

    引言 在EntityFramework的开发过程中我们有时因需求变化或者数据结构设计的变化经常会改动表结构.但数据库Schema发生变化时EF会要求我们做DataMigration 和UpdateDa ...

  9. uva 140 bandwidth (好题) ——yhx

     Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orde ...

  10. etl实现字段值相加

    数据库USERS表: etl步骤: (2) (3) 其中java代码为: import test.Test;          public boolean processRow(StepMetaIn ...