nagios 使用模板生成配置文件

默北 Nagiosnagios 使用模板生成配置文件已关闭评论10,0082字数 2624阅读8分44秒阅读模式

当上架多台服务器时,需要添加多台服务器监控配置文件,既繁琐又苦力活。当然啦,如果分组自动发现也会减轻添加监控的工作量。下面来说说使用模板来生成各自的配置文件,以避免撰写和复制粘贴了很多指令的新主机或服务的配置。

首先来看看什么是M4?文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/

1. M4

m4 是一个通用的宏处理器,所有版本的 UNIX 下都可用。虽然这种语言可以单独使用,但大多数人需要 m4 仅仅是因为 GNU autoconf 中的 “configure” 脚本依赖它。宏处理器(或预处理器)一般用作文本替换工具。最终用户经常会用它来处理要反复使用的文本模板,典型的是用于编程工具,还会用于文本编辑和文字处理工具。文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/

作为专为宏扩展的工具,M4是特别适合打造冗长纯文本配置文件,非常高效。文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/

M4网址: http://www.gnu.org/software/m4/manual/m4.html文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/

2. 创建配置模板

# cd /usr/local/nagios/etc/
# mkdir dynamic-conf
# cd dynamic-conf/
# vim host-service-template.m4
define(`TTLSA_COM', `
        define host {
                host_name $1
                alias $2
                address $3
                contact_groups ifelse(`$#', `4', `$4', `Ops')
                check_command check-host-alive
                check_interval 5
                check_period 24x7
                max_check_attempts 3
                notification_interval 30
                notification_options d,r
                notification_period 24x7
        }
        define service {
                host_name $1
                contact_groups ifelse(`$#', `4', `$4', `Ops')
                check_command check_ping!100,10%!200,20%
                check_interval 5
                check_period 24x7
                max_check_attempts 3
                notification_interval 30
                notification_period 24x7
                retry_interval 1
                service_description PING
        }
')

3. 配置主机

# vim 10.0.1.2.host.m4 
include(`host-service-template.m4')
TTLSA_COM(`10.0.1.2', `Web-server-2', `10.0.1.2')
# vim 10.0.1.3.host.m4 
include(`host-service-template.m4')
TTLSA_COM(`10.0.1.3', `Web-server-3', `10.0.1.3', `Dev')

4. 执行生成配置文件

# m4 10.0.1.2.host.m4 
        define host {
                host_name 10.0.1.2
                alias Web-server-2
                address 10.0.1.2
                contact_groups Ops
                check_command check-host-alive
                check_interval 5
                check_period 24x7
                max_check_attempts 3
                notification_interval 30
                notification_options d,r
                notification_period 24x7
        }
        define service {
                host_name 10.0.1.2
                contact_groups Ops
                check_command check_ping!100,10%!200,20%
                check_interval 5
                check_period 24x7
                max_check_attempts 3
                notification_interval 30
                notification_period 24x7
                retry_interval 1
                service_description PING
        }
# m4 10.0.1.3.host.m4 
        define host {
                host_name 10.0.1.3
                alias Web-server-3
                address 10.0.1.3
                contact_groups Dev
                check_command check-host-alive
                check_interval 5
                check_period 24x7
                max_check_attempts 3
                notification_interval 30
                notification_options d,r
                notification_period 24x7
        }
        define service {
                host_name 10.0.1.3
                contact_groups Dev
                check_command check_ping!100,10%!200,20%
                check_interval 5
                check_period 24x7
                max_check_attempts 3
                notification_interval 30
                notification_period 24x7
                retry_interval 1
                service_description PING
        }

5. 说明

host-service-template.m4文件中的变量可以按照自己的实际情况进行更改。$1是指host_name,$2是指alias,$3是指address, $4是指contact_group,可选的,默认是Ops。文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/

可以将m4 10.0.1.2.cfg 重定向到配置文件中。当然啦,如果为了更进一步自动化,同时显的更加专业,可以使用make命令来自动生成cfg文件。文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/

创建一个Makefile文件包含下面内容:文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/

# vim Makefile 
%.cfg : %.host.m4
        m4 $< > $*.cfg

注意:第二行前面空格是tab缩进,否则会报语法错误。文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/

# make 10.0.1.2.cfg  
m4 10.0.1.2.host.m4 > 10.0.1.2.cfg
# make 10.0.1.3.cfg  
m4 10.0.1.3.host.m4 > 10.0.1.3.cfg

6. 应用实例

在《mysql数据库监控》一文中,我是通过perl脚本来自动添加配置文件的,可以改成模板模式。文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/ 文章源自运维生存时间-https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 07/06/2014 01:00:00
  • 转载请务必保留本文链接:https://www.ttlsa.com/nagios/using-a-template-to-generate-nagios-configuration-file/