njsuntop 发表于 2019-1-19 14:11:28

使用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, priority => '2' } ,
        auth => $ARGV,
        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;
     }
  }



页: [1]
查看完整版本: 使用zabbix api批量修改trigger级别