Skip to content

Instantly share code, notes, and snippets.

@hkdnet
Last active November 4, 2015 10:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkdnet/2fbf6244010a72259a10 to your computer and use it in GitHub Desktop.
Save hkdnet/2fbf6244010a72259a10 to your computer and use it in GitHub Desktop.
# http://qiita.com/HKDnet/items/57a0630705d3f6468a77
# $Env:MackerelApiKey is needed
function Get-MackerelHosts {
$ret = Invoke-RestMethod -Uri "https://mackerel.io/api/v0/hosts.json" -Headers @{ "X-Api-Key" = $Env:MackerelApiKey }
return $ret.hosts
}
function Get-MackerelServices {
$ret = Invoke-RestMethod -Uri "https://mackerel.io/api/v0/services" -Method GET -Headers @{ "X-Api-Key" = $Env:MackerelApiKey}
return $ret.services
}
function Get-MackerelRoles {
param(
[String]
$ServiceName
)
return (Get-MackerelServices | ? { $_.name -eq $ServiceName }).roles
}
function Get-MackerelHost {
param(
[String]
$ServiceName = "",
[String]
$RoleName = ""
)
if ([String]::IsNullOrWhiteSpace($ServiceName)) {
$ServiceName = (Get-MackerelServices | % { $_.name } | peco)
}
if ([String]::IsNullOrWhiteSpace($RoleName)) {
$RoleName = (Get-MackerelRoles -ServiceName $ServiceName | peco)
}
$ret = Invoke-RestMethod -Uri "https://mackerel.io/api/v0/hosts.json?service=$ServiceName&role=$RoleName" -Headers @{ "X-Api-Key" = $Env:MackerelApiKey }
return $ret.hosts | % { $_.name }
}
# 2015-09-08 add
function Get-MackerelSelfInfo {
return Get-MackerelHosts | ? { $_.name -eq $Env:COMPUTERNAME }
}
function Set-MackerelStatus {
param(
[String]
$Id,
[String]
$Status
)
$url = "https://mackerel.io/api/v0/hosts/{0}/status" -f $Id
$body = @{ "status" = $Status } | ConvertTo-Json
$header = @{
"X-Api-Key" = $Env:MackerelApiKey;
"Content-Type" = "application/json"
}
Invoke-RestMethod -Method Post -Uri $url -Headers $header -Body $body
}
function Get-MackerelId {
param(
[String]
$Hostname = $Env:COMPUTERNAME
)
$ret = Get-MackerelHosts | ? { $_.name -eq $Hostname }
return $ret[0].id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment