Skip to content

Commit 4242992

Browse files
committed
Added Metric Cmdlets
1 parent 6e3947f commit 4242992

2 files changed

Lines changed: 172 additions & 4 deletions

File tree

src/S3-Client.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,16 +1198,16 @@ function Global:Remove-S3Bucket {
11981198
$HTTPRequestMethod = "DELETE"
11991199

12001200
if ($Profile) {
1201-
$Result = Invoke-AwsRequest -Profile $Profile -HTTPRequestMethod $HTTPRequestMethod -EndpointUrl $EndpointUrl -Uri $Uri -SkipCertificateCheck:$SkipCertificateCheck -Query $Query -ErrorAction Stop
1201+
$Result = Invoke-AwsRequest -Profile $Profile -HTTPRequestMethod $HTTPRequestMethod -EndpointUrl $EndpointUrl -Uri $Uri -SkipCertificateCheck:$SkipCertificateCheck -ErrorAction Stop
12021202
}
12031203
elseif ($AccessKey) {
1204-
$Result = Invoke-AwsRequest -AccessKey $AccessKey -SecretAccessKey $SecretAccessKey -HTTPRequestMethod $HTTPRequestMethod -EndpointUrl $EndpointUrl -Uri $Uri -SkipCertificateCheck:$SkipCertificateCheck -Query $Query -ErrorAction Stop
1204+
$Result = Invoke-AwsRequest -AccessKey $AccessKey -SecretAccessKey $SecretAccessKey -HTTPRequestMethod $HTTPRequestMethod -EndpointUrl $EndpointUrl -Uri $Uri -SkipCertificateCheck:$SkipCertificateCheck -ErrorAction Stop
12051205
}
12061206
elseif ($Tenant) {
1207-
$Result = Invoke-AwsRequest -Tenant $Tenant -HTTPRequestMethod $HTTPRequestMethod -EndpointUrl $EndpointUrl -Uri $Uri -SkipCertificateCheck:$SkipCertificateCheck -Query $Query -ErrorAction Stop
1207+
$Result = Invoke-AwsRequest -Tenant $Tenant -HTTPRequestMethod $HTTPRequestMethod -EndpointUrl $EndpointUrl -Uri $Uri -SkipCertificateCheck:$SkipCertificateCheck -ErrorAction Stop
12081208
}
12091209
else {
1210-
$Result = Invoke-AwsRequest -Bucket $Bucket -HTTPRequestMethod $HTTPRequestMethod -EndpointUrl $EndpointUrl -Uri $Uri -SkipCertificateCheck:$SkipCertificateCheck -Query $Query -ErrorAction Stop
1210+
$Result = Invoke-AwsRequest -Bucket $Bucket -HTTPRequestMethod $HTTPRequestMethod -EndpointUrl $EndpointUrl -Uri $Uri -SkipCertificateCheck:$SkipCertificateCheck -ErrorAction Stop
12111211
}
12121212

12131213
Write-Output $Result

src/StorageGRID-Webscale.psm1

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,66 @@ function ParseExceptionBody($Response) {
8585
}
8686
}
8787

88+
# helper function to convert datetime to unix timestamp
89+
function ConvertTo-UnixTimestamp {
90+
[CmdletBinding()]
91+
92+
PARAM (
93+
[parameter(Mandatory=$True,
94+
Position=0,
95+
ValueFromPipeline=$True,
96+
ValueFromPipelineByPropertyName=$True,
97+
HelpMessage="Date to be converted.")][DateTime[]]$Date
98+
)
99+
100+
BEGIN {
101+
$epoch = Get-Date -Year 1970 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0
102+
}
103+
104+
PROCESS {
105+
$Date = @($Date)
106+
107+
foreach ($Date in $Date) {
108+
$milliSeconds = [math]::truncate($Date.ToUniversalTime().Subtract($epoch).TotalMilliSeconds)
109+
Write-Output $milliSeconds
110+
}
111+
}
112+
}
113+
114+
# helper function to convert unix timestamp to datetime
115+
function ConvertFrom-UnixTimestamp {
116+
[CmdletBinding()]
117+
118+
PARAM (
119+
[parameter(Mandatory=$True,
120+
Position=0,
121+
ValueFromPipeline=$True,
122+
ValueFromPipelineByPropertyName=$True,
123+
HelpMessage="Timestamp to be converted.")][String]$Timestamp,
124+
[parameter(Mandatory=$True,
125+
Position=0,
126+
ValueFromPipeline=$True,
127+
ValueFromPipelineByPropertyName=$True,
128+
HelpMessage="Unit of timestamp.")][ValidateSet("Seconds","Milliseconds")][String]$Unit="Milliseconds",
129+
[parameter(Mandatory=$False,
130+
Position=1,
131+
HelpMessage="Optional Timezone to be used as basis for Timestamp. Default is system Timezone.")][System.TimeZoneInfo]$Timezone=[System.TimeZoneInfo]::Local
132+
)
133+
134+
PROCESS {
135+
$Timestamp = @($Timestamp)
136+
foreach ($Timestamp in $Timestamp) {
137+
if ($Unit -eq "Seconds") {
138+
$Date = [System.TimeZoneInfo]::ConvertTimeFromUtc(([datetime]'1/1/1970').AddSeconds($Timestamp),$Timezone)
139+
}
140+
else {
141+
$Date = [System.TimeZoneInfo]::ConvertTimeFromUtc(([datetime]'1/1/1970').AddMilliseconds($Timestamp),$Timezone)
142+
}
143+
Write-Output $Date
144+
}
145+
}
146+
}
147+
88148
### Cmdlets ###
89149

