Linux运维之监控CPU和内存的日志工具

一、监控CPU和内存的日志工具的使用

1、阿里云提供了一个监控CPU和内存的脚本,因free版本不同的原因,脚本中的内容有做细微的修改,脚本内容如下:

Linux运维之监控CPU和内存的日志工具
#!/bin/bash
#When the free memory very less ,this script to collect CPU/memory usage information and dmessage information. 
#Version 1.0 time:2014-3-11
#Version 2.0 time:2014-12-23
#Version 3.0 time:2020-07-12

logfile=/tmp/$0.log

check_os_release()
{
  while true
  do
    os_release=$(grep "Red Hat Enterprise Linux Server release" /etc/issue 2>/dev/null)
    os_release_2=$(grep "Red Hat Enterprise Linux Server release" /etc/redhat-release 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]
    then
      if echo "$os_release"|grep "release" >/dev/null 2>&1
      then
        os_release=redhat
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    os_release=$(grep "Aliyun Linux release" /etc/issue 2>/dev/null)
    os_release_2=$(grep "Aliyun Linux release" /etc/aliyun-release 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]
    then
      if echo "$os_release"|grep "release" >/dev/null 2>&1
      then
        os_release=aliyun
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    os_release_2=$(grep "CentOS" /etc/*release 2>/dev/null)
    if [ "$os_release_2" ]
    then
      if echo "$os_release_2"|grep "release" >/dev/null 2>&1
      then
        os_release=centos
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    os_release=$(grep -i "ubuntu" /etc/issue 2>/dev/null)
    os_release_2=$(grep -i "ubuntu" /etc/lsb-release 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]
    then
      if echo "$os_release"|grep "Ubuntu" >/dev/null 2>&1
      then
        os_release=ubuntu
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    os_release=$(grep -i "debian" /etc/issue 2>/dev/null)
    os_release_2=$(grep -i "debian" /proc/version 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]
    then
      if echo "$os_release"|grep "Linux" >/dev/null 2>&1
      then
        os_release=debian
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    break
    done
}

rhel_fun()
{
  while true
  do
    #vm_mem=$(free -m|grep "buffers/cache"|awk ‘{print $4}‘)
    vm_mem=$(free -m|grep "Mem"|awk ‘{print $7}‘)
    cpu=$(top -bn2|grep "Cpu(s)"|awk ‘{print $8}‘|awk -F‘%‘ ‘{print $1}‘|tail -n1)
    check_cpu=$(echo "$cpu <20" |bc)
    if [[ $vm_mem -le 100 ]] || [[ $check_cpu -eq 1  ]]
    then
      echo "======================================================" >>$logfile
      date >>$logfile
      echo "======================================================" >>$logfile
      echo "The memory is too less." >>$logfile
      free -m >>$logfile
      echo "=======================CPU info========================" >>$logfile
      (ps aux|head -1;ps aux|sort -nrk3|grep -v "RSS") >>$logfile
      echo "=======================Memory info=====================" >>$logfile
      (ps aux|head -1;ps aux|sort -nrk6|grep -v "RSS") >>$logfile
      date >>$logfile
      echo "=======================Dmesg info=====================" >>$logfile
      dmesg >>$logfile
      dmesg -c
    fi
    sleep 10
  done
}

debian_fun()
{
  while true
  do
    vm_mem=$(free -m|grep "buffers/cache"|awk ‘{print $4}‘)
    cpu=$(top -bn2|grep "Cpu(s)"|awk ‘{print $5}‘|awk -F‘%‘ ‘{print $1}‘|tail -n1)
    check_cpu=$(echo "$cpu <20" |bc)
    if [[ $vm_mem -le 100 ]] || [[ $check_cpu -eq 1  ]]
    then
      echo "======================================================" >>$logfile
      date >>$logfile
      echo "======================================================" >>$logfile
      echo "The memory is too less." >>$logfile
      free -m >>$logfile
      echo "=======================CPU info========================" >>$logfile
      (ps aux|head -1;ps aux|sort -nrk3|grep -v "RSS") >>$logfile
      echo "=======================Memory info=====================" >>$logfile
      (ps aux|head -1;ps aux|sort -nrk6|grep -v "RSS") >>$logfile
      date >>$logfile
      echo "=======================Dmesg info=====================" >>$logfile
      dmesg >>$logfile
      dmesg -c
    fi
    sleep 10
  done
}

check_os_release

case "$os_release" in
redhat|centos|aliyun)
  yum install bc -y
  rhel_fun
  ;;
debian|ubuntu)
  apt-get install bc -y
  debian_fun
  ;;
esac
View Code

2、上传到/tmp目录中

3、执行如下命令并后台运行该脚本

cd /tmp
nohup bash get_cpu_mem_info.sh &

4、该工具会在/tmp目录下生成一个以脚本名字命名的日志文件,实时记录系统的CPU、内存的使用情况,等到系统异常时可以用于分析日志。

二、监控CPU和内存的日志工具的详解

1、

 

Linux运维之监控CPU和内存的日志工具

上一篇:[转]mysql-5.6.17-win32免安装版配置


下一篇:Clone Linux System