系统内存统计曲线图

系统内存统计曲线图

使用下面的脚本运行生成文本信息

#!/bin/sh

file=`date +"%x"`
filename=${file}.txt
while true
do
sleep 2
cat /proc/stat | sed -n 1p |awk '{print $2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8}' >> $filename
cat /proc/meminfo | sed -n 2p | awk '{print $2}' >> $filename
done

使用下面的python脚本 对获得的内存信息进行绘图

#!/usr/bin/python

import sys
import numpy as np
import matplotlib.pyplot as plt

if __name__ == "__main__":
    sum = 0
    count = 0
    y_store =[]
    y1_show = []
    y2_show = []
    y2_show = []
    y3_show = []
    y4_show = []
    y5_show = []
    y6_show = []
    y7_show = []
    y8_show = []
    y9_show = []
    with open(sys.argv[1], "r") as file:
        while 1:
            line = file.readline()
            if line == '':
                break
            y_t = line.split()
            if len(y_t) > 1:
                count = count + 1
                sum = 0
                for num in y_t:
                    sum = sum + float(num)
                y_store = []
                for num in y_t:
                    y_store.append(float(num) * 100/sum)
                y_store.append(100 - y_store[3])
                y1_show.append(y_store[0])
                y2_show.append(y_store[1])
                y3_show.append(y_store[2])
                y4_show.append(y_store[3])
                y5_show.append(y_store[4])
                y6_show.append(y_store[5])
                y7_show.append(y_store[6])
                y8_show.append(y_store[7])
            elif len(y_t) == 1:
                y9_show.append(float(y_t[0])/200)
        x_aix = np.arange(0,2 * count, 2)
        plt.xlabel('time:(s)')
        plt.ylabel('loading:(%)/200KB')
        plt.plot(x_aix, y1_show, linewidth = '2', color='r', label='usr')
        plt.plot(x_aix, y2_show, linewidth = '2', color='k', label='niced')
        plt.plot(x_aix, y3_show, linewidth = '2', color ='b', label='sys')
        plt.plot(x_aix, y4_show, linewidth = '2', color = 'g', label='idle')
        plt.plot(x_aix, y5_show, linewidth = '2', color = 'm', label='iowait')
        plt.plot(x_aix, y6_show, linewidth = '2', color = 'y', label='irq')
        plt.plot(x_aix, y7_show, linewidth = '2', color = 'k', label='softirq')
        plt.plot(x_aix, y8_show, linewidth = '2', color = 'orange', label='loading')
        plt.plot(x_aix, y9_show, linewidth = '2', color = 'black', label='free memory')
        plt.legend()
        plt.show()
    file.close()

绘制的统计图如下图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值