当前位置: 首页 > news >正文

Linux-函数的使用-编写监控脚本

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

http://www.dtcms.com/a/348673.html

相关文章:

  • 栈的创建和基本操作
  • Arbess V1.1.4版本发布,支持Mysql数据库,Ubuntu系统,新增SSH及Hadess上传下载任务
  • week4-[字符数组]月份
  • TCP连接与UDP协议
  • 构建现代前端工程:Webpack/Vite/Rollup配置解析与最佳实践
  • C++20: std::span
  • 目标检测数据集 第005期-基于yolo标注格式的PCB组件检测数据集(含免费分享)
  • 【Ollama】本地OCR
  • 基于SpringBoot的校园信息共享系统【2026最新】
  • pod管理
  • scanner、arrylist、反转数组
  • FPGA 时序分析(五)
  • 十、redis 入门 之 redis事务
  • (Redis)主从哨兵模式与集群模式
  • 【机器学习】7 Linear regression
  • VScode设置鼠标滚轮调节代码
  • 嵌入式第三十六天(网络编程(TCP))
  • springboot项目搭建步骤
  • 【Flink】部署模式
  • Maven项目中settings.xml终极优化指南
  • Excel 表格 - 乘法与除法处理(保留两位小数四舍五入实现、保留两位小数截断实现、添加百分号)
  • 单片机外设(七)RTC时间获取
  • 深入解析Java NIO多路复用原理与性能优化实践指南
  • 重置MySQL数据库的密码指南(Windows/Linux全适配)
  • 基于springboot的理商管理平台设计与实现、java/vue/mvc
  • 得物25年春招-安卓部分笔试题1
  • Linux camera 驱动流程介绍(rgb: ov02k10)(chatgpt version)
  • AlmaLinux 上 Python 3.6 切换到 Python 3.11
  • EP02:【DA】数据分析的价值创造与应用流程
  • 基于SpringBoot的新能源汽车租赁管理系统【2026最新】