Shell Script Basics (v)

Source: Internet
Author: User

First, the alarm system main script
[[email protected] ~]# cd/usr/local/sbin/# #脚本基本都放在这个文件夹下, convenient management [[email protected] sbin]# mkdir mon## Create a monitoring script home directory [[[email protected] sbin]# CD mon[[email protected] mon]# mkdir bin conf shares log mail# #创建监控子目录 [[ Email protected] mon]# CD bin[[email protected] bin]# vim main.sh# #创建入口shell脚本文件, the main script file is written to the following #!/bin/bash# Written by taoyuan# whether to send mail switch export send=1# filter IP address export addr= '/sbin/ifconfig |grep-a1 ' ens33: ' |awk '/inet/{print $} ' ' dir= ' pwd ' #只需要最后一级目录名last_dir = ' echo $dir |awk-f '/' {print $NF} ' #下面的判断目的是, when the script is executed, we are in the bin directory, otherwise the monitoring script, Mail and logs are most likely not found if [$last _dir = = "Bin"] | | [$last _dir = = "bin/"]; Then conf_file= ". /conf/mon.conf "Else echo" you shoud cd bin dir "Exitfiexec 1>> /log/mon.log 2>> /log/err.logecho "' Date +"%f%t "' Load average"/bin/bash: /shares/load.sh# first check whether the configuration file needs to monitor 502if grep-q ' to_mon_502=1 ' $conf _file; Then export log= ' grep ' logfile= ' $conf _file |awk-f ' = ' {print $} ' |sed ' s///g '/bin/bash. /shares/502.shfi
Second, alarm system configuration file
[[email protected] bin]# cd ../conf/[[email protected] conf]# vim mon.conf##to config the options if to monitor## 定义mysql的服务器地址、端口以及user、passwordto_mon_cdb=0 ##0 or 1,default 0,0 not monitor,1 monitordb_ip=10.20.30.40db_port=3315db_user=usernamedb_pass=passwd## httpd 如果是1则监控,为0不监控to_mon_httpd=0## php 如果是1则监控,为0不监控to_mon_php_socket=0## http_code_502 需要定义访问日志的路径to_mon_502=1logfile=/data/log/xxx.xxx.com/access.log## request_count 定义日志路径以及域名to_mon_request_count=0req_log=/data/log/www.discuz.net/access.logdomainname=www.discuz.net
Third, alarm system monitoring Project
[[email protected] conf]# CD. /shares/[[email protected] shares]# vim load.sh#!/bin/bash# #Writen by zlinux# #load = ' uptime |awk-f ' average: ' { Print $ ' |cut-d ', '-f1|sed ' s///g ' |cut-d.-f1 ' If [$load-gt] && [$send-eq ' 1 ']then echo ' $addr ' dat E+%t ' load is $load '; /log/load.tmp/bin/bash. /mail/mail.sh $addr \_load $load. /log/load.tmpfiecho "' Date +%t ' load is $load" [[[email protected] shares]# vim 502.sh#!  /bin/bashd= ' date-d "1 min" +%h:%m ' c_502= ' grep: $d: $log |grep ' 502 ' |wc-l ' if [$c _502-gt] && [$send = = 1 ]; Then echo, "$addr $d 502 count is $c _502" ... /log/502.tmp/bin/bash. /mail/mail.sh $addr \_502 $c _502. /log/502.tmpfiecho "' Date +%t ' 502 $c _502" [[email protected] shares]# vim disk.sh#!/bin/bash# #Writen by zlinu# #rm- F.. /log/disk.tmpfor R in ' df-h |awk-f ' [%]+ ' {print $} ' |grep-v use ' do if [$r-gt] && [$send-eq "1"]t Hen echo "$addr ' date +%t ' disk useage is $r" >> /Log/disk.tmpfiif [-F.. /log/disk.tmp]then df-h >> /log/disk.tmp/bin/bash. /mail/mail.sh $addr \_disk $r. /log/disk.tmp echo "' Date +%t ' disk useage is the Nook" else echo "' Date +%t ' disk useage in ok" fi

Shell Script Basics (v)

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.