Munin

来自 ArchWiki

Munin 是一个网络资源监控工具,可以帮助分析资源趋势和瓶颈。Munin 具有主服务器/节点架构,其中主服务器定期从节点获取数据,并通过 Web 界面以图形方式呈现信息。默认安装提供了大量图形,几乎无需任何工作,并且可以轻松创建新的图形并作为插件添加。 [1]

您可以查看 OpenStreetMap 的 Munin 安装 示例。

安装

安装 主服务器上的 munin 软件包和您希望监控的设备上的 munin-node 软件包。

您也可以将它们都安装在同一台机器上,以便主服务器监控自身。

更多文档可以在 Munin 文档 Wiki 中找到。

配置

Munin 主服务器

目录

创建一个目录,munin-master 将在其中写入生成的 HTML 和图形图像。munin 用户必须具有此目录的写入权限。

以下示例使用 /srv/http/munin,因此可以在 https://127.0.0.1/munin/ 查看生成的输出,前提是已安装并运行 Web 服务器

# mkdir /srv/http/munin
# chown munin:munin /srv/http/munin

取消注释 /etc/munin/munin.conf 中的 htmldir 条目,并将其更改为上一步中创建的目录

htmldir /srv/http/munin

Cron

crontab

运行以下命令,使 Munin 每 5 分钟收集数据并更新生成的 HTML 和图形图像

# crontab /etc/munin/munin-cron-entry -u munin

配置电子邮件服务器以将邮件发送给 munin 用户。如果使用 postfix,请添加以下内容

/etc/postfix/aliases
munin:    root

并运行

# newaliases
systemd 定时器

可以使用 systemd 定时器代替 cron 作业。

这需要一个服务单元配置

/etc/systemd/system/munin-cron.service
[Unit]
Description=Survey monitored computers
After=network.target

[Service]
User=munin
ExecStart=/usr/bin/munin-cron

以及一个定时器单元配置

/etc/systemd/system/munin-cron.timer
[Unit]
Description=Survey monitored computers every five minutes

[Timer]
OnCalendar=*-*-* *:00/5:00

[Install]
WantedBy=multi-user.target

现在,重新加载 systemd 配置,启用/启动 munin-cron.timer 并验证一切正常

# journalctl --unit munin-cron.service
# less /var/log/munin/munin-update.log

权限

/etc/munin/munin.conf 中启用 graph_strategy cgi 时,请确保目录 /var/lib/munin/cgi-tmp 归用户和组 munin 所有,以便 /usr/share/munin/cgi/munin-cgi-graph 脚本能够将 png 文件写入此目录。

# chown munin: /var/lib/munin/cgi-tmp

测试

一旦配置 munin-cron 运行,Munin 就可以开始生成图形。确保每个节点上的 munin-node.service 正在运行。跳转到 #Munin 节点 部分可能会有所帮助,并在节点启动并运行后返回此处。

munin 用户身份手动运行 munin-cron 命令,它将立即触发 HTML 和图形图像的生成,而无需等待下一次 cron 运行

[munin]$ munin-cron

如果配置了 munin 日志记录,则日志通常位于 /var/log/munin/ 中。在运行 munin-cron 命令后,在单独的终端中监视 munin-update.log 日志可能有助于诊断问题。

# tail -f /var/log/munin/munin-update.log

最后,通过将浏览器指向输出目录或 https://127.0.0.1/munin/ 来测试界面。

注意: 图形可能需要一段时间才能有数据,请耐心等待。等待大约 30 分钟到一个小时。

Munin 节点

守护进程

在节点上,启用/启动 munin-node.service

IPv6

为了在 munin-node 上支持 IPv6,使用

/etc/munin/munin-node.conf
host :::1

安装

自定义

在运行 munin 之前,您可能需要设置系统的主机名。在 /etc/munin/munin.conf 中,默认主机名是 myhostname。可以将其更改为任何首选主机名。主机名将用于在 /var/lib/munin 中分组和命名 .rrd 文件,并进一步用于在您选择的 munin-master 目录中分组 HTML 文件和图形。

插件

使用 --suggest 选项运行 munin-node-configure,让 Munin 建议它认为可以在您的安装上工作的插件

# munin-node-configure --suggest

如果有您想要使用的插件的建议,请按照该建议并再次运行该命令。当您对 munin-node-configure 建议的插件感到满意时,使用 --shell 选项运行它以配置插件

# munin-node-configure --shell | sh
添加

基本上,所有插件都以以下方式添加(尽管存在例外,请查看每个插件!)

下载插件,然后将其复制或移动到 /usr/lib/munin/plugins

# cp plugin /usr/lib/munin/plugins/

然后将插件链接到 /etc/munin/plugins

# ln -s /usr/lib/munin/plugins/plugin /etc/munin/plugins/
注意: 某些插件(称为通配符插件)可以通过使用不同的名称链接它们来一次用于多个设备。这些插件以一个下划线结尾,并链接为 <plugin>_<device> 以用于 <device>。请参阅 if_ 插件示例。

