Module Module1 Const PROGNAME As String = "check_taskscheduler2" Const PROGVER As String = "v0.1" Const OK As Integer = 0 Const WARNING As Integer = 1 Const CRITICAL As Integer = 2 Const UNKNOWN As Integer = 3 Dim str_tasks As String() Dim dict_tasks As New System.Collections.Generic.Dictionary(Of String, String) Dim dict_new_tasks As New System.Collections.Generic.Dictionary(Of String, String) Sub Main() Main(System.Environment.GetCommandLineArgs()) End Sub Private Sub Main(ByVal args() As String) Dim str_task_excl As String = "|" Dim str_task_incl As String = "|" Dim str_task_warn As String = "|" Dim str_task_warn2 As String = "|" Dim str_task_crit As String = "|" Dim str_task_crit2 As String = "|" Dim str_args As String() Dim arg_raw As String = "" Dim arg_key As String Dim arg_val As String For intCount = 1 To UBound(args) arg_raw = args(intCount) If arg_raw = "" Then Continue For If Left(Trim(arg_raw), 1) = "$" And Right(Trim(arg_raw), 1) = "$" Then Continue For arg_key = "" arg_val = "" 'Piece together the proper key/value pairs If Left(arg_raw, 1) = "-" Or Left(arg_raw, 1) = "/" Then If Left(arg_raw, 2) = "--" And InStr(arg_raw, "=") Then str_args = Split(arg_raw, "=") arg_key = str_args(0).Trim("-"c).ToLower() arg_val = str_args(1) Else arg_key = args(intCount).Trim("-"c).Trim("/"c) If arg_key = "help" Then print_help() Environment.Exit(OK) End If If arg_key = "version" Then Console.WriteLine(PROGNAME & " " & PROGVER) Environment.Exit(OK) End If intCount += 1 If intCount > UBound(args) Then error_out(arg_key) End If arg_val = args(intCount) End If Else error_out(arg_raw) End If 'Normalize the keys Select Case arg_key Case "task" arg_key = "t" Case "ignore" arg_key = "i" Case "hostname" arg_key = "H" Case "warning" arg_key = "w" Case "warning-task" arg_key = "W" Case "critical" arg_key = "c" Case "critical-task" arg_key = "C" End Select 'Combine like keys Select Case arg_key Case "t" str_task_incl = str_task_incl & arg_val & "|" Case "i" str_task_excl = str_task_excl & arg_val & "|" Case "H" unsupported(arg_key) Case "w" str_task_warn = str_task_warn & arg_val & "|" Console.WriteLine(" -w, --warning=VALUE Raise a warning if the task has not been started in the last X seconds") unsupported(arg_key) Case "W" str_task_warn2 = str_task_warn2 & arg_val & "|" Console.WriteLine(" -W, --warning-task=NAME,VALUE") unsupported(arg_key) Case "c" str_task_crit = str_task_crit & arg_val & "|" Console.WriteLine(" -c, --critical=VALUE Raise a critical if the task has not been started in the last X seconds") unsupported(arg_key) Case "C" str_task_crit2 = str_task_crit2 & arg_val & "|" Console.WriteLine(" -C, --critical-task=NAME,VALUE") unsupported(arg_key) Case Else unsupported(arg_key) End Select Next intCount 'Catch double dippers If str_task_excl <> "|" And str_task_incl <> "|" Then Console.WriteLine("TASKSCHEDULER2 CRITICAL: you can include OR exclude tasks. Not both.") Environment.Exit(CRITICAL) End If 'Console.WriteLine("str_task_excl = " & str_task_excl) 'Console.WriteLine("str_task_incl = " & str_task_incl) 'Build a dictionary of tasks Dim ts As New Microsoft.Win32.TaskScheduler.TaskService iterate_tasks(ts.RootFolder) 'Whittle down to only the ones we want to watch If str_task_incl <> "|" And str_task_excl = "|" Then 'Console.WriteLine("Incl") For Each kvp As System.Collections.Generic.KeyValuePair(Of String, String) In dict_tasks If Not str_task_incl.Contains("|" & kvp.Key & "|") Then Continue For str_task_incl = str_task_incl.Replace("|" & kvp.Key & "|", "||") dict_new_tasks.Add(kvp.Key, kvp.Value) Next End If 'Remove the tasks to ignore If str_task_incl = "|" And str_task_excl <> "|" Then 'Console.WriteLine("Excl") For Each kvp As System.Collections.Generic.KeyValuePair(Of String, String) In dict_tasks If str_task_excl.Contains("|" & kvp.Key & "|") Then str_task_excl = str_task_excl.Replace("|" & kvp.Key & "|", "||") Continue For End If dict_new_tasks.Add(kvp.Key, kvp.Value) Next End If If str_task_incl = "|" And str_task_excl = "|" Then 'Console.WriteLine("Cloning") For Each kvp As System.Collections.Generic.KeyValuePair(Of String, String) In dict_tasks dict_new_tasks.Add(kvp.Key, kvp.Value) Next End If 'Logic through the gray areas Dim result As Integer = OK If Replace(str_task_excl, "|", "") <> "" Then Console.WriteLine("TASKSCHEDULER2 WARNING: extraneous exclusion(s)") result = WARNING End If If Replace(str_task_incl, "|", "") <> "" Then Console.WriteLine("TASKSCHEDULER2 WARNING: extraneous inclusion(s)") result = WARNING End If 'Console.WriteLine("str_task_excl = " & str_task_excl) 'Console.WriteLine("str_task_incl = " & str_task_incl) Dim str_out As String = "" Dim int_out As Integer = 0 For Each kvp As System.Collections.Generic.KeyValuePair(Of String, String) In dict_new_tasks int_out += 1 If kvp.Value <> 0 Then str_out = str_out & kvp.Key & " finshed " & kvp.Value & ", " result = CRITICAL End If Next 'Write the outputs If result = OK Then If int_out = 0 Then Console.WriteLine("TASKSCHEDULER2: No scheduled tasks definied.") Else Console.WriteLine("TASKSCHEDULER2: All scheduled tasks (" & int_out.ToString & " tasks) has been executed successfully.") End If ElseIf result = CRITICAL Then Console.WriteLine("TASKSCHEDULER2 WARNING: " & str_out.Trim().Trim(","c)) End If Environment.Exit(result) End Sub Sub error_out(ByVal str_in As String) 'Dim args As String() 'args = System.Environment.GetCommandLineArgs() 'Dim arg_raw As String = "" 'For i As Integer = 1 To UBound(args) ' arg_raw &= args(i) & " ' " 'Next Console.WriteLine("TASKSCHEDULER2 CRITICAL: arguments did not parse correctly " & str_in) Environment.Exit(CRITICAL) End Sub Sub iterate_tasks(ByRef taskfolder As Microsoft.Win32.TaskScheduler.TaskFolder) For Each t As Microsoft.Win32.TaskScheduler.Task In taskfolder.Tasks Try If Not t.Definition.RegistrationInfo.Author.Contains("\") Then Continue For dict_tasks.Add(t.Name, t.LastTaskResult) Catch ex As Exception 'Console.WriteLine(ex.Message) 'Console.WriteLine(ex.StackTrace) End Try Next If taskfolder.SubFolders.Count <> 0 Then For Each sf As Microsoft.Win32.TaskScheduler.TaskFolder In taskfolder.SubFolders iterate_tasks(sf) Next End If End Sub Sub print_help() Console.WriteLine(PROGNAME & " " & PROGVER) Console.WriteLine("Copyright (C) 2010 Bobbie Stivers, Information Products Inc" & vbCrLf) Console.WriteLine("Check the MS (TM) Windows Task Scheduler Version 2.") Console.WriteLine("Warning: It doesn't like Version 1") Console.WriteLine("This plugin relies heavily on 'Task Scheduler Managed Wrapper' http://taskscheduler.codeplex.com") Console.WriteLine(" Thank you David Hall!") print_usage() Console.WriteLine("") Console.WriteLine("# This program is free software; you can redistribute it and/or" & vbCrLf & "# modify it under the terms of the GNU General Public License" & vbCrLf & _ "# as published by the Free Software Foundation; either version 2" & vbCrLf & "# of the License, or (at your option) any later version" & vbCrLf & _ "#" & vbCrLf & "# This program is distributed in the hope that it will be useful," & vbCrLf & _ "# but WITHOUT ANY WARRANTY; without even the implied warranty" & vbCrLf & "# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" & vbCrLf & _ "# GNU General Public License for more details." & vbCrLf & "#" & vbCrLf & _ "# You should have received a copy of the GNU General Public License" & vbCrLf & "# along with this program (or with Nagios); if not, write to the" & vbCrLf & _ "# Free Software Foundation, Inc., 59 Temple Place - Suite 330," & vbCrLf & "# Boston, MA 02111-1307, USA") Console.WriteLine("") Console.WriteLine("Task Scheduler Managed Wrapper License:") Console.WriteLine("THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ") Console.WriteLine("IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ") Console.WriteLine("FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ") Console.WriteLine("AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,") Console.WriteLine("WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF ") Console.WriteLine("OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.") End Sub Sub print_usage() Console.WriteLine("Usage: check_taskscheduler2.exe [OPTIONS]...") Console.WriteLine("Mandatory arguments to long options are mandatory for short options too.") Console.WriteLine(" -t, --task=NAME Check the task only (may be repeated)") Console.WriteLine(" -i, --ignore=NAME Ignore the task (may be repeated)") Console.WriteLine(" -H, --hostname=NAME Check the task scheduler on this host") Console.WriteLine(" -w, --warning=VALUE Raise a warning if the task has not been started in the last X seconds") Console.WriteLine(" -W, --warning-task=NAME,VALUE") Console.WriteLine(" -c, --critical=VALUE Raise a critical if the task has not been started in the last X seconds") Console.WriteLine(" -C, --critical-task=NAME,VALUE") Console.WriteLine(" --help display this help and exit") Console.WriteLine(" --version output version information and exit" & vbCrLf) Console.WriteLine("Report bugs to ,") End Sub Sub unsupported(ByVal str_in As String) Console.WriteLine("TASKSCHEDULER2 CRITICAL: argument key '" & str_in & "' is not supported just yet. Sorry.") Environment.Exit(CRITICAL) End Sub End Module