SELinux阻止nrpe访问MariaDB

Icinga2通过NRPE监控同一局域网中的几台虚拟机。

[机器A]

[机器B]

  • CentOS 6

  • MariaDB v10.1.12正常运行

Datatir和套接字设置在my.cnf中:

datadir=/database/mariadb socket=/database/mariadb/mysql.sock 

还有以下符号链接:

 /var/lib/mysql -> /database/mariadb 

所有者:上面所有的组是mysql:mysql。

  • 启用S​​ELinux

  • / usr / lib64 / nagios / plugins / check_mysql v2.0.3

具有以下安全上下文:

 -rwxr-xr-x. root root system_u:object_r:nagios_services_plugin_exec_t:s0 /usr/lib64/nagios/plugins/check_mysql 
  • nrpe.cfg包含以下行:

command[check_mysql]=/usr/lib64/nagios/plugins/check_mysql -H localhost -u xxx -p xxx -P 3306

现在的问题是:

Icinga(来自机器A)报告:

 "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)" 

如果我手动在机器B上运行下面的行:

 sudo -u nrpe /usr/lib64/nagios/plugins/check_mysql -H localhost -u xxx -p xxx -P 3306 

结果是好的(退出代码0):

 Uptime: 2085 Threads: 1 Questions: 68204 Slow queries: 0 Opens: 37 Flush... 

只有当我禁用机器B上的SELinux( echo 0 > /selinux/enforce )时,Icinga才能连接到mysql并显示状态OK。 但是我不想禁用SELinux。 我试图find适当的设置,使SELinux启用和Icinga正确连接到MySQL。

[编辑]

每当Icinga在计算机BI上检查mysql时,请在计算机B上的audit.log中查看以下两行新行:

 type=AVC msg=audit(1460038526.265:69): avc: denied { read } for pid=4858 comm="check_mysql" name="mysql" dev=dm-0 ino=130900 scontext=system_u:system_r:nagios_services_plugin_t:s0 tcontext=unconfined_u:object_r:var_lib_t:s0 tclass=lnk_file type=SYSCALL msg=audit(1460038526.265:69): arch=c000003e syscall=42 success=no exit=-13 a0=3 a1=7fffe4d270f0 a2=6e a3=7fffe4d263e0 items=0 ppid=4857 pid=4858 auid=4294967295 uid=497 gid=498 euid=497 suid=497 fsuid=497 egid=498 sgid=498 fsgid=498 tty=(none) ses=4294967295 comm="check_mysql" exe="/usr/lib64/nagios/plugins/check_mysql" subj=system_u:system_r:nagios_services_plugin_t:s0 key=(null) 

尝试这个:

 setsebool -P nagios_run_sudo 1 

在主机与NRPE。 SELinux策略中默认禁用此选项。

我终于设法解决了。 我分享下面的解决scheme,因为它也可能对其他人有用。

我创build了一个名为audit.log的工作文件,其中包含以下几行:

 type=AVC msg=audit(1460038526.265:69): avc: denied { read } for pid=4858 comm="check_mysql" name="mysql" dev=dm-0 ino=130900 scontext=system_u:system_r:nagios_services_plugin_t:s0 tcontext=unconfined_u:object_r:var_lib_t:s0 tclass=lnk_file type=SYSCALL msg=audit(1460038526.265:69): arch=c000003e syscall=42 success=no exit=-13 a0=3 a1=7fffe4d270f0 a2=6e a3=7fffe4d263e0 items=0 ppid=4857 pid=4858 auid=4294967295 uid=497 gid=498 euid=497 suid=497 fsuid=497 egid=498 sgid=498 fsgid=498 tty=(none) ses=4294967295 comm="check_mysql" exe="/usr/lib64/nagios/plugins/check_mysql" subj=system_u:system_r:nagios_services_plugin_t:s0 key=(null) 

我跑了:

 sealert -a audit.log > sealert.log 

结果sealert.log包含了这个问题的解释:

SELinux阻止/ usr / lib64 / nagios / plugins / check_mysql从lnk_file mysql的读访问权限。

还有解决这个问题的build议。 正如那里所build议的,我跑了以下几点:

 grep check_mysql audit.log | audit2allow -M mypol 

这输出了两个文件:mypol.pp和mypol.te

最后我跑了以下,完全解决了这个问题:

 semodule -i mypol.pp 

你的问题是,套接字通常驻留在/var/lib/mysql中的数据文件。

由于您正在使用不同的数据库目录,因此无论符号链接如何,SELinux都会阻止请求。

你可以尝试通过TCP连接来访问MySQL(使用127.0.0.1不是 localhost

但是,这仍然可能导致SELinux与MySQL守护进程本身的问题。