现在测试您的插件。您不需要使用插件的完整路径,munin-run 应该能够找到它

# munin-run plugin

重启 munin-node.service。最后,刷新网页。

额外的插件

有很多 Munin 插件在那里等待安装。MuninExchange 是一个很好的起点,如果您找不到满足您需求的插件,则很容易编写自己的插件。请查看 Munin 文档 Wiki 上的 Developing Plugins 以了解如何操作。

移除

如果要删除插件,只需删除 /etc/munin/plugins 中的链接文件即可 - 无需从 /usr/lib/munin/plugins 中删除插件。

# rm /etc/munin/plugins/plugin
调试

如果您遇到插件无法按预期工作(例如根本没有输出)的情况,直接运行它可能会很有趣。幸运的是,有一种方法可以做到这一点。按照此处的说明进行操作,例如,您会注意到,启用后插件 apache_accesses 根本没有输出。为了直接运行插件

# munin-run apache_accesses

以下错误

LWP::UserAgent not found at /etc/munin/plugins/apache_accesses line 86.

表明找不到 perl 函数。要解决此问题,安装 缺少的库,在本例中为 perl-libwww

权限

由于许多插件读取日志文件,因此将 munin 用户添加log 组非常有用

# usermod -a -G log munin

Web 服务器 (可选)

本指南设置 Munin 生成静态 HTML 和图形图像,并将它们写入您选择的目录中。您可以使用任何 Web 浏览器在本地查看这些生成的文件。如果您想从远程机器查看生成的文件,那么您需要安装和配置以下 Web 服务器之一

或在 Web 服务器 类别中找到的其他服务器之一。

Apache

Apache VirtualHost 示例

本文或章节已过时。

原因: 本节谈到“下一个主要版本”,并提供了一个链接,该链接必须替换为 2018 年的文档副本。这些年来肯定发生了很多变化。(在 Talk:Munin 中讨论)

基于此处找到的信息

在 Munin 的下一个主要版本中,事情将变得更加简单。查看一下

基本静态 HTML

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin  root@localhost

    DocumentRoot /srv/http/munin

    ErrorLog /var/log/httpd/munin-error.log
    CustomLog /var/log/httpd/munin-access.log combined
</VirtualHost>

带有 DynaZoom 特性的静态 HTML

安装 perl-cgi-fast

您必须启用以下其中一项

  • mod_cgid(如果使用 mpm_prefork_module,则为 mod_cgi),方法是取消注释 httpd.conf 中的行。
  • 或安装 mod_fcgidAUR 并在 httpd.conf 中添加 LoadModule fcgid_module modules/mod_fcgid.so
<VirtualHost *:80>
   ServerName localhost
   ServerAdmin  root@localhost

   DocumentRoot /srv/http/munin

   ErrorLog /var/log/httpd/munin-error.log
   CustomLog /var/log/httpd/munin-access.log combined

   # Rewrites
   RewriteEngine On

   # Images
   RewriteRule ^/munin-cgi(.*) /usr/share/munin/cgi/$1 [L] 

   # Ensure we can run (fast)cgi scripts
   <Directory "/usr/share/munin/cgi">
       Require all granted
       Options +ExecCGI
       <IfModule mod_fcgid.c>
           SetHandler fcgid-script
       </IfModule>
       <IfModule !mod_fcgid.c>
           SetHandler cgi-script
       </IfModule>
   </Directory>
</VirtualHost>

DynaZoom 权限

当 Munin 无法绘制图形并记录类似于 Could not draw graph "/var/lib/munin/cgi-tmp/munin-cgi-graph/" 的消息时,请检查以确保在 /var/lib/munin/cgi-tmp/munin-cgi-graph/ 上设置了组权限,以便 munin-cgi-graph 目录具有可写组权限,例如 drwxrwxr-x 3 munin http 4096 Aug 9 20:11 munin-cgi-graph

ls -l /var/lib/munin/cgi-tmp/

如果未设置组权限,则

chmod g+w /var/lib/munin/cgi-tmp/munin-cgi-graph/

完全动态

如果要将 html_strategygraph_strategy 设置为 cgi,请使用此 VirtualHost。页面加载时间会更长,因为所有 HTML 和 PNG 文件都将动态生成,但 munin-cron 运行时间会更短,因为它不会执行 munin-html 和 munin-graph。如果您的主服务器轮询许多节点,并且 munin-cron 运行时间可能超过 5 分钟,则此功能可能对您来说是必要的。

安装 perl-cgi-fast

您必须启用以下其中一项

  • mod_cgid(如果使用 mpm_prefork_module,则为 mod_cgi),方法是取消注释 httpd.conf 中的行。
  • 或安装 mod_fcgidAUR 并在 httpd.conf 中添加 LoadModule mod_fcgid modules/mod_fcgid.so
