Zabbix APIを叩いて、日本語ホストグループ名で登録したいのだが…

ZabbixのAPIを叩いて、ホストグループやホストの一括登録をやらせるプログラムを書いているのだけど、日本語の扱いで上手く行っていなくて、悩んでいたりする。

Zabbix自身、GUIからであればホストグループ名やホストの表示名に日本語が使えることはわかっているので、API経由での登録処理で生成するJSONデータの中での2バイトコードの扱いが正しく出来れば問題がなくなるのだろうけど、その辺りの情報が少ないんだよね。

どうやっているかというと…

今、参考にしているのは、ここ。

Zabbix API starts to play significant role especially when it comes to integration of Zabbix with third-party software like configuration and incident management systems as well as for automation of routine tasks. It is incredibly difficult to manage monitoring of thousands of hosts without some automation in place!

[From Getting started with Zabbix API | Zabbix Weblog]

にあるホスト名を取得して一覧表示をするスクリプト。


これに手を入れて、登録したいホストグループ名を書きだしたテキストファイルを標準入力から読み込んで、ホストグループを作成するスクリプトが、コレ。

#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use JSON::RPC::Client;
use Data::Dumper;
# Authenticate yourself
my $client = new JSON::RPC::Client;
my $url = 'http://172.16.0.254/zabbix/api_jsonrpc.php';
my $authID;
my $response;
my $json = {
    jsonrpc => "2.0",
    method => "user.login",
    params => {
        user => "admin",
        password => "zabbix"
    },
    id => 1
};
$response = $client->call($url, $json);
# Check if response was successful
die "Authentication failed\n" unless $response->content->{'result'};
$authID = $response->content->{'result'};
print "Authentication successful. Auth ID: " . $authID . "\n-----\n";
while (my $hostgroup = <STDIN> ) {
    chomp($hostgroup);
    $json = {
        jsonrpc=> '2.0',
        method => 'hostgroup.create',
        params => {
            name => "$hostgroup",
        },
        id => 2,
        auth => "$authID",
    };
    $response = $client->call($url, $json);
    print $hostgroup." :\n\t-> ".JSON->new->encode($response->content)."\n\n";
};

これを、"ZBX_groupcreate.pl"という名前で保存。
投入したいデータは、下のように作成。

[soukaku@messiah|~]$ more scripts/tmp/group_test.txt
テスト用グループ(日本語表記) 01
テスト用グループ(日本語表記) 02
テスト用グループ(日本語表記) 03
テスト用グループ(日本語表記) 04
テスト用グループ(日本語表記) 05
テスト用グループ(日本語表記) 06
テスト用グループ(日本語表記) 07

で、"ZBX_groupcreate.pl"にホストグループ一覧のテキストを標準入力に食わせるようにして実行してみる。

[soukaku@messiah|~]$ perl scripts/ZBX_groupcreate.pl < scripts/tmp/group_test.txt
Authentication successful. Auth ID: 5497e183506bd44369be7a86ed9d08dc
-----
テスト用グループ(日本語表記) 01 :
	-> {"jsonrpc":"2.0","id":2,"result":{"groupids":["42"]}}
テスト用グループ(日本語表記) 02 :
	-> {"jsonrpc":"2.0","id":2,"result":{"groupids":["43"]}}
テスト用グループ(日本語表記) 03 :
	-> {"jsonrpc":"2.0","id":2,"result":{"groupids":["44"]}}
テスト用グループ(日本語表記) 04 :
	-> {"jsonrpc":"2.0","id":2,"result":{"groupids":["45"]}}
テスト用グループ(日本語表記) 05 :
	-> {"jsonrpc":"2.0","id":2,"result":{"groupids":["46"]}}
テスト用グループ(日本語表記) 06 :
	-> {"jsonrpc":"2.0","id":2,"result":{"groupids":["47"]}}
テスト用グループ(日本語表記) 07 :
	-> {"jsonrpc":"2.0","id":2,"result":{"groupids":["48"]}}

実行そのものにはエラーが出ないのだけど、GUIで登録された結果を確認すると、文字化けした状態で登録されているのが判る。

ホストグループの設定画面(一部)

ちなみに、正しく表示されているのは、GUIで入力した分。
API経由でホストグループ一覧を取得して、日本語で正しく表示されている分と正しく登録できていない分を見比べてみると、

                          'name' => "\x{30c6}\x{30b9}\x{30c8}\x{7528}\x{30b0}\x{30eb}\x{30fc}\x{30d7}\x{ff08}\x{65e5}\x{672c}\x{8a9e}\x{8868}\x{8a18}\x{ff09} 02",
                          'groupid' => '20'
                        },
                        {
                          'name' => "\x{e3}\x{83}\x{86}\x{e3}\x{82}\x{b9}\x{e3}\x{83}\x{88}\x{e7}\x{94}\x{a8}\x{e3}\x{82}\x{b0}\x{e3}\x{83}\x{ab}\x{e3}\x{83}\x{bc}\x{e3}\x{83}\x{97}\x{ef}\x{bc}\x{88}\x{e6}\x{97}\x{a5}\x{e6}\x{9c}\x{ac}\x{e8}\x{aa}\x{9e}\x{e8}\x{a1}\x{a8}\x{e8}\x{a8}\x{98}\x{ef}\x{bc}\x{89} 01",
                          'groupid' => '42'
                        },

というように、格納されているデータの形が違うことが判る。

「ここを何とか出来れば」と思いながら、行き当たりばったりに、ググってその結果に書いてることを試して、というのを繰り返しているのだけど、どうも上手く行かなくてクリンチ状態に陥っているんだよねぇ…。う〜む。

#ホストグループに日本語名を使う、というのが少数派なのかな〜。
#ココをクリアすれば、ホストの方の表示名も日本語表記で一括登録できるように出来るんだけどなぁ〜。

トラックバック(1)

昨日エントリーした ZabbixのAPIを叩いて、ホストグループやホストの一括登録をやらせるプログラムを書いているのだけど、日本語の扱いで上手く行っていな... 続きを読む

コメントする