Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions zabbix v5/DiscoverScheduledTasks.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Script: DiscoverSchelduledTasks
# Author: Romain Si
#
# This script is intended for use with Zabbix > 3.x
#
#
# Add to Zabbix Agent
# UserParameter=TaskSchedulerMonitoring[*],powershell -NoProfile -ExecutionPolicy Bypass -File "C:\_zabbix\DiscoverScheduledTasks.ps1" "$1" "$2"
#
## Change the $path variable to indicate the Scheduled Tasks subfolder to be processed as "\nameFolder\" or "\nameFolder2\subfolder\" see (Get-ScheduledTask -TaskPath )


$path = "\"


Function Convert-ToUnixDate ($PSdate) {
$epoch = [timezone]::CurrentTimeZone.ToLocalTime([datetime]'1/1/1970')
(New-TimeSpan -Start $epoch -End $PSdate).TotalSeconds
}

$ITEM = [string]$args[0]
$ID = [string]$args[1]

switch ($ITEM) {
"DiscoverTasks" {
$apptasks = Get-ScheduledTask -TaskPath $path | where {$_.state -like "Ready" -or "Running"}
$apptasksok = $apptasks.TaskName
$idx = 1
write-host "{"
write-host " `"data`":[`n"
foreach ($currentapptasks in $apptasksok)
{
if ($idx -lt $apptasksok.count)
{

$line= "{ `"{#APPTASKS}`" : `"" + $currentapptasks + "`" },"
write-host $line
}
elseif ($idx -ge $apptasksok.count)
{
$line= "{ `"{#APPTASKS}`" : `"" + $currentapptasks + "`" }"
write-host $line
}
$idx++;
}
write-host
write-host " ]"
write-host "}"}}


switch ($ITEM) {
"TaskLastResult" {
[string] $name = $ID
$pathtask = Get-ScheduledTask -TaskPath "*" -TaskName "$name"
$pathtask1 = $pathtask.Taskpath
$taskResult = Get-ScheduledTaskInfo -TaskPath "$pathtask1" -TaskName "$name"
Write-Output ($taskResult.LastTaskResult)
}}

switch ($ITEM) {
"TaskLastRunTime" {
[string] $name = $ID
$pathtask = Get-ScheduledTask -TaskPath "*" -TaskName "$name"
$pathtask1 = $pathtask.Taskpath
$taskResult = Get-ScheduledTaskInfo -TaskPath "$pathtask1" -TaskName "$name"
$taskResult1 = $taskResult.LastRunTime
$date = get-date -date "01/01/1970"
$taskResult2 = (New-TimeSpan -Start $date -end $taskresult1).TotalSeconds
Write-Output ($taskResult2)
}}

switch ($ITEM) {
"TaskNextRunTime" {
[string] $name = $ID
$pathtask = Get-ScheduledTask -TaskPath "*" -TaskName "$name"
$pathtask1 = $pathtask.Taskpath
$taskResult = Get-ScheduledTaskInfo -TaskPath "$pathtask1" -TaskName "$name"
$taskResult1 = $taskResult.NextRunTime
$date = get-date -date "01/01/1970"
$taskResult2 = (New-TimeSpan -Start $date -end $taskresult1).TotalSeconds
Write-Output ($taskResult2)
}}

switch ($ITEM) {
"TaskState" {
[string] $name = $ID
$pathtask = Get-ScheduledTask -TaskPath "*" -TaskName "$name"
$pathtask1 = $pathtask.State
Write-Output ($pathtask1)
}}
60 changes: 60 additions & 0 deletions zabbix v5/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
================DiscoverScheduledTasks ================

This template use PowerShell Cmdlets to discover and manage Windows Tasks Scheduled.

Items : Task Last Result (Status for each tasks), Task Last Run Time, Task Next Run time

Discovery : All tasks Active or Running

Triggers : [HIGH] => Last Result of tasks FAILED<br>
Triggers : [HIGH] => State of tasks Disabled

Install :
Import template,
Install the Zabbix agent on your host,
Copy DiscoverScheduledTasks.ps1 in your zabbix agent directory,
In powershell script change $path variable for subsfolders,
Add the following line to your Zabbix agent configuration file :


EnableRemoteCommands=1
UnsafeUserParameters=1
UserParameter=TaskSchedulerMonitoring[*],powershell -NoProfile -ExecutionPolicy Bypass -File "C:\_zabbix\DiscoverScheduledTasks.ps1" "$1" "$2"
Timeout=30



do not forget to restart the zabbix agent!!









####### USER-DEFINED MONITORED PARAMETERS #######

### Option: UnsafeUserParameters
# Allow all characters to be passed in arguments to user-defined parameters.
# The following characters are not allowed:
# \ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @
# Additionally, newline characters are not allowed.
# 0 - do not allow
# 1 - allow
#
# Mandatory: no
# Range: 0-1
# Default:
# UnsafeUserParameters=0
UnsafeUserParameters=1

### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
#
# Mandatory: no
# Default:
# UserParameter=
UserParameter=TaskSchedulerMonitoring[*],powershell -NoProfile -ExecutionPolicy Bypass -File "C:\_zabbix\DiscoverScheduledTasks.ps1" "$1" "$2"
Loading