python执行shell获取硬件参数写入mysql的方法
作者:shichen2014 发布时间:2024-01-26 15:56:04
本文实例讲述了python执行shell获取硬件参数写入mysql的方法。分享给大家供大家参考。具体分析如下:
最近要获取服务器各种参数,包括cpu、内存、磁盘、型号等信息。试用了Hyperic HQ、Nagios和Snmp,它们功能都挺强大的,但是于需求不是太符,亦或者太heavy。
于是乎想到用python执行shell获取这些信息,python执行shell脚本有以下三种方法:
1. os.system()
os.system('ls')
#返回结果0或者1,不能得到命令的输出
2. os.popen()
output = os.popen('ls')
print output.read()
#打印出的是命令输出,但是得不到执行的返回值
3. commands.getstatusoutput()
(status, output) = commands.getstatusoutput('ls')
print status, output
#打印出返回值和命令输出
可以根据需要选取其中一种方法,以下是python执行shell获取硬件参数写入mysql,并定期更新的程序:
'''
Created on Dec 10, 2014
@author: liufei
'''
#coding=utf-8
import time, sched, os, string
from datetime import datetime
import MySQLdb
s = sched.scheduler(time.time,time.sleep)
def event_func():
try:
#主机名
name = os.popen(""" hostname """).read()
#cpu数目
cpu_num = os.popen(""" cat /proc/cpuinfo | grep processor | wc -l """).read()
#内存大小
mem = os.popen(""" free | grep Mem | awk '{print $2}' """).read()
#机器品牌
brand = os.popen(""" dmidecode | grep 'Vendor' | head -1 | awk -F: '{print $2}' """).read()
#型号
model = os.popen(""" dmidecode | grep 'Product Name' | head -1 | awk -F: '{print $2}' """).read()
#磁盘大小
storage = os.popen(""" fdisk -l | grep 'Disk /dev/sd' | awk 'BEGIN{sum=0}{sum=sum+$3}END{print sum}' """).read()
#mac地址
mac = os.popen(""" ifconfig -a | grep HWaddr | head -1 | awk '{print $5}' """).read()
name = name.replace("\n","").lstrip()
cpu_num = cpu_num.replace("\n","").lstrip()
memory_gb = round(string.atof(mem.replace("\n","").lstrip())/1000.0/1000.0, 1)
brand = brand.replace("\n","").lstrip()
model = model.replace("\n","").lstrip()
storage_gb = storage.replace("\n","").lstrip()
mac = mac.replace("\n","").lstrip()
print name
print cpu_num
print memory_gb
print storage_gb
print brand
print model
print mac
conn=MySQLdb.connect(host='xx.xx.xx.xx',user='USERNAME',passwd='PASSWORD',db='DBNAME',port=3306)
cur=conn.cursor()
cur.execute('select mac from servers where mac=%s',mac)
data = cur.fetchone()
if data is None:
value = [name, brand, model, memory_gb, storage_gb, cpu_num, mac, datetime.now(), datetime.now()]
cur.execute("insert into servers(name, brand, model, memory_gb, storage_gb, cpu_num, mac, created_at, updated_at) values(%s, %s, %s, %s, %s, %s, %s, %s, %s)",value)
else:
value1 = [name, brand, model, memory_gb, storage_gb, cpu_num, datetime.now(), mac]
cur.execute("update servers set name=%s,brand=%s,model=%s,memory_gb=%s,storage_gb=%s,cpu_num=%s, updated_at=%s where mac=%s",value1)
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
def perform(inc):
s.enter(inc,0,perform,(inc,))
event_func()
def mymain(inc=10):
s.enter(0,0,perform,(inc,))
s.run()
if __name__ == "__main__":
mymain()
希望本文所述对大家的Python程序设计有所帮助。


猜你喜欢
- 小朋友你可能有很多问号~,上一小节不是已经一顿操作猛如虎搭建好 Python + PyCharm 可用开发环境了吗?为什么这节又来个项目运行
- 兄弟组件之间的通信同样是在项目中经常会遇到的组件间的通信问题之一, 这种问题的最根本方法就是: 把兄弟组件内部的变量提升到一个中央仓库。借助
- # -*- coding: UTF-8 -*-from __future__ import unicode_literalsimport I
- 本文实例讲述了Python使用dict.fromkeys()快速生成一个字典。分享给大家供大家参考,具体如下:>>> re
- 本文实例为大家分享了vue组件watch属性的具体代码,供大家参考,具体内容如下<!doctype html><html&
- 01命名规则:项目名前面一数字编号,随着知识点递增,编号递增例如:01_python基础、02_分支、03_循环每个项目下的文件名建议使用小
- 观看本文前最好有一定的Linux命令基础,具体为centos7.3环境中清除使用yum安装的Mysql卸载前请先关闭Mysql服务servi
- 最近做个软件,用PyQT写的,在实现菜单栏点击弹出新窗口的时候严重被卡壳,发现用WxPython的思想和方式来做完全无法实现。PyQT的中文
- 拆包是指将一个结构中的数据拆分为多个单独变量中。以元组为例:>>> a = ('windows', 10,
- /* 全选择*/ function SB002SelectAll() { var table = document.getElementBy
- 这篇文章主要介绍了python读取ini配置文件过程示范,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 老声长谈,着是困惑很多人的问题,如果处理不好,都是乱码,说这些话并不是我对编码很精通,只是在这方面是得留神,自己总结了一点小经验(容易出现乱
- 介绍RANGE分区基于一个给定的连续区间范围,早期版本RANGE主要是基于整数的分区。在5.7版本中DATE、DATETIME列也可以使用R
- os.makedir(path)和os.makedirs(path)今天工作中将hadoop文件同步到服务器磁盘,由于文件类别目录较多,迁移
- Brendan Eich于1995年在Netscape发明了JavaScript语言,这个语言在过去的一些年里曾是一个最被误解的语言,随着A
- 抓取“xmly”鬼故事音频import json # 在这个url,音频链接为JSON动态生成,所以用到了json模块impor
- 折线图是数据分析中非常常用的图形。其中,折线图主要是以折线的上升或下降来表示统计数量的增减变化的统计图。用于分析自变量和因变量之间的趋势关系
- 前言TestSuite一直是unittest的灵活与精髓之处,在繁多的测试用例中,可以任意挑选和组合各种用例集,比如smoke用例集、lev
- 一:概念 队列是一个”先进先出“
- 有时候我们要去别的接口取数据,可能因为网络原因偶尔失败,为了能自动重试,写了这么一个装饰器。这个是python2.7x 的版本,python