how to enable schedule MM for 200 hosts at a time

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
grayloglearn
Posts: 222
Joined: Thu Jul 06, 2017 8:55 am

how to enable schedule MM for 200 hosts at a time

Post by grayloglearn »

Hi Team,

We got one requirement in nagios core saying as Schedule MM for 200 hosts at a time. How we can enable this without going to host and enable schedule down time. Is there any other way to put the schedule MM for 200 host at a time.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: how to enable schedule MM for 200 hosts at a time

Post by ssax »

You would need to script it out using something like these:

https://assets.nagios.com/downloads/nag ... btnSubmit=

The easiest method would be for you to put all the hosts in a hostgroup and then use these ones:

For just the hosts in a hostgroup you would use this one:

https://assets.nagios.com/downloads/nag ... and_id=123

Then you still need to do their services so you would use this one:

https://assets.nagios.com/downloads/nag ... and_id=124

Let us know if you have any questions.
grayloglearn
Posts: 222
Joined: Thu Jul 06, 2017 8:55 am

Re: how to enable schedule MM for 200 hosts at a time

Post by grayloglearn »

Hi ,

I am not understanding above urls could you please suggest if possible. host1 and host 2 along with services need to be need to put under maintenance mode could you help us on with the script.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: how to enable schedule MM for 200 hosts at a time

Post by ssax »

This should get you going, the rest is up to you if you want any changes:

Put the hosts in a hostgroup, save, apply config.

Then use this script:

Code: Select all

#!/bin/sh
# This is a sample shell script showing how you can submit the SCHEDULE_HOSTGROUP_HOST_DOWNTIME command
# to Nagios. Adjust variables to fit your environment as necessary.

# Need to run both of these external commands to do the HOSTS and their SERVICES
# Command Format: SCHEDULE_HOSTGROUP_HOST_DOWNTIME;<hostgroup_name>;<start_time>;<end_time>;<fixed>;<trigger_id>;<duration>;<author>;<comment>
# Command Format: SCHEDULE_HOSTGROUP_SVC_DOWNTIME;<hostgroup_name>;<start_time>;<end_time>;<fixed>;<trigger_id>;<duration>;<author>;<comment>

HOSTGROUP_NAME="$1"
START_TIME="$2"
END_TIME="$3"
FIXED="$4"
TRIGGER_ID="$5"
DURATION="$6"
AUTHOR="$7"
COMMENT="$8"

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

# Submit downtime for all hosts in the hostgroup
now=`date +%s`
/bin/printf "[%lu] SCHEDULE_HOSTGROUP_HOST_DOWNTIME;$HOSTGROUP_NAME;$START_TIME;$END_TIME;$FIXED;$TRIGGER_ID;$DURATION;$AUTHOR;$COMMENT\n" $now > $commandfile

# Submit downtime for all services on the hosts in the hostgroup
now=`date +%s`
/bin/printf "[%lu] SCHEDULE_HOSTGROUP_SVC_DOWNTIME;$HOSTGROUP_NAME;$START_TIME;$END_TIME;$FIXED;$TRIGGER_ID;$DURATION;$AUTHOR;$COMMENT\n" $now > $commandfile 
Call it like this for FLEXIBLE downtime (flexible downtime begins between START_TIME and can begin at up to END_TIME, then it goes until it hits DURATION (in seconds)):

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME' START_TIME END_TIME 0 TRIGGER_ID DURATION 'AUTHOR' 'COMMENT'

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME' $(date +%s) $(date -d "now  + 15 minutes" +%s) 0 0 10800 'nagiosadmin' 'Your Comment'
OR like this for FIXED downtime (fixed downtime begins at START_TIME and goes until END_TIME):

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME' START_TIME END_TIME 1 TRIGGER_ID 0 'AUTHOR' 'COMMENT'

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME' $(date +%s) $(date -d "now  + 2 hours" +%s) 1 0 0 'nagiosadmin' 'Your Comment'
Let us know if you have any questions.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: how to enable schedule MM for 200 hosts at a time

Post by ssax »

Here is an alternative that supports passing multiple hostgroup names (comma separated):

External Command script for submitting downtimes for hosts AND their services via hostgroup:

Put the hosts in a hostgroup, save, apply config.

Then use this script:

Code: Select all

#!/bin/sh
# This is a sample shell script showing how you can submit the SCHEDULE_HOSTGROUP_HOST_DOWNTIME command
# to Nagios. Adjust variables to fit your environment as necessary.

# Need to run both of these external commands to do the HOSTS and their SERVICES
# Command Format: SCHEDULE_HOSTGROUP_HOST_DOWNTIME;<hostgroup_name>;<start_time>;<end_time>;<fixed>;<trigger_id>;<duration>;<author>;<comment>
# Command Format: SCHEDULE_HOSTGROUP_SVC_DOWNTIME;<hostgroup_name>;<start_time>;<end_time>;<fixed>;<trigger_id>;<duration>;<author>;<comment>

HOSTGROUP_NAMES="$1" # Can be a comma separated list
START_TIME="$2"
END_TIME="$3"
FIXED="$4"
TRIGGER_ID="$5"
DURATION="$6"
AUTHOR="$7"
COMMENT="$8"

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

IFS=',' read -ra HOSTGROUPS <<< "$HOSTGROUP_NAMES"
for HOSTGROUP in "${HOSTGROUPS[@]}"; do
    # Submit downtime for all hosts in the hostgroup
	now=`date +%s`
	/bin/printf "[%lu] SCHEDULE_HOSTGROUP_HOST_DOWNTIME;$HOSTGROUP;$START_TIME;$END_TIME;$FIXED;$TRIGGER_ID;$DURATION;$AUTHOR;$COMMENT\n" $now > $commandfile

	# Submit downtime for all services on the hosts in the hostgroup
	now=`date +%s`
	/bin/printf "[%lu] SCHEDULE_HOSTGROUP_SVC_DOWNTIME;$HOSTGROUP;$START_TIME;$END_TIME;$FIXED;$TRIGGER_ID;$DURATION;$AUTHOR;$COMMENT\n" $now > $commandfile
