Using the Python language to write the basic system Information of Linux (iii): Python and database programming, the acquisition of information into the database

Source: Internet
Author: User

compiling Linux basic system information using Python language (iii): Python and database programminglinks to the first two articles:get Linux Basic system information using Python language (i): Get Linux version, kernel, current timeUsing the Python language to write the basic system Information of Linux (ii): File system usage Acquisition

First, the experimental environment:

Python2.7.10, Pycharm, VM virtual machine, CentOS6.3, MySQL

Second, MySQLdb module:

MySQLdb mode is a python dedicated to the MySQL database module, in addition to the development of the Python environment really feel much more trouble than other ... This light installation of this MYSQLDB module took a long time, encountered a lot of problems, but fortunately still successful use, and later I will add an article I was how to install the tutorial, will list the problems I encountered for your reference.

Third, realize:

Here we introduce the database, we are using the MySQL database installed on the CentOS. The installation of the database is not introduced here.

In MySQL we put all the tables in a database called Linux, which contains two tables: A Linux table that holds Linux information data, and an FDISK table that holds hard disk data.

The SQL for both tables:

1 CREATE TABLELinux//Linux Tables2 (3Os_versionVARCHAR( +) not NULL,4Os_kernalVARCHAR( +) not NULL,5Os_dataVARCHAR( +)PRIMARY KEY,6 )7 8 CREATE TABLEFdisk//fdisk table9 (TenF_dateTIMESTAMP( -) not NULL, OneFileSystemVARCHAR( +) not NULL, ARomVARCHAR( +) not NULL, -UsedVARCHAR( +) not NULL, -Not_usedVARCHAR( +) not NULL, theUsed_perVARCHAR( +) not NULL, -MountVARCHAR( +) not NULL -)

All operations for the table are in a PY file, including insert and query operations

mysql_db.py

1 #Coding=utf-82 ImportMySQLdb3 Import_mysql_exceptions4Createlinuxsql ="""5 CREATE TABLE Linux6 (7 os_version VARCHAR (+) not NULL,8 os_kernal VARCHAR (+) not NULL,9 os_data VARCHAR (PRIMARY) KEY,Ten  One ) A """ -Createfdisksql =""" - CREATE TABLE fdisk the ( - f_date TIMESTAMP (+) not NULL, - filesystem VARCHAR (+) not NULL, - ROM VARCHAR (+) not NULL, + used VARCHAR (+) not NULL, - not_used VARCHAR (+) not NULL, + used_per VARCHAR (+) not NULL, A Mount VARCHAR (+) not NULL at ) - """ - classMySQL: -     def __init__(self): -         Try: -Self.conn = MySQLdb.connect (host='192.168.179.129', user='Root', passwd='ro', db='Linux') inSelf.cur =self.conn.cursor () -         except_mysql_exceptions. Operationalerror,e: to             Print 'database cannot connect' +     def __del__(self): - self.cur.close () the Self.conn.commit () * self.conn.close () $     defSelect (self):Panax Notoginseng         Try: -Self.cur.execute ("select * FROM Linux") the              foreachinchSelf.cur.fetchall (): +                 Print each A         except_mysql_exceptions. Programmingerror,e: the Self.cur.execute (createlinuxsql) +     defOs_insert (self,version,kernal,data): -         Try: $Self.cur.execute ("INSERT INTO Linux VALUES ('%s ', '%s ', '%s ')"%(version,kernal,data)) $         except_mysql_exceptions. Programmingerror,e: - Self.cur.execute (createlinuxsql) -Self.cur.execute ("INSERT INTO Linux VALUES ('%s ', '%s ', '%s ')"%(version,kernal,data)) the     defFdisk_select (self): -         Try:WuyiSelf.cur.execute ("SELECT * FROM fdisk") the             PrintSelf.cur.fetchall () -         except_mysql_exceptions. Programmingerror,e: Wu Self.cur.execute (createfdisksql) -     defFdisk_insert (Self,filesystemz,romz,usedz,nousedz,usedperz,mountz): About         Try: $Self.cur.execute ("INSERT INTO fdisk (filesystem,rom,used,not_used,used_per,mount) - VALUES ('%s ', '%s ', '%s ', '%s ', '%s ', '%s ')"%(Filesystemz, - Romz,usedz,nousedz,usedperz,mountz)) -         except_mysql_exceptions. Programmingerror,e: A Self.cur.execute (createfdisksql) +Self.cur.execute ("INSERT INTO fdisk (filesystem,rom,used,not_used,used_per,mount) the VALUES ('%s ', '%s ', '%s ', '%s ', '%s ', '%s ')"%(Filesystemz, -Romz,usedz,nousedz,usedperz,mountz))

You do not have to manually create the table, as long as the database exists automatically establishes the table

At the same time, some previous code has been updated, update only fill in the changes

os_info.py:

1 #Coding=utf-82 ImportLinux_status3 Importos_info_in4  frommysql_dbImportMySQL5 classOs_info:6     def __init__(self):7         #self.linux_stat=linux_status.linux_status ()8self.os_infos_in=os_info_in.os_info_in ()9     defOs_info (self):Tenself.linux_stat=linux_status.linux_status () OneSelf.linux_stat.os_version =self.os_infos_in.os_version () ASelf.linux_stat.os_kernal =Self.os_infos_in.os_kernel () -Self.linux_stat.os_date =self.os_infos_in.os_date () -         returnSelf.linux_stat the     defOs_in_mysql (self): -os_db=MySQL () -Os_db.os_insert (Self.os_infos_in.os_version (), Self.os_infos_in.os_kernel (), Self.os_infos_in.os_date ())

linux_fdisk.py:

def Fdisk_mysql (self):        self.fdisk_mysql=MySQL ()        i=0        J=1 while         J ==1:            try:                Self.fdisk_mysql.fdisk_insert (self.disk_info[i],self.disk_info[i +1],self.disk_info[i+2],                                   self.disk_info[i+3],self.disk_info[i+4],self.disk_info[i+5  ])                i=i+6            except  exception,e:                J=0

Iv. Display of results:

MySQL does not support displaying Chinese, changing Linux to English (export Lang=en_us)

Last ~

This series is first written here ~ ~ Wait for what new ideas will continue to write ~ ~

Welcome to God plus me qq:707475486~ Note: Blog Park on the line ~ so I have any problems can be very easy to ask! Everybody progress together ~~~~~ recently work relatively busy so this article is more slow ~

Using the Python language to write the basic system Information of Linux (iii): Python and database programming, the acquisition of information into the database

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.