获取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 ------ ...
随机推荐
- Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理9
前两天因有事就没来得及写.今天刚刚好空了.这次写的是对角色和管理员对页面按钮之间的控制.先看页面效果 说明:先根据角色设置好角色的权限,然后管理员在对应的角色下的权限去设置其权限. 在设置角色权限的时 ...
- x01.Weiqi.12: 定式布局
定式 下一步当将定式保存到数据库中,如布局中的代码所示,但其初始的代码更有利于理解.以小飞挂为例: // 0 // + 0 0 // + // // + List<Pos> P_LuSta ...
- 每建一个Activity都要注册权限Manifest.xml
每建一个Activity都要注册权限Manifest.xml 但是有时候自动注册好了,注意!不然的话是不能调用的!!!!!<activity android:name=".MainVi ...
- mysql常用基本操作
mysql常用操作 查看都有哪些库 show databases; 查看某个库的表 use 库名; show tables; 查看表的字段 desc 表名; 当前是哪个用户 select user() ...
- Neutron 理解 (8): Neutron 是如何实现虚机防火墙的 [How Neutron Implements Security Group]
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- Linux Kernel代码艺术——系统调用宏定义
我们习惯在SI(Source Insight)中阅读Linux内核,SI会建立符号表数据库,能非常方便地跳转到变量.宏.函数等的定义处.但在处理系统调用的函数时,却会遇到一些麻烦:我们知道系统调用函数 ...
- jsp前3章试题分析
/bin:存放各种平台下用于启动和停止Tomcat的脚本文件 /logs:存放Tomcat的日志文件 /webapps:web应用的发布目录 /work:Tomcat把由JSP生成的Servlet存放 ...
- IntelliJ IDEA 快捷键大全
IntelliJ IDEA 快捷键大全 (2012-03-27 20:33:44) 转载▼ 标签: ide intellij快捷键 杂谈 分类: IDE工具 最近刚接触IntelliJ这个工具,用了几 ...
- java多线程系类:JUC线程池:03之线程池原理(二)(转)
概要 在前面一章"Java多线程系列--"JUC线程池"02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包 ...
- 系统修改利器XueTr
Windows系统修改利器XueTr 周银辉 在Windows下如果遇到某些进程弄死结束不了,某些文件弄死删不掉,拷贝不出来 (可能是因为你没有管理员权限,可能是因为人家是病毒,可能是系统保护文件,可 ...