ラベル Zabbix API の投稿を表示しています。 すべての投稿を表示
ラベル Zabbix API の投稿を表示しています。 すべての投稿を表示

2015/11/08

Power BI - 11 - Zabbix API を Power Query で使ってみる

Web.Contents (Power Query) を使うとコンテンツのダウンロードができる。バイナリコンテンツを加工することで必要なデータにすることができるのだけど、POST が必要な Webサービスもあるので。
Web.Contents(url as text, optional options as nullable record) as binary
引数 "options" を使ってリクエスト時に Content などを渡すとよい。そこで、Zabbix を使うことがあったので、Power Query + Zabbix API を試してみた。なお、Zabbix API は JSON-RPC というプロトコルなので、JSON 形式のパラメータを送信 / JSON 形式で結果を受信する。Content-Type は application/json-rpc。

例えば、Zabbix API のバージョンを得るためのリクエストは、
{
    "jsonrpc": "2.0",
    "method": "apiinfo.version",
    "params": [],
    "id": 1
}
こんな感じなので、Power Query では
let
    ZabbixServer = "http://Server/zabbix/api_jsonrpc.php",
    RequerstJSON = Text.Format(
         "{""jsonrpc"":""2.0"",""method"":""apiinfo.version"",""id"":#{0}}",
         {Number.Round(Number.RandomBetween(1,9999))}
    ),
    Options = [Headers = [#"Content-Type" = "application/json-rpc"],
               Content = Text.ToBinary(RequerstJSON)],
    Response = Web.Contents(ZabbixServer, Options),
    ImportedJSON = try Json.Document(Response),
    Result = if ImportedJSON[HasError] 
             then ImportedJSON[Error]
             else if ImportedJSON[Value][id] = null 
                  then ImportedJSON[Value][error]
                  else ImportedJSON[Value]
in
    Result