使用zabbix api批量修改trigger級別

之前的模板沒有考慮到不同設備的trigger級別的區分。現在模板太多了。所以寫個批量腳本修改級別。

zabbix的trigger組織形式確實複雜(詳見下面的sql)。。。。。。。。。

回到zabbix api。

首先需要調用user.login方法獲得一個session id(貌似session id獲得之後是不會變的。反正我一個id用了好久)。

cat zbxapi_login.pl

use JSON::RPC::Client;

  my $client = new JSON::RPC::Client;

  my $uri    = 'http://6.86.3.14/zabbix/api_jsonrpc.php';

  my $callobj = {

     jsonrpc  => '2.0',

     method  => 'user.login',

     params => { user => 'admin', password => 'zabbix' } ,

     id  => '1',

  };

  my $res = $client->call($uri, $callobj);


  if($res) {

     if ($res->is_error) {

         print "Error : ", $res->error_message;

     }

     else {

         print $res->result;

     }

  }

  else {

     print $client->status_line;

  }

使用trigger.update修改trigger屬性。

cat zbxapi_trigger_se.pl

##description:set triggers to warning. the triggers verify by hosts groups and items.

##use as : perl zbxapi_trigger_se.pl 0487d1827fd9a14be022491a59b1dcc8

use Data::Dumper;

use JSON::RPC::Client;

use DBI;


sub connect_db {

my $dbh = DBI->connect('DBI:mysql:zabbix:6.86.3.14','zabbix','zabbix') or die  "cannot connect mysql:". DBI->errstr;

      return $dbh;

}


my $db = connect_db();

my $sth = $db->prepare(q{select triggerid from functions where itemid in   (   select itemid from items where name in    ('Free swap space in %','Host uptime (in sec)','CPU $2 time ($3)','Host status','Number of running processes $1')    and hostid in    (    select hostid from hosts_groups where groupid in ('28','41','43','44','45','46','48','54','90','91')    )   )}) or die $db->errstr;

       $sth->execute() or die $sth->errstr;

$ups = $sth->fetchall_arrayref();

foreach my $triggerid (@$ups)

  {

  my $client = new JSON::RPC::Client;

  my $uri    = 'http://6.86.3.14/zabbix/api_jsonrpc.php';

  my $callobj = {

     jsonrpc  => '2.0',

     method  => 'trigger.update',

     params => { triggerid => @$triggerid[0], priority => '2' } ,

     auth => $ARGV[0],

     id  => '1',

     };

  my $res = $client->call($uri, $callobj);

  if($res) {

     if ($res->is_error) {

         print "Error : ", $res->error_message;

     }

     else {

         print Dumper( $res->result);

     }

  }

  else {

     print $client->status_line;

  }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章