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 ...
随机推荐
- sql rollup解决责任人收支余额
问题的提出是周聪之前问过我的项目往来查询,不好在NC上一次性查询到.然后我就搞了一个很长的项目对账,发布了NC的节点. 现在我做了总二的总账,每次领导问我项目还有多少钱,收了多少付了多少,我还要通过科 ...
- Android OpenCV 图像识别
最近打算写一个android 平台opencv 的小程序,着手查找了一下资料.网络上的资料参差不齐,有一些都比较老旧,我参考了前面的方法找到了一个简单的搭建方法,分享给大家. 0,环境的搭建: jav ...
- flex loaderInfo为null在creationComplete事件中
原文: http://yunzhongxia.iteye.com/blog/1152670 Flex4中application变为FlexGlobals.topLevelApplication,很 ...
- firefox与IE对js和CSS的区别(转http://log-cd.javaeye.com/blog/548665)
? "700px" : document.body.clientWidth>1000 ? "1000px" : "auto");// ...
- JavaScript中Math--random()/floor()/round()/ceil()
Math.random():返回0-1之间的任意数,不包括0和1: Math.floor(num):返回小于等于num的整数,相当于四舍五入的四舍,不五入:例子:Math.floor(1.0);Mat ...
- 【转】抛弃EF,20分构建一个属于自己的ORM框架
链接:http://www.cnblogs.com/irenebbkiss/p/4157364.html
- 黑马程序员_ JAVA中的多线程
------- android培训.java培训.期待与您交流! ---------- 尽管线程对象的常用方法可以通过API文档来了解,但是有很多方法仅仅从API说明是无法详细了解的. 本来打算用一节 ...
- Ping 命令的使用方法总结
一.Ping 命令 “Ping”命令是我们在判断网络故障常用的命令,但您真正明白这个命令运行后会发生什么,以及出现的各种信息说明了什么吗?其实熟练的掌握 Ping 命令的各种技巧可以帮助你解决很多网络 ...
- 如何将arcgis的mxd文档存储为相对路径
在默认情况下,ArcGIS 10中地图文件mxd中添加的图层所引用的文件路径均为绝对路径.这就意味着,如果你在地图中引用了“D:\data\DEM.shp”文件,那map.mxd文件中保存的该层文件路 ...
- 你需要知道的swift必备函数 map
map这东西在oc中并未用过,但是swift在处理数组的时候显得格外的游刃有余,这归功于map这个函数: map函数 arr.map(<#T##transform: (Int) throws ...