实验室最近在做的项目要做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. FM/PCM与FM/PPM的区别

    FM/PCM的优点:     1 高可靠性和高抗干扰性.大家知道,一般PPM遥控设备都要求在操作时先开发射机后开接收机,先关接收机后关发射机.其原因是在没有发射信号时,接受机会因自身内部的噪音或外界的 ...

  2. android StringBuffer类的使用

    参考 http://www.cnblogs.com/springcsc/archive/2009/12/03/1616330.html StringBuffer类和String一样,也用来代表字符串, ...

  3. 在windows上安装scikit-learn开发环境

    操作系统:Windows 10 64位 1.安装python 前往https://www.python.org/downloads/下载对应操作系统的版本,笔者下载了32位的python 2.7.13 ...

  4. 每天一个 Linux 命令(4):mkdir

    linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录- 2.命令功能 ...

  5. silverlight 鼠标事件处理

    托管代码注册鼠标事件 不需要再.xaml中声明注册事件. .xaml <Ellipse Name=" Fill="Orange" Canvas.Top=" ...

  6. Visual Studio Enterprise 2015下载 Update3

    Visual Studio 2015 是一个丰富的集成开发环境,可用于创建出色的 Windows.Android 和 iOS 应用程序以及新式 Web 应用程序和云服务. 1.适用于各种规模和复杂程度 ...

  7. 使用Jmeter录制脚本

    相对于LoadRunner跟SilkPerformer来说,Jmeter确实有差距,但毕竟前两者太贵,Jmeter胜在免费开源. 先看下LoadRunner录制的脚本如下,美如画,结构清晰,易于修改编 ...

  8. Android开发--布局

    一:LinearLayout  1.线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下罗列在屏幕上.每一个LinearLayout里面又可分为垂直布局(android:orie ...

  9. PHP递归实现层级树状展现数据

    树状数据展现很常用,今天学习了PHP的递归,也来总结总结! PHP代码: function _getTreeList_custom1($data,$parent_id,$depth) { $retur ...

  10. JDK源码调试

    1.首先遇到了一个问题line unavailable,然后通过以下方式解决: http://blog.csdn.net/xuefeng0707/article/details/8738869 对于想 ...