Python executes shell to obtain hardware parameters and writes them to mysql.
This example describes how to write hardware parameters to mysql by executing shell in python. Share it with you for your reference. The specific analysis is as follows:
Recently, you need to obtain various Server parameters, including cpu, memory, disk, and model information. Hyperic HQ, Nagios, and Snmp have been used for trial. They all have powerful functions, but they are either too powerful or too heavy.
Therefore, you can use python to execute shell to obtain the information. There are three methods for python to execute shell scripts:
1. OS. system ()
Copy codeThe Code is as follows: OS. system ('LS ')
# If the returned result is 0 or 1, the command output cannot be obtained.
2. OS. popen ()
Copy codeThe Code is as follows: output = OS. popen ('LS ')
Print output. read ()
# Command output is printed, but the returned value is not obtained.
3. commands. getstatusoutput ()
Copy codeThe Code is as follows: (status, output) = commands. getstatusoutput ('LS ')
Print status, output
# Print the return value and command output
You can select one of the methods as needed. The following is a program in which python executes shell to obtain hardware parameters and write them into mysql, and regularly updates them:
Copy codeThe Code is as follows:
'''
Created on Dec 10,201 4
@ Author: liufei
'''
# Coding = UTF-8
Import time, sched, OS, string
From datetime import datetime
Import MySQLdb
S = sched. schedep (time. time, time. sleep)
Def event_func ():
Try:
# Host Name
Name = OS. popen ("" hostname "). read ()
# Number of CPUs
Cpu_num = OS. popen ("" cat/proc/cpuinfo | grep processor | wc-l ""). read ()
# Memory size
Mem = OS. popen ("" free | grep Mem | awk '{print $2}' "). read ()
# Machine brand
Brand = OS. popen ("dmidecode | grep 'bad' | 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.exe cute ('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.exe cute ("insert into servers (name, brand, model, memory_gb, storage_gb, cpu_num, mac, created_at, updated_at) values (% s, % s, % s, % s) ", value)
Else:
Value1 = [name, brand, model, memory_gb, storage_gb, cpu_num, datetime. now (), mac]
Cur.exe cute ("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 ()
Counter t 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 will help you with Python programming.