天天看点

如何使用Monit部署服务器监控系统

一个给你的监控系统增加冗余度的方法是安装独立的监控软件(作为后备),至少在网络中的关键/核心服务器上。这样在集中式监控系统挂掉的情况,你还有能力通过后备的监控方式来获取核心服务器的运行状况。

<a target="_blank"></a>

如何使用Monit部署服务器监控系统

我已经在一些机器使用monit几年了,而且我对它的可靠性非常满意。甚至作为全面的监控系统,对任何linux系统管理员来说monit也是非常有用和强大的。在这篇教程中,我会展示如何在一个本地服务器部署monit(作为后备监控系统)来监控常见的服务。在部署过程中,我只会展示我们用到的部分。

monit已经被包含在多数linux发行版的软件仓库中了。

debian、ubuntu或者linux mint:

<code>$ sudo aptitude install monit</code>

fedora或者centos/rhel:

<code># yum install monit</code>

monit自带一个文档完善的配置文件,其中包含了很多例子。主配置文件在/etc/monit.conf(fedora/centos/rhel 中),或者/etc/monit/monitrc(debian/ubuntu/mint 中)。monit配置文件有两部分:“global”(全局)和“services”(服务)。

monit可以使用邮件服务来发送通知,也可以使用http/https页面来展示。我们先使用如下配置的web状态页面吧:

monit监听1966端口。

对web状态页面的访问是通过ssl加密的。

使用monituser/romania作为用户名/口令登录。

只允许通过localhost、myhost.mydomain.ro和在局域网内部(192.168.0.0/16)访问。

monit使用pem格式的ssl证书。

之后的步骤,我会使用一个基于red hat的系统。在基于debian的系统中的步骤也是类似的。

首先,在/var/cert生成一个自签名的证书(monit.pem):

<code># mkdir /var/certs</code>

<code># cd /etc/pki/tls/certs</code>

<code># ./make-dummy-cert monit.pem</code>

<code># cp monit.pem /var/certs</code>

<code># chmod 0400 /var/certs/monit.pem</code>

现在将下列代码片段放到monit的主配置文件中。你可以创建一个空配置文件,或者基于自带的配置文件修改。

<code>set httpd port 1966 and</code>

<code>ssl enable</code>

<code>pemfile /var/certs/monit.pem</code>

<code>allow monituser:romania</code>

<code>allow localhost</code>

<code>allow 192.168.0.0/16</code>

<code>allow myhost.mydomain.ro</code>

邮件服务器的机器名:smtp.monit.ro

monit使用的发件人:[email protected]

邮件的收件人:[email protected]

邮件服务器使用的smtp端口:587(默认是25)

有了以上信息,邮件通知就可以这样配置:

<code>set mailserver smtp.monit.ro port 587</code>

<code>set mail-format {</code>

<code>from: [email protected]</code>

<code>subject: $service $event at $date on $host</code>

<code>message: monit $action $service $event at $date on $host : $description.</code>

<code></code>

<code>yours sincerely,</code>

<code>monit</code>

<code>}</code>

<code>set alert [email protected]</code>

就像你看到的,monit会提供几个内部变量(<code>$date</code>、<code>$event</code>、<code>$host</code>等),你可以按照你的需求自定义邮件内容。如果你想要从monit所在机器发送邮件,就需要一个已经安装的与sendmail兼容的程序(如postfix或者ssmtp)。

接下来就该配置monit守护进程了。可以将其设置成这样:

在120秒后进行第一次检测。

每3分钟检测一次服务。

使用syslog来记录日志。

如下代码段可以满足上述需求。

<code>set daemon 120</code>

<code>with start delay 240</code>

<code>set logfile syslog facility log_daemon</code>

我们必须定义“idfile”,monit守护进程的一个独一无二的id文件;以及“eventqueue”,当monit的邮件因为smtp或者网络故障发不出去,邮件会暂存在这里;以及确保/var/monit路径是存在的。然后使用下边的配置就可以了。

<code>set idfile /var/monit/id</code>

<code>set eventqueue</code>

<code>basedir /var/monit</code>

现在“global”部分就完成了。monit配置文件看起来像这样:

<code># global section</code>

<code># status webpage and acl's</code>

<code># mail-server</code>

<code># email-format</code>

<code># delay checks</code>

<code># idfile and mail queue path</code>

现在是时候验证我们的工作了,你可以通过运行如下命令来验证存在的配置文件(/etc/monit.conf):

<code># monit -t</code>

<code>control file syntax ok</code>

如果monit提示任何错误,请再检查下配置文件。幸运的是,错误/警告信息是可以帮助你发现问题的,比如:

<code>monit: cannot stat the ssl server pem file '/var/certs/monit.pem' -- no such file or directory</code>

<code>/etc/monit/monitrc:10: warning: hostname did not resolve 'smtp.monit.ro'</code>

一旦你确认配置文件没问题了,可以启动monit守护进程,然后等2到3分钟:

<code># service monit start</code>

如果你使用的是systemd,运行:

<code># systemctl start monit</code>

现在打开一个浏览器窗口,然后访问<code>https://&lt;monit_host&gt;:1966</code>。将<code>&lt;monit_host&gt;</code>替换成monit所在机器的机器名或者ip地址。

如果你使用的是自签名的ssl证书,你会在浏览器中看到一个警告信息。继续访问即可。

如何使用Monit部署服务器监控系统

你完成登录后,就会看到这个页面。

如何使用Monit部署服务器监控系统

原文发布时间为:2015-05-30

本文来自云栖社区合作伙伴“linux中国”

继续阅读