由于Centos7和6的系统变化挻大的,所以先看看Centos7的内存信息是怎么样的。

系统版本:

[root@docker ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@docker ~]#

查看内存信息:其实只需要关注前5行即可;

[root@docker ~]# 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[root@docker ~]# [root@docker ~]# free -m              total        used        free      shared  buff/cache   availableMem:           2001         762          81           0        1156        1035Swap:           511         142         369[root@docker ~]#

内存使用率的计算:mem_used=MemTotal-MemFree-Buffers

python 代码:

#/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 = 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()

安装pymysql模块

pip install pymysql

创建数据库以及表:

MariaDB [(none)]> create database memory charset=utf8;MariaDB [(none)]> use memory;MariaDB [(none)]> CREATE TABLE `memory` (    `memory` int(11) DEFAULT NULL,    `time` int(11) DEFAULT NULL  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;授权用户MariaDB [(none)]> grant all on *.* to dba@'localhost' identified by '123456';MariaDB [(none)]> flush privileges;

执行python代码,每隔一秒就会打印一个ok到终端,然后在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)

注:Mysql的表只用了两个字段,一个内存使用率,这个值是以兆为单位的,另一个就是时间了。

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(11) DEFAULT NULL,  `time` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb41 row in set (0.00 sec)MariaDB [memory]>