Python execution shell Get hardware parameter write MySQL method _python

Source: Internet
Author: User

The example in this article describes how Python performs a shell acquisition of hardware parameters written to MySQL. Share to everyone for your reference. The specific analysis is as follows:

Recently to get the various parameters of the server, including CPU, memory, disk, model and other information. Hyperic HQ, Nagios, and SNMP have been tried, but the requirements are not too strong or too heavy.

With the idea of using Python to get this information, Python executes shell scripts in three ways:

1. Os.system ()

Copy Code code as follows:
Os.system (' ls ')
#返回结果0或者1, cannot get the output of the command

2. Os.popen ()
Copy Code code as follows:
Output = Os.popen (' ls ')
Print Output.read ()
#打印出的是命令输出, but no return value was executed

3. Commands.getstatusoutput ()
Copy Code code as follows:
(status, Output) = Commands.getstatusoutput (' ls ')
Print status, output
#打印出返回值和命令输出

You can choose one of these methods as needed, and the following is a program that Python executes the shell to get hardware parameters to write to MySQL and regularly updates:

Copy Code code as follows:

'''
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 $} ' "" ". Read ()
#机器品牌
Brand = Os.popen ("" "Dmidecode | grep ' Vendor ' | head-1 | Awk-f: ' {print $} ' "" ". Read ()
#型号
Model = Os.popen ("" "Dmidecode | grep ' Product Name ' | head-1 | Awk-f: ' {print $} ' "" ". 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 $} ' "" ". 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 will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.