<VirtualHost *:80>
   ServerName localhost
   ServerAdmin  root@localhost

   DocumentRoot /srv/http/munin

   ErrorLog /var/log/httpd/munin-error.log
   CustomLog /var/log/httpd/munin-access.log combined


   # Rewrites
   RewriteEngine On

   # Static content in /static
   RewriteRule ^/favicon.ico /etc/munin/static/favicon.ico [L] 
   RewriteRule ^/static/(.*) /etc/munin/static/$1          [L] 

   # HTML
   RewriteCond %{REQUEST_URI} .html$ [or]
   RewriteCond %{REQUEST_URI} =/
   RewriteRule ^/(.*)          /usr/share/munin/cgi/munin-cgi-html/$1 [L] 

   # Images
   RewriteRule ^/munin-cgi(.*) /usr/share/munin/cgi/$1 [L] 

   <Directory "/etc/munin/static">
       Require all granted
   </Directory>

   # Ensure we can run (fast)cgi scripts
   <Directory "/usr/share/munin/cgi">
       Require all granted
       Options +ExecCGI
       <IfModule mod_fcgid.c>
           SetHandler fcgid-script
       </IfModule>
       <IfModule !mod_fcgid.c>
           SetHandler cgi-script
       </IfModule>
   </Directory>
</VirtualHost>

Nginx

Munin 2.0.x

此 Nginx 设置示例基于 Munin 2.0.x munin 主服务器安装。它需要 FastCGI,并在 munin.conf 配置中使用 html_strategy cgigraph_strategy cgi

安装 Munin-Master 上的 nginxperl-cgi-fastperl-html-template-expr 软件包。

由于我们将使用 cgi 策略,因此需要启用 systemd 套接字文件。因此,创建了 /run/munin/fcgi-graph.sock/run/munin/fcgi-html.sock 套接字,以供 Nginx FastCGI 配置挂钩。

启用/启动 munin-graph.socketmunin-html.socket

创建 munin vhost 配置文件

/etc/nginx/sites-available/munin
server {
    server_name yourhost.example.com;
    listen 80;
    access_log /var/log/nginx/munin-access.log;
    error_log /var/log/nginx/munin-error.log info;
    location ^~ /munin-cgi/munin-cgi-graph/ {
        fastcgi_split_path_info ^(/munin-cgi/munin-cgi-graph)(.*);
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass unix:/run/munin/fcgi-graph.sock;
        include fastcgi_params;
    }
    location /munin/static/ {
        alias /etc/munin/static/;
    }
    location /munin/ {
        fastcgi_split_path_info ^(/munin)(.*);
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass unix:/run/munin/fcgi-html.sock;
        include fastcgi_params;
    }
}

然后重启 Web 服务器 (nginx.service)。

如果一切顺利,请将浏览器指向您的主机 http://yourhost.example.com/munin/,您应该会看到 Munin 概述页面。

Munin 2.1.x

尽管 Arch 仓库中尚无 Munin 2.1.x 版本。但值得一提的是,2.1.x 系列将不再使用 FastCGI,而是被 munin-httpd 取代。此页面 已经包含了一个示例配置。

技巧和窍门

MySQL

MySQL 插件具有额外的依赖项:perl-dbiperl-dbd-mysqlperl-cache-cacheAURperl-ipc-shareliteAUR

此外,建议通过单独的 MySQL 用户访问数据库。要通过以下 MySQL 命令创建另一个用户

MariaDB> CREATE USER 'muninuser'@'localhost' IDENTIFIED BY 'muninpassword';
MariaDB> GRANT SUPER,PROCESS ON *.* TO 'muninuser'@'localhost';
MariaDB> GRANT SELECT ON mysql.* TO 'muninuser'@'localhost';
MariaDB> FLUSH PRIVILEGES; 

要配置 Munin 以使用此新用户,请创建

/etc/munin/plugin-conf.d/mysql_
[mysql_*]
     env.mysqlconnection DBI:mysql:mysql;host=127.0.0.1;port=3306
     env.mysqluser muninuser
     env.mysqlpassword muninpassword

S.M.A.R.T.

要启用 S.M.A.R.T. 数据的监控,请安装 smartmontools 软件包,并使用

/etc/munin/plugin-conf.d/munin-node
[smart_*]
    user root
    group disk

然后为要监控的每个磁盘创建适当的符号链接。以 sda 为例

# ln -s /usr/lib/munin/plugins/smart_ /etc/munin/plugins/smart_sda

lm_sensors

安装 lm_sensors 并根据 lm_sensors#Configuration 进行配置。假设一切顺利,创建一些符号链接

# ln -s /usr/lib/munin/plugins/sensors_ /etc/munin/plugins/sensors_fan 
# ln -s /usr/lib/munin/plugins/sensors_ /etc/munin/plugins/sensors_temp
# ln -s /usr/lib/munin/plugins/sensors_ /etc/munin/plugins/sensors_volt