Skip to content

Commit 4b13680

Browse files
author
Klas Pihl
committedFeb 1, 2023
Initial code
1 parent 0d9dde7 commit 4b13680

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
 
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<#
2+
.SYNOPSIS
3+
MS SQL query with multiple arguments.
4+
.DESCRIPTION
5+
Customer specific sensor
6+
PRTG sensor 'Microsoft SQL v2 Sensor' can define one (1) imput parameter.
7+
Uses module SqlServer
8+
.NOTES
9+
2023-03-01 Version 1 Klas.Pihl@Atea.se
10+
.LINK
11+
https://github.com/klaspihl/PRTG/
12+
https://stackoverflow.com/a/38981021
13+
14+
.PARAMETER ServerInstance
15+
SQL server instance, defaults device placeholders
16+
17+
.PARAMETER GroupID
18+
GroupID of batch job
19+
20+
.PARAMETER DATAAREAID
21+
DATAAREAID of batch job
22+
23+
.PARAMETER Age
24+
Age i minutes since started
25+
26+
.PARAMETER DatabaseName
27+
Name of database
28+
29+
.PARAMETER TableName
30+
Name of table
31+
32+
.EXAMPLE
33+
get-PRTGSQLBatchJob.ps1 -GroupID 123 -DATAAREAID 789 -Age 60 -DatabaseName ERP1 -TableName Batch
34+
Uses PRTGs 'Set placeholders as environment values'
35+
36+
.EXAMPLE
37+
get-PRTGSQLBatchJob.ps1 -ServerInstance SQLServer1 -GroupID 123 -DATAAREAID 789 -Age 60 -DatabaseName ERP1 -TableName Batch
38+
Run query on other sql-server then device.
39+
#>
40+
[CmdletBinding()]
41+
param (
42+
[string]$ServerInstance=$env:prtg_host,
43+
[string]$GroupID,
44+
[string]$DATAAREAID,
45+
[int]$Age,
46+
[string]$DatabaseName,
47+
[string]$TableName
48+
49+
)
50+
#############################################################################
51+
#If Powershell is running the 32-bit version on a 64-bit machine, we
52+
#need to force powershell to run in 64-bit mode .
53+
#############################################################################
54+
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
55+
if ($myInvocation.Line) {
56+
&"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
57+
}else{
58+
&"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
59+
}
60+
exit $lastexitcode
61+
}
62+
#############################################################################
63+
64+
#Main code
65+
try {
66+
import-module SqlServer -ErrorAction Stop
67+
68+
$Query = "SELECT * FROM {0}.dbo.{1}
69+
WHERE Status = 1
70+
AND (GroupID = '{2}')
71+
AND DATEDIFF(mi,DATEADD(ss, StartTime, StartDate),GETDATE()) > {3}
72+
AND (EndDate = '1900-01-01' AND EndTime = 0)
73+
AND DATAAREAID = '{4}'" -f $DatabaseName,$TableName,$GroupID,$Age,$DATAAREAID
74+
75+
$RunTime = Measure-Command {$BatchJob = Invoke-Sqlcmd -ServerInstance $ServerInstance -Query $Query -ErrorAction Stop}
76+
77+
$Output = [PSCustomObject]@{
78+
prtg = [PSCustomObject]@{
79+
result =
80+
[PSCustomObject]@{
81+
Channel = 'Jobs'
82+
Float = 0
83+
Value = $BatchJob.count
84+
LimitMode = 1
85+
LimitMaxError = "0.5"
86+
},
87+
[PSCustomObject]@{
88+
Channel = "Execution time"
89+
Float = 0
90+
Value = ([math]::round($RunTime.TotalMilliseconds))
91+
CustomUnit = 'ms'
92+
LimitMode = 1
93+
LimitMaxError = 5000
94+
}
95+
text = ("{0} jobs in {1}.dbo.{2} older then {3} minutes" -f $BatchJob.count,$DatabaseName,$TableName,$Age)
96+
}
97+
}
98+
99+
} Catch {
100+
$error
101+
$Output = [PSCustomObject]@{
102+
prtg = [PSCustomObject]@{
103+
error = 1
104+
text = $error[0].Exception.Message
105+
}
106+
}
107+
}
108+
Write-Output ($Output | ConvertTo-Json -depth 5 | ForEach-Object { [System.Text.RegularExpressions.Regex]::Unescape($PSItem) })

0 commit comments

Comments
 (0)
Please sign in to comment.