Ubuntu系统监控cpu memery 磁盘Io次数 IO速率 网卡 运行时间等信息的采集
实验室最近在做的项目要做ubuntu系统监控,要获得系统的一些信息并返回给web服务器.
web服务器与ubuntu主机的通信我写的程序用的是socket,至于为什么不用java程序ssh到对应的主机上当初这个方案被否决了,只好专心写自己的socket.
系统的cpu信息的获得可以用cat /proc/cpuinfo
下面是我截取的命令执行部分结果

这里面有几个非常重要的参数
- processor 逻辑处理器的id。
- physical id 物理封装的处理器的id。
- core id 每个核心的id。
- cpu cores 位于相同物理封装的处理器中的内核数量。
- siblings 位于相同物理封装的处理器中的逻辑处理器的数量
系统内存的使用率我用的是free命令

总内存 已用内存 和剩余内存都可以得到 使用率的获得也就是用这几个参数做一些计算
系统的磁盘Io我用的是iostat -x -k 1 2 这里取得是第二次刷新的数据 原因是第一次的数据是从系统开机到现在的累计值 不是需求要得到的 这里截取命令的截取结果

各个参数的含义这里就不详细解释了 在网上都解释的很详细
网卡信息的采集 我用的是lspci

Ethernet controller 既是系统的网卡
系统的运行时间 用到的命令是cat /proc/uptime

