Creating a new time period
In this recipe, we'll add a new time period definition to the Nagios Core configuration to allow us to set up monitoring for hosts and services only during weekdays. There's a default configuration defined as workhours
that would almost suit us, except that it doesn't include the evenings. We'll make a new one from scratch, and we'll make another one to cover the weekends too.
Getting ready
You should have a working Nagios Core 3.0 or better server running.
How to do it...
We can set up our new time period, which we'll call weekdays
, as follows:
Change to our Nagios Core configuration objects directory, and edit the file called
timeperiods.cfg
:# cd /usr/local/nagios/etc/objects # vi timeperiods.cfg
Add the following definitions to the end of the file:
define timeperiod { timeperiod_name weekdays alias Weekdays 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 } define timeperiod { timeperiod_name weekends alias Weekends saturday 00:00-24:00 sunday 00:00-24:00 }
Restart the Nagios Core server:
# /etc/init.d/nagios restart
How it works...
In our host and service definitions, there are two directives, check_period
and notification_period
. These directives are used to define the times during which a host or service should be checked, and the times when notifications about them should be sent. The 24x7
and workhours
periods are defined in the timeperiods.cfg
file that we just edited, and are used in several of the examples and templates.
We've just added two more of these time periods, which we can now use in our definitions for hosts and services. The first is called weekdays
, and corresponds to any time during a weekday; the second is called weekends
, and corresponds to any time that's not a weekday. Note that in both cases, we specified the dates and times by naming each individual day, and the times to which they corresponded.
There's more...
The definitions for dates are quite clever, and can be defined in a variety of ways. The following are all valid definitions for days and time periods:
june 1 - july 15 00:00-24:00
: Any time from June 1st to July 15th, inclusivethursday -1 00:00-24:00
: Any time on the last Thursday of every monthday 1 - 10 13:00-21:00
: From 1 PM to 9 PM on any day from the 1st of any month to the 10th of any month, inclusive
It's likely that the standard 24x7
and workhours
definitions will be fine for day-to-day monitoring, maybe with a weekdays
and weekends
definition added. However, there may well come a time when we need a specific host or service monitored on an unusual schedule, particularly if we're debugging a specific problem that only manifests around a certain time, or have a lot of contacts to manage, or a complex on-call roster.
Note that Nagios Core can behave in unusual ways, particularly with uptime reporting, if the time periods for our monitoring of hosts and services don't add up to 24 hours. Ideally, we should check and notify all our hosts and services in at least some way around the clock, but dealing with the notifications in different ways depending on schedule; for example, we could page the systems administrators about a non-critical system during work hours, but just e-mail them when they're asleep!
See also
The Automating contact rotation, Configuring notification periods, and Configuring notification groups recipes in Chapter 4, Configuring Notifications