Shell script monitors embedded Linux system business, CPU, memory, etc.

Source: Internet
Author: User

Bash in custom cards is not available, and standard SH is used to complete system and business monitoring

#! /bin/sh#process_name= "/guard /aps /sa /zyguard" process_name= "/MP /MCU" #获取主机IP地址ip = ' ifconfig eth0 |grep  ' inet addr "| cut -f 2 -d ": "|cut -f  1 -d  " " ' gstr= "/guard" space= " " first_process= "" #获取初始业务进程号for  i in $ process_namedoif [[  $i  ==  $gstr  ]]thenbf_process= ' ps |grep  $i |grep -v  grep|grep -v  '/bin/bash ' |awk  ' {print $1} ' first_process= $First _process$i ': ' $Bf _ Process$spaceelsebf_process= ' ps |grep  $i |grep -v grep|awk  ' {print $1} ' First_ process= $First _process$i ":" $Bf _process$spacefidonewhile truedofor_num=1sleep 3second_process= "" # Second get business process number for i in  $Process _namedoif [[  $i  ==  $gstr  ]]thenbs_process= ' ps |grep  $i |grep -v grep|grep -v  '/bin/bash ' |awk  ' {print $1} ' Second_ process= $Second _process$i ":" $BS_process$spaceelsebs_process= ' ps |grep  $i |grep -v grep|awk  ' {print $1} ' Second_ process= $Second _process$i ":" $Bs _process$spacefidoneecho  "first_process:" $First _processecho  "Second_ Process: "$Second _process# See if all business processes have changed, output to log file for j in  $Process _namedoecho  $jOne _f_ Process= ' echo  $First _process|awk -v t= "${for_num}"   ' {print  $t} ' one_s_process= ' echo   $Second _process|awk -v t= "${for_num}"   ' {print  $t} ' for_num=$ ((for_num+1)) echo  " One_f_process: "$One _f_processecho " one_s_process: "$One _s_processif [[ " $One _f_process " !=   "$One _s_process"  ]]thentime_now= ' Date ' echo  $Time _nowecho  $jecho   $One _s_processecho   $One _f_processecho -e  $Time _now "\ T" $j "\tcollapse\tthe new process:" $One _s_process "\ t \tthe old process: "$One _f_process >> /usr/tmp/$ip ' _collapse '. txtfidone# get the business process number First_ Process= "" for i in  $Process _namedoif [[  $i  ==  $gstr  ]]thenbf_process= ' ps |grep  $i |grep -v grep|grep -v  '/bin/bash ' |awk  ' {print $1} ' first_process= $First _process$ I ":" $Bf _process$spaceelsebf_process= ' ps |grep  $i |grep -v grep|awk  ' {print $1} ' first_process= $First _process$i ":" $Bf _process$spacefidone# system memory Utilization monitoring memory_use_rate= ' free | grep  mem | awk  ' {printf '%d ', $3/$2*100} ' if [ 80 -le  $Memory _use_rate ] thentime_now= ' Date ' echo -e  $Time _now "\tsystm memory\tmemory_use_rate\tat present the  use value: "$Memory _use_rate"%\tthreshold:80% " >> /usr/tmp/$ip ' _alarm '. txtfi# System CPU Residual rate monitoring cpu_residual_rate= ' mpstat|grep all|awk  ' {printf '%d ', $11} ' echo  $Cpu _residual_rateif  [  $Cpu _residual_rate -le 20 ]thentime_now= ' Date ' echo  $Time _nowecho -e   $Time _now "\TSYSTM CPU\tcpu_residual_rate (%idle) \tat present the use value: "$Cpu _residual_rate"%\tThreshold:20% " >> /usr/tmp/$ip ' _alarm '. txtfi# Business Memory Utilization monitoring system_memory= ' free | grep mem |  awk  ' {printf '%d ', $2/1024} ' for k in  $Process _namedoif [[  $k  == $ Gstr ]]thenprocess_memory_use= ' ps |grep  $k |grep -v grep|grep -v  '/bin/bash ' |awk  ' {print $3} ' var_length=${#Process_Memory_Use}var_position= ' expr  $var _length - 1 ' M=${process_memory_use: $var _position:1}if [[  $m  ==  "M"  ]]thenpro_mem_use=${process_ memory_use:0: $var _position}elsepro_mem_use= ' expr  $Process _memory_use / 1024 ' FiSystem_Memory_ Threshold= ' free | grep mem | awk  ' {printf '%d ', $2/1024*0.8} ' if [[  $Pro _mem_use  -ge  $System _memory_threshold ]]thentime_now= ' Date ' echo -e  $Time _now "\ t "$k" \tprocess_mEmory_use\tat present the use value: "$Pro _mem_use" Mb\tthreshold: "$System _memory_threshold" Mb " >> /usr/tmp/$ip ' _alarm '. txtfielseprocess_memory_use= ' ps |grep  $k |grep -v  grep|awk  ' {print $3} ' var_length=${#Process_Memory_Use}var_position= ' expr  $var _length  - 1 ' m=${process_memory_use: $var _position:1}if [[  $m  ==  "M"  ]]thenpro_mem_ use=${process_memory_use:0: $var _position}elsepro_mem_use= ' expr  $Process _memory_use / 1024 ' Fisystem_memory_threshold= ' free | grep mem | awk  ' {printf '%d ', $2/1024*0.8} ' if  [[  $Pro _mem_use  -ge  $System _memory_threshold ]]thentime_now= ' Date ' echo -e   $Time _now "\ T" $k "\tprocess_memory_use\tat present the use value:" $Pro _mem_use "Mb\ Tthreshold: "$System _memory_threshold" Mb " >> /usr/tmp/$ip ' _alarm '. Txtfifidonedone


Shell script monitors embedded Linux system business, CPU, memory, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.