Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

特定の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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした