文章目录
- 摘要
-
- 1. 查询CPU使用率命令:top -bn1 | grep “Cpu(s)” | awk ‘{split($0,arr,” “);print 100-arr[8]}’
- 2. 查询内存命令(单位:G):top -bn1 | grep “KiB Mem” | awk ‘{split($0,arr,” “);print arr[4]/1024/1024,arr[8]/1024/1024}’
- 3.查询磁盘命令:df -h / | tail -n +2 | grep -v “Filesystem” | awk ‘{split($0,arr,” “);print arr[2],arr[3],arr[5]}’
- 4.查询网络IO命令:ifstat
- 5.查询文件服务器中数据库空间占用大小命令:df -h /home/ems/3rdparty/mysql/data
- 6.查询每秒存储队列信息命令:vmstat -d|grep sda|awk ‘{print $11}’
- 7.查询IOPS信息命令:iostat -d|grep sda|awk ‘{print $3″ “$4}’
- 8.查询文件系统中磁盘使用情况命令:df -BG
- 代码
摘要
1. 查询CPU使用率命令:top –bn1 | grep “Cpu(s)” | awk ‘{split($0,arr,” “);print 100-arr[8]}’
[root@unm5800 ~]# top -bn1 | grep "Cpu(s)" | awk '{split($0,arr," ");print 100-arr[8]}'
8.7
2. 查询内存命令(单位:G):top –bn1 | grep “KiB Mem” | awk ‘{split($0,arr,” “);print arr[4]/1024/1024,arr[8]/1024/1024}’
[root@unm5800 ~]# top -bn1 | grep "KiB Mem" | awk '{split($0,arr," ");print arr[4]/1024/1024,arr[8]/1024/1024}'
15.1709 9.70124
3.查询磁盘命令:df –h / | tail -n +2 | grep –v “Filesystem” | awk ‘{split($0,arr,” “);print arr[2],arr[3],arr[5]}’
[root@unm5800 ~]# df -h / | tail -n +2 | grep -v "Filesystem" | awk '{split($0,arr," ");print arr[2],arr[3],arr[5]}'
296G 153G 55%
4.查询网络IO命令:ifstat
[root@unm5800 ~]# ifstat
#kernel
Interface RX Pkts/Rate TX Pkts/Rate RX Data/Rate TX Data/Rate
RX Errs/Drop TX Errs/Drop RX Over/Rate TX Coll/Rate
lo 10764K 0 10764K 0 18446744070881M 0 18446744070881M 0
0 0 0 0 0 0 0 0
enp91s0f0 9181K 0 19534K 0 18446744071947M 0 220464K 0
0 0 0 0 0 0 0 0
enp91s0f1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
eno1 609746 0 1161K 0 128777K 0 940272K 0
0 4546 0 0 0 0 0 0
enp91s0f2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
eno2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
enp91s0f3 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
enp0s20f0u1u6 31847 0 0 0 2316K 0 0 0
0 0 0 0 0 0 0 0
virbr0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
docker0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
br-070bd1b4b466 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
br-08fd260a964c 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
br-67b9049e39d6 1494 0 1494 0 59760 0 170316 0
0 0 0 0 0 0 0 0
br-e3d56b9f14ab 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
br-014f940f0996 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
vethf216528 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
veth930c642 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
veth5bb57ee 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
veth154692a 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
vethc30949a 1494 0 1494 0 80676 0 170316 0
0 0 0 0 0 0 0 0
veth821f11e 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
veth6bb839e 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
veth9671271 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
这些字段是用于描述ifstat命令输出结果中的网络接口的信息。下面是对这些字段的介绍:
- ifstat <网络接口名称>
- Interface(接口):网络接口的名称。
- RX Pkts/Rate(接收数据包数/速率):接收的数据包数量和速率。
- TX Pkts/Rate(发送数据包数/速率):发送的数据包数量和速率。
- RX Data/Rate(接收数据量/速率):接收的数据量(以字节为单位)和速率。
- TX Data/Rate(发送数据量/速率):发送的数据量(以字节为单位)和速率。
- RX Errs/Drop(接收错误/丢弃):接收过程中产生的错误和丢弃的数据包数量。
- TX Errs/Drop(发送错误/丢弃):发送过程中产生的错误和丢弃的数据包数量。
- RX Over/Rate(接收溢出/速率):接收缓冲区溢出次数和速率。
- TX Coll/Rate(发送冲突/速率):发送时发生碰撞的次数和速率。 这些字段提供了关于网络接口的
详细统计信息,包括数据包数量、数据量、速率、错误情况、丢弃情况、溢出情况和碰撞情况等。通过观察这些字段的数值,可以了解网络接口的使用情况、性能状况和可能存在的问题。
5.查询文件服务器中数据库空间占用大小命令:df –h /home/ems/3rdparty/mysql/data
[root@unm5800 ~]# df -h /home/ems/3rdparty/mysql/data
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda1 296G 153G 128G 55% /
6.查询每秒存储队列信息命令:vmstat -d|grep sda|awk ‘{print $11}’
[root@unm5800 ~]# vmstat -d|grep sda|awk '{print $11}'
28415
vmstat -d命令
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 6321733 2992979 326537984 192604637 16335233 11611252 558374392 397953929 0 159463
dm-0 2017377 0 126201641 106958407 7052267 0 129722136 163552294 0 85875
dm-1 4396348 0 35174488 92541369 10793514 0 86348112 1687958320 0 21982
dm-2 3184149 0 165098377 75003459 6532775 0 342299704 396611035 0 89434
根据您提供的 vmstat -d 命令输出,磁盘统计信息中显示了以下设备:
- sda:这是一个磁盘设备的名称,通常代表物理磁盘。它可能是你的系统上的主要硬盘。
- dm-0、dm-1、dm-2:这些是表示设备映射(Device Mapper)的块设备名称。在 Linux 系统中,dm- 表示逻辑卷(Logical Volume)或软件 RAID(Redundant Array of Independent Disks)。
Device Mapper 是 Linux 内核提供的一个模块,用于进行逻辑卷管理、软件 RAID、加密存储等操作。这些逻辑卷或软件 RAID 经过设备映射后,可以在操作系统中当作普通块设备使用。
因此,vmstat -d 命令输出显示了系统上的物理磁盘 sda 和经过设备映射的逻辑卷或软件 RAID dm-0、dm-1、dm-2 的磁盘统计信息。
根据您提供的 vmstat -d 命令输出中的 sec 值为 159463,它表示每秒的输入/输出操作数。这个值是计算从系统启动到当前时刻的累积值。
在 vmstat -d 命令的输出中,sec 表示系统每秒钟处理的输入和输出操作的数量。这包括从磁盘读取的数据量、向磁盘写入的数据量以及其他可能的输入/输出操作(如网络数据传输等)。
因此,sec 值代表每秒的输入/输出操作数,既包括读取操作也包括写入操作。
7.查询IOPS信息命令:iostat -d|grep sda|awk ‘{print $3″ “$4}’
[root@unm5800 ~]# iostat -d|grep sda|awk '{print $3" "$4}'
220.44 3219.56
iostat -d 是一个 Linux 命令,用于显示块设备的输入/输出统计信息。该命令会提供有关磁盘和存储设备的各种性能指标。
Linux 3.10.0-1160.el7.x86_64 (localhost.localdomain) 2023年08月16日 _x86_64_ (4 CPU)
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 17.50 126.18 215.67 163243972 279015092
dm-0 7.00 48.77 50.08 63099436 64789492
dm-1 11.74 13.59 33.37 17587244 43174056
dm-2 7.51 63.79 132.21 82525552 171049324
- Device(设备):列出了系统中的每个块设备,如硬盘驱动器、固态硬盘等。
- tps(每秒传输请求数):表示每秒传输到设备的 I/O 请求的数量。包括读请求和写请求。
- kB_read/s(每秒读取数据量):表示每秒从设备读取的数据量。
- kB_wrtn/s(每秒写入数据量):表示每秒写入设备的数据量。
- kB_read(读取的总数据量):表示自系统启动以来从设备读取的总数据量。
- kB_written(写入的总数据量):表示自系统启动以来写入设备的总数据量。
这些指标可以帮助你了解块设备的性能状况,包括每秒的传输请求数量、读写速度以及设备的总体活动情况。
8.查询文件系统中磁盘使用情况命令:df -BG
[root@unm5800 ~]# df -BG
文件系统 1G-块 已用 可用 已用% 挂载点
devtmpfs 8G 0G 8G 0% /dev
tmpfs 8G 1G 8G 1% /dev/shm
tmpfs 8G 1G 7G 11% /run
tmpfs 8G 0G 8G 0% /sys/fs/cgroup
/dev/sda1 296G 153G 128G 55% /
overlay 296G 153G 128G 55% /var/lib/docker/overlay2/a42d4eb53de76f220eadbacbeecf268c53bc130615f5faf1b0472801bcae4492/merged
overlay 296G 153G 128G 55% /var/lib/docker/overlay2/59b75e561bd8b29088b503779940edbd2a0ad5518994e55a01db625407dfcf12/merged
overlay 296G 153G 128G 55% /var/lib/docker/overlay2/c7a3fd966d533eff6c199de317f6e815d6a1147665a7d81370fa71f4320df410/merged
overlay 296G 153G 128G 55% /var/lib/docker/overlay2/e3c1fe4e379a56b230e40d07d17cce6ba0330aebf3d25e85aa5d9e09ed2ee30a/merged
overlay 296G 153G 128G 55% /var/lib/docker/overlay2/2a5ea591d02955647e5f6c9e566b5c6a99d6ebed817bdc4c410e7b627c690622/merged
shm 1G 0G 1G 0% /var/lib/docker/containers/d6ad7cbc285e3e8d15477c891aded41533f6032c0a46297123a013ef35c12bd6/mounts/shm
shm 1G 0G 1G 0% /var/lib/docker/containers/f3ac596eb8e7a8a0454c8a90f3c1b23e52c5aab23911b335cbba3f5ed7f98574/mounts/shm
shm 1G 0G 1G 0% /var/lib/docker/containers/ccc00bbe149d20d0af4b8ccc581eebed79488742b06c21a93d1e4854d1ff0c83/mounts/shm
shm 1G 1G 1G 1% /var/lib/docker/containers/78f5e497c3104bd442776a272aac2d08abc1a2cdd8e8b917b44ee49e3b7f407a/mounts/shm
shm 1G 0G 1G 0% /var/lib/docker/containers/19835c87c59e5f6bb3f96f929b70076dc3d418089afe80aec63a7692a994b546/mounts/shm
tmpfs 2G 0G 2G 0% /run/user/0
代码
package com.util;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import util.bean.MonitorPo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
public class StringUtilsTests2 {
private static Logger log = LoggerFactory.getLogger(StringUtilsTests2.class);
@Test
void parseString1() {
MonitorPo monitorPo = new MonitorPo();
InputStream in = null;
BufferedReader read = null;
Process pro = null;
String cmd = "";
String[] cmds = null;
try {
getCPUInfo(cmd, cmds, monitorPo, pro, in, read);
getMemoryInfo(cmd, cmds, monitorPo, pro, in, read);
getDiskInfo(cmd, cmds, monitorPo, pro, in, read);
getNetworkResourceInformation(cmd, cmds, monitorPo, pro, in, read);
getUsageOfTheMysqlDiskSpaceInTheFileSystem(cmd, cmds, monitorPo, pro, in, read);
getUsageOfTheDiskSpaceInTheFileSystem(cmd, cmds, monitorPo, pro, in, read);
getIopsResourceInformation(cmd, cmds, monitorPo, pro, in, read);
getStoresQueueInputOrOutputOperandsPerSecond(cmd, cmds, monitorPo, pro, in, read);
} catch (IOException|InterruptedException e) {
log.error("-realTimeMonitoring-Exception:{}", e);
} finally {
try {
if (pro != null)
pro.destroy();
if (read != null)
read.close();
if (in != null)
in.close();
} catch (IOException e) {
log.error("-realTimeMonitoring-finally-IOException:{}", e);
}
}
}
//查询CPU
private static void getCPUInfo(String cmd, String[] cmds, MonitorPo monitorPo, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {
cmd = "top -bn1 | grep "Cpu(s)" | awk '{split($0,arr," ");print 100-arr[8]}'";
String result = getSingleResult(cmd, cmds, pro, in, read);
if (!StringUtils.isEmpty(result)) {
monitorPo.setCpu(Double.valueOf(Double.parseDouble(result)));
} else {
monitorPo.setCpu(Double.valueOf(0.0D));
}
}
//查询内存
private static void getMemoryInfo(String cmd, String[] cmds, MonitorPo monitorPo, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {
cmd = "top -bn1 | grep "KiB Mem" | awk '{split($0,arr," ");print arr[4]/1024/1024,arr[8]/1024/1024}'";
String result = getSingleResult(cmd, cmds, pro, in, read);
String[] arr = result.split(" ");
double memoryTotal = Double.parseDouble(arr[0]);
double memoryUsed = Double.parseDouble(arr[1]);
DecimalFormat df = new DecimalFormat("#.##");
double memoryUseRatio = Double.parseDouble(df.format(memoryUsed / memoryTotal * 100.0D));
monitorPo.setMemoryTotal(Double.valueOf(memoryTotal));
monitorPo.setMemory(Double.valueOf(memoryUsed));
monitorPo.setMemoryUseRatio(Double.valueOf(memoryUseRatio));
}
//查询磁盘
private static void getDiskInfo(String cmd, String[] cmds, MonitorPo monitorPo, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {
cmd = "df -h / | tail -n +2 | grep -v "Filesystem" | awk '{split($0,arr," ");print arr[2],arr[3],arr[5]}'";
String result = getSingleResult(cmd, cmds, pro, in, read);
String[] arr = result.split(" ");
monitorPo.setDiskTotal(Double.valueOf(Double.parseDouble(arr[0].replaceAll("G", ""))));
monitorPo.setDiskUse(Double.valueOf(Double.parseDouble(arr[1].replaceAll("G", ""))));
monitorPo.setDiskUseRatio(Double.valueOf(Double.parseDouble(arr[2].replaceAll("%", ""))));
}
//查询网络IO速率
public static void getNetworkResourceInformation(String cmd, String[] cmds, MonitorPo monitorPo, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {
log.info("-getNetworkResourceInformation");
cmd = "ifstat";
cmds = new String[] { "/bin/sh", "-c", cmd };
log.info("-cmd:{}", cmd);
pro = Runtime.getRuntime().exec(cmds);
if (pro.waitFor() == 0) {
in = pro.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = read.readLine()) != null) {
log.info("-line:{}", line);
if (line.startsWith("eno") || line.startsWith("eth")) {
String[] split = line.split("\s+");
String networkRxRate = split[1];
String networkTxRate = split[3];
monitorPo.setNetworkRxRate(Double.valueOf(Double.parseDouble(networkRxRate)));
monitorPo.setNetworkTxRate(Double.valueOf(Double.parseDouble(networkTxRate)));
break;
}
}
}
}
//查询文件服务器中数据库空间占用大小
public static void getUsageOfTheMysqlDiskSpaceInTheFileSystem(String cmd, String[] cmds, MonitorPo monitorPo, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {
log.info("-getUsageOfTheMysqlDiskSpaceInTheFileSystem");
cmd = "df -h /home/ems/3rdparty/mysql/data";
cmds = new String[] { "/bin/sh", "-c", cmd };
log.info("-cmd:{}", cmd);
pro = Runtime.getRuntime().exec(cmds);
if (pro.waitFor() == 0) {
in = pro.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = read.readLine()) != null) {
log.info("-line:{}", line);
if (line.startsWith("/dev")) {
String[] split = line.split("\s+");
monitorPo.setDatabaseDiskSpaceIsUsed(split[2]);
}
}
}
}
//查询每秒存储队列信息
public static void getStoresQueueInputOrOutputOperandsPerSecond(String cmd, String[] cmds, MonitorPo monitorPo, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {
log.info("-getStoresQueueInputOrOutputOperandsPerSecond");
cmd = "vmstat -d|grep sda|awk '{print $11}'";
cmds = new String[] { "/bin/sh", "-c", cmd };
log.info("-cmd:{}", cmd);
pro = Runtime.getRuntime().exec(cmds);
if (pro.waitFor() == 0) {
in = pro.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = read.readLine()) != null) {
log.info("-line:{}", line);
monitorPo.setQueueOperands(Double.valueOf(Double.parseDouble(line)));
}
}
}
//查询IOPS信息
public static void getIopsResourceInformation(String cmd, String[] cmds, MonitorPo monitorPo, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {
log.info("-getIopsResourceInformation");
cmd = "iostat -d|grep sda|awk '{print $3" "$4}'";
cmds = new String[] { "/bin/sh", "-c", cmd };
log.info("-cmd:{}", cmd);
pro = Runtime.getRuntime().exec(cmds);
if (pro.waitFor() == 0) {
in = pro.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = read.readLine()) != null) {
log.info("-line:{}", line);
String[] split = line.split("\s+");
String IOPSKbRead = split[0];
String IOPSKbWrite = split[1];
monitorPo.setIopsKbRead(Double.valueOf(Double.parseDouble(IOPSKbRead)));
monitorPo.setIopsKbWrite(Double.valueOf(Double.parseDouble(IOPSKbWrite)));
}
}
}
//查询文件系统中磁盘使用情况
public static void getUsageOfTheDiskSpaceInTheFileSystem(String cmd, String[] cmds, MonitorPo monitorPo, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {
log.info("-getUsageOfTheDiskSpaceInTheFileSystem");
cmd = "df -BG";
cmds = new String[] { "/bin/sh", "-c", cmd };
log.info("-cmd:{}", cmd);
pro = Runtime.getRuntime().exec(cmds);
if (pro.waitFor() == 0) {
in = pro.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = read.readLine()) != null) {
log.info("-line:{}", line);
if (line.endsWith("/")) {
String[] split = line.split("\s+");
String used = split[2];
if (used.contains("G"))
used = used.replace("G", "");
monitorPo.setDiskSpaceIsUsed(Double.valueOf(Double.parseDouble(used)));
}
}
}
}
//java执行linux命令
public static String getSingleResult(String cmd, String[] cmds, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {
cmds = new String[] { "/bin/sh", "-c", cmd };
log.info("-cmd:{}", cmd);
pro = Runtime.getRuntime().exec(cmds);
String line = "";
in = pro.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
if (pro.waitFor() == 0 && (line = read.readLine()) != null) {
log.info("-line:{}", line);
return line;
}
return null;
}
}
原文地址:https://blog.csdn.net/a924382407/article/details/134810408
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_48110.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。