One 、 Script

Today, I mainly share one shell Script , Used to obtain linux System CPU、 Memory 、 disk IO Etc .

#!/bin/bash#  Get the local server to monitor IP Address IP=`ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'`echo "IP Address :"$IP
 #  obtain cpu Total number of cores cpu_num=`grep -c "model name" /proc/cpuinfo`echo "cpu Total number of cores :"$cpu_num
 # 1、 obtain CPU utilization #################################################us  User space occupancy CPU percentage #sy  Kernel space footprint CPU percentage #ni  User process space changes the priority of the process occupation CPU percentage #id  Free CPU percentage #wa  Waiting for input and output CPU Time percentage #hi  Hardware interrupt #si  Software interrupt ##################################################  Get user space usage CPU percentage cpu_user=`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`echo " User space occupancy CPU percentage :"$cpu_user
 #  Get kernel space usage CPU percentage cpu_system=`top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%"`echo " Kernel space footprint CPU percentage :"$cpu_system
 #  Get free CPU percentage cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`echo " Free CPU percentage :"$cpu_idle
 #  Get the waiting input and output CPU percentage cpu_iowait=`top -b -n 1 | grep Cpu | awk '{print $10}' | cut -f 1 -d "%"`echo " Wait for input and output to occupy CPU percentage :"$cpu_iowait
 #2、 obtain CPU Context switch and interrupt times #  obtain CPU Interruption times cpu_interrupt=`vmstat -n 1 1 | sed -n 3p | awk '{print $11}'`echo "CPU Interruption times :"$cpu_interrupt
 #  obtain CPU Number of context switches cpu_context_switch=`vmstat -n 1 1 | sed -n 3p | awk '{print $12}'`echo "CPU Number of context switches :"$cpu_context_switch
 #3、 obtain CPU Load information #  obtain CPU15 Load average from minutes ago to now cpu_load_15min=`uptime | awk '{print $11}' | cut -f 1 -d ','`echo "CPU 15 Load average from minutes ago to now :"$cpu_load_15min
 #  obtain CPU5 Load average from minutes ago to now cpu_load_5min=`uptime | awk '{print $10}' | cut -f 1 -d ','`echo "CPU 5 Load average from minutes ago to now :"$cpu_load_5min
 #  obtain CPU1 Load average from minutes ago to now cpu_load_1min=`uptime | awk '{print $9}' | cut -f 1 -d ','`echo "CPU 1 Load average from minutes ago to now :"$cpu_load_1min
 #  Get task queue ( Number of processes waiting in ready state )cpu_task_length=`vmstat -n 1 1 | sed -n 3p | awk '{print $1}'`echo "CPU Task queue length :"$cpu_task_length
 #4、 Get memory information #  Get the total amount of physical memory mem_total=`free | grep Mem | awk '{print $2}'`echo " Total physical memory :"$mem_total
 #  Gets the total amount of memory used by the operating system mem_sys_used=`free | grep Mem | awk '{print $3}'`echo " Total memory used ( operating system ):"$mem_sys_used
 #  Gets the total amount of unused memory of the operating system mem_sys_free=`free | grep Mem | awk '{print $4}'`echo " Total memory remaining ( operating system ):"$mem_sys_free
 #  Gets the total amount of memory used by the application mem_user_used=`free | sed -n 3p | awk '{print $3}'`echo " Total memory used ( Applications ):"$mem_user_used
 #  Gets the total amount of unused memory for the application mem_user_free=`free | sed -n 3p | awk '{print $4}'`echo " Total memory remaining ( Applications ):"$mem_user_free
 
 #  Get the total size of the swap partition mem_swap_total=`free | grep Swap | awk '{print $2}'`echo " Total size of swap partition :"$mem_swap_total
 #  Gets the size of the used swap partition mem_swap_used=`free | grep Swap | awk '{print $3}'`echo " Swap partition size used :"$mem_swap_used
 #  Get the size of the remaining swap partition mem_swap_free=`free | grep Swap | awk '{print $4}'`echo " The size of the remaining swap partition :"$mem_swap_free
 #5、 Get disk I/O Statistics echo " Designated equipment (/dev/sda) Statistical information "#  Number of read requests to the device 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 the device per second :"$disk_sda_ws
 #  To the device I/O Average request queue length disk_sda_avgqu_sz=`iostat -kx | grep sda| awk '{print $9}'`echo " To the device I/O Average request queue length "$disk_sda_avgqu_sz
 #  Every time you send it to the device I/O Average request time disk_sda_await=`iostat -kx | grep sda| awk '{print $10}'`echo " Every time you send it to the device I/O Average request time :"$disk_sda_await
 #  To the device I/O Average service time disk_sda_svctm=`iostat -kx | grep sda| awk '{print $11}'`echo " To the device I/O Average service time :"$disk_sda_svctm
 #  Send... To the device I/O Requested CPU Time percentage disk_sda_util=`iostat -kx | grep sda| awk '{print $12}'`echo " Send... To the device I/O Requested CPU Time percentage :"$disk_sda_util

