shell实现对机器内存和CPU使用情况的监控
来源:互联网 发布:excel生成数据透视表 编辑:程序博客网 时间:2024/06/05 11:13
这个脚本主要的目的是监控机器上CPU和内存的使用情况,如果超过一定的阈值,打印出占用CPU或者内存资源较多的进程信息,也可以替换成其它方式。
主要功能如下:
1)将内存和CPU资源的监控情况记录到日志文件中
2)控制日志文件大小,周期性检查(30天)日志文件大小,当日志文件超过一定程度之后自动进行清理日志文件。
声明: 下面获取内存和CPU使用情况的代码参考《Linux.Shell编程从入门到精通》最后一个章节的部分代码,如有侵犯,请告知,谢谢。
#! /bin/bash
# this scripts is used to check host status(including memory CPU and so on)
# 20171024 -- first version
LOG_FILE="/tmp/monitor_log"
check_interval=10
max_usage_ration_memory=80
max_usage_ration_cpu=90
top_process_number=5
function check_log_size() {
if [ -f "$1" ]
then
file_size=$(stat "$1" | grep Size | awk '{print $2}')
echo "current log file size : $file_size"
if [[ "$file_size" -gt 1048576 ]]
then
echo "Clean old records as log file($1) is too big $(date)" 2>&1 | tee "$1"
fi
fi
}
function disply_top_memory_process_information() {
process_number=$(ps aux | sort -nk 4r | wc -l)
echo "current process number is $process_number"
if [[ "$process_number" -gt "$top_process_number" ]]
then
process_number=$((top_process_number+1))
fi
echo "the most $((process_number-1)) process costing memory resouces are as follows: " 2>&1 | tee -a "$1"
echo "$(ps aux | sort -nk 4r | head -$process_number) " 2>&1 | tee -a "$1"
echo "timestamp is $(date)"
}
function monitor_memory() {
memory_total=$(grep MemTotal /proc/meminfo | awk '{print $2}' )
memory_free=$(grep MemFree /proc/meminfo | awk '{print $2}' )
memory_usage=$((100-memory_free*100/memory_total))
echo "Current memory usage: $memory_usage at $(date)" 2>&1 | tee -a "$1"
if [[ "$memory_usage" -gt "$max_usage_ration_memory" ]]
then
echo "Current Memory usage is too high "
disply_top_memory_process_information "$1"
return 1
else
return 0
fi
}
function disply_top_cpu_process_information() {
process_number=$(ps aux | sort -nk 3r | wc -l)
echo "current process number is $process_number"
if [[ "$process_number" -gt "$top_process_number" ]]
then
process_number=$((top_process_number+1))
fi
echo "the most $((process_number-1)) process costing cpu resouces are as follows: " 2>&1 | tee -a "$1"
echo "$(ps aux | sort -nk 4r | head -$process_number) " 2>&1 | tee -a "$1"
echo "timestamp is $(date)"
}
function get_cpu_info() {
grep -i "^cpu[0-9]\+" /proc/stat | awk '{used+=$2+$3+$4; unused+=$5+$6+$7+$8} END {print used, unused}'
}
function monitor_cpu() {
cpu_info_1=$(get_cpu_info)
sleep "$check_interval"
cpu_info_2=$(get_cpu_info)
cpu_usage=$(echo "$cpu_info_1" "$cpu_info_2" | awk '{used=$3-$1;total+=$3+$4-$1-$2; print int(used*100/total)}')
echo "Current cpu usage: $cpu_usage at $(date)" 2>&1 | tee -a "$1"
if [[ "$cpu_usage" -gt "$max_usage_ration_cpu" ]]
then
echo "Current CPU usage is too high"
disply_top_cpu_process_information $1
return 1
else
return 0
fi
}
check_time=1
check_log_size $LOG_FILE
while [[ "1" -lt "2" ]]
do
check_time=$((check_time+1))
if [[ "$check_time" -gt "367200" ]] #3600*24*30/10=367200
then
check_log_size $LOG_FILE
check_time=1
fi
monitor_memory $LOG_FILE
monitor_cpu $LOG_FILE
sleep "$check_interval"
done
- shell实现对机器内存和CPU使用情况的监控
- shell脚本监控系统负载、CPU和内存使用情况
- shell脚本监控系统负载、CPU和内存使用情况
- 代码实现cpu的检测和内存使用情况
- 使用shell 脚本对 Linux 系统进程的CPU使用率与内存资源进行监控
- 使用shell 脚本对 Linux 系统进程的CPU使用率与内存资源进行监控
- 监控CPU和内存的使用
- Linux--shell监控CPU和内存
- 一个监控CPU使用情况的脚本
- python实时得到cpu和内存的使用情况
- LINUX下查看CPU和内存的使用情况
- 使用JConsole监控进程、线程、内存、cpu、类情况
- 使用JConsole监控进程、线程、内存、cpu、类情况
- 使用JConsole监控进程、线程、内存、cpu、类情况
- Loadrunner 监控Windows资源(CPU、内存)使用情况
- Linux监控cpu以及内存使用情况之top命令
- 使用JAVA SNMP监控HP UNIX的CPU和内存
- 使用CACTI监控H3C设备的CPU和内存使用率
- MySQL——修改root密码的4种方法(以windows为例)
- 使用国内源部署Ceph
- 使用python实现简单的加密解密机制
- 公司内网,虚拟机使用NAT模式连接外网问题解决
- 关于es的一些列文章
- shell实现对机器内存和CPU使用情况的监控
- pdf在线预览
- docker入门示例
- 9.Nginx数据结构之链表List
- Unity中使用FlatBuffers
- 初次使用 method swizzling 失败记录
- ubuntu14.04 LTS64位服务器搭建php网站环境,亲测可用
- Python制作安卓游戏外挂
- type UnaryServerInterceptor type UnaryServerInterceptor func(ctx context.Context, req interface{}, i