Skip to content

Commit 003a60c

Browse files
committed
PrtgSensorHistoricData + Uptime
1 parent d261ed2 commit 003a60c

File tree

8 files changed

+168
-24
lines changed

8 files changed

+168
-24
lines changed

prtgshell2.cs

84 Bytes
Binary file not shown.

prtgshell2.dll

0 Bytes
Binary file not shown.

prtgshell2.psd1

-1.34 KB
Binary file not shown.

prtgshell2.psm1

9.62 KB
Binary file not shown.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
function Get-PrtgSensorHistoricData {
3+
<#
4+
.SYNOPSIS
5+
Returns historic data from a specified time period from a sensor object.
6+
.DESCRIPTION
7+
Returns a table of data using the specified start and end dates and the specified interval.
8+
.PARAMETER SensorId
9+
The sensor to retrieve data for.
10+
.PARAMETER RangeStart
11+
DateTime object specifying the start of the history range.
12+
.PARAMETER RangeEnd
13+
DateTime object specifying the End of the history range.
14+
.PARAMETER IntervalInSeconds
15+
The minimum interval to include, in seconds. The default is one hour (3600 seconds). A value of zero (0) will return raw data.
16+
.EXAMPLE
17+
Get-PrtgSensorHistoricData 2321 (Get-Date "2016-06-23 12:15") (Get-Date "2016-06-23 16:15") 60
18+
#>
19+
20+
[CmdletBinding()]
21+
Param (
22+
[Parameter(Mandatory=$True,Position=0)]
23+
[int] $SensorId,
24+
25+
[Parameter(Mandatory=$True,Position=1)]
26+
[datetime] $RangeStart,
27+
28+
[Parameter(Mandatory=$True,Position=2)]
29+
[datetime] $RangeEnd,
30+
31+
[Parameter(Mandatory=$True,Position=3)]
32+
[int] $IntervalInSeconds = 3600
33+
)
34+
35+
BEGIN {
36+
$PrtgServerObject = $Global:PrtgServerObject
37+
}
38+
39+
PROCESS {
40+
$Parameters = @{
41+
"id" = $SensorId
42+
"sdate" = $RangeStart.ToString("yyyy-MM-dd-HH-mm-ss")
43+
"edate" = $RangeEnd.ToString("yyyy-MM-dd-HH-mm-ss")
44+
"avg" = $IntervalInSeconds
45+
}
46+
47+
$url = $PrtgServerObject.UrlBuilder("api/historicdata.csv",$Parameters)
48+
49+
$QueryObject = $PrtgServerObject.HttpQuery($url,$false)
50+
51+
$DataPoints = $QueryObject.RawData | ConvertFrom-Csv | ? { $_.'Date Time' -ne 'Averages' }
52+
53+
#$APropertyName = (($DataPoints | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name) -notmatch "Coverage") -notmatch "Date Time" | Select-Object -First 1
54+
}
55+
56+
END {
57+
return $DataPoints
58+
}
59+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
# optional thing to add here:
3+
# make it so we can define a target month in this report, rather than manually specifying the start and end date.
4+
5+
6+
function Get-PrtgSensorUptime {
7+
<#
8+
.SYNOPSIS
9+
Returns five-nines-style uptime for a specified time period from a sensor object.
10+
.DESCRIPTION
11+
Returns five-nines-style uptime for a specified time period from a sensor object.
12+
.PARAMETER SensorId
13+
The sensor to retrieve data for.
14+
.PARAMETER RangeStart
15+
DateTime object specifying the start of the history range.
16+
.PARAMETER RangeEnd
17+
DateTime object specifying the End of the history range.
18+
.EXAMPLE
19+
Get-PrtgSensorUptime 2321 (Get-Date "2016-06-23 12:15") (Get-Date "2016-06-23 16:15")
20+
#>
21+
22+
[CmdletBinding()]
23+
Param (
24+
[Parameter(Mandatory=$true,Position=0)]
25+
[int] $SensorId,
26+
27+
[Parameter(Mandatory=$true,Position=1)]
28+
[datetime] $RangeStart,
29+
30+
[Parameter(Mandatory=$false,Position=2)]
31+
[datetime] $RangeEnd
32+
)
33+
34+
BEGIN {
35+
$PrtgServerObject = $Global:PrtgServerObject
36+
37+
if (!$RangeEnd) {
38+
$RangeStart = Get-Date ($RangeStart.ToString('MMMM yyyy'))
39+
$RangeEnd = $RangeStart.AddMonths(1).AddSeconds(-1)
40+
}
41+
}
42+
43+
PROCESS {
44+
$HistoricData = Get-PrtgSensorHistoricData $SensorId $RangeStart $RangeEnd 0
45+
46+
$APropertyName = (($HistoricData | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name) -notmatch "Coverage") -notmatch "Date Time" | Select-Object -First 1
47+
48+
# maybe this is valid?
49+
$UpEntries = $HistoricData.$APropertyName | ? { $_ -ne "" }
50+
51+
}
52+
53+
END {
54+
$returnobject = "" | select SensorId,RangeStart,RangeEnd,TotalDatapoints,UpDatapoints,DownDatapoints,UptimePercentage
55+
$returnobject.SensorId = $SensorId
56+
$returnobject.RangeStart = $RangeStart
57+
$returnobject.RangeEnd = $RangeEnd
58+
$returnobject.TotalDatapoints = $HistoricData.Count
59+
$returnobject.UpDatapoints = $UpEntries.Count
60+
$returnobject.DownDatapoints = $HistoricData.Count - $UpEntries.Count
61+
$returnobject.UptimePercentage = ($UpEntries.Count / $HistoricData.Count) * 100
62+
63+
return $returnobject
64+
}
65+
}

src/cmdlets/Get-PrtgServer.ps1

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function Get-PrtgServer {
4646
When specified, the cmdlet returns nothing on success.
4747
#>
4848

49+
[CmdletBinding()]
4950
Param (
5051
[Parameter(Mandatory=$True,Position=0)]
5152
[ValidatePattern("\d+\.\d+\.\d+\.\d+|(\w\.)+\w")]
@@ -70,50 +71,68 @@ function Get-PrtgServer {
7071
)
7172

7273
BEGIN {
74+
75+
$PrtgServerObject = New-Object PrtgShell.PrtgServer
76+
77+
$PrtgServerObject.Server = $Server
78+
$PrtgServerObject.UserName = $UserName
79+
$PrtgServerObject.PassHash = $PassHash
80+
7381
if ($HttpOnly) {
7482
$Protocol = "http"
7583
if (!$Port) { $Port = 80 }
84+
7685
} else {
7786
$Protocol = "https"
7887
if (!$Port) { $Port = 443 }
7988

80-
$PrtgServerObject = New-Object PrtgShell.PrtgServer
81-
82-
$PrtgServerObject.Protocol = $Protocol
83-
$PrtgServerObject.Port = $Port
84-
$PrtgServerObject.Server = $Server
85-
$PrtgServerObject.UserName = $UserName
86-
$PrtgServerObject.PassHash = $PassHash
87-
88-
$PrtgServerObject.OverrideValidation()
89+
#$PrtgServerObject.OverrideValidation()
8990
}
91+
92+
$PrtgServerObject.Protocol = $Protocol
93+
$PrtgServerObject.Port = $Port
9094
}
9195

9296
PROCESS {
9397
$url = $PrtgServerObject.UrlBuilder("api/getstatus.xml")
9498

9599
try {
96-
$QueryObject = HelperHTTPQuery $url -AsXML
100+
#$QueryObject = HelperHTTPQuery $url -AsXML
101+
#$PrtgServerObject.OverrideValidation()
102+
$QueryObject = $PrtgServerObject.HttpQuery($url)
97103
} catch {
98104
throw "Error performing HTTP query"
99105
}
100106

101107
$Data = $QueryObject.Data
102108

103-
$data.status.ChildNodes | % {
104-
if ($_.Name -ne "IsAdminUser") {
105-
$PrtgServerObject.$($_.Name) = $_.InnerText
106-
} else {
107-
# TODO
108-
# there's at least four properties that need to be treated this way
109-
# this is because this property returns a text "true" or "false", which powershell always evaluates as "true"
110-
$PrtgServerObject.$($_.Name) = [System.Convert]::ToBoolean($_.InnerText)
109+
# the logic and future-proofing of this is a bit on the suspect side.
110+
# the idea is that we want to get all the properties that it returns
111+
# and shove them into our new object, but if the object is missing
112+
# the property in the first place we will get an error. this happens
113+
# periodically when paessler adds new properties to the output.
114+
#
115+
# so how do we gracefully handle new properties?
116+
foreach ($ChildNode in $data.status.ChildNodes) {
117+
# for now, we outright ignore them.
118+
if (($PrtgServerObject | Get-Member | Select-Object -ExpandProperty Name) -contains $ChildNode.Name) {
119+
120+
if ($ChildNode.Name -ne "IsAdminUser") {
121+
$PrtgServerObject.$($ChildNode.Name) = $ChildNode.InnerText
122+
} else {
123+
# TODO
124+
# there's at least four properties that need to be treated this way
125+
# this is because this property returns a text "true" or "false", which powershell always evaluates as "true"
126+
$PrtgServerObject.$($ChildNode.Name) = [System.Convert]::ToBoolean($ChildNode.InnerText)
127+
}
128+
111129
}
112130
}
113131

114132
$global:PrtgServerObject = $PrtgServerObject
115133

116134
#HelperFormatTest ###### need to add this back in
135+
# this tests for a decimal-placement bug that existed in the output from some old versions of prtg
117136

118137
if (!$Quiet) {
119138
return $PrtgServerObject | Select-Object @{n='Connection';e={$_.ApiUrl}},UserName,Version

src/cs/PrtgServer.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,16 @@ private static bool OnValidateCertificate(object sender, X509Certificate certifi
166166
public void OverrideValidation() {
167167
ServicePointManager.ServerCertificateValidationCallback = OnValidateCertificate;
168168
ServicePointManager.Expect100Continue = true;
169-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
169+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
170170
}
171-
172-
171+
173172
private System.Uri prtguri;
174173

175174
// this likely doesn't need to be public
176175
// or exist, this is just for peeking
177-
public System.Uri PrtgUri {
178-
get { return this.prtguri; }
179-
}
176+
//public System.Uri PrtgUri {
177+
// get { return this.prtguri; }
178+
//}
180179

181180
private Hashtable parsed_querystring;
182181

@@ -212,6 +211,8 @@ public HttpQueryReturnObject HttpQuery(string Url, bool AsXml = true) {
212211
// also, all requests should not be treated as XML for this to be more generic
213212
// (the powershell version had an "-asxml" flag to handle this)
214213

214+
this.OverrideValidation();
215+
215216
HttpWebResponse Response = null;
216217
HttpStatusCode StatusCode = new HttpStatusCode();
217218

0 commit comments

Comments
 (0)