第一个参数1188788.89既是系统运行了多少S
采集的命令 都知道了 接下来就是用java程序采集了 不多说 贴上自己写的部分代码 由于要用socket传给客户端 所以对字符都有一些分隔符处理 方便客户端对字符串解析
public static String getmachineinfo() throws IOException {
StringBuffer Machineinfo = new StringBuffer();
StringBuffer Machineio = new StringBuffer();
StringBuffer MachineUptime = new StringBuffer();
StringBuffer MachineCpu = new StringBuffer();
StringBuffer MachineNetworkcard = new StringBuffer();
StringBuffer MachineMemory = new StringBuffer();
StringBuffer MachineHostname = new StringBuffer();
double Machinerunningvm=0;
String[] cmd = new String[] { "iostat", "-x", "-k", "1", "2" };
Process process = Runtime.getRuntime().exec(cmd);
int count = 0;// 计数
double ioreadspeedsum = 0;// 统计实体机每个磁盘io的和
double iowritespeedsum = 0;
double ioreadnumsum = 0;
double iowritenumsum = 0;
LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
String[] words = line.split("\\s+");
if (words.length > 3) {
if (words[0].equals("Device:"))// 用来判断取第二次刷新的数据
count += 1;
if (words[0].contains("sd") && count == 2)// 取第二次刷新的数据
// 获得Io信息并取得累加值
{
ioreadnumsum += Double.valueOf(words[3]);
iowritenumsum += Double.valueOf(words[4]);
ioreadspeedsum += Double.valueOf(words[5]);
iowritespeedsum += Double.valueOf(words[6]);
}
}
}
Machineio.append(ioreadnumsum + " " + iowritenumsum + " " + ioreadspeedsum + " " + iowritespeedsum + " ");
cmd = new String[] { "cat", "/proc/uptime" };
process = Runtime.getRuntime().exec(cmd);
br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
if ((line = br.readLine()) != null) {
String[] words = line.split("\\s+");
MachineUptime.append(words[0] + " ");
}
ArrayList<String> cpuidjudge = new ArrayList<String>();
String cpuid = new String();
String cpumodelname = new String();
String cpucore = new String();
String cpusiblings = new String();
cmd = new String[] { "cat", "/proc/cpuinfo" };
process = Runtime.getRuntime().exec(cmd);
br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
String[] words = line.split("\\s+");
if (words.length > 1) {
if (words[0].equals("physical"))// 用来判断Physical id
{
cpuid = words[3];
}
if (words[0].equals("model") && words[1].equals("name"))// 用来判断cpu
// model
// name
{
cpumodelname = line.toString().replaceAll("model name :", "").replaceAll("\\s+", " ");
}
if (words[0].equals("siblings"))// 用来判断siblings
{
cpusiblings = words[2];
}
if (words[0].equals("cpu") && words[1].equals("cores"))// 用来判断cpu
// cores
{
cpucore = words[3];
if (!(cpuidjudge.contains(cpuid))) {
cpuidjudge.add(cpuid);
MachineCpu.append(cpuid + "-" + cpumodelname + "-" + cpusiblings + "-" + cpucore + "|");// 加的横杠和竖杠是为了客户端接收后对字符的分解处理
}
}
}
}
cmd = new String[] { "lspci" };
process = Runtime.getRuntime().exec(cmd);
br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
String[] words = line.split("\\s+");
if (words[1].equals("Ethernet") && words[2].contains("controller")) {// 用来判断网关
for (int i = 3; i < words.length; i++) {
MachineNetworkcard.append(words[i] + " ");
}
MachineNetworkcard.append("-");
}
}
cmd = new String[] { "free" };
process = Runtime.getRuntime().exec(cmd);
br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
String[] words = line.split("\\s+");
if (words[0].equals("Mem:"))// 用来判断内存
{
MachineMemory.append(words[1] + " " + words[2] + " " + words[3]);
}
}
cmd = new String[] { "hostname" };
process = Runtime.getRuntime().exec(cmd);
br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
MachineHostname.append(br.readLine());
System.out.println(MachineHostname.toString());
cmd = new String[] { "virsh","list"};
process = Runtime.getRuntime().exec(cmd);
br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
String[] words = line.split("\\s+");
if (words.length>2&&words[3].contains("running"))// 用来判断
{
Machinerunningvm+=1;
}
}
Machineinfo.append(Machineio + "\n" + MachineUptime + "\n" + MachineCpu + "\n" + MachineNetworkcard+"\n"+MachineMemory+"\n"+MachineHostname+"\n"+Machinerunningvm);
return Machineinfo.toString();
}
暂时先写这么多吧
Ubuntu系统监控cpu memery 磁盘Io次数 IO速率 网卡 运行时间等信息的采集的更多相关文章
- 利用shell监控cpu、磁盘、内存使用率
利用shell监控cpu.磁盘.内存使用率,达到警报阈值发邮件进行通知 并配合任务计划,即可及时获取报警信息 #!/bin/bash ################################# ...
- 获取并检查系统负载\CPU\内存\磁盘\网络
安装依赖 需要net-tools.namp! CentOS:yum -y install net-tools nmap Ubuntu:apt-get update && apt-get ...
- python监控CPU/内存/磁盘,超过指定百分比,发送邮件
#!/usr/bin/python #coding:utf-8 #导入psutil模块 import psutil import yagmail def mail(subject,contents): ...
- Ubuntu系统监控indicator-sysmonitor
参考: http://www.cnblogs.com/EasonJim/p/7130171.html 安装indicator-sysmonitor sudo add-apt-repository pp ...
- ubuntu系统无法访问无法磁盘最佳解决办法
出现如下错误: Error mounting /dev/sda8 at /media/fzh/System: Command-line `mount -t "ntfs" -o &q ...
- linux系统CPU,内存,磁盘,网络流量监控脚本
前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...
- [转]linux 系统监控、诊断工具之 IO wait
1.问题: 最近在做日志的实时同步,上线之前是做过单份线上日志压力测试的,消息队列和客户端.本机都没问题,但是没想到上了第二份日志之后,问题来了: 集群中的某台机器 top 看到负载巨高,集群中的机器 ...
- centos8平台使用pidstat监控cpu/内存/io
一,安装pidstat: 1,安装 [root@localhost yum.repos.d]# yum install sysstat 2,查看版本: [root@localhost ~]# pids ...
- 一键获取linux内存、cpu、磁盘IO等信息脚本编写,及其原理详解
更多linux知识,请关注公众号:一口Linux 一.脚本 今天主要分享一个shell脚本,用来获取linux系统CPU.内存.磁盘IO等信息. #!/bin/bash # 获取要监控的本地服务器IP ...
随机推荐
- MVC:上传文件时限制文件类型
之前写过一篇:MVC:上传文件 今天补充下一个功能:如何限制上传文件类型 文件类型可以在前段限制,但是太容易被绕过,最好还是在后端处理. 修改upload方法代码: [HttpPost] public ...
- 通过dblink的方式查看表的结构
有dba权限: SELECT * FROM DBA_TAB_COLUMNS@DBLINK_TEST WHERE TABLE_NAME = '表名'; 没有dba权限:SELECT * FROM USE ...
- 织梦如何重新安装 织梦dir 二次安装
一般安装过一次织梦就不需要重新再去下载安装第二次了,我们可以在原有的基础上重新安装一次.这个是织梦cms后台自带的功能,还比较方便,下面小编就分享下如何重装织梦. 如果在本地重装要打开集成环境,在浏览 ...
- Python基础篇【第2篇】: Python自定义函数
Python函数 1. 函数定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print ...
- HTML音乐播放——切歌
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- R型思维模式对软件开发的影响(草稿)
The pragmatic programmers 一直在工作之余读些书,之前主要是纯英文版的计算机相关的算法,编译器,数学等,想通过读这些书来提高自己每日工作效能,结果收效甚微.一是,因为纯英文的书 ...
- MFC重载关闭按钮
首先介绍一下WindowProc函数. 函数功能:该函数是一个应用程序定义的函数.它处理发送给窗口的消息.WNDPROC类型定义了一个指向该回调函数的指针.WindowProc是用于应用程序定义函数的 ...
- 纸上谈兵:排序算法简介及C实现
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 排序算法(Sorting Algorithm)是计算机算法的一个组成部分. 排序的 ...
- ant安装(linux)
1.下载 下载地址:http://ant.apache.org/bindownload.cgi 下载apache-ant-1.9.7-bin.tar.gz(当前最新版本),将该下载包拷贝到/data/ ...
- 图片form表单提交和id提交
<form action="${pageContext.request.contextPath }/sarchServlet" method="post" ...