获取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()
获取centos6.5系统信息脚本的更多相关文章
- 运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程,把获取的信息存入数据库
运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程 有关前两篇的链接: 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 运 ...
- 运用Python语言编写获取Linux基本系统信息(二):文件系统使用情况获取
本文跟着上一篇文章继续写,上一篇文章的链接 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 一.随便说说 获取文件系统使用情况的思路和上一篇获取主要系统是 ...
- 重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息
原文:重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息 [源码下载] 重新想象 Windows 8 Store ...
- java web 获取客户端操作系统信息
package com.java.basic.pattern; import java.util.regex.Matcher; import java.util.regex.Pattern; /** ...
- sql 获取批处理信息的脚本(优化器在处理批处理时所发生的优化器事件)
--获取批处理信息的脚本(优化器在处理批处理时所发生的优化器事件) SET NOCOUNT ON; DBCC FREEPROCCACHE; --清空过程缓存 GO --使用tempdb..Optsta ...
- 获取ios设备系统信息的方法 之 [UIDevice currentDevice]
获取iphone的系统信息使用[UIDevice currentDevice],信息如下: [[UIDevice currentDevice] systemName]:系统名称,如iPhone OS ...
- centos6服务启动脚本及开机启动过程
centos6服务启动脚本 centos6的服务启动脚本都放在/etc/rc.d/init.d/下,/etc/init.d/是/etc/rc.d/init.d/的软链接: centos6的服务启动脚本 ...
- 获取网站title的脚本
脚本在此 公司的商城需要添加一个脚本,这个脚本就是观察首页页面是否正常,虽然已经配置了zabbix监控网站是否200,但是有一些特殊的情况,比如网页可以打开但是页面是"file not fo ...
- DOS windows 使用bat脚本获取 IP MAC 系统信息
@echo select disk 0 >dpjs.txt @echo detail disk >>dpjs.txt diskpart /s dpjs.txt@echo ------ ...
随机推荐
- mybatis 配置返回集合collection时只有一条记录
查询语句配置如下: <select id="selectCustomerList" resultMap="CustomerDtoMap" paramete ...
- Keystone 命令汇总
Keystone 命令汇总 目录 [隐藏] 1 用户(User) 1.1 查看用户列表 1.2 创建用户 1.3 删除用户 1.4 显示用户详细信息 1.5 更新用户的密码 1.6 赋予用户一个角 ...
- ubuntu 下安装 texlive 并设置 ctex 中文套装
1 安装 texlive2013 1.1 下载 texlive2013 下载地址:http://ftp.ctex.org/mirrors/CTAN/systems/texlive/Images/tex ...
- linux命令之tail
tail用于输出文件末尾部分.一个比较有用的功能是tail + grep实现类似于安卓开发时调试使用的logcat,具体操作是: 一般我是用SecureCRT连接linux,然后使用SecureCRT ...
- Visual Studio SetSite failed for package [JavaScriptWebExtensionsPackage] 错误解决方案一则
安装 AspNet5.ENU.RC1.exe Microsoft ASP.NET and Web Tools 2015 (RC) – Visual Studio 2015 打开VS后发生了错误 < ...
- Linux字符串截取命令
一.简单截取 假设有变量 var=http://www.google.com/test.htm 1. # 号截取,删除左边字符,保留右边字符.echo ${var#*//}其中 var 是变量名,# ...
- [django]表格的添加与删除实例(可以借鉴参考)
自己并未采用任何表格插件,参考网上例子,自己编写出来的django网页实例,请各位参考! 首先看图做事,表格布局采用bootstrap,俗话说bootstrap橹多了就会css了,呵呵,下面看图: 上 ...
- Web报表工具FineReport中JavaScript的使用
报表软件FineReport采用的是jQuery v1.9.2框架,jQuery是一个快速的,简洁的JavaScript库,能让用户更方便地处理HTML documents.events,实现动画效果 ...
- JS入门学习,编写一个简易月历
//今天最头疼的地方在于 getElementsByClassName()的 [] ~~ //错了N遍后只能说有点点头绪,如果不加[] 查找的就是全部吧 加上[]能精确控制的标签或者class < ...
- 最小生成树 kruskal算法 codevs 1638 修复公路
1638 修复公路 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description A地区在地震过后,连接所有村庄的公 ...