前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一种监控apache服务的方法

一种监控apache服务的方法

原创
作者头像
mariolu
发布2023-02-06 08:59:37
1.2K0
发布2023-02-06 08:59:37
举报
image.png
image.png

一、为什么使用Monit

Monit用于管理和监视 UNIX 系统上的进程、文件、目录和文件系统。

当你无法监控服务器的服务可用性时,最好借助自动监控和重启实用程序。

当然monit有shell 和 perl 脚本替代品来监控服务。如果服务失败脚本将尝试重新启动服务并向我发送一封自动电子邮件。但是monit 是一个完整的解决方案。比如说

  • monit 可以在一个进程不运行时启动它
  • 在它没有响应时重新启动它
  • 在它使用太多资源时停止它
  • 可以使用 monit 来监视文件、目录和设备的更改,例如时间戳更改、校验和更改或大小更改。出于安全目的,你可以监控不应更改的文件的 md5 校验和。
  • 支持各种apache服务的开源包,这里apache服务包括lighttpd,tomcat等等 而且它非常容易配置并且非常可定制。

这是它的官网:https://mmonit.com/monit/,源码https://github.com/arnaudsj/monit

二、安装

在 Debian 或 Ubuntu Linux 下安装 monit

代码语言:shell
复制
apt-get install monit

Red Hat enterprise Linux / CentOS Linux下安装monit(源码安装)

许多发行版都包含 monit。然而,官方 Red Hat Enterprise Linux 中并未包含 monit。这里使用源码安装

代码语言:shell
复制
cd /YOUR/WORKSPACE
git clone https://github.com/arnaudsj/monit.git
cd monit
make
make install
cp monitrc /etc/monitrc

到这里已经安装好monit

默认情况下,monit 二进制文件位于/usr/local/bin/monit

三、如何配置监视器?

monitrc是 monit 配置文件的名称,放置于/etc/monitrc位置。但是每个发行版将文件放在不同的位置:.

=> 源代码安装:/etc/monitrc

=> Debian/Unentu Linux 安装:/etc/monit/monitrc

3.1、一个配置例子

打开 monit 配置文件和设置值如下:

代码语言:shell
复制
vi /etc/monitrc
  • a) 将其作为守护进程运行并以 2 分钟的间隔检查服务(例如 web、mysql、sshd) 。

set daemon 120

  • b) 使用“daemon”工具设置 syslog 日志记录:

set logfile syslog facility log_daemon

  • c) 设置邮件服务器名称发送邮件提醒 设置邮件格式如

from email

set mailserver mail.cyberciti.biz set mail-format { from: alert@nixcraft.in subject: $SERVICE $EVENT at $DATE

message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION. }

d) 然后是最重要的部分,比如说这个例子:如果由于任何原因失败或被 Linux 内核杀死,请重新启动 lighttpd 或 apache web 服务器:

check process lighttpd with pidfile /var/run/lighttpd.pid group lighttpd start program = "/etc/init.d/lighttpd start" stop program = "/etc/init.d/lighttpd stop"

if failed host 75.126.43.232 port 80

protocol http then restart

if 5 restarts within 5 cycles then timeout

这里有些变量需要根据个性化自己定制

使用 pidfile /var/run/lighttpd.pid 检查进程 lighttpd:您正在指定 lighttpd pid 文件和守护程序名称

group lighttpd : 指定组名,允许或用于启动/重启lighttpd

start program = “/etc/init.d/lighttpd start”:启动lighttpd服务器的命令

stop program = “/etc/init.d/lighttpd stop” : 停止 lighttpd 服务器的命令

如果主机 127.0.0.1 端口 80 失败:服务器 IP 地址和端口号 (80)

protocol http then restart : 如果上面的 IP 和端口失败重启网络服务器

if 5 restarts within 5 cycles then timeout : 尝试重启5次;如果 monit 无法重新启动网络服务器 5 次;只是超时以避免竞争条件。

3.2、其他服务器配置例子

这里有些例子:https://gist.github.com/franck/1415908

还比如说

这是mysql 服务器重启配置指令:

group database

start program = "/etc/init.d/mysqld start"

stop program = "/etc/init.d/mysqld stop"

if failed host 127.0.0.1 port 3306 then restart

if 5 restarts within 5 cycles then timeout

还有这里sshd 服务器配置指令:

check process mysqld with pidfile /var/run/mysqld/mysqld.pid

check process sshd with pidfile /var/run/sshd.pid

start program "/etc/init.d/sshd start"

stop program "/etc/init.d/sshd stop"

if failed host 127.0.0.1 port 22 protocol ssh then restart

if 5 restarts within 5 cycles then timeout

还有httpd服务器重启配置指令:

check process httpd with pidfile /var/run/httpd.pid

group apache

start program = "/etc/init.d/httpd start"

stop program = "/etc/init.d/httpd stop"

if failed host 127.0.0.1 port 80

protocol http then restart

if 5 restarts within 5 cycles then timeout

将 IP 地址 127.0.0.1 替换为您的实际 IP 地址。如果使用的是 Debian,这样启动 monit:

代码语言:shell
复制
/etc/init.d/monit start

如果使用的是Red Hat Enterprise Linux,使用systemd方式启动

创建monit.service

代码语言:txt
复制
vim /etc/systemd/system/monit.service

输入以下内容

代码语言:txt
复制
[Unit]                                                                                                                                                     
Description=monit apache service

[Service]
User=root
Group=root

ExecStart="/data/mariolu/run_monit.sh"
ExecStop=""
Restart=restart-always    

[Install]
WantedBy=multi-user.target

这里的ExecStart是一个自己写的脚本文件,你可以创建自己的脚本run_monit.sh,然后在这个文件写入monit的启动命令

image.png
image.png

/usr/local/bin/monit -Ic /etc/monitrc

然后这样启动monit,

service monit restart

这样monit就会永久监控服务

四、验证是否部署成功

您可以验证 monit 是否从 /var/log/messages 日志文件启动:

代码语言:shell
复制
tail -f /var/log/messages

输出:

Nov 21 04:39:21 server monit8759: Starting monit daemon

Nov 21 04:39:21 server monit8759: Monit started

如果 lighttpd 死了,你会在日志文件中看到如下内容:

Nov 21 04:45:13 server monit8759: 'lighttpd' process is not running

Nov 21 04:45:13 server monit8759: 'lighttpd' trying to restart

Nov 21 04:45:13 server monit8759: 'lighttpd' start: /etc/init.d/lighttpd

您可以使用 monit 来监视在本地主机上运行或从 /etc/init.d/ 位置启动的守护进程或类似程序,例如

=> Apache Web 服务器

=> SSH 服务器

=> Postfix/Sendmail MTA

=> MySQL 等

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、为什么使用Monit
  • 二、安装
  • 三、如何配置监视器?
    • 3.1、一个配置例子
      • 3.2、其他服务器配置例子
      • 四、验证是否部署成功
      相关产品与服务
      云数据库 MySQL
      腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档