Homemade server monitoring script - Log Warning Service (Legacy) (shell) - back version - publicly available


#! /bin/bash
#By swztdza
#注意,可以单独使用,但是联动主脚本使用更佳!


        s=1
        while [ $s -ge 0 ] #死循环
        do

        echo "----------------服务器状态监测,每 1 秒检测一次 ctrl+c可以退出程序----------------"
        date +"%Y-%m-%d %T  %A"

        echo -e "\e[43m----------------cpu  status----------------\e[0m"

        pd1=$(uptime | awk '{print $6}')
   
        if [ $pd1 == "min," ];then

                echo -e "\e[1;34m分别为1分钟,5分钟,15分钟cpu负载:\e[0m"  $(uptime | awk '{print ($11,$12,$13)}')

                sz5=$(uptime | awk '{print $11}'| sed s/,//)

                sz4=$(echo $(uptime | awk '{print $11}')| sed s/,// | awk '{if($0 < 0.6)
               #使用awk工具判断浮点数,并赋值给sz4,标准值设置为0.6
                        print "0"
                else 
                        { print "1"  } }')
                       #注意各种符号,花括号,单引号,双引号

                if [ $sz4 == "0" ];then
                        echo '负载正常'
                else
                        echo -e '\e[1;31m当前负载过高警告!当前负载过高警告!已生成>警告日志文件:/root/LTlog/warning.txt\e[0m'

                        sz6=$(date +"%Y-%m-%d %T  %A")
                        echo -e "\e[1;31m$sz6\e[0m" >> /root/LTlog/warning.txt
                        #使用变量赋值的作用是需要改变颜色

                        echo -e "\e[1;31m当前负载过高警告!当前负载过高警告!当前负载值:$sz5\e[0m"  >> /root/LTlog/warning.txt

                fi


                echo -e "\e[1;34m登陆用户数:\e[0m" $(uptime | awk '{print ($7)}')
                echo -e "\e[1;34m服务器已运行 \e[0m" $(uptime | awk '{print ($3)}') "\e[1;31m天\e[0m"
                echo -e "\e[0m"
        else

                echo -e "\e[1;34m分别为1分钟,5分钟,15分钟cpu负载:\e[0m"  $(uptime | awk '{print ($10,$11,$12)}')

                sz2=$(uptime | awk '{print $10}'| sed s/,//)

                sz1=$(echo $(uptime | awk '{print $10}')| sed s/,// | awk '{if($0 < 0.6)
                #使用awk工具判断浮点数,并赋值给sz1,标准值设置为0.6

                        print "0"
                else 
                        #{ print "当前负载过高警告!当前负载过高警告!已生成日志文件
:/root/LTlog/warning.txt";
                        #打印出字符串

                        #print ("当前负载过高",$0) >> "/root/LTlog/warning.txt";
                        #awk追加重定向案例

                        { print "1"  } }')
                        #注意各种符号,花括号,单引号,双引号

                if [ $sz1 == "0" ];then
                        echo '负载正常'
                else
                        echo -e '\e[1;31m当前负载过高警告!当前负载过高警告!已生成警告日志文件:/root/LTlog/warning.txt\e[0m'

                        sz3=$(date +"%Y-%m-%d %T  %A")
                        echo -e "\e[1;31m$sz3\e[0m" >> /root/LTlog/warning.txt       
                        #使用变量赋值的作用是需要改变颜色

                        echo -e "\e[1;31m当前负载过高警告!当前负载过高警告!当前负载值:$sz2\e[0m"  >> /root/LTlog/warning.txt

                fi
                echo -e "\e[43m----------------disk status----------------\e[0m"
               df -h | sed -n "1,2p"
               echo ""
               echo -e "\e[43m----------------Mem  status----------------\e[0m"
               free | sed -n "1,2p"
               echo -e "\e[43m----------------Net  status----------------\e[0m"
               ifconfig eth0 | grep bytes

               sleep 1  #每秒刷新一次
               clear      #清屏
               done     #循环结构结束

 

Released six original articles · won praise 0 · Views 78

Guess you like

Origin blog.csdn.net/a84050933/article/details/103997876