This page covers configuration steps for Nagios Core deployments including hosts, services, contacts, notifications, and advanced configurations.
Current Nagios Core version: 4.5.11
| Component | Configuration Path |
|---|---|
| Main config | /usr/local/nagios/etc/nagios.cfg |
| Resource file | /usr/local/nagios/etc/resource.cfg |
| Objects directory | /usr/local/nagios/etc/objects/ |
| Contacts | /usr/local/nagios/etc/objects/contacts.cfg |
| Commands | /usr/local/nagios/etc/objects/commands.cfg |
| Time periods | /usr/local/nagios/etc/objects/timeperiods.cfg |
| Templates | /usr/local/nagios/etc/objects/templates.cfg |
| Local configs | /usr/local/nagios/etc/conf.d/ |
Edit /usr/local/nagios/etc/nagios.cfg:
# Nagios Core Configuration File
# Location of main config
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
# Directory for additional configs
cfg_dir=/usr/local/nagios/etc/conf.d
# Resource file for macros
resource_file=/usr/local/nagios/etc/resource.cfg
# Status file
status_file=/usr/local/nagios/var/status.dat
# Check result path
check_result_path=/usr/local/nagios/var/checkresult
# Temp file
temp_file=/usr/local/nagios/var/nagios.tmp
# Log file
log_file=/usr/local/nagios/var/nagios.log
# Log rotation
log_rotation_method=d
# Log archive path
log_archive_path=/usr/local/nagios/var/archives
# Logging options
log_notifications=1
log_service_retries=1
log_host_retries=1
log_event_handlers=1
log_initial_states=0
log_current_states=0
log_external_commands=1
# Notification options
enable_notifications=1
# Performance data
process_performance_data=0
# Check execution
execute_host_checks=1
execute_service_checks=1
# Passive checks
accept_passive_host_checks=1
accept_passive_service_checks=1
# Event handlers
enable_event_handlers=1
# Flap detection
enable_flap_detection=1
# Soft states
retain_state_information=1
state_retention_file=/usr/local/nagios/var/retention.dat
retention_update_interval=60
use_retained_program_state=1
use_retained_scheduling_info=1
# Command check interval
command_check_interval=15s
# External commands
check_external_commands=1
external_command_buffer_slots=4096
Create /usr/local/nagios/etc/conf.d/hosts.cfg:
# Web Server Host
define host {
use linux-server
host_name web-server-01
alias Web Server 01
address 192.168.1.10
max_check_attempts 5
check_period 24x7
notification_interval 30
notification_period 24x7
contact_groups admins
action_url http://192.168.1.10/server-status
notes Production web server
notes_url http://wiki.example.com/web-server-01
}
# Database Server Host
define host {
use linux-server
host_name db-server-01
alias Database Server 01
address 192.168.1.20
max_check_attempts 5
check_period 24x7
notification_interval 30
notification_period 24x7
contact_groups admins,database-team
notes Production MySQL database server
}
# Network Switch
define host {
use network-switch
host_name core-switch-01
alias Core Switch 01
address 192.168.1.1
max_check_attempts 3
check_period 24x7
notification_interval 60
notification_period 24x7
contact_groups network-admins
}
Create /usr/local/nagios/etc/conf.d/services.cfg:
# HTTP Service
define service {
use generic-service
host_name web-server-01
service_description HTTP
check_command check_http
check_interval 5
retry_interval 1
max_check_attempts 4
notification_interval 30
notification_period 24x7
contact_groups admins
}
# SSH Service
define service {
use generic-service
host_name web-server-01
service_description SSH
check_command check_ssh
check_interval 5
retry_interval 1
max_check_attempts 4
}
# MySQL Service
define service {
use generic-service
host_name db-server-01
service_description MySQL
check_command check_mysql!-u nagios -p password
check_interval 5
retry_interval 1
max_check_attempts 4
contact_groups admins,database-team
}
# Disk Space
define service {
use generic-service
host_name web-server-01
service_description Root Partition
check_command check_local_disk!20%!10%!/
check_interval 5
retry_interval 1
}
# CPU Load
define service {
use generic-service
host_name web-server-01
service_description Current Load
check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
check_interval 5
retry_interval 1
}
# Memory Usage
define service {
use generic-service
host_name web-server-01
service_description Memory Usage
check_command check_local_mem!80%!90%
check_interval 5
retry_interval 1
}
# Ping
define service {
use generic-service
host_name web-server-01
service_description Ping
check_command check_ping!100.0,20%!500.0,60%
check_interval 1
retry_interval 1
}
Edit /usr/local/nagios/etc/objects/contacts.cfg:
# Admin Contact
define contact {
contact_name admin
alias System Administrator
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email admin@example.com
}
# On-Call Contact
define contact {
contact_name oncall
alias On-Call Engineer
service_notification_period 24x7
host_notification_period 24x7
service_notification_options c,r
host_notification_options d,r
service_notification_commands notify-service-by-email;notify-service-by-sms
host_notification_commands notify-host-by-email;notify-host-by-sms
email oncall@example.com
pager 555-1234
}
# Database Team
define contact {
contact_name dba-team
alias Database Administration Team
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email dba-team@example.com
}
# Admins Group
define contactgroup {
contactgroup_name admins
alias System Administrators
members admin,oncall
}
# Database Team Group
define contactgroup {
contactgroup_name database-team
alias Database Administration Team
members dba-team
}
# Network Admins Group
define contactgroup {
contactgroup_name network-admins
alias Network Administrators
members admin,network-team
}
Edit /usr/local/nagios/etc/objects/commands.cfg:
# HTTP Check
define command {
command_name check_http
command_line $USER1$/check_http -H $HOSTADDRESS$ $ARG1$
}
# SSH Check
define command {
command_name check_ssh
command_line $USER1$/check_ssh -H $HOSTADDRESS$ $ARG1$
}
# Ping Check
define command {
command_name check_ping
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$
}
# Disk Check
define command {
command_name check_local_disk
command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
}
# Load Check
define command {
command_name check_local_load
command_line $USER1$/check_load -w $ARG1$ -c $ARG2$
}
# Memory Check
define command {
command_name check_local_mem
command_line $USER1$/check_mem -w $ARG1$ -c $ARG2$
}
# MySQL Check
define command {
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ $ARG1$
}
# SMTP Check
define command {
command_name check_smtp
command_line $USER1$/check_smtp -H $HOSTADDRESS$ $ARG1$
}
# DNS Check
define command {
command_name check_dns
command_line $USER1$/check_dns -H $HOSTADDRESS$ $ARG1$
}
# Notify Host by Email
define command {
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "Nagios\n\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mail -s "** $HOSTSTATE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}
# Notify Service by Email
define command {
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "Nagios\n\nService: $SERVICEDESC$\nHost: $HOSTNAME$\nState: $SERVICESTATE$\nAddress: $HOSTADDRESS$\nInfo: $SERVICEOUTPUT$\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mail -s "** $SERVICESTATE$ Service Alert: $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
Edit /usr/local/nagios/etc/objects/timeperiods.cfg:
# 24x7 Monitoring
define timeperiod {
timeperiod_name 24x7
alias 24 Hours A Day, 7 Days A Week
sunday 00:00-24:00
monday 00:00-24:00
tuesday 00:00-24:00
wednesday 00:00-24:00
thursday 00:00-24:00
friday 00:00-24:00
saturday 00:00-24:00
}
# Business Hours
define timeperiod {
timeperiod_name business-hours
alias Business Hours (8am-6pm)
monday 08:00-18:00
tuesday 08:00-18:00
wednesday 08:00-18:00
thursday 08:00-18:00
friday 08:00-18:00
}
# Night Hours
define timeperiod {
timeperiod_name night-hours
alias Night Hours (6pm-8am)
monday 18:00-24:00,00:00-08:00
tuesday 18:00-24:00,00:00-08:00
wednesday 18:00-24:00,00:00-08:00
thursday 18:00-24:00,00:00-08:00
friday 18:00-24:00,00:00-08:00
saturday 00:00-24:00
sunday 00:00-24:00
}
# Never (for disabling notifications)
define timeperiod {
timeperiod_name never
alias No Time Period
}
Create /usr/local/nagios/etc/conf.d/escalations.cfg:
# Service Escalation
define serviceescalation {
host_name web-server-01
service_description HTTP
first_notification 3
last_notification 0
notification_interval 15
contact_groups admins,oncall
}
# Host Escalation
define hostescalation {
host_name web-server-01
first_notification 3
last_notification 0
notification_interval 15
contact_groups admins,oncall
}
# Web service depends on HTTP
define servicedependency {
host_name web-server-01
service_description HTTP
dependent_host_name web-server-01
dependent_service_description Web Application
execution_failure_criteria c,u
notification_failure_criteria c,u
}
# Application depends on database
define servicedependency {
host_name db-server-01
service_description MySQL
dependent_host_name web-server-01
dependent_service_description Web Application
execution_failure_criteria c,u
notification_failure_criteria c,u
}
Create /usr/local/nagios/etc/conf.d/hostgroups.cfg:
# Web Servers Group
define hostgroup {
hostgroup_name webservers
alias Web Servers
members web-server-01,web-server-02
}
# Database Servers Group
define hostgroup {
hostgroup_name databases
alias Database Servers
members db-server-01,db-server-02
}
# Network Devices Group
define hostgroup {
hostgroup_name network-devices
alias Network Devices
members core-switch-01,edge-switch-01,firewall-01
}
# Validate Nagios configuration
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# Check for errors
echo $? # Should return 0
# Restart Nagios service
sudo systemctl restart nagios
# Or use nagios command
sudo /etc/init.d/nagios restart
# Check service status
sudo systemctl status nagios
# Check Nagios log
sudo tail -f /usr/local/nagios/var/nagios.log
# Enable passive checks for a service
define service {
use generic-service
host_name web-server-01
service_description Application Health
check_command check_dummy!0
active_checks_enabled 0
passive_checks_enabled 1
accept_passive_service_checks 1
}
# Restart Apache when HTTP check fails
define service {
use generic-service
host_name web-server-01
service_description HTTP
check_command check_http
event_handler restart_apache
event_handler_enabled 1
}
define command {
command_name restart_apache
command_line /usr/bin/ssh -i /etc/nagios/ssh_key nagios@$HOSTADDRESS$ 'sudo systemctl restart apache2'
}
# Enable flap detection for a service
define service {
use generic-service
host_name web-server-01
service_description HTTP
check_command check_http
flap_detection_enabled 1
flap_detection_options c,w,u
low_flap_threshold 5.0
high_flap_threshold 20.0
}
# Check for configuration errors
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# Check object definitions
sudo grep -r "define" /usr/local/nagios/etc/conf.d/
# Check notification logs
sudo grep "NOTIFICATION" /usr/local/nagios/var/nagios.log
# Test email notification
echo "Test" | mail -s "Test" admin@example.com
# Check check result queue
sudo cat /usr/local/nagios/var/checkresult/*
# Monitor Nagios process
sudo top -p $(pgrep nagios)
Every deployment is unique. We provide consulting for:
Get personalized assistance: office@linux-server-admin.com | Contact Page