[^<]+<\/A><\/td> | [^<]+<\/A><\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td>/i) {
669 |
670 | debug("Found service downtime:\n"
671 | ."Host: ".$1." Service: ".$2." EntryTime: ".$3
672 | ." User: ".$4." Comment: ".$5." Start: ".$6
673 | ." End: ".$7." Type: ".$8." Duration: ".$9
674 | ." DowntimeID: ".$10." TriggerID: ".$11);
675 |
676 | push @arr, { host => $1, service => $2, entryTime => $3, user => $4, comment => $5, start => $6, end => $7, type => $8, duration => $9, downtimeId => $10, triggerId => $11 };
677 | }
678 | }
679 | }
680 | } elsif ($type eq 'multisite') {
681 | my @downtimes = @{decode_json($oResponse->content)};
682 | my @header = @{ shift(@downtimes) }; # remove the header
683 |
684 | # header2index map
685 | my %h2i;
686 | for my $idx (0 .. $#header) {
687 | $h2i{ ${header[$idx]} } = $idx;
688 | }
689 |
690 | foreach my $row (@downtimes) {
691 | my @row = @{$row};
692 |
693 | push @arr, {
694 | host => $row[ $h2i{host} ],
695 | service => $row[ $h2i{service_description} ],
696 | entryTime => $row[ $h2i{downtime_entry_time} ],
697 | user => $row[ $h2i{downtime_author} ],
698 | comment => $row[ $h2i{downtime_comment} ],
699 | start => $row[ $h2i{downtime_start_time} ],
700 | end => $row[ $h2i{downtime_end_time} ],
701 | type => $row[ $h2i{downtime_fixed} ],
702 | duration => $row[ $h2i{downtime_duration} ],
703 | downtimeId => $row[ $h2i{downtime_id} ],
704 | origin => $row[ $h2i{downtime_origin} || 0 ],
705 | recurring => $row[ $h2i{downtime_recurring} || 0 ],
706 | triggerId => 'N/A',
707 | };
708 |
709 | if (scalar(@row) < 10) {
710 | error("The \"downtime_id\" field is missing in the \"downtime\" view. Please add it as last field.");
711 | }
712 | }
713 | }
714 |
715 | # return array of hashes
716 | return \@arr;
717 | }
718 |
719 | sub about {
720 | print <<'ABOUT';
721 | Usage:
722 | nagios_downtime [-i ] [-m add] [-H ] [-s ]
723 | [-t ] [-S ] [-p ]
724 | [-u ] [-p ] [-d]
725 | nagios_downtime -m del [-i ] [-H ] [-s ]
726 | [-S ] [-p ] [-u ]
727 | [-p ] [-d]
728 | nagios_downtime -h
729 |
730 | Nagios Downtime Script by Lars Michelsen
731 | Sends a HTTP(S) request to the nagios cgis to add a downtime for a host or
732 | service. Since version 0.5 the script can remove downtimes too when being
733 | called in "del" mode.
734 |
735 | Parameters:
736 | -i, --interface Type of interface to be used to set downtimes (nagios or
737 | multisite). Defaults to nagios. If OMD with Multisite or Check_MK is used set to multisite.
738 |
739 | -m, --mode Mode to run the script in (Available: add, del)
740 |
741 | -H, --hostname Name of the host the downtime should be scheduled for.
742 | Important: The name must be same as in Nagios.
743 | -s, --service Name of the service the downtime should be scheduled for.
744 | Important: The name must be same as in Nagios.
745 | When empty or not set a host downtime is being submited.
746 | -t, --downtime Duration of the fixed downtime in minutes
747 | -c, --comment Comment for the downtime
748 |
749 | -S, --server Nagios Webserver address (IP or DNS)
750 | -p, --path Web path to API url. In case of nagios interface, the
751 | path to the Nagios cgi-bin (Default: /nagios/cgi-bin).
752 | In case of multisite API this path should point to
753 | the base path of multisite (e.g. /check_mk or /omdsite/check_mk)
754 | -u, --user Username to be used for accessing the API
755 | -P, --password Password for accessing the API
756 |
757 | -d, --debug Enable debug mode
758 | -h, --help Show this message
759 |
760 | If you call nagios_downtime without parameters the script takes the default options which are
761 | hardcoded in the script.
762 |
763 | ABOUT
764 | }
765 |
766 | sub gettime {
767 | my $timestamp;
768 | $timestamp = shift;
769 |
770 | if($timestamp eq "") {
771 | $timestamp = time;
772 | }
773 |
774 | my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime($timestamp);
775 | # correct values
776 | $year += 1900;
777 | $month += 1;
778 |
779 | # add leading 0 to values lower than 10
780 | $month = $month < 10 ? $month = "0".$month : $month;
781 | $mday = $mday < 10 ? $mday = "0".$mday : $mday;
782 | $hour = $hour < 10 ? $hour = "0".$hour : $hour;
783 | $min = $min < 10 ? $min = "0".$min : $min;
784 | $sec = $sec < 10 ? $sec = "0".$sec : $sec;
785 |
786 | switch ($nagiosDateFormat) {
787 | case "euro" {
788 | return $mday."-".$month."-".$year." ".$hour.":".$min.":".$sec;
789 | }
790 | case "us" {
791 | return $month."-".$mday."-".$year." ".$hour.":".$min.":".$sec;
792 | }
793 | case "iso8601" {
794 | return $year."-".$month."-".$mday." ".$hour.":".$min.":".$sec;
795 | }
796 | case "strict-iso8601" {
797 | return $year."-".$month."-".$mday."T".$hour.":".$min.":".$sec;
798 | }
799 | else {
800 | error("No valid date format given in \$nagiosDateFormat");
801 | }
802 | }
803 | }
804 |
805 | # #############################################################
806 | # EOF
807 | # #############################################################
808 |
--------------------------------------------------------------------------------
/nagios_downtime.conf-sample:
--------------------------------------------------------------------------------
1 | $type = "multisite";
2 | $server = "monitoring-host.local";
3 | $basePath = "/sysmon/check_mk";
4 |
5 | $user = "svc-downtime";
6 | $userPw = "test-password";
7 |
8 | $hostname = hostname;
9 |
--------------------------------------------------------------------------------
/nagios_downtime.init:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # Copyright (c) 2008-2009 Lars Michelsen
3 | # All rights reserved.
4 | #
5 | # Author: Lars Michelsen
6 | #
7 | # chkconfig: 345 99 01
8 | # description: Schedules a downtime in Nagios
9 | #
10 | ### BEGIN INIT INFO
11 | # Provides: nagios_downtime
12 | # Required-Start: $network
13 | # Required-Stop:
14 | # Default-Start: 3 4 5
15 | # Default-Stop: 0 1 2 6
16 | # Short-Description: nagios_downtime schedules a downtime to Nagios
17 | # Description: The nagios_downtime script schedules a downtime
18 | # to the specified Nagios host. The downtime script needs HTTP-access
19 | # to the Nagios cmd.cgi to be able to post a downtime.
20 | ### END INIT INFO
21 |
22 | BAR_BIN=/usr/bin/nagios_downtime
23 | #BAR_BIN=./nagios_downtime
24 |
25 | # Source function library, for e.g. echo_success or echo_failure
26 | if [ -f /etc/rc.d/init.d/functions ]; then
27 | . /etc/rc.d/init.d/functions
28 | else
29 | echo_success() {
30 | return 0
31 | }
32 | echo_failure() {
33 | return 1
34 | }
35 | fi
36 |
37 | # Check for missing binaries
38 | test -x $BAR_BIN || {
39 | echo "$BAR_BIN not installed";
40 | if [ "$1" = "stop" ]; then
41 | exit 0;
42 | else
43 | exit 5;
44 | fi;
45 | }
46 |
47 | case "$1" in
48 | stop)
49 | echo -n "Scheduling Nagios downtime (nagios_downtime)... "
50 | $BAR_BIN
51 |
52 | if [ $? -eq 0 ]; then
53 | if [ -f /var/lock/subsys/nagios_downtime ]; then
54 | rm -f /var/lock/subsys/nagios_downtime
55 | fi
56 | echo_success
57 | echo "done."
58 | exit 0
59 | else
60 | if [ -f /var/lock/subsys/nagios_downtime ]; then
61 | rm -f /var/lock/subsys/nagios_downtime
62 | fi
63 | echo_failure
64 | echo "ERROR"
65 | exit 1
66 | fi
67 | ;;
68 | start)
69 | echo -n "Removing Nagios downtime (nagios_downtime)... "
70 | $BAR_BIN -m del
71 |
72 | if [ $? -eq 0 ]; then
73 | if [ -d /var/lock/subsys ]; then
74 | touch /var/lock/subsys/nagios_downtime
75 | fi
76 | echo_success
77 | echo "done."
78 | exit 0
79 | else
80 | if [ -d /var/lock/subsys ]; then
81 | touch /var/lock/subsys/nagios_downtime
82 | fi
83 | echo_failure
84 | echo "ERROR"
85 | exit 1
86 | fi
87 | ;;
88 | status)
89 | echo -n "Script only running while start or stop the System..."
90 | ;;
91 | *)
92 | # If no parameters are given, print which are avaiable
93 | echo "Usage: $0 {stop|start}"
94 | exit 1
95 | ;;
96 | esac
97 |
--------------------------------------------------------------------------------
/nagios_downtime.vbs:
--------------------------------------------------------------------------------
1 | ' ##############################################################################
2 | ' nagios_downtime.vbs
3 | '
4 | ' Copyright (c) 2005-2015 Lars Michelsen
5 | ' http://larsmichelsen.com/
6 | '
7 | ' Permission is hereby granted, free of charge, to any person
8 | ' obtaining a copy of this software and associated documentation
9 | ' files (the "Software"), to deal in the Software without
10 | ' restriction, including without limitation the rights to use,
11 | ' copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | ' copies of the Software, and to permit persons to whom the
13 | ' Software is furnished to do so, subject to the following
14 | ' conditions:
15 | '
16 | ' The above copyright notice and this permission notice shall be
17 | ' included in all copies or substantial portions of the Software.
18 | '
19 | ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 | ' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 | ' OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 | ' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 | ' HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 | ' WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 | ' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 | ' OTHER DEALINGS IN THE SOFTWARE.
27 | ' ##############################################################################
28 | ' SCRIPT: nagios_downtime
29 | ' AUTHOR: Lars Michelsen
30 | ' DECRIPTION: Sends a HTTP(S)-GET to the nagios web server to
31 | ' enter a downtime for a host or service.
32 | ' CHANGES:
33 | ' 2005-07-11 v0.1 First creation of the script
34 | ' |
35 | ' |
36 | ' changes not tracked in details
37 | ' |
38 | ' V
39 | ' 2006-03-22 v0.6 - Added basic auth support
40 | ' - Several doc changes
41 | '
42 | ' 2009-10-20 v0.7 - Complete recode according to current perl script
43 | ' - Reworked command line parameters
44 | ' - Script can handle different Nagios date formats now
45 | ' - Script can now delete downtimes when downtime id has been
46 | ' saved while scheduling the downtime before
47 | '
48 | ' 2009-11-04 v0.8 - Default http/https ports are not added to the url anymore
49 | ' - Added option to ignore certificate problems
50 | ' - Fixed problem deleting service downtimes
51 | '
52 | ' 2010-12-02 v0.8.1 - Fixed / in to \ in path definitions (Thx Ronny Bunke)
53 | ' - Modify Messages for Icinga (Thx Ronny Bunke)
54 | '
55 | ' 2011-01-26 v0.8.2 - Applied changes to better handle nagios response texts.
56 | ' Might fix a problem with deleting downtims (Thx Rob Sampson)
57 | ' 2012-03-14 v0.8.3 Big thanks to Olaf Morgenstern for the following points:
58 | ' - Added the logEvt procedure to write messages to the
59 | ' Windows eventlog
60 | ' - Added the -e switch to enable Windows event logging
61 | ' - Added logEvt calls for relevant Wscript.echo's
62 | ' - Better handling of expired downtimes: If the downtime was
63 | ' not found on the nagios server, delete it from the local
64 | ' downtime ID savefile. To prevent from deleting downtimes
65 | ' on nagios server errors, the procedure getNagiosDowntimeId
66 | ' was changed to exit the script on errors.
67 | ' - Added the cleanupDowntimeIds procedure to cleanup the
68 | ' internal downtime ID savefile
69 | ' - Added an additional "clean" mode
70 | ' - Reindented code to 4 spaces for each level
71 | ' 2012-03-22 v0.8.4 Again thanks to Olaf Morgenstern for improving the script
72 | ' - Added clean mode to some remarks and an error message
73 | ' - Changed about() function to show "cscript WScript.ScriptName"
74 | ' - Corrected indenting of one line
75 | ' - In del mode, print more details if downtime could not be
76 | ' found on nagios server
77 | ' 2012-12-05 v0.9 - Added support for scheduling downtimes via the JSON API of
78 | ' Check_MK Multisite
79 | ' - Internal recode of variables
80 | ' 2013-07-31 v0.10 - Added support for deleting downtimes via Check_MK multisite
81 | ' 2015-08-25 v0.11 - Fixed downtime deletion with Check_MK newer than 1.2.7
82 | ' ##############################################################################
83 |
84 | Option Explicit
85 |
86 | Dim ty, webProto, server, webServer, webPort, basePath
87 | Dim user, userPw, authName, nagiosDateFormat, proxyAddress
88 | Dim storeDowntimeIds, downtimePath, downtimeId, downtimeType, downtimeDuration
89 | Dim downtimeComment, debug, version, ignoreCertProblems, evtlog
90 |
91 | ' ##############################################################################
92 | ' Configuration (-> Here you have to set some values!)
93 | ' ##############################################################################
94 |
95 | ' Interface/API to use to set downtimes. Can be set to "nagios" to use the
96 | ' default CGI based webinterface or "multisite" to set downtimes via Check_MK
97 | ' Multisite
98 | ty = "nagios"
99 |
100 | ' Protocol for the GET Request, In most cases "http", "https" is also possible
101 | webProto = "http"
102 | ' IP or FQDN of Nagios server (example: nagios.domain.de)
103 | server = "localhost"
104 | ' IP or FQDN of Nagios web server. In most cases same as $server, if
105 | ' empty automaticaly using $server
106 | webServer = ""
107 | ' Port of Nagios webserver
108 | ' This option is only being recognized when it is not the default port for the
109 | ' choosen protocol in "webProto" option
110 | webPort = 80
111 | ' Web path to Nagios cgi-bin (example: /nagios/cgi-bin) (NO trailing slash!)
112 | ' In case of Icinga this would be "/icinga/cgi-bin" by default
113 | ' Or if type is multisite, path to multisite (e.g. /check_mk)
114 | basePath = "/nagios/cgi-bin"
115 |
116 | ' User to take for authentication and author to enter the downtime (example:
117 | ' nagiosadmin). In case of Icinga this would be "icingaadmin" by default
118 | user = "nagiosadmin"
119 | ' Password for above user
120 | userPw = "nagiosadmin"
121 | ' Name of authentication realm, set in the Nagios .htaccess file
122 | ' (example: "Nagios Access")
123 | authName = "Nagios Access"
124 |
125 | '
126 | ' Nagios CGI specific options
127 | ' (only needed when $ty is set to "nagios")
128 | '
129 | ' Nagios date format (same like set in value "date_format" in nagios.cfg)
130 | nagiosDateFormat = "us"
131 |
132 | ' When you have to use a proxy server for access to the nagios server, set the
133 | ' URL here. The proxy will be set for this script for the choosen web protocol
134 | ' When this is set to 'env', the proxy settings will be read from IE settings
135 | ' When this is set to '', the script will use a direct connection
136 | proxyAddress = ""
137 | ' When using ssl it may be ok for you to ignore untrusted/expired certificats
138 | ' Setting this to 1 all ssl certificate related problems should be ignored
139 | ignoreCertProblems = 0
140 |
141 | ' Enable fetching and storing the downtime ids for later downtime removal
142 | ' The downtime IDs will be stored in a defined temp directory
143 | storeDowntimeIds = 1
144 | ' The script will generate temporary files named (.txt or
145 | ' -.txt). The files will contain the script internal
146 | ' downtime ids and/or the nagios downtime ids.
147 | ' These files are needed for later downtime removal
148 | downtimePath = "%temp%"
149 |
150 | ' Some default options (Usualy no changes needed below this)
151 |
152 | ' Script internal downtime id for a new downtime
153 | ' Using the current timestamp as script internal downtime identifier
154 | ' Not important to have the real timestamp but having a uniq counter
155 | ' which increases
156 | downtimeId = CLng(DateDiff("s", "01/01/1970 00:00:00", Now) - 3600)
157 | ' Default downtime type (1: Host Downtime, 2: Service Downtime)
158 | downtimeType = 1
159 | ' Default Downtime duration in minutes
160 | downtimeDuration = 10
161 | ' Default Downtime text
162 | downtimeComment = "Downtime-Script"
163 | ' Default mode for Windows event logging: off => 0 or on => 1
164 | evtlog = 0
165 | ' Default Debugmode: off => 0 or on => 1
166 | debug = 0
167 | ' Script version
168 | version = "0.10"
169 |
170 | ' ##############################################################################
171 | ' Don't change anything below, except you know what you are doing.
172 | ' ##############################################################################
173 |
174 | Dim arg, p, i, oBrowser, oResponse, hostname, service, timeStart, timeEnd, url
175 | Dim help, timeNow, timezone, mode, oFs, oFile, oNetwork, oShell, baseUrl, params
176 |
177 | Const HTTPREQUEST_PROXYSETTING_PRECONFIG = 0
178 | Const HTTPREQUEST_PROXYSETTING_DIRECT = 1
179 | Const HTTPREQUEST_PROXYSETTING_PROXY = 2
180 | Const FOR_READING = 1
181 | Const FOR_WRITING = 2
182 | Const FOR_APPENDING = 8
183 | Const CREATE_IF_NOT_EXISTS = True
184 | Const HTTPREQUEST_SSLERROR_IGNORE_FLAG = 4
185 | Const HTTPREQUEST_SECURITY_IGNORE_ALL = 13056
186 |
187 | ' Constants for type of event log entry
188 | const EVENTLOG_SUCCESS = 0
189 | const EVENTLOG_ERROR = 1
190 | const EVENTLOG_WARNING = 2
191 | const EVENTLOG_INFORMATION = 4
192 | const EVENTLOG_AUDIT_SUCCESS = 8
193 | const EVENTLOG_AUDIT_FAILURE = 16
194 |
195 | Set oShell = CreateObject("WScript.Shell")
196 | Set oFS = CreateObject("Scripting.FilesystemObject")
197 |
198 | hostname = ""
199 | service = ""
200 | url = ""
201 | timeNow = Now
202 | timezone = "local"
203 | mode = "add"
204 | help = 0
205 |
206 | Dim urls
207 | Set urls = CreateObject("Scripting.Dictionary")
208 | urls.Add "nagios", CreateObject("Scripting.Dictionary")
209 | urls.Item("nagios").Add "host_downtime", "[baseUrl]/cmd.cgi?cmd_typ=55&cmd_mod=2" & _
210 | "&host=[hostname]&com_author=[user]&com_data=[comment]" & _
211 | "&trigger=0&start_time=[start_time]&end_time=[end_time]" & _
212 | "&fixed=1&childoptions=1&btnSubmit=Commit"
213 | urls.Item("nagios").Add "service_downtime", "[baseUrl]/cmd.cgi?cmd_typ=56&cmd_mod=2" & _
214 | "&host=[hostname]&service=[service]" & _
215 | "&com_author=[user]&com_data=[comment]" & _
216 | "&trigger=0&start_time=[start_time]&end_time=[end_time]" & _
217 | "&fixed=1&btnSubmit=Commit"
218 | urls.Item("nagios").Add "del_host_downtime", "[baseUrl]/cmd.cgi?cmd_typ=78&cmd_mod=2&down_id=[downtime_id]&btnSubmit=Commit"
219 | urls.Item("nagios").Add "del_service_downtime", "[baseUrl]/cmd.cgi?cmd_typ=79&cmd_mod=2&down_id=[downtime_id]&btnSubmit=Commit"
220 | urls.Item("nagios").Add "all_downtimes", "[baseUrl]/extinfo.cgi?type=6"
221 |
222 | urls.Add "multisite", CreateObject("Scripting.Dictionary")
223 | urls.Item("multisite").Add "host_downtime", "[baseUrl]/view.py?output_format=json&_transid=-1&" & _
224 | "_do_confirm=yes&_do_actions=yes&_username=[user]&_secret=[password]&" & _
225 | "view_name=hoststatus&host=[hostname]&_down_comment=[comment]&" & _
226 | "_down_from_now=yes&_down_minutes=[duration]"
227 | urls.Item("multisite").Add "service_downtime", "[baseUrl]/view.py?output_format=json&_transid=-1&" & _
228 | "_do_confirm=yes&_do_actions=yes&_username=[user]&_secret=[password]&" & _
229 | "view_name=service&host=[hostname]&service=[service]&" & _
230 | "_down_comment=[comment]&_down_from_now=yes&_down_minutes=[duration]"
231 | urls.Item("multisite").Add "del_host_downtime", "[baseUrl]/view.py?output_format=json&_transid=-1" & _
232 | "&_do_confirm=yes&_do_actions=yes&_username=[user]&_secret=[password]" & _
233 | "&view_name=api_downtimes&_remove_downtimes=Remove&downtime_id=[downtime_id]"
234 | urls.Item("multisite").Add "del_service_downtime", "[baseUrl]/view.py?output_format=json&_transid=-1" & _
235 | "&_do_confirm=yes&_do_actions=yes&_username=[user]&_secret=[password]" & _
236 | "&view_name=api_downtimes&_remove_downtimes=Remove&downtime_id=[downtime_id]"
237 | urls.Item("multisite").Add "all_downtimes", "[baseUrl]/view.py?output_format=json&_transid=-1" & _
238 | "&_do_confirm=yes&_do_actions=yes&_username=[user]&_secret=[password]" & _
239 | "&view_name=api_downtimes"
240 |
241 | Dim messages
242 | Set messages = CreateObject("Scripting.Dictionary")
243 | messages.Add "nagios", CreateObject("Scripting.Dictionary")
244 | messages.Item("nagios").Add "success", "Your command request was successfully submitted to Nagios for processing"
245 | messages.Item("nagios").Add "not_authorized", "Sorry, but you are not authorized to commit the specified command."
246 | messages.Item("nagios").Add "no_author", "Author was not entered"
247 |
248 | messages.Add "multisite", CreateObject("Scripting.Dictionary")
249 | messages.Item("multisite").Add "success", "Successfully sent 1 commands"
250 | messages.Item("multisite").Add "not_authorized", "Invalid automation secret"
251 | messages.Item("multisite").Add "no_author", "TODO"
252 |
253 | ' Read all params
254 | i = 0
255 | Do While i < Wscript.Arguments.Count
256 | If WScript.Arguments(i) = "/H" or WScript.Arguments(i) = "-H" or UCase(WScript.Arguments(i)) = "-HOSTNAME" or UCase(WScript.Arguments(i)) = "/HOSTNAME" then
257 | ' Hostname: /H, /hostname, -H, -hostname
258 | i = i + 1
259 |
260 | If i < Wscript.Arguments.Count Then
261 | hostname = WScript.Arguments(i)
262 | Else
263 | err "No hostname given"
264 | End If
265 | ElseIf WScript.Arguments(i) = "/i" or WScript.Arguments(i) = "-i" or UCase(WScript.Arguments(i) = "-INTERFACE") or UCase(WScript.Arguments(i)) = "/INTERFACE" then
266 | ' Type: /i, /interface, -i, -interface
267 | i = i + 1
268 |
269 | ty = WScript.Arguments(i)
270 |
271 | ElseIf WScript.Arguments(i) = "/m" or WScript.Arguments(i) = "-m" or UCase(WScript.Arguments(i) = "-MODE") or UCase(WScript.Arguments(i)) = "/MODE" then
272 | ' Mode: /m, /mode, -m, -mode
273 | i = i + 1
274 |
275 | mode = WScript.Arguments(i)
276 | ElseIf WScript.Arguments(i) = "/S" or WScript.Arguments(i) = "-S" or UCase(WScript.Arguments(i)) = "/SERVER" or UCase(WScript.Arguments(i)) = "-SERVER" then
277 | ' Nagios Server: /S, /server, -S, -server
278 | i = i + 1
279 |
280 | server = WScript.Arguments(i)
281 | ElseIf WScript.Arguments(i) = "/p" or WScript.Arguments(i) = "-p" or UCase(WScript.Arguments(i)) = "/PATH" or UCase(WScript.Arguments(i)) = "-PATH" then
282 | ' Nagios CGI Path: /p, /path, -p, -path
283 | i = i + 1
284 |
285 | basePath = WScript.Arguments(i)
286 | ElseIf WScript.Arguments(i) = "/u" or WScript.Arguments(i) = "-u" or UCase(WScript.Arguments(i)) = "/USER" or UCase(WScript.Arguments(i)) = "-USER" then
287 | ' Nagios User: /u, /user, -u, -user
288 | i = i + 1
289 |
290 | user = WScript.Arguments(i)
291 | ElseIf WScript.Arguments(i) = "/P" or WScript.Arguments(i) = "-P" or UCase(WScript.Arguments(i)) = "/PASSWORD" or UCase(WScript.Arguments(i)) = "-PASSWORD" then
292 | ' Nagios Password: /P, /password, -P, -password
293 | i = i + 1
294 |
295 | userPw = WScript.Arguments(i)
296 | ElseIf WScript.Arguments(i) = "/s" or WScript.Arguments(i) = "-s" or UCase(WScript.Arguments(i)) = "/SERVICE" or UCase(WScript.Arguments(i)) = "-SERVICE" then
297 | ' Servicename: /s, /service, -s, -service
298 | i = i + 1
299 |
300 | service = WScript.Arguments(i)
301 | ElseIf WScript.Arguments(i) = "/t" or WScript.Arguments(i) = "-t" or UCase(WScript.Arguments(i)) = "/DOWNTIME" or UCase(WScript.Arguments(i)) = "-DOWNTIME" Then
302 | ' downtime duration: /t, /downtime, -t, -downtime
303 | i = i + 1
304 |
305 | downtimeDuration = WScript.Arguments(i)
306 | ElseIf WScript.Arguments(i) = "/c" or WScript.Arguments(i) = "-c" or UCase(WScript.Arguments(i)) = "/COMMENT" or UCase(WScript.Arguments(i)) = "-COMMENT" Then
307 | ' downtime comment: /c, /comment, -c, -comment
308 | i = i + 1
309 |
310 | downtimeComment = WScript.Arguments(i)
311 |
312 | ElseIf UCase(WScript.Arguments(i)) = "/E" or UCase(WScript.Arguments(i)) = "-E" or UCase(WScript.Arguments(i)) = "/EVTLOG" or UCase(WScript.Arguments(i)) = "-EVTLOG" Then
313 | ' log to Window event log: /e, -e, /evtlog, -evtlog
314 | evtlog = 1
315 |
316 | ElseIf UCase(WScript.Arguments(i)) = "/D" or UCase(WScript.Arguments(i)) = "-D" or UCase(WScript.Arguments(i)) = "/DEBUG" or UCase(WScript.Arguments(i)) = "-DEBUG" Then
317 | ' debug mode: /d, -d, /debug, -debug
318 | debug = 1
319 | ElseIf WScript.Arguments(i) = "/?" or WScript.Arguments(i) = "-?" or WScript.Arguments(i) = "/h" or WScript.Arguments(i) = "-h" or WScript.Arguments(i) = "-help" or WScript.Arguments(i) = "/help" Then
320 | ' help: /?, /h, /help, -?, -h, -help
321 | help = 1
322 | Else
323 | ' ....
324 | End If
325 |
326 | i = i + 1
327 | Loop
328 |
329 | ' Read optional config
330 | If oFS.FileExists("nagios_downtime.vbs.conf") Then
331 | ExecuteGlobal(oFS.OpenTextFile("nagios_downtime.vbs.conf").ReadAll())
332 | End If
333 |
334 | If help = 1 Then
335 | Call about()
336 | WScript.Quit(1)
337 | End If
338 |
339 | ' Mode can be add, del or clean, default is "add"
340 | If mode = "" Then
341 | mode = "add"
342 | End If
343 |
344 | ' Get hostname if not set via param
345 | If hostname = "" Then
346 | ' Read the hostname
347 | Set oNetwork = WScript.CreateObject("WScript.Network")
348 | hostname = LCase(oNetwork.ComputerName)
349 | End If
350 |
351 | ' When no nagios webserver is set the webserver and Nagios should be on the same
352 | ' host
353 | If webServer = "" Then
354 | webServer = server
355 | End If
356 |
357 | ' When a service name is set, this will be a service downtime
358 | If service <> "" Then
359 | downtimeType = 2
360 | End If
361 |
362 | ' Initialize the port to be added to the url. If default http port (80) or
363 | ' default ssl port don't add anything
364 | If webProto = "http" And webPort = 80 Then
365 | webPort = ""
366 | ElseIf webProto = "https" And webPort <> 443 Then
367 | webPort = ""
368 | Else
369 | webPort = ":" & webPort
370 | End If
371 |
372 | ' Append the script internal downtime id when id storing is enabled
373 | ' The downtime ID is important to identify the just scheduled downtime for
374 | ' later removal. The CGIs do not provide the downtime id right after sending
375 | ' the schedule request. So it is important to tag the downtime with this.
376 | If storeDowntimeIds = 1 Then
377 | downtimeComment = downtimeComment & " (ID:" & downtimeId & ")"
378 | End If
379 |
380 | ' Expand the environment string in downtime path
381 | If storeDowntimeIds = 1 Then
382 | downtimePath = oShell.ExpandEnvironmentStrings(downtimePath)
383 | End If
384 |
385 | ' Calculate the start of the downtime
386 | timeStart = gettime(timeNow)
387 |
388 | ' Calculate the end of the downtime
389 | timeEnd = gettime(DateAdd("n", downtimeDuration, timeNow))
390 |
391 | ' Check if Nagios web server is reachable via ping, if not, terminate the script
392 | If PingTest(webServer) Then
393 | err "Given Nagios web server """ & webServer & """ not reachable via ping!"
394 | End If
395 |
396 | ' Initialize the browser
397 | Set oBrowser = CreateObject("WinHttp.WinHttpRequest.5.1")
398 |
399 | ' Set the proxy address depending on the configured option
400 | If proxyAddress = "env" Then
401 | oBrowser.SetProxy HTTPREQUEST_PROXYSETTING_PRECONFIG
402 | dbg "Proxy-Mode: Env (" & HTTPREQUEST_PROXYSETTING_PRECONFIG & ")"
403 |
404 | ElseIf proxyAddress = "" Then
405 | oBrowser.SetProxy HTTPREQUEST_PROXYSETTING_DIRECT
406 | dbg "Proxy-Mode: Direct (" & HTTPREQUEST_PROXYSETTING_DIRECT & ")"
407 |
408 | Else
409 | oBrowser.SetProxy HTTPREQUEST_PROXYSETTING_PROXY, proxyAddress
410 | dbg "Proxy-Mode: Proxy (" & HTTPREQUEST_PROXYSETTING_PROXY & "): " & proxyAddress
411 | End If
412 |
413 | ' When enabled ignore all certificate problems
414 | If ignoreCertProblems = 1 Then
415 | oBrowser.Option(HTTPREQUEST_SSLERROR_IGNORE_FLAG) = HTTPREQUEST_SECURITY_IGNORE_ALL
416 | End If
417 |
418 | baseUrl = webProto & "://" & webServer & webPort & basePath
419 |
420 | ' Handle the given action
421 | Select Case mode
422 | Case "add"
423 | ' Add a new scheduled downtime
424 | ' ##########################################################################
425 | Set params = CreateObject("Scripting.Dictionary")
426 | params.add "baseUrl", baseUrl
427 | params.add "hostname", hostname
428 | params.add "user", user
429 | params.add "password", userPw
430 | params.add "comment", downtimeComment
431 | params.add "start_time", timeStart
432 | params.add "end_time", timeEnd
433 | params.add "duration", downtimeDuration
434 |
435 | If downtimeType = 1 Then
436 | ' Schedule Host Downtime
437 | url = get_url("host_downtime", params)
438 | Else
439 | ' Schedule Service Downtime
440 | params.add "service", service
441 | url = get_url("service_downtime", params)
442 | End If
443 |
444 | dbg "HTTP-GET: " & url
445 |
446 | oBrowser.Open "GET", url
447 | setBrowserOptions()
448 | oBrowser.Send
449 |
450 | dbg "HTTP-Response (" & oBrowser.Status & "): " & oBrowser.ResponseText
451 |
452 | ' Handle response code, not in detail, only first char
453 | Select Case Left(oBrowser.Status, 1)
454 | ' 2xx response code is OK
455 | Case 2
456 | If InStr(oBrowser.ResponseText, get_msg("success")) > 0 Then
457 |
458 | ' Save the id of the just scheduled downtime
459 | If storeDowntimeIds = 1 Then
460 | saveDowntimeId()
461 | log EVENTLOG_SUCCESS, "OK: Downtime was submitted successfully"
462 | WScript.Quit(0)
463 | Else
464 | log EVENTLOG_INFORMATION, "Downtime IDs are not set to be stored"
465 | WScript.Quit(1)
466 | End If
467 | ElseIf InStr(oBrowser.ResponseText, get_msg("not_authorized")) > 0 Then
468 | err "Maybe not authorized or wrong host- or servicename"
469 |
470 | ElseIf InStr(oBrowser.ResponseText, get_msg("no_author")) > 0 Then
471 | err "No Author entered, define Author in user var"
472 |
473 | Else
474 | err "Some undefined error occured, turn debug mode on to view what happened"
475 | End If
476 | Case 3
477 | err "HTTP Response code 3xx says ""moved url"" (" & oBrowser.Status & ")"
478 | Case 4
479 | err "HTTP Response code 4xx says ""client error"" (" & oBrowser.Status & ")" & _
480 | "Hint: This could be caused by wrong auth credentials and/or datetime settings in this script"
481 | Case 5
482 | err "HTTP Response code 5xx says ""server Error"" (" & oBrowser.Status & ")"
483 | Case Else
484 | err "HTTP Response code unhandled by script (" & oBrowser.Status & ")"
485 | End Select
486 | Case "del"
487 | ' Delete the last scheduled downtime
488 | ' ##########################################################################
489 |
490 | If storeDowntimeIds <> 1 Then
491 | err "Unable to remove a downtime. The storingDowntimeIds option is set to disabled."
492 | End If
493 |
494 | ' Read all internal downtime ids for this host/service
495 | Dim aDowntimes
496 | aDowntimes = getDowntimeIds()
497 |
498 | ' Only proceed when downtimes found
499 | If UBound(aDowntimes)+1 > 0 Then
500 | ' Sort downtimes (lowest number at top)
501 | aDowntimes = bubblesort(aDowntimes)
502 |
503 | dbg "Trying to delete with internal downtime id: " & aDowntimes(0)
504 |
505 | ' Get the nagios downtime id for the last scheduled downtime
506 | Dim nagiosDowntimeId
507 | nagiosDowntimeId = getNagiosDowntimeId(aDowntimes(0))
508 |
509 | dbg "Translated downtime id: " & aDowntimes(0) & "(internal) => " & nagiosDowntimeId & " (Nagios)"
510 |
511 | If nagiosDowntimeId <> "" Then
512 | deleteDowntime(nagiosDowntimeId)
513 | End If
514 |
515 | ' We can safely delete the downtime from the list of saved downtimes,
516 | ' because getNagiosDowntimeId(aDowntimes(0)) will exit the script if
517 | ' it can't get the downtimes from the nagios server.
518 | delDowntimeId(aDowntimes(0))
519 | Else
520 | err "Unable to remove a downtime. No previously scheduled downtime found."
521 | End If
522 | Case "clean"
523 | ' Cleanup the stored downtime ids
524 | ' ##########################################################################
525 | dbg "Cleanup mode selected."
526 | cleanupDowntimeIds
527 | Case Else
528 | err "Unknown mode was set (Available: add, del, clean)"
529 | WScript.Quit(1)
530 | End Select
531 |
532 | Set oBrowser = Nothing
533 | Set oShell = Nothing
534 | Set oFile = Nothing
535 | Set oFS = Nothing
536 |
537 | ' Regular end of script
538 | ' ##############################################################################
539 |
540 | ' #############################################################
541 | ' Subs
542 | ' #############################################################
543 |
544 | sub dbg(msg)
545 | If debug = 1 Then
546 | log EVENTLOG_INFORMATION, msg
547 | End If
548 | End Sub
549 |
550 | sub err(msg)
551 | log EVENTLOG_ERROR, "ERROR: " & msg
552 | WScript.Quit(1)
553 | End Sub
554 |
555 | Sub log(logType, msg)
556 | WScript.echo msg
557 | If evtlog = 1 Then
558 | oShell.LogEvent logType, WScript.ScriptName & ":" & VBCRLF & msg
559 | End If
560 | End Sub
561 |
562 | Sub setBrowserOptions()
563 | oBrowser.SetRequestHeader "User-Agent", "nagios_downtime.vbs / " & version
564 |
565 | dbg "User-Agent: " & "nagios_downtime.vbs / " & version
566 |
567 | ' Only try to auth if auth informations are given
568 | If authName <> "" And userPw <> "" Then
569 |
570 | dbg "Nagios Auth: " & authName
571 | dbg "Nagios User: " & user
572 | dbg "Nagios Password: " & userPw
573 |
574 | ' Set the login information (0: Server auth / 1: Proxy auth)
575 | oBrowser.SetCredentials user, userPw, 0
576 | End If
577 | End Sub
578 |
579 | Function get_url(key, params)
580 | url = urls.Item(ty).Item(key)
581 | Dim k
582 | For Each k In params.Keys
583 | url = Replace(url, "[" & k & "]", params.Item(k))
584 | Next
585 | get_url = url
586 | End Function
587 |
588 | Function get_msg(key)
589 | get_msg = messages.Item(ty).Item(key)
590 | End Function
591 |
592 | Function bubblesort(arrSort)
593 | Dim i, j, arrTemp
594 | For i = 0 to UBound(arrSort)
595 | For j = i + 1 to UBound(arrSort)
596 | If arrSort(i) < arrSort(j) Then
597 | arrTemp = arrSort(i)
598 | arrSort(i) = arrSort(j)
599 | arrSort(j) = arrTemp
600 | End If
601 | Next
602 | Next
603 | bubblesort = arrSort
604 | End Function
605 |
606 |
607 | Sub about()
608 | WScript.echo "Usage:" & vbcrlf & vbcrlf & _
609 | "cscript " & WScript.ScriptName & " [-i ] [-m add] [-H ] [-s ] [-t ]" & vbcrlf & _
610 | " [-S ] [-p ] [-u ]" & vbcrlf & _
611 | " [-p ] [-e] [-d]" & vbcrlf & _
612 | "cscript " & WScript.ScriptName & " -m del [-i ] [-H ] [-s ] [-S ]" & vbcrlf & _
613 | " [-p ] [-u ] [-p ] [-e] [-d]" & vbcrlf & _
614 | "cscript " & WScript.ScriptName & " -m clean [-i ] [-H ] [-s ] [-S ]" & vbcrlf & _
615 | " [-p ] [-u ] [-p ] [-e] [-d]" & vbcrlf & _
616 | "cscript " & WScript.ScriptName & " -h" & vbcrlf & _
617 | "" & vbcrlf & _
618 | "Nagios Downtime Script by Lars Michelsen " & vbcrlf & _
619 | "Sends a HTTP(S) request to the nagios cgis to add a downtime for a host or" & vbcrlf & _
620 | "service. Since version 0.7 the script can remove downtimes too when being" & vbcrlf & _
621 | "called in ""del"" mode." & vbcrlf & _
622 | "" & vbcrlf & _
623 | "Parameters:" & vbcrlf & _
624 | " -i, --interface Type of interface to be used to set downtimes (nagios or" & vbcrlf & _
625 | " multisite). Defaults to nagios." & vbcrlf & _
626 | " -m, --mode Mode to run the script in (Available: add, del, clean)" & vbcrlf & _
627 | "" & vbcrlf & _
628 | " -H, --hostname Name of the host the downtime should be scheduled for." & vbcrlf & _
629 | " Important: The name must be same as in Nagios." & vbcrlf & _
630 | " -s, --service Name of the service the downtime should be scheduled for." & vbcrlf & _
631 | " Important: The name must be same as in Nagios. " & vbcrlf & _
632 | " When empty or not set a host downtime is being submitted." & vbcrlf & _
633 | " -t, --downtime Duration of the fixed downtime in minutes" & vbcrlf & _
634 | " -c, --comment Comment for the downtime" & vbcrlf & _
635 | " " & vbcrlf & _
636 | " -S, --server Nagios Webserver address (IP or DNS)" & vbcrlf & _
637 | " -p, --path Web path to Nagios cgi-bin (Default: /nagios/cgi-bin)" & vbcrlf & _
638 | " -u, --user Usernate to be used for accessing the CGIs" & vbcrlf & _
639 | " -P, --password Password for accessing the CGIs" & vbcrlf & _
640 | " " & vbcrlf & _
641 | " -e, --evtlog Enable logging to Windows event log " & vbcrlf & _
642 | " -d, --debug Enable debug mode" & vbcrlf & _
643 | " -h, --help Show this message" & vbcrlf & _
644 | "" & vbcrlf & _
645 | "If you call " & WScript.ScriptName & " without parameters the script takes the default" & vbcrlf & _
646 | "options which are hardcoded in the script." & vbcrlf & _
647 | ""
648 | End Sub
649 |
650 | Sub delDowntimeId(internalId)
651 | Dim file, aDowntimes, id
652 |
653 | file = downtimePath & "\"
654 | If downtimeType = 1 Then
655 | file = file & hostname & ".txt"
656 | Else
657 | file = file & hostname & "-" & service & ".txt"
658 | End If
659 |
660 | ' Read all downtimes to array
661 |
662 | Set oFile = oFS.OpenTextfile(file, FOR_READING)
663 | Do While Not oFile.AtEndOfStream
664 | Push aDowntimes, oFile.Readline
665 | Loop
666 | oFile.Close
667 |
668 | ' Filter downtime
669 | ArrayRemoveVal aDowntimes, internalId
670 |
671 | ' Write downtimes back to file
672 | Set oFile = oFS.OpenTextfile(file, FOR_WRITING, CREATE_IF_NOT_EXISTS)
673 | For Each id In aDowntimes
674 | dbg "Rewriting id to file: " & id
675 | oFile.Writeline id
676 | Next
677 | oFile.Close
678 |
679 | Set oFile = Nothing
680 | End Sub
681 |
682 | Sub cleanupDowntimeIds()
683 | Dim aDowntimes, nagiosDowntimeId, count, id
684 | ' Read all internal downtime ids for this host/service from file
685 | aDowntimes = getDowntimeIds()
686 |
687 | ' Only proceed when stored downtime ids found
688 | count = UBound(aDowntimes)
689 | If UBound(aDowntimes)+1 > 0 Then
690 | For Each id In aDowntimes
691 | ' Get the nagios downtime id
692 | nagiosDowntimeId = getNagiosDowntimeId(id)
693 |
694 | dbg "Translated downtime id: " & id & "(internal) => " & nagiosDowntimeId & " (Nagios)"
695 |
696 | If nagiosDowntimeId = "" Then
697 | ' no nagios downtime found -> delete the stored internal downtime id
698 | ' We can safely delete the downtime from the list of stored downtimes,
699 | ' because getNagiosDowntimeId(id) will exit the script if
700 | ' it can't get the downtimes from the nagios server.
701 |
702 | dbg "Internal downtime id " & id & " not found on nagios server"
703 | dbg "Deleting internal downtime id " & id & " from file"
704 |
705 | delDowntimeId(id)
706 | log EVENTLOG_INFORMATION, "Internal downtime id " & id & " deleted from file"
707 | End If
708 | Next
709 | Else
710 | log EVENTLOG_INFORMATION, "INFO: Nothing to do. No stored downtime ids found."
711 | End If
712 | End Sub
713 |
714 | Function getDowntimeIds()
715 | Dim file, aDowntimes, sLine, oRegex, oMatches
716 | aDowntimes = Array()
717 |
718 | file = downtimePath & "\"
719 | If downtimeType = 1 Then
720 | file = file & hostname & ".txt"
721 | Else
722 | file = file & hostname & "-" & service & ".txt"
723 | End If
724 |
725 | Set oRegex = New RegExp
726 | oRegex.Pattern = "[0-9]+"
727 |
728 | ' Read all downtimes to array
729 |
730 | If oFS.FileExists(file) Then
731 | Set oFile = oFS.OpenTextfile(file, FOR_READING)
732 | Do While Not oFile.AtEndOfStream
733 | sLine = oFile.Readline
734 |
735 | ' Do some validation
736 | If oRegex.Execute(sLine).Count > 0 Then
737 | Push aDowntimes, sLine
738 | End If
739 | Loop
740 | oFile.Close
741 | Else
742 | err "Could not open temporary file (" & file & ")"
743 | WScript.Quit(1)
744 | End If
745 |
746 | getDowntimeIds = aDowntimes
747 | End Function
748 |
749 | Sub saveDowntimeId()
750 | Dim file
751 |
752 | file = downtimePath & "\"
753 | If downtimeType = 1 Then
754 | file = file & hostname & ".txt"
755 | Else
756 | file = file & hostname & "-" & service & ".txt"
757 | End If
758 |
759 | dbg "Saving downtime to file: " & file
760 |
761 | Set oFile = oFS.OpenTextfile(file, FOR_APPENDING, CREATE_IF_NOT_EXISTS)
762 | oFile.Writeline downtimeId
763 | oFile.Close
764 |
765 | ' FIXME: Error handling
766 | 'err "Could not write downtime to temporary file (" & $file & ")"
767 | 'WScript.Quit(1)
768 | End Sub
769 |
770 | Function getNagiosDowntimeId(internalId)
771 | getNagiosDowntimeId = ""
772 |
773 | Dim aDowntimes, id
774 | ' Get all downtimes
775 | aDowntimes = getAllDowntimes()
776 |
777 | ' Filter the just scheduled downtime
778 | For Each id In aDowntimes
779 | ' Matching by:
780 | ' - internal id in comment field
781 | ' - triggerId: N/A
782 | If id("triggerId") = "N/A" And InStr(id("comment"), "(ID:" & internalId & ")") > 0 Then
783 | dbg "Found matching downtime: " & id("host") & " " & id("service") & " " & id("entryTime") & " " & id("downtimeId")
784 |
785 | getNagiosDowntimeId = id("downtimeId")
786 | End If
787 | Next
788 | End Function
789 |
790 | Sub deleteDowntime(nagiosDowntimeId)
791 | If nagiosDowntimeId = "" Then
792 | err "Unable to delete downtime. Nagios Downtime ID not given"
793 | End If
794 |
795 | Set params = CreateObject("Scripting.Dictionary")
796 | params.add "baseUrl", baseUrl
797 | params.add "user", user
798 | params.add "password", userPw
799 | params.add "downtime_id", nagiosDowntimeId
800 |
801 | If downtimeType = 1 Then
802 | ' Host downtime
803 | url = get_url("del_host_downtime", params)
804 | Else
805 | ' Service downtime
806 | url = get_url("del_service_downtime", params)
807 | End If
808 |
809 | dbg "HTTP-GET: " & url
810 |
811 | oBrowser.Open "GET", url
812 | setBrowserOptions()
813 | oBrowser.Send
814 |
815 | dbg "HTTP-Response (" & oBrowser.Status & "): " & oBrowser.ResponseText
816 |
817 | ' Handle response code, not in detail, only first char
818 | ' Exit the script if we can't get the downtimes from the nagios server
819 | Select Case Left(oBrowser.Status, 1)
820 | ' 2xx response code is OK
821 | Case 2
822 | If InStr(oBrowser.ResponseText, get_msg("success")) > 0 Then
823 | log EVENTLOG_SUCCESS, "OK: Downtime (ID: " & nagiosDowntimeId & ") has been deleted"
824 | ElseIf InStr(oBrowser.ResponseText, get_msg("not_authorized")) > 0 Then
825 | err "Maybe not authorized or wrong host- or servicename"
826 |
827 | ElseIf InStr(oBrowser.ResponseText, get_msg("no_author")) > 0 Then
828 | err "No Author entered, define Author in user var"
829 |
830 | Else
831 | err "Some undefined error occured, turn debug mode on to view what happened"
832 | End If
833 | Case 3
834 | err "HTTP Response code 3xx says ""moved url"" (" & oBrowser.Status & ")"
835 |
836 | Case 4
837 | err "HTTP Response code 4xx says ""client error"" (" & oBrowser.Status & ")" & _
838 | "Hint: This could be caused by wrong auth credentials and/or datetime settings in this script"
839 |
840 | Case 5
841 | err "HTTP Response code 5xx says ""server Error"" (" & oBrowser.Status & ")"
842 |
843 | Case Else
844 | err "HTTP Response code unhandled by script (" & oBrowser.Status & ")"
845 | End Select
846 | End Sub
847 |
848 | Function getAllDowntimes()
849 | Dim aDowntimes, oRegex, oMatches, oDict
850 | aDowntimes = Array()
851 |
852 | ' Url to downtime page
853 | Set params = CreateObject("Scripting.Dictionary")
854 | params.add "baseUrl", baseUrl
855 | params.add "user", user
856 | params.add "password", userPw
857 | url = get_url("all_downtimes", params)
858 |
859 | dbg "HTTP-GET: " & url
860 |
861 | ' Fetch information via HTTP-GET
862 | oBrowser.Open "GET", url
863 | setBrowserOptions()
864 | oBrowser.Send
865 |
866 | dbg "HTTP-Response (" & oBrowser.Status & "): " & oBrowser.ResponseText
867 |
868 | ' Handle response code, not in detail, only first char
869 | ' Exit on error
870 | Select Case Left(oBrowser.Status, 1)
871 | ' 2xx response code is OK
872 | Case 2
873 | dbg "OK: Got downtime response from nagios server"
874 | Case 3
875 | err "HTTP Response code 3xx says ""moved url"" (" & oBrowser.Status & ")"
876 | Case 4
877 | err "HTTP Response code 4xx says ""client error"" (" & oBrowser.Status & ")" & VBCRLF & _
878 | "Hint: This could be caused by wrong auth credentials and/or datetime settings in this script"
879 | Case 5
880 | err "HTTP Response code 5xx says ""server Error"" (" & oBrowser.Status & ")"
881 | Case Else
882 | err "HTTP Response code unhandled by script (" & oBrowser.Status & ")"
883 | End Select
884 |
885 | If ty = "nagios" Then
886 | Set oRegex = New RegExp
887 | oRegex.IgnoreCase = True
888 |
889 | ' Parse all downtimes to an array
890 | Dim lineType, sLine
891 | lineType = ""
892 | ' Removed vbCrLf here
893 | For Each sLine In Split(oBrowser.ResponseText, vblf)
894 | ' Filter only downtime lines
895 | oRegex.Pattern = "CLASS=\'downtime(Odd|Even)"
896 | Set oMatches = oRegex.Execute(sLine)
897 |
898 | If oMatches.Count > 0 Then
899 | lineType = "downtime" & oMatches(0).SubMatches(0)
900 |
901 | oRegex.Pattern = "[^<]+<\/A>" & _
903 | "<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)" & _
905 | "<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)" & _
907 | "<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td> | ([^<]+)<\/td>"
909 | Set oMatches = oRegex.Execute(sLine)
910 |
911 | If oMatches.Count > 0 Then
912 | ' Host downtime:
913 | ' | dev.nagvis.org | 10-13-2009 09:15:35 | Nagios Admin | Perl Downtime-Script | 01-10-2010 09:15:35 | 01-10-2010 09:25:35 | Fixed | 0d 0h 10m 0s | 9 | N/A |
914 |
915 | Set oDict = CreateObject("Scripting.Dictionary")
916 |
917 | dbg "Found host downtime:" & _
918 | "Host: " & oMatches(0).SubMatches(0) & _
919 | " EntryTime: " & oMatches(0).SubMatches(1) & _
920 | " User: " & oMatches(0).SubMatches(2) & _
921 | " Comment: " & oMatches(0).SubMatches(3) & _
922 | " Start: " & oMatches(0).SubMatches(4) & _
923 | " End: " & oMatches(0).SubMatches(5) & _
924 | " Type: " & oMatches(0).SubMatches(6) & _
925 | " Duration: " & oMatches(0).SubMatches(7) & _
926 | " DowntimeID: " & oMatches(0).SubMatches(8) & _
927 | " TriggerID: " & oMatches(0).SubMatches(9)
928 |
929 | oDict.Add "host", oMatches(0).SubMatches(0)
930 | oDict.Add "service", ""
931 | oDict.Add "entryTime", oMatches(0).SubMatches(1)
932 | oDict.Add "user", oMatches(0).SubMatches(2)
933 | oDict.Add "comment", oMatches(0).SubMatches(3)
934 | oDict.Add "start", oMatches(0).SubMatches(4)
935 | oDict.Add "end", oMatches(0).SubMatches(5)
936 | oDict.Add "type", oMatches(0).SubMatches(6)
937 | oDict.Add "duration", oMatches(0).SubMatches(7)
938 | oDict.Add "downtimeId", oMatches(0).SubMatches(8)
939 | oDict.Add "triggerId", oMatches(0).SubMatches(9)
940 |
941 | ' Push to array
942 | ReDim Preserve aDowntimes(UBound(aDowntimes) + 1)
943 | Set aDowntimes(UBound(aDowntimes)) = oDict
944 | Else
945 | oRegex.Pattern = " [^<]+" & _
947 | "<\/A><\/td> | [^<]+" & _
949 | "<\/A><\/td> | ([^<]+)<\/td>" & _
950 | " | ([^<]+)<\/td> | ([^<]+)<\/td> | " & _
952 | "([^<]+)<\/td> | ([^<]+)<\/td>" & _
953 | " | ([^<]+)<\/td> | ([^<]+)<\/td> | " & _
955 | "([^<]+)<\/td> | ([^<]+)<\/td>"
956 | Set oMatches = oRegex.Execute(sLine)
957 |
958 | If oMatches.Count > 0 Then
959 | ' Service downtime:
960 | ' | dev.nagvis.org | HTTP | 10-13-2009 10:28:30 | Nagios Admin | test | 10-13-2009 10:28:11 | 10-13-2009 12:28:11 | Fixed | 0d 2h 0m 0s | 145 | N/A |
961 |
962 | Set oDict = CreateObject("Scripting.Dictionary")
963 |
964 | dbg "Found service downtime:" & _
965 | "Host: " & oMatches(0).SubMatches(0) & _
966 | " Service: " & oMatches(0).SubMatches(1) & _
967 | " EntryTime: " & oMatches(0).SubMatches(2) & _
968 | " User: " & oMatches(0).SubMatches(3) & _
969 | " Comment: " & oMatches(0).SubMatches(4) & _
970 | " Start: " & oMatches(0).SubMatches(5) & _
971 | " End: " & oMatches(0).SubMatches(6) & _
972 | " Type: " & oMatches(0).SubMatches(7) & _
973 | " Duration: " & oMatches(0).SubMatches(8) & _
974 | " DowntimeID: " & oMatches(0).SubMatches(9) & _
975 | " TriggerID: " & oMatches(0).SubMatches(10)
976 |
977 | oDict.Add "host", oMatches(0).SubMatches(0)
978 | oDict.Add "service", oMatches(0).SubMatches(1)
979 | oDict.Add "entryTime", oMatches(0).SubMatches(2)
980 | oDict.Add "user", oMatches(0).SubMatches(3)
981 | oDict.Add "comment", oMatches(0).SubMatches(4)
982 | oDict.Add "start", oMatches(0).SubMatches(5)
983 | oDict.Add "end", oMatches(0).SubMatches(6)
984 | oDict.Add "type", oMatches(0).SubMatches(7)
985 | oDict.Add "duration", oMatches(0).SubMatches(8)
986 | oDict.Add "downtimeId", oMatches(0).SubMatches(9)
987 | oDict.Add "triggerId", oMatches(0).SubMatches(10)
988 |
989 | ' Push to array
990 | ReDim Preserve aDowntimes(UBound(aDowntimes) + 1)
991 | Set aDowntimes(UBound(aDowntimes)) = oDict
992 | End If
993 | End If
994 | End If
995 | Next
996 | ElseIf ty = "multisite" Then
997 |
998 | ' basic, simple json parsing
999 | Dim parsed, row
1000 | parsed = parseMultisiteData(oBrowser.ResponseText)
1001 |
1002 | For Each row In parsed
1003 | Set oDict = CreateObject("Scripting.Dictionary")
1004 |
1005 | If UBound(row) = 11 Then
1006 | ' Handle new indices since http://git.mathias-kettner.de/git/?p=check_mk.git;a=commitdiff;h=c909636fe1eb4085d346f650c9f8387d7780c913
1007 | ' It would be much better to work with the column names. I know...
1008 | oDict.Add "host", row(0)
1009 | oDict.Add "service", row(1)
1010 | oDict.Add "entryTime", row(4)
1011 | oDict.Add "user", row(3)
1012 | oDict.Add "comment", row(10)
1013 | oDict.Add "start", row(5)
1014 | oDict.Add "end", row(6)
1015 | oDict.Add "type", row(7)
1016 | oDict.Add "duration", row(8)
1017 | oDict.Add "downtimeId", row(11)
1018 | oDict.Add "triggerId", "N/A"
1019 | Else
1020 | oDict.Add "host", row(0)
1021 | oDict.Add "service", row(1)
1022 | oDict.Add "entryTime", row(3)
1023 | oDict.Add "user", row(2)
1024 | oDict.Add "comment", row(8)
1025 | oDict.Add "start", row(4)
1026 | oDict.Add "end", row(5)
1027 | oDict.Add "type", row(6)
1028 | oDict.Add "duration", row(7)
1029 | oDict.Add "downtimeId", row(9)
1030 | oDict.Add "triggerId", "N/A"
1031 | End If
1032 |
1033 | ' Push to array
1034 | ReDim Preserve aDowntimes(UBound(aDowntimes) + 1)
1035 | Set aDowntimes(UBound(aDowntimes)) = oDict
1036 | Next
1037 | End If
1038 |
1039 | getAllDowntimes = aDowntimes
1040 | End Function
1041 |
1042 | ' no real json parsing. Simply extracting all strings, then splitting by fields
1043 | Function parseMultisiteData(data)
1044 | Dim length, index, elem_start, element, rows, row, char, first_row
1045 | length = Len(data)
1046 | index = 0
1047 | elem_start = -1
1048 | rows = Array()
1049 | row = Array()
1050 | first_row = 1
1051 | While index < length
1052 | index = index + 1
1053 | char = Mid(data, index, 1)
1054 |
1055 | if elem_start = -1 Then
1056 | If char = """" Then
1057 | ' Initialize an element, search for starts
1058 | elem_start = index + 1
1059 | ElseIf char = vblf Then
1060 | ' Is the line complete? Then add it to the rows array. The 8 here is
1061 | ' a value just to te sure we do not add empty/incomplete rows. In old
1062 | ' cmk releases the number of columns was 9, currently we get 11
1063 | If UBound(row) > 8 Then
1064 | If first_row <> 1 Then
1065 | Push rows, row
1066 | Else
1067 | first_row = 0 ' simply drop the header row
1068 | End If
1069 | row = Array()
1070 | End If
1071 | End If
1072 | ElseIf char = """" Then
1073 | element = Mid(data, elem_start, index - elem_start)
1074 | Push row, element
1075 | elem_start = -1
1076 | End If
1077 | WEnd
1078 |
1079 | ' Unfished row? Add it!
1080 | If UBound(row) <> -1 Then
1081 | Push rows, row
1082 | End If
1083 |
1084 | parseMultisiteData = rows
1085 | End Function
1086 |
1087 | ' Test whether or not a host is reachable via ping.
1088 | ' Use hostname or ip address as ping target parameter
1089 | Function PingTest(strHostOrIP)
1090 | Dim strCommand, objSh
1091 | Set objSh = CreateObject("WScript.Shell")
1092 | strCommand = "%ComSpec% /C %SystemRoot%\system32\ping.exe -n 1 " & strHostOrIP & " | " & "%SystemRoot%\system32\find.exe /i " & Chr(34) & "TTL=" & Chr(34)
1093 | PingTest = CBool(objSh.Run(strCommand, 0, True))
1094 | Set objSh = Nothing
1095 | End Function
1096 |
1097 | Function gettime(dateTime)
1098 |
1099 | If dateTime = "" Then
1100 | dateTime = Now
1101 | End If
1102 |
1103 | Dim sec, min, h, mday, m, y
1104 | sec = Second(dateTime)
1105 | min = Minute(dateTime)
1106 | h = Hour(dateTime)
1107 | mday = Day(dateTime)
1108 | m = Month(dateTime)
1109 | y = Year(dateTime)
1110 |
1111 | ' add leading 0 to values lower than 10
1112 | If m < 10 Then
1113 | m = "0" & m
1114 | End If
1115 | If mday < 10 Then
1116 | mday = "0" & mday
1117 | End If
1118 | If h < 10 Then
1119 | h = "0" & h
1120 | End If
1121 | If min < 10 Then
1122 | min = "0" & min
1123 | End If
1124 | If sec < 10 Then
1125 | sec = "0" & sec
1126 | End If
1127 |
1128 | Select Case nagiosDateFormat
1129 | Case "euro"
1130 | gettime = mday & "-" & m & "-" & y & " " & h & ":" & min & ":" & sec
1131 | Case "us"
1132 | gettime = m & "-" & mday & "-" & y & " " & h & ":" & min & ":" & sec
1133 | Case "iso8601"
1134 | gettime = y & "-" & m & "-" & mday & " " & h & ":" & min & ":" & sec
1135 | Case "strict-iso8601"
1136 | gettime = y & "-" & m & "-" & mday & "T" & h & ":" & min & ":" & sec
1137 | Case Else
1138 | err "No valid date format given in nagiosDateFormat var"
1139 | End Select
1140 | End Function
1141 |
1142 | Function Push(ByRef mArray, ByVal mValue)
1143 | Dim mValEl
1144 |
1145 | If IsArray(mArray) Then
1146 | Redim Preserve mArray(UBound(mArray) + 1)
1147 | mArray(UBound(mArray)) = mValue
1148 | Else
1149 | If IsArray(mValue) Then
1150 | mArray = mValue
1151 | Else
1152 | mArray = Array(mValue)
1153 | End If
1154 | End If
1155 |
1156 | Push = UBound(mArray)
1157 | End Function
1158 |
1159 | Sub ArrayRemoveVal(ByRef arr, ByVal val)
1160 | Dim i, j
1161 | If IsArray(arr) Then
1162 | i = 0 : j = -1
1163 | For i = 0 To UBound(arr)
1164 | If arr(i) <> val Then
1165 | j = j + 1
1166 | arr(j) = arr(i)
1167 | End If
1168 | Next
1169 | ReDim Preserve arr(j)
1170 | End If
1171 | End Sub
1172 |
1173 |
1174 | ' #############################################################
1175 | ' EOF
1176 | ' #############################################################
1177 |
--------------------------------------------------------------------------------
/nagios_downtime.vbs.conf-sample:
--------------------------------------------------------------------------------
1 | server = "monitoring-server.local"
'ty = "nagios"
'authName = "OMD Monitoring Site sysmon"
'basePath = "/sysmon/nagios/cgi-bin"
'user = "omdadmin"
'userPw = "omd"
'nagiosDateFormat = "iso8601"
ty = "multisite"
basePath = "/sysmon/check_mk"
user = "svc-downtime"
userPw = "testpassword"
Set oNetwork = WScript.CreateObject("WScript.Network")
hostname = LCase(oNetwork.ComputerName)
2 |
--------------------------------------------------------------------------------
|