Last active
November 4, 2015 10:43
-
-
Save hkdnet/2fbf6244010a72259a10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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