跳至主要内容

监控你的lnmp状态是否正常

脚本介绍

本脚本主要用于监控 LNMP 环境中的 PHP/Nginx/MySQL 服务是否可用,若不可用则写入到日志,并通过 SendMail 发送邮件进行通知!

服务器组件安装

1、sendmail安装

之前在另外一篇文章有说到过,请同学们移步--->利用shell监控服务器状态并且EMAIL获取报警

2、sharutils安装

Sendmail作为大多数Linux默认自带的邮件工具,把它利用起来配合做一些简单的系统管理工作,我觉得是非常好的。
之前我用sendmail每天定期将一些我需要的报告通过“正文”的方式直接发送到我的邮箱,但由于内容越来越多,看起来很不方便,如是想把这些报告通过附件的方式发送,而sendmail默认是不带有这个功能的,需要配合安装一个叫sharutils的软件包来实现。

apt install mailutils
 apt install sharutils

3、测试组件

利用uuencode使sendmail能发送带有附件的邮件

uuencode /home/wwwlogs/access.log access.log | mail -s "my lnmp log" your@mail.com

脚本完整代码

该 Shell 脚本已经在军哥的 LNMP 环境测试成功。
宝塔、WDCP 等搭建的可能不支持,有需要请自行修改。
脚本中的收件人邮箱、监测站点的 url,请修改为你自己的

#!/bin/bash
###获取当前时间
time="$(date +"%Y%m%d-%H:%M")"
###查看fpm服务是否运行
i=`netstat -an | grep php-cgi | wc -l`
if [ $i = 0 ]
        then
               ###重启php服务
               /etc/init.d/php-fpm restart
               ### 写入日志 
               echo "$time php-fpm service is down .... restart..." >> /var/log/php-fpm.log
               ### 提取日志文件最新的20行内容并写入到txt文档
               tail -n 20 /var/log/php-fpm.log > /root/php-mail.txt
               ### 提取txt内容并邮箱通知
               uuencode /root/php-mail.txt php-mail.txt | mail -s "$time php-fpm service restart" you@mail.com
fi
###查看mysql服务是否运行
i=`netstat -anpt | grep mysqld | awk '{print $4}' | awk -F: '{print $2}' | wc -l`
if [ $i = 0 ]
        then
               ### 重启mysql服务
               /etc/init.d/mysql restart
               ### 写入日志
               echo "$time mysqld service is down .... restart..." >> /var/log/mysql-error.log
               ### 提取日志文件最新的20行内容并写入到txt文档
               tail -n 20 /var/log/mysql-error.log > /root/mysql-mail.txt
               ### 提取txt内容并邮箱通知
               uuencode /root/mysql-mail.txt mysql-mail.txt | mail -s "$time Mysql service restart" you@mail.com
fi
###查看nginx服务是否运行
i=`netstat -anpt | grep nginx | awk '{print $4}' | awk -F: '{print $2}' | wc -l`
if [ $i = 0 ]
        then
               ### 重启nginx服务
               /etc/init.d/nginx restart
               ### 写入日志
               echo "$time nginx service is down .... restart..." >> /var/log/nginx-error.log
               ### 提取日志文件最新的20行内容并写入到txt文档
               tail -n 20 /var/log/nginx-error.log > /root/nginx-mail.txt
               ### 提取txt内容并邮箱通知
               uuencode /root/nginx-mail.txt nginx-mail.txt | mail -s "$time Nginx service service" you@mail.com
fi

## 判断状态码是否为200
url=http://127.0.0.1/nginx.php
i=$(curl -I -m 10 -o /dev/null -s -w %{http_code} $url)
if [ $i -ne 200 ]; then
                 /etc/init.d/mysql restart
                 /etc/init.d/nginx restart
                 /etc/init.d/php-fpm restart
                 echo " $time 监测页: $url 状态码: $i 行为: 异常&重启" >> /var/log/httpcode.log
fi

建立nginx.php文件,并且放到网站目录下

< ?php
echo "200_OK";
?>

食用方法

1、在 root 目录下保存脚本代码,并另存为 monitor-lnmp.sh(按需自行更换名称)

2、给予文件可执行权限

chmod a+x /root/monitor-lnmp.sh

3、加入到 crond 任务计划

执行命令 crontab -e 添加一条任务记录,例如每十分钟执行一次检测

*/10 * * * * bash /root/monitor-lnmp.sh

评论

此博客中的热门博文

阿里云通过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...