3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Webページの応答時間をPowerShellからMackerelにメトリック投稿する

Last updated at Posted at 2017-03-21

特定のWEBページにアクセスしたときの応答時間を、定期的にMackerelに送信してサイトの監視を行う。
newrelicのsyntheticsみたいな事をやる。
プライベートIPのWEBページなどの場合は、上記のサービスは使えないので、Powershellをタスクで実行してやってみる。

下記のメトリックの投稿のAPIを使う
https://mackerel.io/ja/api-docs/entry/host-metrics

APIキーについては下記を参照
https://mackerel.io/ja/api-docs/

hostIdについては対象のサーバのmackerel-agentが
Linuxであれば、/var/lib/mackerel-agent/idを参照
Windowsであれば、C:\Program Files (x86)\Mackerel のなかにIDファイルがあるはず。

下記をタスクで5分おき等で実行。


$url = "http://サイトのURL";

$request = New-Object System.Net.WebClient
$request.UseDefaultCredentials = $true
$request.UserAgent = Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13F69 Safari/601.1"

$start = Get-Date

$pageRequest = $request.DownloadString($url)
$timetaken = ((Get-Date) - $start).TotalMilliseconds / 1000
$request.Dispose()

$headers = @{
    "X-Api-Key" = "***********************"
    "Content-Type" = "application/json"}

$metric = '[{"hostId": "*****","name": "custom.適当なメトリック名", "time": '+$utime+', "value": '+$TimeTaken+'}]'

Invoke-RestMethod -Uri https://mackerel.io/api/v0/tsdb -Method Post -Headers $headers -Body $metric 

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?