cmdb客户端服务器信息采集一
#cmdb脚本程序一
#!/usr/bin/python
# coding:utf-8 """
采集机器自身信息
1 主机名
2 内存
3 ip与mac地址
4 cpu信息
5 硬盘分区信息
6 制造商信息
7 出厂日期
8 系统版本
"""
import socket
import psutil
import subprocess
import time
import platform
import json
import requests device_white = ['eth1', 'eth2', 'eth3', 'bond0', 'bond1'] def get_hostname():
return socket.gethostname() def get_meminfo():
with open("/proc/meminfo") as f:
tmp = int(f.readline().split()[1])
return tmp / 1024 def get_device_info():
ret = []
for device, device_info in psutil.net_if_addrs().iteritems():
if device in device_white:
tmp_device = {}
for sinc in device_info:
if sinc.family == 2:
tmp_device['ip'] = sinc.address
if sinc.family == 17:
tmp_device['mac'] = sinc.address
ret.append(tmp_device)
return ret def get_cpu_info():
ret = {'cpu':'','num':0}
with open('/proc/cpuinfo') as f:
for line in f:
tmp = line.split(":")
key = tmp[0].strip()
if key == "processor":
ret['num'] += 1
if key == "model name":
ret['cpu'] = tmp[1].strip()
return ret def get_disk_info():
cmd = """/sbin/fdisk -l|grep Disk|egrep -v 'identifier|mapper|Disk label'"""
disk_data = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
patition_size = []
for dev in disk_data.stdout.readlines():
# size = int(dev.strip().split()[4]) / 1024 / 1024/ 1024
size = int(dev.strip().split(',')[1].split()[0]) / 1024 / 1024/ 1024
patition_size.append(str(size))
return " + ".join(patition_size) # 获取制造商信息
def get_manufacturer_info():
ret = {}
cmd = """/usr/sbin/dmidecode | grep -A6 'System Information'"""
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()
elif 'UUID' in line:
ret['uuid'] = line.split(':')[1].strip()
return ret # 获取出厂日期
def get_real_date():
cmd = """/usr/sbin/dmidecode | grep -i release"""
date_data = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
real_date = date_data.stdout.readline().split(':')[1].strip()
return time.strftime('%Y-%m-%d', time.strptime(real_date, "%m/%d/%Y")) def get_os_version():
return ' '.join(platform.linux_distribution()) def get_innerip(ipinfo):
inner_device = ['eth1', 'bond0']
ret = {}
for info in ipinfo:
if info.has_key('ip') and info.get('device', None) in inner_device:
ret['ip'] = info.get('ip')
ret['mac_address'] = info.get('mac')
return ret
return {} def main():
data = {}
data['hostname'] = get_hostname()
device_info = get_device_info()
data.update(get_innerip(device_info))
data['ipinfo'] = json.dumps(device_info) cpu_info = get_cpu_info()
data['server_cpu'] = "{cpu} {num}".format(**cpu_info)
data['server_disk'] = get_disk_info()
data['server_mem'] = get_meminfo()
data.update(get_manufacturer_info())
data['manufacture_date'] = get_real_date()
data['os'] = get_os_version()
if 'virtualbox' == data['server_type']:
data['vm_status'] = 0
else:
data['vm_status'] = 1
# return data
send(data) def send(data):
#通过向服务端的api接口操作写入服务数据库,因为这里我们暂时没有搭配,暂时空余
#url = "http://39.106.11.3:8000/resources/server/reporting/"
#r = requests.post(url, data = data)
#print r
print data if __name__ == "__main__":
main()
cmdb客户端服务器信息采集一的更多相关文章
- 搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 (1)
搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 原文地址(英文):http://www.networkcomms.net/creating ...
- 【读书笔记】iOS-使用Web Service-基于客户端服务器结构的网络通信(一)
Web Service技术是一种通过Web协议提供服务,保证不同平台的应用服务可以互操作,为客户端程序提供不同的服务. 目前3种主流的Web Service实现方案用:REST,SOAP和XML-RP ...
- 可以创建专业的客户端/服务器视频会议应用程序的音频和视频控件LEADTOOLS Video Conferencing SDK
LEADTOOLS Video Streaming Module控件为您创建一个自定义的视频会议应用程序和工具提供所有需要的功能.软件开发人员可以使用Video Streaming Module SD ...
- Linux 下 简单客户端服务器通讯模型(TCP)
原文:Linux 下 简单客户端服务器通讯模型(TCP) 服务器端:server.c #include<stdio.h> #include<stdlib.h> #include ...
- MySQL1:客户端/服务器架构
一.MySQL的客户端/服务器架构 前言 之前对MySQL的认知只限于会写些SQL,本篇算是笔记,记录和整理下自己对MySQL不熟悉的地方. 大致逻辑: MySQL的服务器程序直接和我们存储的数据打交 ...
- 客户端-服务器通信安全 sign key
API接口签名校验,如何安全保存appsecret? - 知乎 https://www.zhihu.com/question/40855191 要保证一般的客户端-服务器通信安全,可以使用3个密钥. ...
- python 异步IO-aiohttp与简单的异步HTTP客户端/服务器
参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143209814 ...
- QT--TCP网络编程(客户端/服务器)
QT -----TCP网络编程 1.主要流程 1.客户端 创建QTcpSocket对象 连接到服务器 --connectToHost() 发送数据 ---write() 读取数据 ---readA ...
- PostgreSQL编码格式:客户端服务器、客户端、服务器端相关影响
关于字符编码这块,官网链接: https://www.postgresql.org/docs/current/charset.html 刚刚写了几百字的东西因为断网,导致全没有了,重头再写,我就只想记 ...
随机推荐
- how to read openstack code: action extension
之前我们看过了core plugin, service plugin 还有resource extension. resource extension的作用是定义新的资源.而我们说过还有两种exten ...
- linux定时重启节约内存
linux服务器上运行的一些程序,比较消耗内存,需要定时重启,进行内存定期释放 0 2 * * * sudo /sbin/reboot && echo $(date) '重启成功' ...
- the attribute buffer size is too small 解决方法
在进行查询的时候引发The attribute buffer size is too small错误解决 http://bbs.esrichina-bj.cn/esri/viewthread.php? ...
- 【深度探索c++对象模型】Function语义学之虚函数
虚函数的一般实现模型:每一个class有一个virtual table,内含该class中的virtual function的地址,然后每个object有一个vptr,指向virtual table. ...
- iOS远程推送原理
远程推送 就是从远程server推送消息给client的通知.当然须要联网. 远程推送服务APNs (Apple Push NotificationServices) 为什么须要远程推送通知? 传统获 ...
- UI 07 _ 导航视图控制器 与 属性传值
首先, 先创建三个VC. 完毕点击按钮, 进入下一页, 并可以返回. 要先把导航视图控制器创建出来. 在AppDelegate.m 文件里代码例如以下: #import "AppDelega ...
- curl_setopt GET的方法
$ch ") ; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; curl_setopt($ch, CURLOPT_BINARYTRANSF ...
- javascript/jquery模板引擎——Handlebars初体验
Handlebars.js下载地址:http://handlebarsjs.com/ 最近自己在建一个站,采用完全的前后端分离的方式,现在正在做前端的部分.其中有项功能是需要ajax调用后端接口,返回 ...
- 李洪强iOS开发之录音和播放实现
李洪强iOS开发之录音和播放实现 //首先导入框架后,导入头文件.以下内容为托控件,在storyboard中拖出两个按钮为录音和播放按钮 //创建一个UIViewController在.h文件中写 # ...
- PyTorch 60 分钟入门教程:数据并行处理
可选择:数据并行处理(文末有完整代码下载) 作者:Sung Kim 和 Jenny Kang 在这个教程中,我们将学习如何用 DataParallel 来使用多 GPU. 通过 PyTorch 使用多 ...