forked from bezibaerchen/prtgscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprtg-check-mailstore-jobs.ps1
120 lines (96 loc) · 3.82 KB
/
prtg-check-mailstore-jobs.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
## 2018-09-20_BEZ Script to check Job Status in Mailstore
## Please check GitHub repository for updates to that script: https://github.com/bezibaerchen/prtgscripts
# Define credentials for mailstore server
$user="<user>"
$pass="<pass>"
# Define mailstore server
$msserver="<servername_fqdn>"
# Import Mailstore Powershell Module
Import-Module 'C:\scripts\PRTG\mailstore\MS.PS.Lib.psd1'
# Open new API connection to mailstore serverSideExecution
$msapiclient = New-MSApiClient -Username $user -Password $pass -MailStoreServer $msserver -Port 8463 -IgnoreInvalidSSLCerts
# Get current date in format 2018-09-20T08:45:43
$date=(get-date -format s)
# Get current date minus 24h hours
$date_24h_ago=(get-date (get-date).AddDays(-1) -format s)
# set counters to 0
$errorcount=0
$successfulcount=0
$completedWithErrors=0
$cancelled=0
# get configured profiles from Mailstore serverSideExecution
$return=Invoke-MSApiCall $msapiclient "GetProfiles" @{raw = "true"}
# Loop through profiles set to automatic execution
foreach($id in ($return.result| where {$_.serverSideExecution.automatic -eq $true}).id)
{
# loop through jobs set to automatic with result succeeded
$result=Invoke-MSApiCall $msapiclient "GetWorkerResults" @{fromIncluding = $date_24h_ago; toExcluding = $date; timeZoneID = "W. Europe Standard Time"; profileID=$id} | where {$_.result.result -eq "succeeded"}
# raise counter for successful jobs
if($result) {
$successfulcount=$successfulcount+1
}
}
foreach($id in ($return.result| where {$_.serverSideExecution.automatic -eq $true}).id)
{
# loop through jobs set to automatic with result succeeded
$result=Invoke-MSApiCall $msapiclient "GetWorkerResults" @{fromIncluding = $date_24h_ago; toExcluding = $date; timeZoneID = "W. Europe Standard Time"; profileID=$id} | where {$_.result.result -eq "completedWithErrors"}
# raise counter for successful jobs
if($result) {
$completedWithErrors=$completedWithErrors+1
}
}
foreach($id in ($return.result| where {$_.serverSideExecution.automatic -eq $true}).id)
{
# loop through jobs set to automatic with result succeeded
$result=Invoke-MSApiCall $msapiclient "GetWorkerResults" @{fromIncluding = $date_24h_ago; toExcluding = $date; timeZoneID = "W. Europe Standard Time"; profileID=$id} | where {$_.result.result -eq "cancelled"}
# raise counter for successful jobs
if($result) {
$cancelled=$cancelled+1
}
}
# Loop through profiles set to automatic execution
foreach($id in ($return.result| where {$_.serverSideExecution.automatic -eq $true}).id)
{
# loop through jobs set to automatic with StatusCode not being succeeded
$result=Invoke-MSApiCall $msapiclient "GetWorkerResults" @{fromIncluding = $date_24h_ago; toExcluding = $date; timeZoneID = "W. Europe Standard Time"; profileID=$id} | where {$_.result.result -eq "failed"}
# raise counter for non-successful jobs
if($result) {
$errorcount=$errorcount+1
}
}
# Output result in PRTG readable XML
"<prtg>
<result>
<channel>Successful jobs</channel>
<value>$successfulcount</value>
<showChart>1</showChart>
<showTable>1</showTable>
<unit>Count</unit>
<mode>absolute</mode>
</result>
<result>
<channel>Jobs completed with errors</channel>
<value>$completedWithErrors</value>
<showChart>1</showChart>
<showTable>1</showTable>
<unit>Count</unit>
<mode>absolute</mode>
</result>
<result>
<channel>Jobs cancelled</channel>
<value>$cancelled</value>
<showChart>1</showChart>
<showTable>1</showTable>
<unit>Count</unit>
<mode>absolute</mode>
</result>
<result>
<channel>Jobs Failed</channel>
<value>$errorcount</value>
<showChart>1</showChart>
<showTable>1</showTable>
<unit>Count</unit>
<mode>absolute</mode>
</result>
<text>Successful jobs: $successfulcount Jobs completed with errors: $completedWithErrors Jobs cancelled: $cancelled Jobs Failed: $errorcount</text>
</prtg>"