19

这次,我是如何监控服务器CPU和内存的

 4 years ago
source link: http://www.cnblogs.com/chenyongblog/p/11831498.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

背景

在新项目A中,要结合业务做性能测试。对于做过N次性能测试的我,这次有些巧妇有难无米之炊的感觉。以往的项目,服务器都是部署在AWS或者阿里云,像这样的云服务器厂商是可以通过轻松配置各种Dashboard来实时监控整个系统服务器的各种新能。然而,这一次,服务器不在云上,并且不允许连接外网。那出一个类似的服务器CPU和内存实时使用情况的折线图,该如何做呢?

思路

以结果为导向,需要的是一张压测时服务器CPU和内存的折线图,那么如何画图?Excel可以插入类似的折线图,那么数据怎么来?Linux中有些命令可以记录当时的服务器CPU和内存的情况,如果有脚本或者命令把这些数据实时记录下来,并且把这些数据导出到csv文件,然后借助Excel就能得我们要的结果。

过程

脚本

#!/bin/bash
fileName=$1

echo "CPU%,MEM%,TIME" > $fileName
for (( i = 0; i < 3000; i++ )) do
    output=`top -b -n1 | grep "Cpu(s)" | awk '{print $2 ","}' | tr -d '\n' && free -m | grep 'Mem' | awk '{print $3/$2 * 100 ","}' | tr -d '\n' && date | awk '{print $4}'`>temp
    echo "$output" >> $fileName
    sleep 1
done

生成图

iu2meqy.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK