preface

For me, the purpose of process monitoring is to find abnormal processes, and the abnormal process is basically reflected in its CPU usage, memory usage, bandwidth usage and disk usage. That is, we can monitor our process by CPU, memory, bandwidth, and disk usage.

The use of the CPU

You can run the uptime command and the top command to view the CPU usage. The top command covers the uptime command.

View the information using uptime

$ uptime
22:31  up 5 days, 12:33, 5 users, load averages: 1.84 2.00 2.02
Copy the code

The first item of the uptime command (22:31) is the current time; The second item (up) indicates that the system is running. The third item (5 days, 12:33) indicates the total system startup time. The last item indicates the average system load in the past 1 minute, 5 minutes, and 15 minutes.

What is a load? System load is the average number of processes in a runnable or non-interruptible state, and load simply represents how busy the CPU is. For a single CPU, if the load is 1, the CPU usage is 100%. If the CPU has four cores and the load is 1, the CPU usage is about 25%.

Viewing by Top

#This line is the load information that uptime getsTop-22:59:28 UP 42 days, 22:15, 2 Users, Load Average: 0.14, 0.10, 0.09
#This line is CPU usage
%Cpu(S): 19.1US, 0.3SY, 0.0Ni, 80.3ID, 0.3WA, 0.0hi, 0.0Si, 0.0ST

#Case of a single processPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2853 mysql 20 0 1185416 459856 7804s 17.9 24.5 3992:26 mysqld 29026 Ubuntu 20 0 842104 173924 13400 S 0.7 9.3 88:37.12 PythonCopy the code

The preceding information contains a line of %Cpu(s). The fields are described as follows:

Sy: system time: indicates the time that the CPU occupies in kernel mode. If SY is high, it indicates that the system is not properly designed in some way. Ni: nice time: indicates the time spent by the system to adjust the process priority id: Idle Time wa: waiting Time: indicates the time spent by the CPU to wait for I/O operations Hi: indicates the time spent by the CPU to adjust the process priority. Si: soft Irq time: soft Irq time st: Steal Time is added after hardware virtualization became popular. It indicates the time that the system is forced to wait for virtual cpusCopy the code

In addition, the top command also contains the CPU usage of a single process. When the CPU usage of a process is too high, it is usually abnormal.

Memory usage

You can run the free and top commands to view the memory usage.

View through Free

The free command shows various memory usage, such as physical memory, swap memory, and so on, mainly from the /proc/meminfo file.

$ free -hTotal Used Free shared buff/ Cache available Mem: 2.0g 450M 1.0g 868K 534M 1.3g Swap: 1.0g 0B 1.0GCopy the code

The fields are explained as follows:

  • Total: indicates the total memory
  • Used: used memory (total-free buffers-cache)
  • Free: unused memory
  • Shared: Memory used (mostly) by TMPFS
  • Buff/Cache: Buff + Cache (kernel dependent)
  • Available: Available memory, which estimates how much memory is available to start a new application without swapping

There are several formats for presentation (just to list the common ones) :

#Human readable
$ free -hTotal Used Free shared buff/ Cache available Mem: 2.0g 450M 1.0g 868K 534M 1.3g Swap: 1.0G 0B 1.0G free-b # The unit of bytes is free-k # The unit of KB free-m # The unit of MB Free-g # The unit of GBCopy the code

Viewing by Top

#Basically consistent with freeKiB Mem : 1877052 total, 137892 free, 859412 used, 879748 buff/cache KiB Swap: 0 total, 0 free, 0 used. 849288 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2853 mysql 20 0 1185416 459856 7804 S 17.9 24.5 3992:26 mysqld 29026 Ubuntu 20 0 842104 173924 13400s 0.7 9.3 88:37.12 Python 17.9 24.5 3992:26 mysqld 29026 Ubuntu 20 0 842104 173924 13400s 0.7 9.3 88:37.12 PythonCopy the code

The above keywords are explained as follows:

  • PR: indicates the priority level of the process. The smaller the priority, the higher the execution priority
  • VIRT: virtual memory occupied by a process
  • RES: physical memory occupied by a process
  • SHR: shared memory used by a process
  • S: indicates the process status. S means sleep, R means running, Z means dead, and N means the process has a negative priority value
  • %CPU: indicates the CPU usage of the process
  • %MEM: The percentage of physical memory and total memory used by the process

Note: The idea behind Linux memory management is to maximize memory utilization, so the kernel allocates cached memory. Cached is not free, so a small amount of free memory does not mean a small amount of available memory. Some of the cached is converted to free.

For applications, pay more attention to the consumption of virtual memory Swap. If the Swap usage is too high, the physical memory is insufficient. When the physical memory is insufficient, the operating system will put part of the physical memory into the disk, resulting in disk I/O. The disk I/O speed is much slower than the physical memory. This is the common “lag” phenomenon

Bandwidth usage

To check the network usage, you can use sysstat to install the following command:

$ sudo apt install sysstat

This is a toolkit with several commands available after installation, namely SAR, iostat, and mpstat

yangan@yangan-X555LI:~$sar-n DEV 1 1 Linux 4.15.0-47-generic (yangan-x555li) 2019年04月29日 _x86_64_ (4 CPU) 08 hours 48 minutes 46 seconds IFACE RXPCK /s TXPCK /s rxkB/s txkB/s RXCMP /s TXCMP /s RXMCST /s % IFUtil 08 分 48分47秒 ENP2S0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

#instructionsEnp2s0 is a wired nic, LO is a local loopback network, and WLP3S0 is a wireless NIC. RXPCK /s: number of received data packets TXPCK /s: number of sent data packets rxkB/s: number of received bytes RXCMP /s: number of received compressed packets RXMCST /s: number of received broadcast packets per secondCopy the code

Disk usage

The sysstat toolkit above also includes tools for viewing disk usage as follows:

yangan@yangan-X555LI:~$iostat -d -k Linux 4.15.0-47-generic (yangan-x555li) 2019年04月29日 _x86_64_ (4 CPU) Device TPS KB_read /s kB_wrtn/s kB_read kB_wrtn sDA 0.10 0.66 0.02 139205 3392 SDB 6.44 14.47 431.21 3072049 91530664 loop8 0.00 0.00 0.00 493 0 Loop9 0.00 0.00 0.00 121 0 TPS: Number of I/O requests processed per second kB_read/s: data volume read from the device per second kB_wrtn/s: data volume written to the device per second kB_read: total data read kB_wrtn: total data volume writtenCopy the code

Write in the last

Dear bosses, creation is not easy, but it needs to be honed and summarized constantly, welcome to pay attention to me, I am Yan Gan, I will share with you all kinds of practical programming knowledge and skills, your praise and attention is the biggest motivation for my progress and creation!!