Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use Net::Domain qw(hostname hostfqdn);
- use JSON;
- use LWP::UserAgent;
- use HTTP::Request;
- use HTTP::Status;
- use Data::Dumper;
- use strict;
- my $base = "http://zabbix.fresh";
- my $api = $base . "/api_jsonrpc.php";
- my $api_user = "_api";
- my $api_pass = "_api_pass_word_";
- my $req_id = 0;
- my $auth_token;
- sub request {
- my ($url, $method, $data) = @_;
- my $json = encode_json({"jsonrpc" => "2.0", "method" => $method, "params" => $data, "auth" => $auth_token, "id" => $req_id++});
- my $req = HTTP::Request->new(POST => $url);
- $req->content_type('application/json');
- $req->content($json);
- my $ua = LWP::UserAgent->new;
- my $res = $ua->request($req);
- die "HTTP protocol error: " . $res->code if not is_success($res->code);
- my $resp = decode_json($res->decoded_content);
- die "Zabbix API error: " . Dumper($resp->{'error'}) if $resp->{'error'};
- return $resp->{'result'};
- }
- $auth_token = request($api, "user.login", { "user" => $api_user, "password" => $api_pass });
- my @res;
- @res = `/sbin/ip -o -4 route show to default`;
- my ($defgw_dev) = $res[0] =~ / dev (\w+)/;
- $defgw_dev = " dev $defgw_dev" if $defgw_dev;
- @res = `/sbin/ip -o -4 addr show scope global$defgw_dev`;
- my ($ip) = $res[0] =~m|inet ([\d\.]+)/\d+ brd|;
- my $host_config = {
- "host" => hostname,
- "interfaces" => [{
- "type" => 1,
- "main" => 1,
- "useip" => 1,
- "ip" => $ip,
- "dns" => hostfqdn,
- "port" => "10050"
- }],
- "groups" => [
- { "groupid" => "2" }, # Linux Servers
- { "groupid" => "18" } # PostgreSQL Servers
- ],
- "templates" => [
- { "templateid" => "10109" },# T_Linux
- { "templateid" => "10107" } # T_DiskStats
- ]
- };
- my $res = request($api, "host.create", $host_config);
- print Dumper($res);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement