跳至主要内容

利用shell监控服务器状态并且EMAIL获取报警

其实之前博客中也有说到过,现在又老调重启了~因为我把阿里云的监控全部删除了.要脱坑就全部一起脱不想留下任何有关联的东西!不多说了~我们开始来折腾吧~

首先我们来配置MAIL部分,MAIL部分我们只用到发送所以就装个sendmail就好了

sudo apt-get install sendmail  
sudo apt-get install sendmail-cf
sudo apt-get install mailutils
下面几个包是可选的:
squirrelmail //提供webmail
spamassassin //提供邮件过滤
mailman //提供邮件列表支持
dovecot // 提供IMAP和POP接收邮件服务器守护进程
注意:
使用带附件的功能,则还需要安装sharutils,
安装命令:
sudo apt-get install sharutils;(yum install sharutils )

终端输入命令:

ps aux |grep sendmail

出现这个代表已经安装成功了!接下来就设置配置

二、配置
sendmail 默认只会为本机用户发送邮件,只有把它扩展到整个Internet,才会成为真正的邮件服务器。
打开sendmail的配置宏文件:/etc/mail/sendmail.mc
 vi  /etc/mail/sendmail.mc
找到如下行:
DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl
修改Addr=0.0.0.0 ,表明可以连接到任何服务器。
生成新的配置文件:
cd /etc/mail  
mv sendmail.cf sendmail.cf~      //做一个备份  
m4 sendmail.mc > sendmail.cf   //>的左右有空格,提示错误没有安装sendmail-cf 
三、测试发送邮件
测试例子:
echo "content:e9china server normal" | mail -s "title:Server connection" notice@e9china.net
说明文档:常用发送邮件方式如下
1.如何写一般的邮件: mail test@126.com Cc 编辑抄送对象,Subject:邮件主题,输入回车,邮件正文后,按Ctrl-D结束
2.快速发送方式: echo “邮件正文” | mail -s 邮件主题 test@126.com
3.以文件内容作为邮件正文来发送: mail -s test test@126.com < test.txt
4.发送带附件的邮件: uuencode 附件名称 附件显示名称 | mail -s 邮件主题 发送地址
例如: uuencode test.txt test.txt | mail -s Test test@126.com
第一次执行可能收信比较慢,可以多执行几次等收到信以后把发件人添加到白名单.下次收信基本都是1分钟之内可以做到!需要秒收的朋友那就得自己配置stmp了!
如果执行shell的时候出现: You have new mail in /var/mail/root

直接删除目录即可

rm -rf /var/mail/root

这样邮件发送配置好了.然后我们来配置shell了

首先我们建立2个php文件,放置到url可以访问到目录:

nginx.php文件

vi nginx.php
<?php
echo "200_OK";
?>

mysql.php文件

vi mysql.php

<?php
$link = mysql_connect('localhost', 'root', '123456');
if (!$link) {
    die('Connect error');
}
echo 'Connected successfully';
mysql_close($link);
?> 

建立shell监控文件

check_p+m.sh

#!/bin/bash
wget http://192.168.1.1/nginx.php  --spider
c_monitor=$?
if [ $c_monitor = "1" ] || [ $c_monitor = "4" ] || [ $c_monitor = "8" ] || [ $c_monitor -ne "0" ]; then
echo "# Error no 200 status, time: " $(date +"%y-%m-%d %H:%M:%S") "e9china.net(192.168.1.1) WEB IS DOWN test:http://192.168.1.1/nginx.php" | mail -s "e9china.net: Website is offline." notice@e9china.net
/etc/init.d/php-fpm restart
fi

############################  check the MySQL ###################################

/etc/init.d/mysql  status &> /dev/null
if [ $? -ne 0 ] ; then
    echo "At time: 'data': mysql service is down. " >> /var/log/messages
    echo "# MySQL Error status, time: " $(date +"%y-%m-%d %H:%M:%S") "e9china.net(192.168.1.1) MYSQL IS DOWN test:http://192.168.1.1/mysql.php" | mail -s "e9china.net: MySQL is offline." notice@e9china.net 
    /usr/bin/killall mysql   
    /etc/init.d/mysql  restart
fi

这里我们标题和内容都加入了服务器名称和url,目的是当你收到报警邮件的是可以点击url看一下状态!不用急急忙忙的上服务器~这个设置人不在电脑旁的时候非常有用!

然后我们给以check_p+m.sh可以root允许的权限

chmod 777 check_p+m.sh

我们现在需要他每1分钟检查一下,那么就用到我们的老朋友crontab了

* */1 * * * bash /root/check_p+m.sh

这样就完成了.完成以后记得要测试,看会不会生效.如果你使用的是lnmp那么命令如下:

/etc/init.d/php-fpm stop
/etc/init.d/mysql stop

执行完毕等待一分钟,看看会不会自动启动和接受到邮件.好了教程就到这里结束了

评论

此博客中的热门博文

阿里云通过Nginx中转Linode/digitalocean/AWS的V2ray协议,实现负载均衡

开篇文的废话.原因是Sandy做站服务器太多了,发现利用率不高,皮又痒了又想折腾了.v2ray是利器但是给人的感觉就是只能用一台服务器,官方也说不支持负载均衡,不会A服务器挂机,自动跳转到B服务器.这里就有点折磨人了.每个服务器挂了都要自己去改配置文件. 我们访问国外的服务器,如果没CN2的线路是非常慢的,起码不快!哈哈~现在VPS都便宜起码人手一个人有5台以上吧?这样的话我们就可以开整. 开整条件:国内VPS或者阿里云有CN2线路的机器最好是100MB-200MB的带宽的.保证你爽歪歪.但是Sandy在阿里云HK30MB下也测试了也可以达到30000KB以上,羡慕吧~我们可以开始了! 老规则工具先出: 没被gwf和谐的服务器1台(下文统称:主服务器) 国外服务器2台以上(下文统称:从服务器) nginx 1.9.5版本以上(推荐:lnmp1.4) v2ray croe 第一步: 在 主服务器 安装lnmp1.4,这里就不细说教程了.直接去 官方 看教程 安装完毕以后我们来升级Nginx让他支持stream模块 cd lnmp1.4 vi lnmp.conf 在Nginx_Modules_Options=‘’里面加入--with-stream Nginx_Modules_Options='--with-stream’ 然后执行升级命令 ./upgrade.sh nginx  第二步 修改nginx.conf文件 cd /usr/local/nginx/conf/nginx.conf 在 events { use epoll; worker_connections 51200; multi_accept on; } 下面加入 stream{ server{ listen 0.0.0.0:8080; proxy_pass v2ray_backend; proxy_timeout 10m; proxy_connect_timeout 5000ms; } upstream v2ray_backend{ server ...

利用haproxy来做V2RAY负载均衡

Sandy在之前有说过 阿里云通过Nginx中转Linode/digitalocean/AWS的V2ray协议,实现负载均衡 ,有些学童不想折腾nginx,那么我们可以用haproxy来折腾,不管是haproxy还是Nginx都可以在树莓派和局域网的PC机器折腾! 安装haproxy的教程就移步到-> haproxy安装教程 apt-get install haproxy 安装完毕,我们来修改haproxy的配置 vi /etc/haproxy/haproxy.cfg 里面内容全部删除,加入下面的内容 global ulimit-n 51200 log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy pidfile /var/run/haproxy.pid user haproxy group haproxy daemon defaults log global mode tcp retries 3 option dontlognull option redispatch #上游TCP服务器连接等待时间 timeout connect 5s timeout client 24h timeout server 24h #以下为Web管理页面设置,如不需要可以删除 listen admin_stats bind 0.0.0.0:8888 mode http log 127.0.0.1 local0 err stats refresh 30s stats uri /haproxy stats realm welcome login\ Haproxy stats auth admin:admin stats hide-version stat...

ssh over socks5:通过socks5 proxy来连接ssh服务器[转载]

最近因为不可描述的原因,我在aws soul的云主机访问不了,ssh、80、ss全部都被禁掉了。 80端口在chrome配置SwitchyOmega就可以了,但ssh不太好办,shell上配置http_proxy对ssh没什么用。 其实ssh自己就支持proxy。 注意这跟在ssh端口转发:远程和本地中介绍过使用ssh作为proxy的方法不同,ssh -D是为了用ssh做proxy,而不是通过proxy来连接ssh服务器。 TL;DR ssh -o ProxyCommand='nc -x 192.0.2.0:1080 %h %p' user@awshost 原理解析 $ man ssh_config ProxyCommand Specifies the command to use to connect to the server. The command string extends to the end of the line, and is executed using the user's shell ‘exec’ directive to avoid a lingering shell process. In the command string, any occurrence of ‘%h’ will be substituted by the host name to connect, ‘%p’ by the port, and ‘%r’ by the remote user name. The command can be basically anything, and should read from its standard input and write to its standard output. It should eventually connect an sshd(8) server running on some machine, or execute sshd -i somewhere. Host key management will be done using the HostName of the host being connected (defa...