Munin
Munin 是一个网络资源监控工具,可以帮助分析资源趋势和瓶颈。Munin 采用主/节点(master/node)架构,其中主服务器定期从节点获取数据,并通过 Web 界面以图形方式呈现信息。默认安装只需很少的配置即可提供大量图表,并且可以轻松地添加新图表作为插件。[1]
您可以查看 OpenStreetMap 的 Munin 安装 作为示例。
安装
在主服务器上安装 munin 包,并在您希望监控的设备上安装 munin-node 包。
您也可以在同一台机器上同时安装它们,以便主服务器监控自身。
更多文档可在 Munin 文档 Wiki 上找到。
配置
Munin master
目录
创建一个目录,供 munin-master 写入生成的 HTML 和图形图像。munin 用户必须对该目录拥有写入权限。
以下示例使用 /srv/http/munin,因此生成的输出可以在 https:///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 timer
可以使用 systemd timer 代替 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 node 部分,然后在节点启动并运行时再返回。
通过以 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:///munin/ 来测试界面。
Munin node
守护进程
在节点上,启用/启动 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 虚拟主机示例
基于此处找到的信息:
- http://guide.munin-monitoring.org/en/stable-2.0/example/webserver/apache-virtualhost.html
- http://munin-monitoring.org/wiki/MuninConfigurationMasterCGI[死链 2021-05-17—HTTP 404]
在 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_strategy 和 graph_strategy 设置为 cgi,请使用此虚拟主机。页面加载将需要更长时间,因为所有 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 cgi 和 graph_strategy cgi。
安装 nginx、perl-cgi-fast 和 perl-html-template-expr 包到 Munin-Master。
由于我们将使用 cgi 策略,因此需要启用 systemd 套接字文件。因此,为 Nginx FastCGI 配置创建 /run/munin/fcgi-graph.sock 和 /run/munin/fcgi-html.sock 套接字。
启用/启动 munin-graph.socket 和 munin-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
尽管 Munin 2.1.x 版本尚未在 Arch 存储库中提供。值得一提的是,2.1.x 系列将不再使用 FastCGI,而是被 munin-httpd 取代。此 页面 已包含示例配置。
技巧与提示
MySQL
MySQL 插件有额外的依赖项:perl-dbi、perl-dbd-mysql、perl-cache-cacheAUR 和 perl-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