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));
- Hardcoding group ID for group "All"
$hostgroup[0] = "100100000000050";
- Get all hosts
$hosts = ZabbixAPI::fetch_column('host','get',array('extendoutput'=>1))
or die('Unable to get hosts: '.print_r(ZabbixAPI::getLastError(),true));
- 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)