Linux-函數的使用-編寫監控腳本
- 前言
- 一、監控cpu
- 二、采集內存的使用信息
- 三、采集磁盤和分區的使用信息
- 四、顯示進程的信息
前言
編寫監控腳本實現以下功能
監控cpu,內存,磁盤,進程等信息,每隔5分鐘記錄這些信息到日志文件里performance_usage.log
一、監控cpu
1.編寫一個消耗cpu的腳本test1.sh
[root@hz shell]# vim test1.sh
#!/bin/bash
# 消耗cpu資源
i=1
while :
do((i++))echo $i
done
2.執行腳本
[root@hz shell]# bash test1.sh
3.另一個終端編寫監控腳本采集cpu信息
小數運算使用bc計算器
[root@hz shell]# echo “10.4 + 45.7”|bc
56.1
[root@hz shell]# vim monitor.sh
#!/bin/bash
# 采集cpu信息
cpu_info() {top -bn 1|awk '/^%Cpu/{print $2,$4,$8}'|while read us sy idledo# 得到使用的cpu比率used_cpu=$( echo "scale=2;100-$idle"|bc )# 采集信息存放到日志文件,同時輸出屏幕echo "use process: $us system process: $sy idle: $idle used: $used_cpu"|tee -a /var/log/performance_usage.logdone
}
# 調用cpu_info函數
cpu_info[root@hz shell]# bash monitor.sh
use process: 26.5 system process: 50.0 idle: 2.9 used: 97.1
[root@hz shell]# cat /var/log/performance_usage.log
use process: 26.5 system process: 50.0 idle: 2.9 used: 97.1
二、采集內存的使用信息
[root@hz shell]#
free -m
total used free shared buff/cache available
Mem: 3627 504 2985 9 370 3123
Swap: 2047 0 2047
[root@hz shell]#cat /proc/meminfo
MemTotal: 3714640 kB
MemFree: 3057220 kB
MemAvailable: 3198012 kB
Buffers: 2708 kB
Cached: 325128 kB
total 表示總的內存
free 表示重來沒有使用的
used 表示使用的內存
buffer 寫數據的時候的緩存 --》算使用的空間,但是操作系統會回收里面的空間,這樣就可以釋放部分空間
cache 讀數據的時候的緩存
shared 共享內存消耗的空間 --》使用的空間
available 表示整個系統里還可以使用的內存 available=free+buffer/cache里釋放的緩存空間
used=buffer/cache里還在使用的空間+shared+進程真正消耗的空間
[root@hz shell]# mv monitor.sh monitor_cpu_mem.sh
[root@hz shell]# vim monitor_cpu_mem.sh
#!/bin/bashget_time=$(date +%Y%m%d%H%M%S)
# 采集cpu信息
cpu_info() {top -bn 1|awk '/^%Cpu/{print $2,$4,$8}'|while read us sy idledo# 得到使用的cpu比率used_cpu=$( echo "scale=2;100-$idle"|bc )# 采集信息存放到日志文件,同時輸出屏幕echo "$get_time user process: $us system process: $sy idle: $idle used: $used_cpu"|tee -a /var/log/performance_usage.logdone
}# 采集內存信息
mem_info() {free -m|awk '/^Mem/{print $2,$3,$4,$5,$6,$7}'|while read total used free shared buff_cache availabledoecho "$get_time total_mem: $total M available_mem: $available free_mem: $free used_mem: $used_mem buff_cache: $buff_cache shared: $shared"|tee -a /var/log/performance_usage.log done
}
# 調用cpu_info函數
cpu_info# 調用mem_info函數
mem_info[root@hz shell]# bash monitor_cpu_mem.sh
20250628222334 user process: 32.3 system process: 48.4 idle: 0.0 used: 100.0
20250628222334 total_mem: 3627 M available_mem: 2811 free_mem: 2708 used_mem: buff_cache: 327 shared: 9[root@hz shell]# cat /var/log/performance_usage.log
use process: 26.5 system process: 50.0 idle: 2.9 used: 97.1
20250628221600 user process: 0.0 system process: 3.1 idle: 96.9 used: 3.1
20250628221600 total_mem: 3627 M available_mem: 2819 free_mem: 2806 used_mem: buff_cache: 236 shared: 8
20250628221823 user process: 28.1 system process: 46.9 idle: 3.1 used: 96.9
20250628221823 total_mem: 3627 M available_mem: 2816 free_mem: 2802 used_mem: buff_cache: 236 shared: 9
20250628222334 user process: 32.3 system process: 48.4 idle: 0.0 used: 100.0
20250628222334 total_mem: 3627 M available_mem: 2811 free_mem: 2708 used_mem: buff_cache: 327 shared: 9
三、采集磁盤和分區的使用信息
[root@hz shell]# mv monitor_cpu_mem.sh monitor_cpu_mem_disk.sh
[root@hz shell]# cat monitor_cpu_mem_disk.sh
#!/bin/bash
get_time=$(date +%Y%m%d%H%M%S)
# 采集cpu信息
cpu_info() {top -bn 1|awk '/^%Cpu/{print $2,$4,$8}'|while read us sy idledo# 得到使用的cpu比率used_cpu=$( echo "scale=2;100-$idle"|bc )# 采集信息存放到日志文件,同時輸出屏幕echo "$get_time user process: $us system process: $sy idle: $idle used: $used_cpu"|tee -a /var/log/performance_usage.logdone
}# 采集內存信息
mem_info() {free -m|awk '/^Mem/{print $2,$3,$4,$5,$6,$7}'|while read total used free shared buff_cache availabledoecho "$get_time total_mem: $total M available_mem: $available free_mem: $free used_mem: $used_mem buff_cache: $buff_cache shared: $shared"|tee -a /var/log/performance_usage.log done
}
# 采集磁盤和分區的使用信息
disk_info(){# 總共有多少磁盤和磁盤的大小disk_total=$(lsblk|grep "^sd"|awk '{print $1,$4}')# 分區的信息df -Th|egrep "/$|/boot$"|awk '{print $3,$4,$5,$6,$7}'|while read size used free used_percent part_name
doecho "$get_time disk size: $size used: $used free: $free used_percent: $used_persent part_name: $part_name disk_total: $disk_total"|tee -a /var/log/performance_usage.log
done
}
# 調用cpu_info函數
cpu_info# 調用mem_info函數
mem_info# 調用disk_info函數
disk_info[root@hz shell]# bash monitor_cpu_mem_disk.sh
20250628225929 user process: 29.0 system process: 51.6 idle: 0.0 used: 100.0
20250628225929 total_mem: 3627 M available_mem: 2803 free_mem: 2699 used_mem: buff_cache: 328 shared: 9
20250628225929 disk size: 17G used: 6.3G free: 11G used_percent: part_name: / disk_total: sda 20G
20250628225929 disk size: 960M used: 225M free: 736M used_percent: part_name: /boot disk_total: sda 20G[root@hz shell]# cat /var/log/performance_usage.log
use process: 26.5 system process: 50.0 idle: 2.9 used: 97.1
20250628221600 user process: 0.0 system process: 3.1 idle: 96.9 used: 3.1
20250628221600 total_mem: 3627 M available_mem: 2819 free_mem: 2806 used_mem: buff_cache: 236 shared: 8
20250628221823 user process: 28.1 system process: 46.9 idle: 3.1 used: 96.9
20250628221823 total_mem: 3627 M available_mem: 2816 free_mem: 2802 used_mem: buff_cache: 236 shared: 9
20250628222334 user process: 32.3 system process: 48.4 idle: 0.0 used: 100.0
20250628222334 total_mem: 3627 M available_mem: 2811 free_mem: 2708 used_mem: buff_cache: 327 shared: 9
20250628225806 user process: 28.1 system process: 53.1 idle: 0.0 used: 100.0
20250628225806 total_mem: 3627 M available_mem: 2803 free_mem: 2700 used_mem: buff_cache: 328 shared: 9
20250628225806 disk size: 17G used: 6.3G free: 11G used_percent: part_name: / disk_total: sda 20G
20250628225806 disk size: 960M used: 225M free: 736M used_percent: part_name: /boot disk_total: sda 20G
20250628225929 user process: 29.0 system process: 51.6 idle: 0.0 used: 100.0
20250628225929 total_mem: 3627 M available_mem: 2803 free_mem: 2699 used_mem: buff_cache: 328 shared: 9
20250628225929 disk size: 17G used: 6.3G free: 11G used_percent: part_name: / disk_total: sda 20G
20250628225929 disk size: 960M used: 225M free: 736M used_percent: part_name: /boot disk_total: sda 20G
四、顯示進程的信息
需要顯示cpu使用率最高的前5個進程
需要顯示內存使用率最高的前5個進程
進程的總信息,有多少個進程,有多少個進程在running
Tasks: 162 total, 1 running, 161 sleeping, 0 stopped, 0 zombie
ps aux|sort -k3 -nr|head -5
ps aux|sort -k4 -nr|head -5
ps aux --sort=-%cpu |head -n 6|tail -n 5
ps aux --sort=-%mem |head -n 6|tail -n 5
+
表示升序
-
表示降序
%cpu
表示字段名
[root@hz shell]# vim monitor_cpu_mem_disk_process.sh
[root@hz shell]# cat monitor_cpu_mem_disk_process.sh
#!/bin/bashget_time=$(date +%Y%m%d%H%M%S)
# 采集cpu信息
cpu_info() {top -bn 1|awk '/^%Cpu/{print $2,$4,$8}'|while read us sy idledo# 得到使用的cpu比率used_cpu=$( echo "scale=2;100-$idle"|bc )# 采集信息存放到日志文件,同時輸出屏幕echo "$get_time user process: $us system process: $sy idle: $idle used: $used_cpu"|tee -a /var/log/performance_usage.logdone
}# 采集內存信息
mem_info() {free -m|awk '/^Mem/{print $2,$3,$4,$5,$6,$7}'|while read total used free shared buff_cache availabledoecho "$get_time total_mem: $total M available_mem: $available free_mem: $free used_mem: $used_mem buff_cache: $buff_cache shared: $shared"|tee -a /var/log/performance_usage.log done
}
# 采集磁盤和分區的使用信息
disk_info(){# 總共有多少磁盤和磁盤的大小disk_total=$(lsblk|grep "^sd"|awk '{print $1,$4}')# 分區的信息df -Th|egrep "/$|/boot$"|awk '{print $3,$4,$5,$6,$7}'|while read size used free used_percent part_name
doecho "$get_time disk size: $size used: $used free: $free used_percent: $used_persent part_name: $part_name disk_total: $disk_total"|tee -a /var/log/performance_usage.log
done
}# 采集進程的信息
process_info(){# 顯示cpu使用率最高的前5個進程#cpu_top_5=$(ps aux --sort=-%cpu |head -n 6|tail - n 5)ps aux --sort=-%cpu |head -n 6|tail -n 5|awk '{print "pid:"$2,"cpu_percent:"$3,"program_name:"$11}'|tee -a /var/log/performance_usage.log #cpu_top_5=$(ps aux|sort -k3 -nr|head -5)# 顯示內存使用率最高的前5個進程#mem_top_5=$(ps aux --sort=-%mem |head -n 6|tail - n 5)ps aux --sort=-%mem |head -n 6|tail -n 5|awk '{print "pid:"$2,"mem_percent:"$4,"program_name:"$11}'|tee -a /var/log/performance_usage.log
}# 顯示進程的總信息,有多少進程,有多少個進程在runningtop -bn 1|grep "^Task"|awk '{print "total_processes:" $2,"running_processes:"$4}'|tee -a /var/log/performance_usage.log# 調用cpu_info函數
cpu_info# 調用mem_info函數
mem_info# 調用disk_info函數
disk_info# 調用process_info函數
process_info[root@hz shell]# bash monitor_cpu_mem_disk_process.sh
total_processes:165 running_processes:3
20250706222631 user process: 37.5 system process: 46.9 idle: 3.1 used: 96.9
20250706222631 total_mem: 3627 M available_mem: 2753 free_mem: 2612 used_mem: buff_cache: 371 shared: 9
20250706222631 disk size: 17G used: 6.3G free: 11G used_percent: part_name: / disk_total: sda 20G
20250706222631 disk size: 960M used: 225M free: 736M used_percent: part_name: /boot disk_total: sda 20G
pid:22277 cpu_percent:75.9 program_name:bash
pid:20538 cpu_percent:1.2 program_name:sshd:
pid:900 cpu_percent:0.6 program_name:/usr/sbin/mysqld
pid:718 cpu_percent:0.1 program_name:/usr/bin/vmtoolsd
pid:21504 cpu_percent:0.1 program_name:[kworker/0:1-events]
pid:900 mem_percent:11.2 program_name:/usr/sbin/mysqld
pid:709 mem_percent:0.5 program_name:/usr/sbin/NetworkManager
pid:1425 mem_percent:0.5 program_name:/usr/bin/python3
pid:1 mem_percent:0.3 program_name:/usr/lib/systemd/systemd
pid:1433 mem_percent:0.3 program_name:sshd:
授予可執行權限
[root@hz shell]#
chmod +x
monitor_cpu_mem_disk_process.sh
制定計劃任務,執行腳本
[root@hz shell]#
crontab -e
*/5 * * * * /shell/monitor_cpu_mem_disk_process.sh