Execution results :

Two 、 Principle explanation

Know what it is and know why it is , Let's explain the principle of script implementation in detail .

1. Get the local server to monitor IP Address

IP=`ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'`echo "IP Address :"$IP

  1. ifconfig | grep inet Filter out the following results, including strings inet The line of , As shown in the figure below, two lines are circled in red
    among | It means pipe , Input the result of the previous command to | The order on the right
  2. grep -vE 'inet6|127.0.0.1’ Pass the result of the first step through the command grep -vE Filter out those that contain inet6 and 127.0.0.1 The line of
  3. Pass the result of the second step through the command awk Split the string ,$n(0~N) Corresponding to the corresponding parameters , As shown in the figure below $2 Corresponding Address :192.168.0.125,**’{print $2}’** Print out $2 Value
  4. Assign the result of the third step to the variable IP
  5. echo “IP Address :”$IP Print out variables IP Value ,【 The functions of the assignment and printing statements in the following scripts are the same , Don't repeat the explanation 】
    ifconfig

2. obtain cpu Total number of cores

cpu_num=`grep -c "model name" /proc/cpuinfo`echo "cpu Total number of cores :"$cpu_num

  1. stay linux Of /proc The directory contains a lot of information about system resources , among **/proc/cpuinfo** Stored the system runtime cpu A lot of important information about .
  2. be-all cpu Nuclear information is provided by model name The string gives ,
  3. Through the command **grep -c "model name" /proc/cpuinfo** You can calculate the file /proc/cpuinfo A string appears in model name Number of occurrences , You can get cpu Total number of cores .
    -c Indicates the number of occurrences of a statistical string .

As shown below :
cpuinfo

3. obtain CPU utilization

top Commands are often used to monitor linux The state of the system , Is a common performance analysis tool , It can display the resource usage of each process in the system in real time .

#  Get user space usage CPU percentage cpu_user=`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`echo " User space occupancy CPU percentage :"$cpu_user
 #  Get kernel space usage CPU percentage cpu_system=`top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%"`echo " Kernel space footprint CPU percentage :"$cpu_system
 #  Get free CPU percentage cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`echo " Free CPU percentage :"$cpu_idle
 #  Get the waiting input and output CPU percentage cpu_iowait=`top -b -n 1 | grep Cpu | awk '{print $10}' | cut -f 1 -d "%"`echo " Wait for input and output to occupy CPU percentage :"$cpu_iowait

top

  1. top -b -n 1 Display system information and print it in format , It turns out to refresh only once
n  Set the number of screen refreshes before exiting
b  take top The output is formatted to fit the output to the file , You can use this option to create process logs 

  1. grep Cpu Extract string Cpu Where the line is
  2. awk '{print $2}' Split the string from the second step , And call methods print Print out **$2** The corresponding second string ,0.5%us
  3. cut -f 1 -d “%” Said to % Separator , Separate the results of the third step , And display the first string of demerit after segmentation, namely 0.5
-d  "%"  In order to % As a separator , 
-f 1 Display with : Split the first paragraph of each line 

  1. Other scripts, and so on

other Cpu The meaning of the utilization parameter is as follows :

#us  User space occupancy CPU percentage #sy  Kernel space footprint CPU percentage #ni  User process space changes the priority of the process occupation CPU percentage #id  Free CPU percentage #wa  Waiting for input and output CPU Time percentage #hi  Hardware interrupt #si  Software interrupt 

4. obtain CPU Context switch and interrupt times

#  obtain CPU Interruption times cpu_interrupt=`vmstat -n 1 1 | sed -n 3p | awk '{print $11}'`echo "CPU Interruption times :"$cpu_interrupt
 #  obtain CPU Number of context switches cpu_context_switch=`vmstat -n 1 1 | sed -n 3p | awk '{print $12}'`echo "CPU Number of context switches :"$cpu_context_switch#  Get task queue ( Number of processes waiting in ready state )cpu_task_length=`vmstat -n 1 1 | sed -n 3p | awk '{print $1}'`echo "CPU Task queue length :"$cpu_task_length

  1. vmstat yes Virtual Meomory Statistics( Virtual memory statistics ) Abbreviation , Virtual memory of the operating system 、 process 、CPU Activity monitoring . It is to make statistics on the overall situation of the system , The disadvantage is that there is no in-depth analysis of a process .vmstat -n 1 1 The name of each field is displayed only once .
-n: Display field names only once at the beginning .

  1. sed -n 3p Print the result of the first step out of the second 3 That's ok
 Parameter description :
    -n or --quiet or --silent  Cancel auto print mode space , Show only script The result of the treatment .
