[工具] Shell 检测服务器 CPU 占用率并报警写入日志

介绍

基本信息

名称:检测服务器 CPU 占用率并报警写入日志
作用:检测服务器 CPU 占用率并报警写入日志

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 将此脚本添加到周期性计划任务里
4. 如果 CPU 占用率超过了报警值则将报警信息写入 /var/log/message

脚本分割线里的变量

cputhreshold=95 #CPU 报警的占比值

脚本

#!/bin/bash

####################### Separator ########################
cputhreshold=95
####################### Separator ########################

cpumonitor() {
cpu=`top -n 1 -b | grep Cpu | awk -F, '{print $1}'| awk -F: '{print $2}'| awk '{print $1}'`

if [ `echo "$cpu > $1"|bc` -ne 0 ]
then
        logger "CPU_Alarm CPU until $cpu"
fi
}

cpumonitor $cputhreshold

[步骤] CentOS Linux & RHEL 系统的优化 (通过 tuned 实现)

步骤一:系统环境要求

服务器系统要配置好可用的软件源

步骤二:安装 tuned 系统优化软件

# yum -y install tuned

步骤三:显示 tuned 推荐的优化模式

# tuned-adm recommend
virtual-guest

步骤四:切换至 tuned 推荐的优化模式

# tuned-adm profile virtual-guest

步骤五:显示当前的优化模式

# tuned-adm active
Current active profile: virtual-guest

补充:取消 tuned 系统优化的方法

# tuned-adm off