A, scripts,
A shell script is used to obtain information about CPU, memory, disk IO, etc.
#!/bin/Bash # get local server IP address IP=` ifconfig | grep inet | grep - vE 'inet6 | 127.0.0.1' | awk '{print $2}' `
echo "IP address:"$IP # Obtain the number of CPU cores cpu_num=`grep -c "model name" /proc/cpuinfo`
echo "Total CPU cores:"$cpu_num
# 1# Obtain the CPU usage of the user space. Cpu_user =`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`
echo "% CPU occupied by user space:"$cpu_user # cpu_system=`top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%"`
echo "% CPU occupied by kernel space:"$cpu_system # Cpu_idle =`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`
echo "% free CPU:"$CPU_idle # Obtain the CPU input/output ratio cpu_ioWAIT =`top -b -n 1 | grep Cpu | awk '{print $10}' | cut -f 1 -d "%"`
echo "% of CPU waiting for input/output:"$cpu_iowait
#2Number of CPU interrupts cpu_interrupt=`vmstat -n 1 1 | sed -n 3p | awk '{print $11}'`
echo "CPU interrupt times:"$cpu_interrupt # Cpu_context_switch =`vmstat -n 1 1 | sed -n 3p | awk '{print $12}'`
echo "CPU context switch times:"$cpu_context_switch
#3Obtain the average CPU load from 15 minutes ago to now cpu_load_15min=`uptime | awk '{print $11}' | cut -f 1 -d ','`
echo "Average CPU load from 15 minutes ago to now:"$cpu_load_15min # Get the average cpu_load_5min from 5 minutes ago to now`uptime | awk '{print $10}' | cut -f 1 -d ','`
echo "Average CPU load from 5 minutes ago to now:"$cpu_load_5min # Obtain the average cpu_load_1min from 1 minute ago to now`uptime | awk '{print $9}' | cut -f 1 -d ','`
echo "Average CPU load from 1 minute ago to now:"$cpu_load_1min # Get the task queue (number of processes waiting in the ready state) cpu_task_length=`vmstat -n 1 1 | sed -n 3p | awk '{print $1}'`
echo "CPU task queue length:$cpu_task_length
#4# Obtain the total physical memory mem_total=`free | grep Mem | awk '{print $2}'`
echo "Total physical memory:"$mem_total # Get the total memory used by the operating system mem_sys_used=`free | grep Mem | awk '{print $3}'`
echo "Total memory used (OS) :"$mem_sys_used # get total unused memory mem_sys_free=`free | grep Mem | awk '{print $4}'`
echo "Total remaining memory (operating system) :"$mem_sys_free # get the total amount of memory mem_user_used=`free | sed -n 3p | awk '{print $3}'`
echo "Total memory used (application) :"$mem_user_used # Get the total amount of unused memory mem_user_free=`free | sed -n 3p | awk '{print $4}'`
echo "Total amount of memory left (application) :"$mem_user_free # Get total size of swap partition mem_swap_total=`free | grep Swap | awk '{print $2}'`
echo "Total swap partition size:"$mem_swap_total # get the used swap partition size mem_swap_used=`free | grep Swap | awk '{print $3}'`
echo "Used swap partition size:"$mem_swap_used # get the remaining swap partition size mem_swap_free=`free | grep Swap | awk '{print $4}'`
echo "Remaining swap partition size:"$mem_swap_free
#5To obtain disk I/O statistics echo"/dev/sda statistics for the specified device"Number of read requests per second disk_sda_rs=`iostat -kx | grep sda| awk '{print $4}'`
echo "Number of read requests to the device per second:"$disk_sda_rs # Number of write requests to the device per second disk_sda_ws=`iostat -kx | grep sda| awk '{print $5}'`
echo "Number of write requests to device per second:"$disk_sda_ws # Average I/O request queue length disk_sda_AVgqu_sz =`iostat -kx | grep sda| awk '{print $9}'`
echo "Average I/O request queue length to device"$disk_sDA_AVgqu_sz # Average TIME per I/O request to device disk_sda_await=`iostat -kx | grep sda| awk '{print $10}'`
echo Average TIME of each I/O request to the device:$disk_sda_await # Average I/O service time sent to the device disk_sda_svctm=`iostat -kx | grep sda| awk '{print $11}'`
echo Average I/O service time sent to device:$disk_sda_svctm # Percentage of CPU time that initiates I/O requests to the device disk_sda_util=`iostat -kx | grep sda| awk '{print $12}'`
echo "Percentage of CPU time initiating I/O requests to the device:"$disk_sda_util
Copy the code
Execution Result:
Second, principle explanation
But also know why, the following details we explain the principle of script implementation, Linux interview actual combat real questions, practice makes perfect, interested can practice!
1. Obtain the IP address of the local server to be monitored
IP=` ifconfig | grep inet | grep - vE 'inet6 | 127.0.0.1' | awk '{print $2}' `
echo "IP address:"$IP
Copy the code
- Ifconfig | grep inet filter out the following results contain the string inet line, two lines below the red circle | means pipeline, will command results as an input to | right in front of the command
- Grep – vE ‘inet6 | 127.0.0.1’ will be the first step the results through the grep command – vE filter contains inet6 and 127.0.0.1 line
- The results of the second step are divided into strings by command awk, and N (0 n)∗∗ corresponds to corresponding parameters, as shown in the figure how much N (0~ n)** corresponds to corresponding parameters, as shown in the figure how much N (0 n)∗ corresponds to corresponding parameters, and as shown in the figure how much 2 corresponds to the address: 192.168.0.125**, **'{print 2}’** Prints the value of 2
- Assign the result of step 3 to the variable IP
- Echo “IP address: “$IP Print the value of the variable IP.
Obtain the total number of CPU cores
cpu_num=`grep -c "model name" /proc/cpuinfo`
echo "Total CPU cores:"$cpu_num
Copy the code
- **/proc/cpuinfo** contains a lot of important information about the CPU while the system is running.
- All CPU core information is given by the Model Name string,
- To obtain the total number of CPU cores, run the **grep -c “model name” /proc/cpuinfo** command to count the number of times model name appears in /proc/cpuinfo. -c indicates the number of occurrences of a character string.
As follows:
3. Obtain the CPU usage
The top command is often used to monitor the Linux system status. It is a common performance analysis tool and can display the resource usage of each process in the system in real time.
Cpu_user =`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`
echo "% CPU occupied by user space:"$cpu_user # cpu_system=`top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%"`
echo "% CPU occupied by kernel space:"$cpu_system # Cpu_idle =`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`
echo "% free CPU:"$CPU_idle # Obtain the CPU input/output ratio cpu_ioWAIT =`top -b -n 1 | grep Cpu | awk '{print $10}' | cut -f 1 -d "%"`
echo "% of CPU waiting for input/output:"$cpu_iowait
Copy the code
- Top-b -n 1 Displays system information and prints it in format. The result is refreshed only once
N Set the number of screen flusher before exiting b Format the top output into a format suitable for output to a file. You can use this option to create a process logCopy the code
- Grep Cpu Extracts the line where the STRING Cpu resides
- **awk ‘{print 2}’ splits the string obtained in step 2 and calls the method print** to print out the second string corresponding to \*\*2**, 0.5%us
- Cut -f 1 -d “%” indicates that the result of the third step is separated by %, and the first string of the demerit after splitting is displayed, namely 0.5
-d "%"The separator is %, -f1Displays with: split the first paragraph of each lineCopy the code
- The same goes for other scripts
The meanings of other Cpu usage parameters are as follows:
#us % CPU usage of user space # SY % CPU usage of kernel space #ni % CPU usage of user process space that changed priority # ID % IDLE CPU # WA % CPU time waiting for input/output # Hi Hardware interrupt # Si Software interruptCopy the code
4. Obtain the number of CPU context switches and interrupts
Cpu_interrupt =`vmstat -n 1 1 | sed -n 3p | awk '{print $11}'`
echo "CPU interrupt times:"$cpu_interrupt # Cpu_context_switch =`vmstat -n 1 1 | sed -n 3p | awk '{print $12}'`
echo "CPU context switch times:"$cpu_context_switch # Obtain the task queue (number of processes waiting in the ready state) cpu_task_length=`vmstat -n 1 1 | sed -n 3p | awk '{print $1}'`
echo "CPU task queue length:$cpu_task_length
Copy the code
- Vmstat is short for Virtual Meomory Statistics. It monitors the Virtual memory, process, and CPU activities of an OPERATING system (OS). Is the overall situation of the system statistics, the deficiency of a process can not be in-depth analysis. Vmstat -n 1 1 The field name is displayed only once.
-n: Displays the field name only once at the beginning.Copy the code
- Sed -n 3p prints the result of the first step to line 3
Parameter description: -n or --quiet or --silent Cancels the automatic printing mode space and displays only the script processing results. Action description: P: print, also a selected data print. Normally p is run with the sed-n argument ~Copy the code
- **awk ‘{print $1}’ ‘** Split the string from step 2 and print the first string
5. Obtain CPU load information
Cpu_load_15min =`uptime | awk '{print $11}' | cut -f 1 -d ','`
echo "Average CPU load from 15 minutes ago to now:"$cpu_load_15min # Get the average cpu_load_5min from 5 minutes ago to now`uptime | awk '{print $10}' | cut -f 1 -d ','`
echo "Average CPU load from 5 minutes ago to now:"$cpu_load_5min # Obtain the average cpu_load_1min from 1 minute ago to now`uptime | awk '{print $9}' | cut -f 1 -d ','`
echo "Average CPU load from 1 minute ago to now:"$cpu_load_1min
Copy the code
- You can run the uptime command to view how long the server has been running, how many users are currently logged in, and the average load of the server in the past one minute, five minutes, and 15 minutes. The system load is the average number of processes in the runnable or uninterruptable state. A runnable process is either using the CPU or waiting to use it. A process in the non-interruptible state is waiting for some I/O access, such as disk I/O. There’s an average of three time intervals. The meaning of the load mean varies depending on the number of cpus in the system. A load of 1 means full on a single-CPU system, while a load of 4 means idle 75% of the time.
- Reference before the script analysis, * * awk ‘{print $9}’ | the cut – f – 1 d ‘, ‘* * separated the results of the first step, and get the ninth string, and then use the’, ‘separated, and divided after the first string
6. Obtain memory information
Mem_total =`free | grep Mem | awk '{print $2}'`
echo "Total physical memory:"$mem_total # Get the total memory used by the operating system mem_sys_used=`free | grep Mem | awk '{print $3}'`
echo "Total memory used (OS) :"$mem_sys_used # get total unused memory mem_sys_free=`free | grep Mem | awk '{print $4}'`
echo "Total remaining memory (operating system) :"$mem_sys_free # get the total amount of memory mem_user_used=`free | sed -n 3p | awk '{print $3}'`
echo "Total memory used (application) :"$mem_user_used # Get the total amount of unused memory mem_user_free=`free | sed -n 3p | awk '{print $4}'`
echo "Total amount of memory left (application) :"$mem_user_free # Get total size of swap partition mem_swap_total=`free | grep Swap | awk '{print $2}'`
echo "Total swap partition size:"$mem_swap_total # get the used swap partition size mem_swap_used=`free | grep Swap | awk '{print $3}'`
echo "Used swap partition size:"$mem_swap_used # get the remaining swap partition size mem_swap_free=`free | grep Swap | awk '{print $4}'`
echo "Remaining swap partition size:"$mem_swap_free
Copy the code
- The free command displays the usage of system memory, including physical memory, swap memory, and kernel buffer memory.
- Grep Swap filters the results of the first step to show only lines containing the string Swap
- **awk ‘{print $4}’** Splits the result of the second step and prints out the value of the fourth string
7. Obtain disk I/O statistics
echo "/dev/sda statistics for the specified device"Number of read requests per second disk_sda_rs=`iostat -kx | grep sda| awk '{print $4}'`
echo "Number of read requests to the device per second:"$disk_sda_rs # Number of write requests to the device per second disk_sda_ws=`iostat -kx | grep sda| awk '{print $5}'`
echo "Number of write requests to device per second:"$disk_sda_ws # Average I/O request queue length disk_sda_AVgqu_sz =`iostat -kx | grep sda| awk '{print $9}'`
echo "Average I/O request queue length to device"$disk_sDA_AVgqu_sz # Average TIME per I/O request to device disk_sda_await=`iostat -kx | grep sda| awk '{print $10}'`
echo Average TIME of each I/O request to the device:$disk_sda_await # Average I/O service time sent to the device disk_sda_svctm=`iostat -kx | grep sda| awk '{print $11}'`
echo Average I/O service time sent to device:$disk_sda_svctm # Percentage of CPU time that initiates I/O requests to the device disk_sda_util=`iostat -kx | grep sda| awk '{print $12}'`
echo "Percentage of CPU time initiating I/O requests to the device:"$disk_sda_util
Copy the code
- The iostat command is used to monitor system INPUT/output devices and CPU usage. It features disk activity statistics, as well as CPU usage.
-k: displays the status in kilobytes per second instead of blocks per second. -x: displays the extended statusCopy the code
- ** grep sda is used to filter the results of the first step, showing only the lines containing the string sda**
- **awk ‘{print $4}’** Splits the result of the second step and displays only the fourth string
iostat
Iostat is published by Red Hat Enterprise Linux AS. Iostat is also part of Sysstat. So we install sysstat.
Install sysstat package:
sudo apt-get install sysstat
Copy the code
The original link: www.toutiao.com/i6939817123… ?