done

echo 'DONE'
exit
Call it like this for FLEXIBLE downtime (flexible downtime begins between START_TIME and can begin at up to END_TIME, then it goes until it hits DURATION (in seconds)):

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME' START_TIME END_TIME 0 TRIGGER_ID DURATION 'AUTHOR' 'COMMENT'

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME' $(date +%s) $(date -d "now  + 15 minutes" +%s) 0 0 10800 'nagiosadmin' 'Your Comment'

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME1,HOSTGROUPNAME2' $(date +%s) $(date -d "now  + 15 minutes" +%s) 0 0 10800 'nagiosadmin' 'Your Comment'
OR like this for FIXED downtime (downtime begins at START_TIME and goes until END_TIME, in this case I put in 2 hours later):

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME' START_TIME END_TIME 1 TRIGGER_ID 0 'AUTHOR' 'COMMENT'

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME' $(date +%s) $(date -d "now  + 2 hours" +%s) 1 0 0 'nagiosadmin' 'Your Comment'

Code: Select all

/path/to/your/script.sh 'HOSTGROUPNAME1,HOSTGROUPNAME2' $(date +%s) $(date -d "now  + 2 hours" +%s) 1 0 0 'nagiosadmin' 'Your Comment'
grayloglearn
Posts: 222
Joined: Thu Jul 06, 2017 8:55 am

Re: how to enable schedule MM for 200 hosts at a time

Post by grayloglearn »

Do we have simple script like placing hosts in one file and executing the script by giving the start time and end time. The above options are little confusing.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: how to enable schedule MM for 200 hosts at a time

Post by ssax »

No, that was written just for you by me, you can modify it if you'd like but this was the only other existing one I've been able to find:

https://exchange.nagios.org/directory/U ... er/details

I can submit a feature request for this on your behalf if you'd like?
grayloglearn
Posts: 222
Joined: Thu Jul 06, 2017 8:55 am

Re: how to enable schedule MM for 200 hosts at a time

Post by grayloglearn »

please suggest if possible because creating the hostgroups and assign the hosts is one task then again we should execute the script.
Instead of executing the script we can directly set the timings in console only for host groups.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: how to enable schedule MM for 200 hosts at a time

Post by ssax »

This should work for you, please read the instructions at the top:

Code: Select all

#!/bin/bash
# File should contain one host per line, this script will set downtime for the host and all of the hosts services
#
# Call it like this for FIXED DOWNTIME:
#
# FORMAT: /path/to/your/script.sh '/full/path/to/file' START_TIME END_TIME 1 TRIGGER_ID 0 'AUTHOR' 'COMMENT'
#
# EXAMPLE: /path/to/your/script.sh '/tmp/YOURFILE' $(date +%s) $(date -d "now  + 2 hours" +%s) 1 0 0 'nagiosadmin' 'Your Comment'
# - NOTE: $(date +%s) = now
# -       $(date -d "now  + 15 minutes" +%s) = 15 minutes from now
# -       You can adjust those commands to output the time values you want (or you can update the script to do it a way that you like)
#
# Call it like this for FLEXIBLE DOWNTIME:
#
# FORMAT: /path/to/your/script.sh '/full/path/to/file' START_TIME END_TIME 0 TRIGGER_ID DURATION 'AUTHOR' 'COMMENT'
#
# EXAMPLE: /path/to/your/script.sh '/tmp/YOURFILE' $(date +%s) $(date -d "now  + 15 minutes" +%s) 0 0 10800 'nagiosadmin' 'Your Comment'

FILENAME="$1"
START_TIME="$2"
END_TIME="$3"
FIXED="$4"
TRIGGER_ID="$5"
DURATION="$6"
AUTHOR="$7"
COMMENT="$8"

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

if [ ! -f "$FILENAME" ]; then
    echo "File not found!"
    exit 3
fi

readarray -t HOSTS <"$FILENAME"

for HOST in "${HOSTS[@]}"; do
   # Submit downtime for host
   now=`date +%s`
   /bin/printf "[%lu] SCHEDULE_HOST_DOWNTIME;$HOST;$START_TIME;$END_TIME;$FIXED;$TRIGGER_ID;$DURATION;$AUTHOR;$COMMENT\n" $now > $commandfile

   # Submit downtime for services
   now=`date +%s`
   /bin/printf "[%lu] SCHEDULE_HOST_SVC_DOWNTIME;$HOST;$START_TIME;$END_TIME;$FIXED;$TRIGGER_ID;$DURATION;$AUTHOR;$COMMENT\n" $now > $commandfile
done

echo 'DONE'
exit 0
grayloglearn
Posts: 222
Joined: Thu Jul 06, 2017 8:55 am

Re: how to enable schedule MM for 200 hosts at a time

Post by grayloglearn »

I tried as you said.
1) Copied your script and past in host_and_services.sh
2) added the host in /tmp/hosts which required to put mm
3) Added the start and end time
4) Executed the script, later showing Done but not applied in nagios console

Find the individual attachment so that you will come to know the what we done wrong. Excuted as below

[root@localhost ~]# ./host_and_services.sh '/tmp/hosts' 1556357400 1556368200 0 0 'nagiosadmin' 'patching'
DONE
[root@localhost ~]#
Attachments
Not applied in console
Not applied in console
Host details added in file
Host details added in file
twohosts_infile.PNG (2.52 KiB) Viewed 3598 times
Given script by team
Given script by team
Locked