当上架多台服务器时,需要添加多台服务器监控配置文件,既繁琐又苦力活。当然啦,如果分组自动发现也会减轻添加监控的工作量。下面来说说使用模板来生成各自的配置文件,以避免撰写和复制粘贴了很多指令的新主机或服务的配置。
首先来看看什么是M4?
1. M4
m4 是一个通用的宏处理器,所有版本的 UNIX 下都可用。虽然这种语言可以单独使用,但大多数人需要 m4 仅仅是因为 GNU autoconf 中的 “configure” 脚本依赖它。宏处理器(或预处理器)一般用作文本替换工具。最终用户经常会用它来处理要反复使用的文本模板,典型的是用于编程工具,还会用于文本编辑和文字处理工具。
作为专为宏扩展的工具,M4是特别适合打造冗长纯文本配置文件,非常高效。
M4网址: http://www./software/m4/manual/m4.html
2. 创建配置模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
# 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. 执行生成配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
# 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 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
# 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。
可以将m4 10.0.1.2.cfg 重定向到配置文件中。当然啦,如果为了更进一步自动化,同时显的更加专业,可以使用make命令来自动生成cfg文件。
创建一个Makefile文件包含下面内容:
|
# vim Makefile %.cfg : %.host.m4 m4 $< > $*.cfg |
注意:第二行前面空格是tab缩进,否则会报语法错误。
|
# 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脚本来自动添加配置文件的,可以改成模板模式。
|