Subversion Repositories nagios

Rev

Blame | Last modification | View Log | RSS feed

#!/usr/bin/perl
##########################################
##  Author: Nic le Roux
##  Date: 01/07/2004
##  nicl@rohlig.co.za
##  This will schedule regular downtime for
##  Hosts and Service's from cfg file.
##  
##     Please note !!!!!!
##      The cfg file cannot have any comments
##      or beginning or trailing empty lines
##      Maybe someone could assist with that
##              Happy Silence
## Nagios is a registered trademark of Ethan Galstad.
###########################################
#use strict;

use warnings;
use Time::Local;

my $CfgFileSVC;         # Location of Service Downtime CFG File
my $CfgFileHST;         # Location of Host Downtime CFG File
my $EpochTime;          # current epoch time value
my $DayOfWeek;          # current day 0..6 Sun = 0
my $DownStartNormal;    # Downtime Start Normal HH:MM:SS
my $DownStartEpoch;     # Downtime start Epoch 
my $DownEndNormal;      # Downtime End Normal HH:MM:SS
my $DownEndEpoch;       # Downtime End Epoch
my $ServiceName;        # Name of Scheduled Service
my $Hostname;           # Name of Scheduled host
my $DynDownDuration;    # Duration in secs for Dynamic Downtime
my $DownDuration;       # Duration of Downtim in Seconds
my $Username;           # User adding Comments
my $Comments;           # Comments pertaining to scheduled host/service
my $Fixed;              # Downtime fixed or Dynamic (0,1)
my $DaysOfWeek;         # Text Temp for Days of Week
my $WeekDay;            # Current Week Day [0..6] [Sun..Sat]
my @Day;                # Array in which active week days are stored
my $NagiosCMD;          # Location if Nagios Command File

###############################################
##  It Starts
###############################################

# Set CFG File Locations
my $EchoCMD = "/bin/echo";
$CfgFileSVC = "/etc/nagios3/sched_downtime/downtimeschedsvc.cfg";
$CfgFileHST = "/etc/nagios3/sched_downtime/downtimeschedhst.cfg";
$NagiosCMD = "/var/lib/nagios3/rw/nagios.cmd";

# Get current date in Epoch Format
$EpochTime = `date +%s`;
chomp($EpochTime);
# Get Current Week Day
$WeekDay = `date +%w`;

# Open Service CFG File
open(my $Config, $CfgFileSVC) or die "Could not open Config file.\n";
while (<$Config>){
        # Take off new line character
        chomp;
        s/#.*//;                # no comments
        s/^\s+//;               # no leading white
        s/\s+$//;               # no trailing white
        next unless length;     # anything left?

        # Read variable in
        ($DaysOfWeek,$Hostname,$ServiceName,$DownStartNormal,$DownDuration,$Fixed,$DynDownDuration,$Username,$Comments)=split(/,/);
        ($SHours, $SMinutes, $SSeconds)=split(/:/,$DownStartNormal);

        #Check on Which days to run Downtime
        #[0..6] Sun To Sat
        (@Day)=split(/:/,$DaysOfWeek);
        
        if ($Day[$WeekDay] == 1){
           # Convert Start Downtime to Epoch Time
           
           $DownStartEpoch = `date --date $SHours\:$SMinutes\:$SSeconds +%s`;
           # Calculate end of Downtime
           $DownEndEpoch = $DownStartEpoch + $DownDuration;
           chomp($DownStartEpoch);

           #Open  Nagios Command File to append (>>)
           open(my $CMD, ">>$NagiosCMD") or die "Could not open Nagios Command Line.\n";
           # Write Service Downtime entry to Nagios Command File
           printf $CMD "[$EpochTime] SCHEDULE_SVC_DOWNTIME;$Hostname;$ServiceName;$DownStartEpoch;$DownEndEpoch;$Fixed;0;$DynDownDuration;$Username;$Comments\n";
           # Close Nagios Command File
           close($CMD);
        }
}

close($Config);

# Open Host CFG File
open($Config, $CfgFileHST) or die "Could not open Config file.\n";
while (<$Config>){
       # Take off new line character
       chomp;
        s/#.*//;                # no comments
        s/^\s+//;               # no leading white
        s/\s+$//;               # no trailing white
        next unless length;     # anything left?
                        
        #Read Variables in
        ($DaysOfWeek,$Hostname,$DownStartNormal,$DownDuration,$Fixed,$DynDownDuration,$Username,$Comments)=split(/,/);
        ($SHours, $SMinutes, $SSeconds)=split(/:/,$DownStartNormal);
   
       #Check on Which days to run Downtime
       #[0..6] Sun To Sat
       (@Day)=split(/:/,$DaysOfWeek);
   
       # Check To see if service scheduled for current day
       if ($Day[$WeekDay] == 1){

           # Convert Start Downtime to Epoch Time
           $DownStartEpoch = `date --date $SHours\:$SMinutes\:$SSeconds +%s`;

           # Calculate end of Downtime
           $DownEndEpoch = $DownStartEpoch + $DownDuration;
           chomp($DownStartEpoch);
 
           #Open  Nagios Command File to append (>>)
           open(my $CMD, ">>$NagiosCMD") or die "Could not open Nagios Command Line.\n";
           # Write Service Downtime entry to Nagios Command File
           printf $CMD "[$EpochTime] SCHEDULE_HOST_DOWNTIME;$Hostname;$DownStartEpoch;$DownEndEpoch;$Fixed;0;$DynDownDuration;$Username;$Comments\n";
           # Close Nagios Command File
           close($CMD);
        }                                                                                                                                                                                                }
 close($Config);
#################################################
## It Ends
#################################################

#
# Configuration file example for scheduling recurring downtime
#
# The Relevant fields are seperated by a comma ","
#
# Please follow the Following format
# Sun:Mon:Tue:Wed:Thu:Fri:Sat having a 1 in the field corresponding to the day indicates that the script should run
#
# Daysfield,Hostname,StartTime(HH:MM:ss),duration(seconds),fixed/dynamic,Name(user posting downtime),Comments
# 0:1:1:1:1:1:0,host1.co.za,23:59:00,14400,1,7200,Nagios,Scheduled Daily Downtime (Backups)
#

#################################################

#
# Configuration file example for scheduling recurring downtime
#
# The Relevant fields are seperated by a comma ","
#
# Please follow the Following format
# Sun:Mon:Tue:Wed:Thu:Fri:Sat having a 1 in the field corresponding to the day indicates that the script should run
#
# Daysfield,Hostname,service-name,StartTime(HH:MM:ss),duration(seconds),fixed/dynamic,Name(user posting downtime),Comments
# 0:1:1:1:1:1:0,host1.co.za,Host Service,23:59:00,14400,1,7200,Nic le Roux,Scheduled Daily Downtime (Backups)
#

#################################################