Because Centos7 and 6 system changes busty large, so first look at Centos7 memory information is how.
System version:
[Email protected] ~]# cat/etc/redhat-release CentOS Linux release 7.2.1511 (Core) [[email protected] ~]#
View memory information: In fact, only need to focus on the first 5 lines;
[[email protected] ~]# cat /proc/meminfo memtotal: 2049248 kBMemFree: 85408 kbmemavailable: 1061812 kbbuffers: 138044 kBCached: 885028 kBSwapCached: 33308 kBActive: 881088 kBInactive: 832516 kbactive (anon): 315948 kbinactive ( Anon): 375464 kbactive (file): 565140 kbinactive (file): 457052 kBUnevictable: 0 kbmlocked: 0 kbswaptotal: 524284 kBSwapFree: 377836 kBDirty: 24 kBWriteback: 0 kbanonpages: 659500 kbmapped: 71544 kBShmem: 876 kBSlab: 160772 kbsreclaimable: 123148 kBSUnreclaim: 37624 kBKernelStack: 7408 kbpagetables: 20580 kbnfs_unstable: 0 kBBounce: 0 kBWritebackTmp: 0 kbcommitlimit: 1548908 kbcommitted_as: 2998548 kBVmallocTotal: 34359738367 kBVmallocUsed: 156648 kBVmallocChunk: 34359541760 kbhardwarecorrupted: 0 kbanonhugepages: 434176 kbhugepages_total: 0hugepages_free: 0HugePages_Rsvd: 0HugePages_Surp: 0hugepagesize: 2048 kbdirectmap4k: 73664 kBDirectMap2M: 2023424 kBDirectMap1G: 0 kB[[email protected] ~]# [[email protected] ~]# free -m total used free shared buff/cache availablemem : 2001 762 81 0 1156 1035swap: 511 142 369[[email protected] ~]#
Calculation of memory utilization: Mem_used=memtotal-memfree-buffers
Python code:
#/usr/bin/env python import time import pymysql as mysql db = mysql.connect (user= ' DBA ', Passwd= ' 123456 ', db= ' memory ', host= ' localhost ') Db.autocommit (True) cur = Db.cursor () def getmem (): f = open ('/proc/meminfo ') Total = int (F.readline (). Split () [1]) free =&nBsp;int (F.readline (). Split () [1]) memavailable = f.readline (). Split () cache = int (F.readline (). Split () [1]) mem_use = total-free-cache t = time.time () sql = ' Insert into memory (memory,time) values (%s,%s) ' % (mem_use/1024,t) cur.execute (SQL) #print mem_use print (' OK ') while True: time.sleep (1) getmem ()
Installing the pymysql module
Pip Install Pymysql
To create a database and a table:
MariaDB [(None)]> create database memory Charset=utf8; MariaDB [(None)]> use memory; MariaDB [(None)]> CREATE TABLE ' memory ' (' Memory ' int (one) default null, ' time ' int (one-by-one) default null) Engine=in Nodb DEFAULT charset=utf8mb4; authorized user Mariadb [(none)]> grant all on *. * to [email protected] ' localhost ' identified by ' 1234 56 '; MariaDB [(None)]> flush privileges;
Execute Python code, every second will print an OK to the terminal, and then query in MySQL;
mariadb [memory]> select * from memory limit 5;+--------+------------+| memory | time |+ --------+------------+| 1775 | 1513906229 | | 1775 | 1513906230 | | 1775 | 1513906231 | | 1775 | 1513906232 | | 1775 | 1513906233 |+--------+------------+5 rows in set ( 0.00 SEC)
Note: mysql's table uses only two fields, one memory usage, this value is in megabytes, and the other is time.
mariadb [(none)]> use memory; reading table information for completion of table and column Namesyou can turn off this feature to get a quicker startup with -adatabase changedmariadb [memory]> show create table memory\ g*************************** 1. row *************************** Table: memoryCreate Table: CREATE TABLE ' Memory ' ( ' Memory ' int (one) DEFAULT NULL, ' time ' int (one) default null) engine=innodb default charset=utf8mb41 row in set (0.00 sec) MariaDB [ Memory]>
Python gets CentOS7 memory utilization and writes to MySQL