Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

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

特定の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 

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
2
Help us understand the problem. What are the problem?