摘要
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
复制代码 运行 iostat -d 命令会显示如下信息:
- 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;
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |