Python executes shell to obtain hardware parameters and writes them to mysql and pythonmysql.

Source: Internet
Author: User

Python executes shell to obtain hardware parameters and writes them to mysql and pythonmysql.

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 ()

OS. system ('LS ')
# If the returned result is 0 or 1, the command output cannot be obtained.

2. OS. popen ()

Output = OS. popen ('LS') print output. read ()
# Command output is printed, but the returned value is not obtained.

3. commands. getstatusoutput ()

(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:

1 '''2 Created on Dec 10,201 4 3 4 @ author: liufei 5''' 6 # coding = UTF-8 7 import time, sched, OS, string 8 from datetime import datetime 9 import MySQLdb10 11 s = sched. scheduler (time. time, time. sleep) 12 13 def event_func (): 14 try: 15 # host name 16 name = OS. popen ("hostname """). read () 17 # cpu count 18 cpu_num = OS. popen ("cat/proc/cpuinfo | grep processor | wc-l """). read () 19 # memory size 20 mem = OS. popen ("free | grep Mem | awk '{print $2 }'"""). read () 21 # machine brand 22 brand = OS. popen ("dmidecode | grep 'vendor' | head-1 | awk-F: '{print $2 }'"""). read () 23 # model 24 model = OS. popen ("dmidecode | grep 'product name' | head-1 | awk-F: '{print $2 }'"""). read () 25 # disk size 26 storage = OS. popen ("fdisk-l | grep 'disk/dev/sd' | awk' BEGIN {sum = 0} {sum = sum + $3} END {print sum }' """). read () 27 # mac address 28 mac = OS. popen ("ifconfig-a | grep HWaddr | head-1 | awk '{print $5 }'"""). read () 29 30 31 32 name = name. replace ("\ n ",""). lstrip () 33 cpu_num = cpu_num.replace ("\ n ",""). lstrip () 34 memory_gb = round (string. atof (mem. replace ("\ n ",""). lstrip ()/1000.0/1000.0, 1) 35 brand = brand. replace ("\ n ",""). lstrip () 36 model = model. replace ("\ n ",""). lstrip () 37 storage_gb = storage. replace ("\ n ",""). lstrip () 38 mac = mac. replace ("\ n ",""). lstrip () 39 40 print name41 print cpu_num42 print memory_gb43 print storage_gb44 print brand45 print model46 print mac47 48 conn = MySQLdb. connect (host = 'xx. xx. xx. xx ', user = 'username', passwd = 'Password', db = 'dbname', port = 3306) 49 cur = conn. cursor () 50 cur.exe cute ('select mac from servers where mac = % s', mac) 51 data = cur. fetchone () 52 53 if data is None: 54 value = [name, brand, model, memory_gb, storage_gb, cpu_num, mac, datetime. now (), datetime. now ()] 55 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) 56 else: 57 value1 = [name, brand, model, memory_gb, storage_gb, cpu_num, datetime. now (), mac] 58 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) 59 60 conn. commit () 61 cur. close () 62 conn. close () 63 64 TB t MySQLdb. error, e: 65 print "Mysql Error % d: % s" % (e. args [0], e. args [1]) 66 67 def perform (inc): 68 s. enter (inc, 0, perform, (inc,) 69 event_func () 70 71 def mymain (inc = 10): 72 s. enter (0, 0, perform, (inc,) 73 s. run () 74 75 if _ name _ = "_ main _": 76 mymain ()

 

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.