shell CPU

简介: shell CPU

shell CPU

tags: 监控

1. CPU 使用率

  • CPU.sh
MAX=95
EMAIL=server@127.0.0.1
USE=$(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage ""}')
USE=`printf "%.0f\n" $USE`
if [[ $USE -gt $MAX ]]; then
   echo "Percent used: $USE" | mail -s "Running out of CPU power" $EMAIL
else
   echo "all is well !"
fi

执行:

$ bash CPU.sh
all is well !

2. CPU 检查

#!/bin/bash
#################################################################################
# ActiveXperts Network Monitor - Shell script checks
#
# For more information about ActiveXperts Network Monitor and SSH, please
# visit the online ActiveXperts Network Monitor Shell Script Guidelines at:
#   https://www.activexperts.com/support/network-monitor/online/linux/
#################################################################################
# Script
#     cpu.sh
# Description
#     Checks CPU usage on the computer.
# Declare Parameters
#     1) nMaxCpuUsage (number) - maximum allowed CPU usage (%)
# Usage
#     cpu.sh nMaxCpuUsage
# Sample
#     cpu.sh 70
#################################################################################
nMaxCpuUsage=$1
# Validate number of arguments
if [ $# -ne 1 ] ; then
  echo "UNCERTAIN: Invalid number of arguments - Usage: cpu nMaxCpuUsage"
  exit 1
fi
# Validate numeric parameter nMaxCpuUsage
regExpNumber='^[0-9]+$'
if ! [[ $1 =~ $regExpNumber ]] ; then
  echo "UNCERTAIN: Invalid argument: nMaxCpuUsage (number expected)"
  exit 1
fi
# Check the CPU usage
nCpuLoadPercentage=`ps -A -o pcpu | tail -n+2 | paste -sd+ | bc`
nCpuLoadPercentage=$( echo "$nCpuLoadPercentage / 1" | bc )
if [ $nCpuLoadPercentage -le $nMaxCpuUsage ] ; then
  echo "SUCCESS: CPU usage is [$nCpuLoadPercentage%], minimum allowed=[$1%] DATA:$nCpuLoadPercentage"
else
  echo "ERROR: CPU usage is [$nCpuLoadPercentage%], minimum allowed=[$1%] DATA:$nCpuLoadPercentage"
fi
exit 0

执行

$ bash nmaxcpu.sh 70
SUCCESS: CPU usage is [0%], minimum allowed=[70%] DATA:0
$ bash nmaxcpu.sh 80
SUCCESS: CPU usage is [0%], minimum allowed=[80%] DATA:0

更多阅读:

相关文章
|
Shell
我来教你如何将cpu使用率up起来(shell脚本[含注释])
我来教你如何将cpu使用率up起来(shell脚本[含注释])
501 0
|
Shell 测试技术 Linux
通过shell脚本进行linux服务器的CPU和内存压测
通过shell脚本进行linux服务器的CPU和内存压测
321 0
|
5月前
|
监控 Shell Linux
shell linux中用shell写一个占用CPU的脚本
shell linux中用shell写一个占用CPU的脚本
654 0
|
6月前
|
监控 Shell
Shell脚本监控CPU、内存和硬盘利用率
Shell脚本监控CPU、内存和硬盘利用率
|
6月前
|
缓存 运维 算法
【Linux Shell 面试题 】Linux下如何 查看CPU信息
【Linux Shell 面试题 】Linux下如何 查看CPU信息
72 0
|
监控 Shell Linux
Shell - 监控某个进程的内存占用情况、主机CPU、磁盘空间等信息以及守护进程
Shell - 监控某个进程的内存占用情况、主机CPU、磁盘空间等信息以及守护进程
288 0
|
存储 监控 Shell
Linux 性能监控之CPU&内存&I/O监控Shell脚本2
Linux 性能监控之CPU&内存&I/O监控Shell脚本2
512 0
|
监控 Shell Linux
Linux 性能监控之CPU&内存&I/O监控Shell脚本1
Linux 性能监控之CPU&内存&I/O监控Shell脚本1
184 0
|
Java Shell Linux
Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息
Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息
1037 0
|
Shell Linux
LINUX SHELL获得CPU核心(线程)个数
LINUX SHELL获得CPU核心(线程)个数
331 0
下一篇
无影云桌面