Linux系统运行相关信息,获取Linux系统运行情况信息

代码:

#include

#include /* usleep() */

#include

#include

#include

class SystemRuntimeInfo {

public:

void GetSysStatInfo()

{

system("top -bn 1 -i -c >> sys.txt");

}

void GetCpuInfo()

{

system("sar -P ALL -u 1 5 >> cpu.txt");

// system("");

// system("mpstat 1 5 >> cpu.txt");

// system("dstat -c >> cpu.txt");

}

void GetDiskInfo()

{

system("df -lh >> disk.txt");

}

void GetMemoryInfo()

{

system("vmstat 1 5 >> memroy.txt");

}

void GetIoInfo()

{

system("iostat 1 5 >> io.txt");

}

};

int main()

{

unsigned count = 5;

while (count --> 0)

{

SystemRuntimeInfo aInfo;

std::ofstream outfile;

outfile.open("sys.txt", std::ios::app | std::ios::out);

outfile << '

';

aInfo.GetSysStatInfo();

outfile << '

' << std::endl;

outfile.close();

outfile.open("cpu.txt", std::ios::app | std::ios::out);

outfile << '

';

aInfo.GetCpuInfo();

outfile << '

' << std::endl;

outfile.close();

outfile.open("disk.txt", std::ios::app | std::ios::out);

outfile << '

';

aInfo.GetDiskInfo();

outfile << '

' << std::endl;

outfile.close();

outfile.open("memory.txt", std::ios::app | std::ios::out);

outfile << '

';

aInfo.GetMemoryInfo();

outfile << '

' << std::endl;

outfile.close();

usleep(1000);

}

return 0;

}

主要就是使用system()调用shell命令,其中‘>>’ or '>'的意思就是将输出重定向写入到指定的文件中,其区别是:'>'获取的输出会覆盖掉原文件中的内容。

参考资料:

1.https://www.cnblogs.com/Anker/p/3381667.html

2.https://blog.csdn.net/albenxie/article/details/72885951

3.还有几个记不得了,之后也没有找到...sry。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值