shell脚本基础(12)

                                                      shell编写邮件报警脚本

web服务器监控
mysql数据库监控
disk硬盘使用监控

一、邮件服务器的选择
1、postfix软件-发邮件-25
2、dovecot软件-收邮件-110

首先卸载服务器上自带的sendmail
rpm -qa sendmail*
rpm -e sendmail-8.13.1* --nodeps
rppm -qa sendmail*
再安装收发邮件的服务器
yum -y install postfix* dovecot*

   配置postfix
vi /etc/postfix/main.cf
1、修改邮件服务器的主机名(myhostname)
myhostname = mail.lhd.cn
#myhostname = virtual.domain.tld
2、修改mydomain
ydomain = lhd.cn
3、修改myorigin
myorigin =$myhostname
myorigin =$mydomain
4、修改smtp监听端口
inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname,localhost
#inet_interfaces = localhost
5、修改mydestinnation
mydestination = $myhostname,$mydomain
#mydestination = $myhostname.localhost.$mydomain.localhost.$mydomain
#mydestination = $myhostname.localhost.$mydomain.localhost.$mydomain
#        mail.$mydomain, www.$mydomain, ftp.$mydomainsa

6、修改本地网段
#
mynetworks = 19.168.10.0/24,127.0.0.0/8
#mynetworks = $config_directory/mynetworks
#mynetworks = hash:/etc/postfix/networ_table

7、修改relay_domain转发邮件域名
relay_domains = $mydestination
8、修改postfix aliases邮件别名
#alias_maps = dbm:/etc/aliases
alias_maps = hash:/etc/aliases
#alias_maps = hash:/etc/aliases.nis:mail.aliases
#alias_maps = netinfo:/aliases

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

启动postfix打开25号端口
service postfix restart
netstat -anp |grep :25

 
   配置dovecot
vi /etc/dovecot.conf
1、修改protocols支持pop3和pop3s
#protocols we want to be serving:
# imap imaps pop3 pop3s
protocols = imap imaps pop3 pop3s
2、修改pop3和imaps在所在ipv4接口上监听110与143端口
imap_listen= *
pop3_listen = *

二、编写web服务器监控脚本
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@lampym.com

三、编写mysql数据库监控脚本
touch mysql.sh

#!/bin/bash
#mysql.sh

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

if [ $? -eq 0 ];then
    str = "mysql服务器目前处于正常状态"
else
    str = "mysql服务器目前处于关闭或无响应状态"
fi

echo $str|mail -s 'mysql server' admin@lampym.com

四、编写disk空间使用监控脚本
touch disk.sh

#!/bin/bash
#disk.sh

ds = 'df|awk '{if(NR==3){print int($4)}}''

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

if [ $ds -lt 45 ];then
    str = "disk服务器目前处于normal"
else
    str = "disk服务器目前处于question"
fi

echo $str|mail -s 'linux server disk space' admin@lampym.com

五、编写mem空间使用监控脚本
touch mem.sh

#!/bin/bash
#mem.sh

use='free -m|awk '{if(NR==2){print int($3*100/$2)}}''
#tot='free -m|awk '{if(NR==2){print $2}}''

if [ $use -lt 50 ];then
    str = "mem space is less than 50%!!!"
else
    str = "mem space is greater than 50%!!!"
fi

echo $str|mail -s 'linux server mem space' admin@lampym.com


   设置权限
chmod a+x *

nc测试服务是否运行
nc -l 192.168.116.100 10000    在192.168.116.100一个终端中建立10000端口
在另一个终端中nc 192.168.116.101 10000连接10000端口,则在任何一个终端中输入内容在另一个终端中都能查看到内容

nc -w 3 192.168.116.101 10000 连接192.168.116.101三秒钟后断开

注意:
1、设置脚本权限
chmod 755 /etc/init.d/mon.sh 可以设置四个程序在一个脚本中,只需要检查一个脚本就可以
2、crontab -e 
*/5**** bash /etc/init.d/mon.sh
配置自动执行
3、tail -f /var/log/cron
4、邮件报警要提前测试准备邮件系统是否能正常功能工作

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值