Subversion Repositories nagios

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 madcat 1
#!/usr/bin/perl
2
##########################################
3
##  Author: Nic le Roux
4
##  Date: 01/07/2004
5
##  nicl@rohlig.co.za
6
##  This will schedule regular downtime for
7
##  Hosts and Service's from cfg file.
8
##
9
##     Please note !!!!!!
10
## 	The cfg file cannot have any comments
11
##	or beginning or trailing empty lines
12
##	Maybe someone could assist with that
13
##		Happy Silence
14
## Nagios is a registered trademark of Ethan Galstad.
15
###########################################
16
#use strict;
17
 
18
use warnings;
19
use Time::Local;
20
 
21
my $CfgFileSVC;		# Location of Service Downtime CFG File
22
my $CfgFileHST;		# Location of Host Downtime CFG File
23
my $EpochTime;		# current epoch time value
24
my $DayOfWeek;		# current day 0..6 Sun = 0
25
my $DownStartNormal;	# Downtime Start Normal HH:MM:SS
26
my $DownStartEpoch;	# Downtime start Epoch
27
my $DownEndNormal;	# Downtime End Normal HH:MM:SS
28
my $DownEndEpoch;	# Downtime End Epoch
29
my $ServiceName;	# Name of Scheduled Service
30
my $Hostname;		# Name of Scheduled host
31
my $DynDownDuration;	# Duration in secs for Dynamic Downtime
32
my $DownDuration;	# Duration of Downtim in Seconds
33
my $Username;		# User adding Comments
34
my $Comments;		# Comments pertaining to scheduled host/service
35
my $Fixed;		# Downtime fixed or Dynamic (0,1)
36
my $DaysOfWeek;		# Text Temp for Days of Week
37
my $WeekDay;		# Current Week Day [0..6] [Sun..Sat]
38
my @Day;		# Array in which active week days are stored
39
my $NagiosCMD;		# Location if Nagios Command File
40
 
41
###############################################
42
##  It Starts
43
###############################################
44
 
45
# Set CFG File Locations
46
my $EchoCMD = "/bin/echo";
47
$CfgFileSVC = "/etc/nagios3/sched_downtime/downtimeschedsvc.cfg";
48
$CfgFileHST = "/etc/nagios3/sched_downtime/downtimeschedhst.cfg";
49
$NagiosCMD = "/var/lib/nagios3/rw/nagios.cmd";
50
 
51
# Get current date in Epoch Format
52
$EpochTime = `date +%s`;
53
chomp($EpochTime);
54
# Get Current Week Day
55
$WeekDay = `date +%w`;
56
 
57
# Open Service CFG File
58
open(my $Config, $CfgFileSVC) or die "Could not open Config file.\n";
59
while (<$Config>){
60
	# Take off new line character
61
	chomp;
62
        s/#.*//;                # no comments
63
	s/^\s+//;               # no leading white
64
	s/\s+$//;               # no trailing white
65
	next unless length;     # anything left?
66
 
67
	# Read variable in
68
	($DaysOfWeek,$Hostname,$ServiceName,$DownStartNormal,$DownDuration,$Fixed,$DynDownDuration,$Username,$Comments)=split(/,/);
69
	($SHours, $SMinutes, $SSeconds)=split(/:/,$DownStartNormal);
70
 
71
	#Check on Which days to run Downtime
72
	#[0..6] Sun To Sat
73
	(@Day)=split(/:/,$DaysOfWeek);
74
 
75
	if ($Day[$WeekDay] == 1){
76
	   # Convert Start Downtime to Epoch Time
77
 
78
	   $DownStartEpoch = `date --date $SHours\:$SMinutes\:$SSeconds +%s`;
79
	   # Calculate end of Downtime
80
	   $DownEndEpoch = $DownStartEpoch + $DownDuration;
81
	   chomp($DownStartEpoch);
82
 
83
	   #Open  Nagios Command File to append (>>)
84
	   open(my $CMD, ">>$NagiosCMD") or die "Could not open Nagios Command Line.\n";
85
	   # Write Service Downtime entry to Nagios Command File
86
           printf $CMD "[$EpochTime] SCHEDULE_SVC_DOWNTIME;$Hostname;$ServiceName;$DownStartEpoch;$DownEndEpoch;$Fixed;0;$DynDownDuration;$Username;$Comments\n";
87
	   # Close Nagios Command File
88
	   close($CMD);
89
	}
90
}
91
 
92
close($Config);
93
 
94
# Open Host CFG File
95
open($Config, $CfgFileHST) or die "Could not open Config file.\n";
96
while (<$Config>){
97
       # Take off new line character
98
       chomp;
99
        s/#.*//;                # no comments
100
        s/^\s+//;               # no leading white
101
        s/\s+$//;               # no trailing white
102
        next unless length;     # anything left?
103
 
104
	#Read Variables in
105
	($DaysOfWeek,$Hostname,$DownStartNormal,$DownDuration,$Fixed,$DynDownDuration,$Username,$Comments)=split(/,/);
106
	($SHours, $SMinutes, $SSeconds)=split(/:/,$DownStartNormal);
107
 
108
       #Check on Which days to run Downtime
109
       #[0..6] Sun To Sat
110
       (@Day)=split(/:/,$DaysOfWeek);
111
 
112
       # Check To see if service scheduled for current day
113
       if ($Day[$WeekDay] == 1){
114
 
115
           # Convert Start Downtime to Epoch Time
116
           $DownStartEpoch = `date --date $SHours\:$SMinutes\:$SSeconds +%s`;
117
 
118
	   # Calculate end of Downtime
119
           $DownEndEpoch = $DownStartEpoch + $DownDuration;
120
           chomp($DownStartEpoch);
121
 
122
 	   #Open  Nagios Command File to append (>>)
123
           open(my $CMD, ">>$NagiosCMD") or die "Could not open Nagios Command Line.\n";
124
           # Write Service Downtime entry to Nagios Command File
125
           printf $CMD "[$EpochTime] SCHEDULE_HOST_DOWNTIME;$Hostname;$DownStartEpoch;$DownEndEpoch;$Fixed;0;$DynDownDuration;$Username;$Comments\n";
126
           # Close Nagios Command File
127
           close($CMD);
128
   	}																								 }
129
 close($Config);
130
#################################################
131
## It Ends
132
#################################################
133
 
134
#
135
# Configuration file example for scheduling recurring downtime
136
#
137
# The Relevant fields are seperated by a comma ","
138
#
139
# Please follow the Following format
140
# Sun:Mon:Tue:Wed:Thu:Fri:Sat having a 1 in the field corresponding to the day indicates that the script should run
141
#
142
# Daysfield,Hostname,StartTime(HH:MM:ss),duration(seconds),fixed/dynamic,Name(user posting downtime),Comments
143
# 0:1:1:1:1:1:0,host1.co.za,23:59:00,14400,1,7200,Nagios,Scheduled Daily Downtime (Backups)
144
#
145
 
146
#################################################
147
 
148
#
149
# Configuration file example for scheduling recurring downtime
150
#
151
# The Relevant fields are seperated by a comma ","
152
#
153
# Please follow the Following format
154
# Sun:Mon:Tue:Wed:Thu:Fri:Sat having a 1 in the field corresponding to the day indicates that the script should run
155
#
156
# Daysfield,Hostname,service-name,StartTime(HH:MM:ss),duration(seconds),fixed/dynamic,Name(user posting downtime),Comments
157
# 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)
158
#
159
 
160
#################################################
161