zoukankan      html  css  js  c++  java
  • shell脚本编程学习笔记(三)编写邮件报警脚本

    一、shell编写邮件报警脚本

        1、POSTFIX邮件服务器准备

            a、首先卸载服务器上自带的sendmail

                rpm -qa sendmail*    //查看安装的sendmail

                rpm -e sendmail-8.*****    //卸载

            b、安装postfix(发邮件用的,25号端口)和dovecot(收邮件用的,110号端口)

                yum install postfix* dovecot* -y

                rpm -qa | grep postfix*

                rpm -qa | grep devecot*

            c、配置发邮件postfix

                vim /etc/postfix/main.cf

                1)修改myhostname

                   #myhostname = virtual.domain.tld

                   myhostname = mail.g.cn

                2)修改mydomain

                   #mydomain=domain.tld

                   mydomain=g.cn

                3)修改myorigin

                   myorigin=$myhostname

                   myorigin=$mydomain

                4)修改smtp监听端口

                   inet_interfaces=all

                   #inet_interfaces=$myhostname

                   #inet_interfaces=$myhostname,localhost

                   #inet_interfaces=localhost

                5)修改mydestination

                   mydestination=$myhostname,$mydomain

                   #mydestination=$myhostname,localhost.$mydomain,localhost,$mydomain

                   #mydestination=$myhostname,localhost.$mydomain,localhost,$mydomain,mail.$mydomain,www.$mydomain,ftp.$mydomain

                6)修改本地网段

                   mynetworks=192.168.31.60/24,127.0.0.0/8

                   #mynetworks=$config_directory/mynetworks

                   #mynetworks=hash:/etc/postfix/network_table

                7)修改relay_domain转发邮件域名

                   relay_domain=$mydestination

                8)修改postfix aliasex邮件别名

                   #alias_maps=dbm:/etc/aliases

                   alias_maps=hash:/etc/aliases

                   #alias_maps=netinfo:/aliases

                   #alias_database=dbm:/etc/aliases

                   #alias_database=dbm:/etc/maik/aliases

                   alias_database=hash:/etc/aliases

                   #alias_database=hash:/etc/aliases, hash:/opt/majordomo/aliases

                  :wq

                9)启动postfix打开25号端口

                   service postfix restart

                   netstat -anp |grep :25

                   pstree |grep master        //启动后进程叫master

                10)测试是否能发送邮件

                   echo  hello | mail root@g.cn

                   报错:-bash: mail: command not found

                   安装:yum install -y mailx

                   查看邮件:mail

                   删除邮件:d

                   退出:q

                   telnet发送邮件

                      telnet 192.168.31.60  25

                      mail form:发件人

                      rcpt to:收件人

                      data

                      邮件内容

                      quit

            e、配置收邮件dovecot

                vim /etc/dovecot/dovecot.conf

                1)修改protocole支持pop3和pop3s

                  protocols=imap imaps pop3 pop3s

                2)修改pop3和imaps在所在ipv4接口上监听110与143端口

                  imap_listen = *

                  pop3_listen = *

                n)开启dovecot

                  service dovecot start

                  netstat -tunpl |grep :110

                  netstat -tunpl |grep :143

        2、编写web服务器监控

            nc命令:

                Linux中nc命令是一个功能强大的网络工具,全称是netcat。

                 语法:

                    nc [-hlnruz][-g<网关...>][-G<指向器数目>][-i<延迟秒数>][-o<输出文件>][-p<通信端口>][-s<来源位址>][-v...][-w<超时秒数>][主机名称][通信端口...]

                参数说明:

                    -g<网关> 设置路由器跃程通信网关,最丢哦可设置8个。

                    -G<指向器数目> 设置来源路由指向器,其数值为4的倍数。

                    -h 在线帮助。

                    -i<延迟秒数> 设置时间间隔,以便传送信息及扫描通信端口。

                    -l 使用监听模式,管控传入的资料。

                    -n 直接使用IP地址,而不通过域名服务器。

                    -o<输出文件> 指定文件名称,把往来传输的数据以16进制字码倾倒成该文件保存。

                    -p<通信端口> 设置本地主机使用的通信端口。

                    -r 乱数指定本地与远端主机的通信端口。

                    -s<来源位址> 设置本地主机送出数据包的IP地址。

                    -u 使用UDP传输协议。

                    -v 显示指令执行过程。

                    -w<超时秒数> 设置等待连线的时间。

                    -z 使用0输入/输出模式,只在扫描通信端口时使用。

            touch web.sh

                #!/bin/bash

                #web.sh

                nc -w 3 localhost 80 &>/dev/null

                if [ $? -eq 0 ];then

                  str="apache web服务器目前状态处于正常状态!!!"

                else

                  str="apache web服务器目前处于关闭或无响应状态!!!"

                fi

                echo $str|mail -s 'apache web server' admin@g.cn

        3、编写mysql数据库监控

            touch mysql.sh 

                #!/bin/bash

                #mysql.sh

                nc -e 3 localhost 3306 &>/dev/null

                if [ $? -eq 0 ];then

                        str="mysql server status Running!!!"

                else

                        str="mysql server status Shuting!!!"

                fi

                echo $str | mail -s 'mysql server' admin@g.cn

        4、编写Disk硬盘空间监控

            touch disk.sh

                #!/bin/bash

                #disk.sh

                ds=`df |awk '{if(NR==7){print int($5)}}'`    //视情况而定

                if [ $ds -lt 45 ];then

                       str="disk space is less than 45%!!!"

                else

                       str="disk space is greater than 45%!!!"

                fi

                echo $str | mail -s 'linux server disk space' admin@g.cn

        5、编写mem(内存)空间监控脚本

            touch mem.sh

                #!/bin/bash

                #mem.sh

                mem=`free -m |awk '{if(NR==2){printf("%.0f ",(int($3)/int($2))*100)}}'`        //四舍五入取整

                if [ $mem -lt 45 ];then            //表达式内为整数表达式,不能用浮点型

                       str="mem space is less than 45%,Achieve $mem%!!!"

                else

                       str="mem space is greater than 45%,Achieve $mem%!!!"   

                fi

                echo $str | mail -s 'linux server mem space' admin@g.cn

        6、报警脚本重启生效

              1)设置脚本权限

                chmod 755 /etc/init.d/mon.sh    //mon.sh:将所有脚本放到这个脚本中,或者调用其他脚本,发送一封邮件即可

              2)crontab -e                //任务计划

                */5****bash /etc/init.d/mon.sh

                如:10 13 *** /mnt/monitor.dh      //每天的13:10执行这个代码

              3)tail -f /var/log/cron

              4)邮件报警要提前测试准备邮件系统是否正常工作

     

      

  • 相关阅读:
    MongoDB 安装及其配置
    hdu 1241 Oil Deposits
    2014ACM/ICPC亚洲区北京站
    hdu 5901 count prime & code vs 3223 素数密度
    hdu 2191 珍惜现在,感恩生活
    FOJ 2181 快来买肉松饼
    hdu 5384 Danganronpa
    hdu 2222 Keywords Search
    hdu 1300 Pearls
    2016.2.24. 《构建之法》开始阅读
  • 原文地址:https://www.cnblogs.com/yuyangphpweibo/p/7897110.html
Copyright © 2011-2022 走看看