Action description :
    p : Print , Will also print out some selected data . Usually  p  And parameters  sed -n  Run together ~

  1. **awk ‘{print $1}’`** Will be the first 2 Step result of the string segmentation , And print the first string

vmstat

5、 obtain CPU Load information

#  obtain CPU15 Load average from minutes ago to now cpu_load_15min=`uptime | awk '{print $11}' | cut -f 1 -d ','`echo "CPU 15 Load average from minutes ago to now :"$cpu_load_15min
 #  obtain CPU5 Load average from minutes ago to now cpu_load_5min=`uptime | awk '{print $10}' | cut -f 1 -d ','`echo "CPU 5 Load average from minutes ago to now :"$cpu_load_5min
 #  obtain CPU1 Load average from minutes ago to now cpu_load_1min=`uptime | awk '{print $9}' | cut -f 1 -d ','`echo "CPU 1 Load average from minutes ago to now :"$cpu_load_1min

  1. uptime Commands can be used to see how long the server has been running , How many users are currently logged in , And the server in the past 1 minute 、5 minute 、15 Average system load in minutes .
    The system load is in the runnable state runnable Or non interruptible uninterruptable The average number of processes in state . A runnable process is either in use CPU Or waiting to use CPU. Non interruptible processes are waiting for some I/O visit , Like waiting for disk IO.
    There is an average of three time intervals . The meaning of average load depends on CPU It's different in number , The load is 1 For a single CPU It means the load is full , And for someone who has 4 CPU In terms of system, it means 75% All my time is free .
  2. Refer to the previous script analysis ,**awk ‘{print $9}’ | cut -f 1 -d ‘,’** Split the result of the first step , And get the second 9 A string , And then use ‘,’ Separate , And get the first string after segmentation

uptime

6、 Get memory information

#  Get the total amount of physical memory mem_total=`free | grep Mem | awk '{print $2}'`echo " Total physical memory :"$mem_total
 #  Gets the total amount of memory used by the operating system mem_sys_used=`free | grep Mem | awk '{print $3}'`echo " Total memory used ( operating system ):"$mem_sys_used
 #  Gets the total amount of unused memory of the operating system mem_sys_free=`free | grep Mem | awk '{print $4}'`echo " Total memory remaining ( operating system ):"$mem_sys_free
 #  Gets the total amount of memory used by the application mem_user_used=`free | sed -n 3p | awk '{print $3}'`echo " Total memory used ( Applications ):"$mem_user_used
 #  Gets the total amount of unused memory for the application mem_user_free=`free | sed -n 3p | awk '{print $4}'`echo " Total memory remaining ( Applications ):"$mem_user_free
 
 #  Get the total size of the swap partition mem_swap_total=`free | grep Swap | awk '{print $2}'`echo " Total size of swap partition :"$mem_swap_total
 #  Gets the size of the used swap partition mem_swap_used=`free | grep Swap | awk '{print $3}'`echo " Swap partition size used :"$mem_swap_used
 #  Get the size of the remaining swap partition mem_swap_free=`free | grep Swap | awk '{print $4}'`echo " The size of the remaining swap partition :"$mem_swap_free

  1. free Command shows the usage of system memory , Including physical memory 、 Swap memory (swap) And kernel buffer memory .
  2. grep Swap Filter the result of the first step to show only the string containing Swap The line of
  3. **awk ‘{print $4}’** Split the result of the second step , And print out the value of the fourth string

free【 Other scripts refer to the previous analysis 】

7. Get disk I/O Statistics

echo " Designated equipment (/dev/sda) Statistical information "#  Number of read requests to the device 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 the device per second :"$disk_sda_ws
 #  To the device I/O Average request queue length disk_sda_avgqu_sz=`iostat -kx | grep sda| awk '{print $9}'`echo " To the device I/O Average request queue length "$disk_sda_avgqu_sz
 #  Every time you send it to the device I/O Average request time disk_sda_await=`iostat -kx | grep sda| awk '{print $10}'`echo " Every time you send it to the device I/O Average request time :"$disk_sda_await
 #  To the device I/O Average service time disk_sda_svctm=`iostat -kx | grep sda| awk '{print $11}'`echo " To the device I/O Average service time :"$disk_sda_svctm
 #  Send... To the device I/O Requested CPU Time percentage disk_sda_util=`iostat -kx | grep sda| awk '{print $12}'`echo " Send... To the device I/O Requested CPU Time percentage :"$disk_sda_util

  1. iostat Commands are used to monitor system I / O devices and CPU Usage situation . It is characterized by reporting disk activity statistics , It will also report CPU usage .
-k: Display status in kilobytes per second , Instead of using blocks per second -x: Show extended status 

  1. ** grep sda Used to filter the results from the first step , Display only containing strings sda** Which line?
  2. **awk ‘{print $4}’** Split the result of the second step , And only the second 4 A string

iostat

iostat from Red Hat Enterprise Linux AS Release . meanwhile iostat It's also Sysstat Part of . So we need to install sysstat.

install sysstat package :

sudo apt-get install sysstat

Reference resources :
https://www.toutiao.com/i6754887380399849998/