获取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 ------ ...
随机推荐
- 使用SignalR实现消息提醒
Asp.net SignalR是微软为实现实时通信的一个类库.一般情况下,SignalR会使用JavaScript的长轮询(long polling)的方式来实现客户端和服务器通信,随着Html5中W ...
- OOM killer
Linux下有一种OOM KILLER 的机制,它会在系统内存耗尽的情况下,启用自己算法有选择性的kill 掉一些进程. 1. 为什么会有OOM killer 当我们使用应用时,需要申请内存,即进行m ...
- jQuery 3.0的domManip浅析
domManip 这个函数的历史由来已久,从 jQuery 1.0 版本开始便存在了,一直到最新的 jQuery 版本.可谓是元老级工具函数. domManip 的主要功能是为了实现 DOM 的插入和 ...
- mysql explain的使用(优化查询)
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 1.创建数据库 创建的sql语句如下: /* Navicat MySQL D ...
- Ubuntu 配置 no-ip
安装Python开发依赖包 sudo apt-get install python-dev 配置PIP并安装noipy sudo apt-get install python-pip sudo pip ...
- Redis学习总结
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API,其实当前最热门的NoSQL数据库之一,NoSQL还包括了Mem ...
- 理解ThreadLocal(之一)
ThreadLocal是什么 在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编 ...
- u3d_Shader_effects笔记6 第二章 animating sprite
1.前面的心情 上班看shader我也是醉了.写完这篇看代码去了,不过看着看着恐怕就会困.... 还有就是上天,我该怎么做,下一步,大懒: 2.参考源头 http://blog.csdn.net/ca ...
- ubuntu 14.04 以root权限启动chrome
chrome版本 51.0.2704.103 How to run google chrome as root in linux - Unix & Linux Stack Exchange提示 ...
- sublime 插件
由于之前的代码可视化方案太复杂,分析时间太长,不实用,另一方面是而且工作以后业余时间大大减少,因此决定放弃原有路线,从工作中最迫切的需求着手,逐步构建一个实用的工具. 新的方法仍然依赖understa ...