Yes, you can, but it is better to do this in a more organized way.
There are several setups for nagios depending of the version and the distro.
As general thumb of role, in nagios.cfg you have includes directives where you specify files which has host directives, services directives, commands, etc. You can have a single file for all linux servers and other for windows machines, and other for printers.
You can have also a directory where nagios will look for any cfg file to read at start up.
I like to have a single file to declare all hostgroups and servicegroups I want to use.
In nagios.cfg file use a include directive to, uhh, include this file:
Code:
cfg_file=/usr/local/nagios/etc/objects/groups.cfg
In this file declare the hostgroups and servicegroups. It looks like:
Code:
define hostgroup{
hostgroup_name linux-server ; The name of the hostgroup
alias Linux Server ; Long name of the group
}
define hostgroup{
hostgroup_name core-server ; The name of the hostgroup
alias Core Server ; Long name of the group
}
define hostgroup{
hostgroup_name name-server ; The name of the hostgroup
alias Domain Name Server ; Long name of the group
}
... cut ....
define servicegroup {
servicegroup_name dns
alias Domain name service
}
define servicegroup {
servicegroup_name mail
alias E-Mail related services
}
define servicegroup {
servicegroup_name http
alias Web related services
}
I like to declare who is in which group not here in this file (you can if you want. Just add inside each definition a statement like "members hostname1,hostname2" or "members service1,service2").
In my setup, the servers definitions are in a folder. In this folder I have a file.cfg for each server I want to monitor in nagios.
For this setup I use in nagios.cfg:
Code:
cfg_dir=/usr/local/nagios/etc/objects/servers
In each file.cfg in "servers" dir above, I put both the host definition, and the services definitions for that host.
In this file I use the "hostgroup" statement to add the host to one or more hostgroups and in the service definition I use the statement "servicegroups" to add the service to one or more servicegroups.
I have a file named webmail.cfg with definitions to monitoring my webmail server. It is in the hostgroup "linux-server" and in the hostgroup "core-server". It has one service; the webmail. It is in the servicegroup "mail" and in the servicegroup "http", as it is a webmail, of course.
Code:
define host{
use linux-server,host-pnp
host_name webmail
alias Webmail, virtual host
address webmail
hostgroup linux-server, core-server
}
define service{
use local-service,srv-pnp
host_name webmail
service_description Apice internal webmail
servicegroups mail,http
check_command check_http!-u /src/login.php
}
Configuring nagios is a matter of deal with several configuration files, but the primary file is nagios.cfg. You really need to take a close look to it to understand which are the other files and what are its function.
There are no syntactic rule to warn you if you put a service definition in a host.cfg file or even in a template file, as you mentioned.