VMwareの出しているログ管理サーバvRealize Log Insight を使っている環境で、別製品のログもLog Insightで管理できるようにしたい、という要求があった。
該当するログを保存しているサーバはWindows Serverだというので、PowerShellを使用してログを整形し、JSONデータにしてLog Insightサーバに送りつけたのだが、単純に送りつけるだけでは日本語を正常な文字列として認識しなかった。
JSONデータ変換後のデータに対して「[System.Text.Encoding]::UTF8.GetBytes」のエンコードをしてやることで、UTF8のデータとして認識させることができた。
$loginsightserver = "IPアドレス" $loginsightagentID = "34859a98" $result="ジョブの結果" $jobid = [ordered]@{ name = "jobid" content = "ジョブのID" } $appid = [ordered]@{ name = "appid" content = "アプリのID" } $fields = @($jobid,$appid) $restcall = @{ messages = ([Object[]]($messages = [ordered]@{ text = $result fields = ([Object[]]$fields) })) } |convertto-json -Depth 4 $encodedrestcall= [System.Text.Encoding]::UTF8.GetBytes($restcall) Write-Host $restcall $resturl = ("http://" + $loginsightserver + ":9000/api/v1/messages/ingest/" + $loginsightagentID) write-host ("Posting results to Log Insight server: " + $loginsightserver) try { $response = Invoke-RestMethod $resturl -Method Post -Body $encodedrestcall -ContentType 'application/json' -ErrorAction stop write-host "REST Call to Log Insight server successful" write-host $response } catch { write-host "REST Call failed to Log Insight server" write-host $error[0] write-host $resturl }
2019/06/19追記
上記の解説
Log Insightへのログ登録は「/api/v1/events/ingest/{agentId}」を使っている。
1つのフィールドの最大サイズは16KB、JSON化したあとの合計サイズとしては4MBまでをサポートしている。
冒頭で設定している「loginsightagentID」は適当な文字列を入れている。2019/06/19時点の「Getting started with the Log Insight REST API」で示されているサンプルでは「{UUID}」とされており「 aexample-uuid-4b7a-8b09-fbfac4b46fd9 」という文字列が使われているので、なんでも良いらしい。
ログを一括登録する場合の注意点として、現在は上記ドキュメントにも書かれているが、現在時刻から10分以上ズレているタイムスタンプは認めてくれない、というのが標準設定となっている。このため、10分以上ズレているログは、送信した時刻で記録されてしまう仕様となっている。
The timestamp of an event is optional, and expressed as milliseconds-since-epoch UTC. If the submission is authenticated, the timestamp field is trusted. Unauthenticated event submissions have their time clamped to within 10 minutes of the server’s current time.
% config.api-server.max-tolerated-client-time-drift=600000
この制限は「 config.api-server.max-tolerated-client-time-drift 」の値を変更することで解除できる。2018年初頭のバージョンだと「vRealize Log Insightで現在時刻±10分以外のデータも取り込む」に記載した手順で変更することができる。