-
Notifications
You must be signed in to change notification settings - Fork 0
/
prtg_push.ps1
executable file
·152 lines (96 loc) · 3.82 KB
/
prtg_push.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<#
.SYNOPSIS
Send sensor data to PRTG via HTTP Push.
.DESCRIPTION
Send sensor data to PRTG via HTTP Push. Use either directly specified
values or output from existing sensor scripts.
This script should be compatible with the HTTP Push Data and HTTP Push
Count sensors.
.INPUTS
Data. A String of the form "<numeric_value>:<message>" -- the output
format for PRTG custom EXE/Script sensors.
.PARAMETER ProbeAddress
Base address of the PRTG probe including protocol (http or https),
hostname or IP address, and port (e.g.: "https://10.2.3.4:5051").
.PARAMETER Token
Token to match the target sensor in PRTG.
.PARAMETER Data
PRTG sensor script output interpreted in value:message format. This
option is only in effect when the -val option is not used. (May also
be read from piped input.)
.PARAMETER NoData
Don't submit any value or data; only call the sensor webhook. Only
useful for the HTTP Push Count sensor. If this option is supplied,
the -val and -data options and piped data are ignored.
.PARAMETER Val
Value to assign to the target sensor in PRTG. If this option is
supplied, the -data option and piped data are ignored.
.PARAMETER Msg
Optional message text to appear on target sensor in PRTG. Only
applied when the -val option is used.
.PARAMETER Post
Push data via POST request. (If not specified, a GET request is used.)
This option is ignored when the -nodata option is used.
.EXAMPLE
prtg_push.ps1 -ProbeAddress <probe_address> -Token <token> -val <numeric_value> [-msg <message>] [-Post]
Report values directly.
.EXAMPLE
prtg_push.ps1 -ProbeAddress <probe_address> -Token <token> -Data <data> [-Dost]
Push values from sensor script output.
.EXAMPLE
<sensor_script> | prtg_push.ps1 -ProbeAddress <probe_address> -Token <token> [-Post]
Push values from piped sensor script output.
.EXAMPLE
prtg_push.ps1 -ProbeAddress <probe_address> -Token <token> -NoData
Report to a Push Count sensor.
#>
[CmdletBinding(DefaultParameterSetName="ParseData",
PositionalBinding=$false)]
Param(
[Parameter(Mandatory=$true)]
[String]$probeAddress,
[Parameter(Mandatory=$true)]
[String]$token,
[Parameter(Mandatory=$true,ParameterSetName="ParseData",ValueFromPipeline=$true)]
[String]$data,
[Parameter(Mandatory=$true,ParameterSetName="NoData")]
[Switch]$nodata,
[Parameter(Mandatory=$true,ParameterSetName="DirectValue")]
[String]$val,
[Parameter(Mandatory=$false,ParameterSetName="DirectValue")]
[String]$msg,
[Parameter(Mandatory=$false,ParameterSetName="DirectValue")]
[Parameter(Mandatory=$false,ParameterSetName="ParseData")]
[Switch]$post
)
# Load System.Web for URL encoding
Add-Type -AssemblyName System.Web
# Set security protocols to try; older versions of powershell may default
# to a protocol that's too low, causing https requests to fail
[Net.ServicePointManager]::SecurityProtocol = "tls13, tls12, tls11"
$webhook = "{0}/{1}" -f "$probeAddress", "$token"
if ($nodata -eq $true) { # submitting to a push count sensor
# Just call the webhook and don't submit any data
Invoke-WebRequest -Uri "$webhook"
}
else { # submitting to a push data sensor
if (-not $val) {
if ($data) {
# Split $data into its components
$val, $msg = $data.tostring() -split ':', 2
}
else {
Write-Error "ERROR: Did not receive numeric value or script output."
exit 1
}
}
$umsg = [System.Web.HTTPUtility]::UrlEncode($msg)
$submitData = "value={0}&text={1}" -f "$val", "$umsg"
if ($post -eq $true) {
Invoke-WebRequest -Uri "$webhook" -Method "POST" -data "$submitData"
}
else { # GET
$uri = "{0}?{1}" -f "$webhook", "$submitData"
Invoke-WebRequest -Uri "$uri"
}
}