Recurring Maint window (help with external command script)

I’ve been looking at the details on this link

nagios.org/developerinfo/ext … and_id=128

and trying to understand how to write a script that would
schedule downtime for a given host from 2AM to 3AM daily.

I understand how to cron that job to run every day and set Nagios for the scheduled downtime

but I don’t understand how the fixed epoch time values shown for
start (1110741500) and end (1110748700)
are actually recurring.

I’m familiar with how epoch time works, such that
the given examples of

start (1110741500) is:
Sun, 13 Mar 2005 19:18:20 UTC

and
end (1110748700) is:
Sun, 13 Mar 2005 21:18:20 UTC

The example shows fixed epoch times there-
But wouldn’t those times have to be adjusted in the script each time you ran it,
so that each start and end epoch time would be the correct date?

For example, I want to declare a nightly maint window for a host that goes offline for one hour
every single night from 2AM to 3AM :

I’d write a script and cron it to run at 1:30 AM every night.
If I wanted to run that script tomorrow (from when I posted this)

then as I understand the below example, the resulting external command would look like this:

SCHEDULE_HOST_DOWNTIME;MyHost.mydomain.com;1236996000;1236999600;1;0;0;me-myself-and-I;downtime for my host

That would be good for March 14th.
It would NOT be good for March 15th or afterwards.
Every day, I’d have to recalculate the start and end epoch times to tell Nagios what to do on March 15th, March 16th, and so on

Am I right here? that to use this for recurring, you’d have to recalculate values for 2AM and 3AM on each given date?
Does anyone have a script that calculates fresh epoch times for a given start time and end time, with the next days date?

I also don’t understand the meaning of the "now=‘date+%s’ "
variable in there.
There’s no reference to explain what “%s” is meant to be used for.

I understand that we’re adding the current date to the “%s”, but I don’t
have an understanding of what that accomplishes.

Does that in some way solve the date issue for start and end times?

thanks, Tim

For those who don’t want to follow that link, here’s the details from:

nagios.org/developerinfo/ext … and_id=128

Command Format:
SCHEDULE_HOST_DOWNTIME;<host_name>;<start_time>;<end_time>;;<trigger_id>;;;

Description:
Schedules downtime for a specified host.
If the “fixed” argument is set to one (1), downtime will start and end at the times specified by the “start” and “end” arguments.
Otherwise, downtime will begin between the “start” and “end” times and last for “duration” seconds.
The “start” and “end” arguments are specified in time_t format (seconds since the UNIX epoch).
The specified host downtime can be triggered by another downtime entry if the “trigger_id” is set to the ID of another scheduled downtime entry.
Set the “trigger_id” argument to zero (0) if the downtime for the specified host should not be triggered by another downtime entry.

#!/bin/sh

This is a sample shell script showing how you can submit the SCHEDULE_HOST_DOWNTIME command

to Nagios. Adjust variables to fit your environment as necessary.

now=date +%s
commandfile=’/usr/local/nagios/var/rw/nagios.cmd’

/bin/printf “%lu] SCHEDULE_HOST_DOWNTIME;host1;1110741500;1110748700;0;0;7200;Some One;Some Downtime Comment\n” $now > $commandfile

That %s is part of the date command itself… date+%s is formatting the current system date to UTC, so your getting the epochtime of when the command is run, for instance one assumes this is timestamping the command.

[root@localhost ~]# date --help Usage: date [OPTION]... +FORMAT] ... %s seconds since 1970-01-01 00:00:00 UTC ... .

You could make use of this ‘now’ figure if you run it at 1:30AM every time, why not have a variable for “startdowntime” that is “now” + 1800 for 2AM and “enddowntime” that is “startdowntime” + 3600 for 1 hour later, 3AM… then it all works it out for itself.

HTH

/S

so then…

#!/bin/sh

This is a sample shell script showing how you can submit the SCHEDULE_HOST_DOWNTIME command

to Nagios. Adjust variables to fit your environment as necessary.

We run this command 30 minutes before the window begins

at 1:30 AM.

So start and end dates should have 1800 and 3600 seconds added

startmaint=date +%s + 1800
endmaint=date +%s +3600

commandfile=’/usr/local/nagios/var/rw/nagios.cmd’

/bin/printf “%lu] SCHEDULE_HOST_DOWNTIME;myhost;$startmaint;$endmaint;1;0;0;me;myhost maint-window\n” > $commandfile

OR, the other way I understand this:

We run this command 30 minutes before the window begins

at 1:30 AM.

So start and end dates should have 1800 and 3600 seconds added

startmaint=date +%s + 1800

commandfile=’/usr/local/nagios/var/rw/nagios.cmd’

/bin/printf “%lu] SCHEDULE_HOST_DOWNTIME;myhost;$startmaint;0;0;0;3600;me;myhost maint-window\n” > $commandfile

Dunno if you can put a “0” in for end time like that.

Either way, I don’t understand the point (in the example) of having the $now command literally outside the string and having fixed time values that were back in 2005?

Hi Tim

Yes, that’s the idea… either fixed time period of an hour or a defined start and end time should work, can’t help you with getting the shell script exact I’m afraid, I tend to use perl :-S but you certainly are on the path.
The $now would more than likely appear in the log as the timestamp at the time the external command is processed… whether or not nagios goes any further in sanity-checking that the timestamp of the command is recent and that it’s not some spuriuos historical command I can’t speculate, but I suspect not. If it says in the example to put it in there though, probably best to do just that.

As for the example dates, they are exactly that, to show how such a comman may look… I guess the example was written back in 2005…

/S