90150
## accounts ##
@@ -3684,6 +3744,114 @@ function Global:Update-SGWLicense {
36843744

36853745
## metrics ##
36863746

3747+
<#
3748+
.SYNOPSIS
3749+
Retrieves the metric names
3750+
.DESCRIPTION
3751+
Retrieves the metric names
3752+
#>
3753+
function Global:Get-SGWMetricNames {
3754+
[CmdletBinding()]
3755+
3756+
PARAM (
3757+
[parameter(Mandatory=$False,
3758+
Position=0,
3759+
HelpMessage="StorageGRID Webscale Management Server object. If not specified, global CurrentSGWServer object will be used.")][PSCustomObject]$Server
3760+
)
3761+
3762+
Begin {
3763+
if (!$Server) {
3764+
$Server = $Global:CurrentSGWServer
3765+
}
3766+
if (!$Server) {
3767+
Throw "No StorageGRID Webscale Management Server management server found. Please run Connect-SGWServer to continue."
3768+
}
3769+
if ($Server.AccountId) {
3770+
Throw "Operation not supported when connected as tenant. Use Connect-SgwServer without the AccountId parameter to connect as grid administrator and then rerun this command."
3771+
}
3772+
}
3773+
3774+
Process {
3775+
$Uri = $Server.BaseURI + "/grid/metric-names"
3776+
$Method = "GET"
3777+
3778+
try {
3779+
$Result = Invoke-RestMethod -WebSession $Server.Session -Method $Method -Uri $Uri -Headers $Server.Headers
3780+
}
3781+
catch {
3782+
$ResponseBody = ParseExceptionBody $_.Exception.Response
3783+
Write-Error "$Method to $Uri failed with Exception $($_.Exception.Message) `n $responseBody"
3784+
}
3785+
3786+
Write-Output $Result.data
3787+
}
3788+
}
3789+
3790+
<#
3791+
.SYNOPSIS
3792+
Performs an instant metric query at a single point in time
3793+
.DESCRIPTION
3794+
Performs an instant metric query at a single point in time
3795+
#>
3796+
function Global:Get-SGWMetricQuery {
3797+
[CmdletBinding()]
3798+
3799+
PARAM (
3800+
[parameter(Mandatory=$False,
3801+
Position=0,
3802+
HelpMessage="StorageGRID Webscale Management Server object. If not specified, global CurrentSGWServer object will be used.")][PSCustomObject]$Server,
3803+
[parameter(Mandatory=$True,
3804+
Position=1,
3805+
HelpMessage="Prometheus query string.")][String]$Query,
3806+
[parameter(Mandatory=$False,
3807+
Position=2,
3808+
HelpMessage="Query start, default current time (date-time).")][DateTime]$Time,
3809+
[parameter(Mandatory=$False,
3810+
Position=2,
3811+
HelpMessage="Timeout in seconds.")][Int]$Timeout=120
3812+
)
3813+
3814+
Begin {
3815+
if (!$Server) {
3816+
$Server = $Global:CurrentSGWServer
3817+
}
3818+
if (!$Server) {
3819+
Throw "No StorageGRID Webscale Management Server management server found. Please run Connect-SGWServer to continue."
3820+
}
3821+
if ($Server.AccountId) {
3822+
Throw "Operation not supported when connected as tenant. Use Connect-SgwServer without the AccountId parameter to connect as grid administrator and then rerun this command."
3823+
}
3824+
}
3825+
3826+
Process {
3827+
$Uri = $Server.BaseURI + "/grid/metric-query"
3828+
$Method = "GET"
3829+
3830+
$Uri += "?query=$Query"
3831+
3832+
if ($Time) {
3833+
$Uri += "&time=$(Get-Date -Format o $Time.ToUniversalTime())"
3834+
}
3835+
3836+
if ($Timeout) {
3837+
$Uri += "&timeout=$($Timeout)s"
3838+
}
3839+
3840+
3841+
try {
3842+
$Result = Invoke-RestMethod -WebSession $Server.Session -Method $Method -Uri $Uri -Headers $Server.Headers
3843+
}
3844+
catch {
3845+
$ResponseBody = ParseExceptionBody $_.Exception.Response
3846+
Write-Error "$Method to $Uri failed with Exception $($_.Exception.Message) `n $responseBody"
3847+
}
3848+
3849+
$Metrics = $Result.data.result | % { [PSCustomObject]@{Metric=$_.metric.__name__;Instance=$_.metric.instance;Time=(ConvertFrom-UnixTimestamp -Unit Seconds -Timestamp $_.value[0]);Value=$_.value[1]} }
3850+
3851+
Write-Output $Metrics
3852+
}
3853+
}
3854+
36873855
# TODO: Implement metrics cmdlets
36883856

36893857
## ntp-servers ##

0 commit comments

Comments
 (0)