CPU 内存检查小脚本
#!/bin/bash
function echo_red () {
local what=$*
echo -e "\e[1;31m${what}\e[0m"
}
function echo_green () {
local what=$*
echo -e "\e[1;32m${what}\e[0m"
}
cpu_used=`vmstat | awk 'NR==3{print (100-$(NF-2))}'`
cpu_average=`uptime | awk '{print $(NF-2),$(NF-1),$NF}'`
mem_used=`free -h |awk 'NR==2{print int(100-($NF/$2*100))}'`
echo "CPU利用率: $cpu_used%"
echo "CPU负载: $cpu_average"
echo "内存利用率: $mem_used%"
echo "==========检查结果=========="
if [ $cpu_used -ge 60 ]
then
echo_red "cpu利用率过大于60%,请检查."
elif [ $mem_used -ge 80 ]
then
echo_red "内存利用率过大于80%,请检查."
else
echo_green "CPU和内存使用率正常."
fi
#!/bin/bash
#!/usr/bin/env bash
# This script reproduces what the kubelet does
# to calculate memory.available relative to root cgroup.
# current memory usage
memory_capacity_in_kb=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')
memory_capacity_in_bytes=$((memory_capacity_in_kb * 1024))
memory_usage_in_bytes=$(cat /sys/fs/cgroup/memory/memory.usage_in_bytes)
memory_total_inactive_file=$(cat /sys/fs/cgroup/memory/memory.stat | grep total_inactive_file | awk '{print $2}')
memory_working_set=${memory_usage_in_bytes}
if [ "$memory_working_set" -lt "$memory_total_inactive_file" ];
then
memory_working_set=0
else
memory_working_set=$((memory_usage_in_bytes - memory_total_inactive_file))
fi
memory_available_in_bytes=$((memory_capacity_in_bytes - memory_working_set))
memory_available_in_kb=$((memory_available_in_bytes / 1024))
memory_available_in_mb=$((memory_available_in_kb / 1024))
echo "memory.capacity_in_bytes $memory_capacity_in_bytes"
echo "memory.usage_in_bytes $memory_usage_in_bytes"
echo "memory.total_inactive_file $memory_total_inactive_file"
echo "memory.working_set $memory_working_set"
echo "memory.available_in_bytes $memory_available_in_bytes"
echo "memory.available_in_kb $memory_available_in_kb"
echo "memory.available_in_mb $memory_available_in_mb"
网友评论