实验室最近在做的项目要做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速率 网卡 运行时间等信息的采集的更多相关文章

  1. 利用shell监控cpu、磁盘、内存使用率

    利用shell监控cpu.磁盘.内存使用率,达到警报阈值发邮件进行通知 并配合任务计划,即可及时获取报警信息 #!/bin/bash ################################# ...

  2. 获取并检查系统负载\CPU\内存\磁盘\网络

    安装依赖 需要net-tools.namp! CentOS:yum -y install net-tools nmap Ubuntu:apt-get update && apt-get ...

  3. python监控CPU/内存/磁盘,超过指定百分比,发送邮件

    #!/usr/bin/python #coding:utf-8 #导入psutil模块 import psutil import yagmail def mail(subject,contents): ...

  4. Ubuntu系统监控indicator-sysmonitor

    参考: http://www.cnblogs.com/EasonJim/p/7130171.html 安装indicator-sysmonitor sudo add-apt-repository pp ...

  5. ubuntu系统无法访问无法磁盘最佳解决办法

    出现如下错误: Error mounting /dev/sda8 at /media/fzh/System: Command-line `mount -t "ntfs" -o &q ...

  6. linux系统CPU,内存,磁盘,网络流量监控脚本

    前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...

  7. [转]linux 系统监控、诊断工具之 IO wait

    1.问题: 最近在做日志的实时同步,上线之前是做过单份线上日志压力测试的,消息队列和客户端.本机都没问题,但是没想到上了第二份日志之后,问题来了: 集群中的某台机器 top 看到负载巨高,集群中的机器 ...

  8. centos8平台使用pidstat监控cpu/内存/io

    一,安装pidstat: 1,安装 [root@localhost yum.repos.d]# yum install sysstat 2,查看版本: [root@localhost ~]# pids ...

  9. 一键获取linux内存、cpu、磁盘IO等信息脚本编写,及其原理详解

    更多linux知识,请关注公众号:一口Linux 一.脚本 今天主要分享一个shell脚本,用来获取linux系统CPU.内存.磁盘IO等信息. #!/bin/bash # 获取要监控的本地服务器IP ...

随机推荐

  1. 解决jsp下载文件,迅雷下载路径不显示文件名称的问题

    如果浏览器安装了迅雷的插件,在jsp页面调用java后台实现文件下载功能时,会自动弹出迅雷下载,迅雷的下载路径会显示.do或者.xhtml之类的,为了解决这个问题,jsp页面修改如下: 写一个< ...

  2. ruby中proc和lambda的return区别

    学习ruby有一段时间了,但是我看了好几遍proc和lambda的return区别的区别讲解,始终没明白到底什么区别,今天上午又看,终于感觉是茅塞顿开有点领悟了 一下内容部分来自<<rub ...

  3. 智能指针 ADO数据库连接

    ADO库包含三个基本接口:_ConnectionPtr接口._CommandPtr接口和_RecordsetPtr接口._ConnectionPtr接口返回一个记录集或一个空指针.通常使用它来创建一个 ...

  4. CoolTrayIcon4.0

    CoolTrayIcon:在任务栏放置图标的控件,是同类空间中功能最为完善和强大的. 1.支持动态图标 2.交互式气球样式的提示框 3.支持bitmaps到icons的转换 4.支持设计状态预览 5. ...

  5. Karma 4 - Karma 集成 Webpack 进行单元测试

    可以将 karma 与 webpack 结合起来,自动化整个单元测试过程. 配置环境 1. 首先根据 1 完成基本的 karma 测试环境. 2. 安装 webpack 和 webpack 使用的 l ...

  6. ArcGIS for Flex中引入google map作底图

    上篇文章到在ArcGIS View中引入google map,这里讲ArcGIS for Flex中引入google map作底图. 同样道理,以google map作底图,需要编写继承自TiledM ...

  7. Python 爬虫—— requests BeautifulSoup

    本文记录下用来爬虫主要使用的两个库.第一个是requests,用这个库能很方便的下载网页,不用标准库里面各种urllib:第二个BeautifulSoup用来解析网页,不然自己用正则的话很烦. req ...

  8. JS生成雪花

    <script type="text/javascript"> (function(){ function snow(left,height,src){ var div ...

  9. 动态规划 - 最长公共子序列(LCS)

    最长公共子序列也是动态规划中的一个经典问题. 有两个字符串 S1 和 S2,求一个最长公共子串,即求字符串 S3,它同时为 S1 和 S2 的子串,且要求它的长度最长,并确定这个长度.这个问题被我们称 ...

  10. ComboBox的数据绑定

    这里的ComboBox指System.Windows.Forms中的ComboBox. 使用对象数据源 IList<Model> models = ModelService.GetAllM ...