Uploaded image for project: 'ZABBIX BUGS AND ISSUES'
  1. ZABBIX BUGS AND ISSUES
  2. ZBX-2302

API method hostgroup.massadd creates duplicate entries in the db

XMLWordPrintable

    • Icon: Incident report Incident report
    • Resolution: Fixed
    • Icon: Major Major
    • 1.8.3
    • 1.8.2
    • API (A)
    • None

      The script below uses API call hostgroup.massadd to add all hosts to "All" group. If run more than once, database table hosts_groups will have duplicate host-to-group membership entries (those been causing Zabbix performance to be really poor).

      <?php
      require_once("ZabbixAPI.class.php");

      // This enables debugging, this is rather verbose but can help debug problems
      #ZabbixAPI::debugEnabled(TRUE);

      // This logs into Zabbix, and returns false if it fails
      ZabbixAPI::login('http://localhost/zabbix/','apiuser','cut')
      or die('Unable to login: '.print_r(ZabbixAPI::getLastError(),true));

      1. Hardcoding group ID for group "All"
        $hostgroup[0] = "100100000000050";
      1. Get all hosts
        $hosts = ZabbixAPI::fetch_column('host','get',array('extendoutput'=>1))
        or die('Unable to get hosts: '.print_r(ZabbixAPI::getLastError(),true));
      1. Add all hosts to "All" group
        $result = ZabbixAPI::fetch_array('hostgroup','massadd',array('groups'=>$hostgroup,'hosts'=>$hosts))
        or die('Unable to get hosts: '.print_r(ZabbixAPI::getLastError(),true));
        #print "All group members: ".print_r($result, true)."\n<br>";
        ?>

      After having this script running on a hourly basis I got this:

      mysql> select count from groups;
      ----------

      count

      ----------

      106

      ----------
      1 row in set (0.00 sec)

      mysql> select count from hosts;
      ----------

      count

      ----------

      537

      ----------
      1 row in set (0.00 sec)

      mysql> select count from hosts_groups;
      ----------

      count

      ----------

      198631

      ----------
      1 row in set (0.00 sec)

      mysql> select count from hosts_groups where groupid='100100000000050'; # <- All group
      ----------

      count

      ----------

      197568

      ----------
      1 row in set (0.08 sec)

            Unassigned Unassigned
            ivan.gusev Ivan Gusev
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: