Python performs the method of getting hardware parameters written to mysql by the shell
- 2020-04-02 14:30:36
- OfStack
This article illustrates an example of how python performs a shell fetch to write hardware parameters to mysql. Share with you for your reference. Specific analysis is as follows:
Recently to get various server parameters, including CPU, memory, disk, model, and so on. We tried out Hyperic HQ, Nagios, and Snmp. They are all quite powerful, but not quite up to the requirements, or too heavy.
So I came up with the idea of using python to execute the shell to get this information. Python can execute the shell script in the following three ways:
1. OS. The system ()
os.system('ls')
# Returns the result 0 or 1 , could not get the output of the command
2. OS. Popen ()
output = os.popen('ls')
print output.read()
# The output of the command is printed, but the return value is not executed
3. Commands. Getstatusoutput ()
(status, output) = commands.getstatusoutput('ls')
print status, output
# Print out the return value and the command output
One of these methods can be selected as needed. The following is a program for python to execute the shell to get hardware parameters and write to mysql, and to update it regularly:
'''
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:
# The host name
name = os.popen(""" hostname """).read()
#cpu The number of
cpu_num = os.popen(""" cat /proc/cpuinfo | grep processor | wc -l """).read()
# Memory size
mem = os.popen(""" free | grep Mem | awk '{print $2}' """).read()
# The machine brand
brand = os.popen(""" dmidecode | grep 'Vendor' | head -1 | awk -F: '{print $2}' """).read()
# model
model = os.popen(""" dmidecode | grep 'Product Name' | head -1 | awk -F: '{print $2}' """).read()
# Disk size
storage = os.popen(""" fdisk -l | grep 'Disk /dev/sd' | awk 'BEGIN{sum=0}{sum=sum+$3}END{print sum}' """).read()
#mac address
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()
I hope this article has helped you with your Python programming.