实验室最近在做的项目要做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. nodejs初探(四)实现一个多人聊天室

    我们实现的思路是,当有一个人发送过来消息,我们就广播给其他客户端. var net = require('net'); var chatServer = net.createServer(), cli ...

  2. WCF数据通讯

    Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NET Fra ...

  3. RESTClient使用

    RESTClient使用

  4. django model 中class meta

    class Meta: ordering = ['-num', 'length'] verbose_name = 'name' verbose_name_plural = 'names' orderi ...

  5. 魅族手机(魅蓝note)无法作为调试设备连接到mac问题的解决

    问题描述: OS X(Yosemite),ADB(1.0.32),Android Studio(1.0.1),魅蓝note手机(m1 note,Android 4.4.4,Flyme OS 4.2.0 ...

  6. noip2008解题报告

    T1.笨小猴 给出一个单词求出现次数最多和最少之差是不是质数. 很水的.统计一下反正就26个字母. T2.火柴棒等式 给出火柴棒数,求形如 a+b=c能拼成的等式个数. 先减去4根(+,=),然后枚举 ...

  7. oracle中的timestamp字段的值乱码问题修改

    我的解决方案: 直接新增一个系统变量: key值为:NLS_TIMESTAMP_FORMATvalue的值为:YYYY-MM-DD HH24:MI:SSFF6 其它解决方案: 在登录PLSQL之后,查 ...

  8. Android新浪微博获取评论信息、发表评论、转发微博等

    首先前面一节中说过,获取用户的微博信息,这里简单介绍下获取微博的评论信息,以及对微博进行评论,转发微博等. OAuth认证,这里就不多说了, 我说名一下接口: 获取微博的评论列表接口: http:// ...

  9. php中好用的时间函数

    //查询数据30天的数据$y=date("Y",time());$m=date("m",time());$d=date("d",time() ...

  10. Java2OP

    Java2OP D:\Program Files (x86)\Embarcadero\Studio\18.0\bin\converters\java2op\Java2OP.exe Java2OP.ex ...