Python execution shell Gets hardware parameters written to MySQL method

Source: Internet
Author: User
This example describes how Python executes the shell to get hardware parameters written to MySQL. Share to everyone for your reference. The specific analysis is as follows:

Recently to obtain the server various parameters, including CPU, memory, disk, model and other information. Hyperic HQ, Nagios, and SNMP have been tried, but they are powerful, but not too heavy.

So the idea of Python executing the shell to get this information is that Python executes shell scripts in the following three ways:

1. Os.system ()

The code is as follows:

Os.system (' ls ')
#返回结果0或者1, the output of the command cannot be obtained


2. Os.popen ()

The code is as follows:

Output = Os.popen (' ls ')
Print Output.read ()
#打印出的是命令输出, but cannot get the return value of execution


3. Commands.getstatusoutput ()

The code is 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 written to MySQL and regularly updated:

The code is 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 ()

Hopefully this article will help you with Python programming.

  • 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.