martinm76 / zabbix-pdf-report

Reporting tool in PHP for the Zabbix Monitoring Platform. Rather versatile reporting on hosts and host groups.
https://www.zabbix.com/forum/zabbix-cookbook/25368-zabbix-dynamic-pdf-report-generation?filter_time=time_lastmonth
GNU General Public License v3.0
52 stars 55 forks source link

Empty groups list #37

Open dvdp opened 8 months ago

dvdp commented 8 months ago

Running Zabbix 6.4.10 - after updating ZabbixAPI.class.php reports are working again, however only for individual hosts, when I select 'Host Group', the list is empty. Checked the logs, didn't find any error so it looks to me that the line $host_groups = ZabbixAPI::fetch_array('hostgroup','get',array('output'=>array('groupid','name'),'real_hosts'=>'1','with_graphs'=>'1','sortfield'=>'name') ) ... returns an empty array. Any idea?

mdlr01 commented 6 months ago

Hi there,

I encountered a similar issue running Zabbix 6.4.10. The original line of code you mentioned seems to expect parameters (real_hosts and with_graphs) that no longer have direct support or behave differently in recent Zabbix API versions.

I managed to populate the 'Host Group' dropdown by adjusting the query to fetch all host groups first and then filter them based on whether they contain any hosts with graphs. Here's the adjusted approach:

Original Code:

// This original code might not work as expected due to parameter compatibility issues with newer Zabbix versions.

#$host_groups = ZabbixAPI::fetch_array('hostgroup','get', array('output'=>array('groupid','name'),'real_hosts'=>'1','with_graphs'=>'1','sortfield'=>'name') )
#    or die('Unable to get hosts: '.print_r(ZabbixAPI::getLastError(),true));

// First, fetch all host groups without applying 'real_hosts' and 'with_graphs' filters.

$host_groups = ZabbixAPI::fetch_array('hostgroup', 'get', array(
    'output' => array('groupid', 'name'),
    'sortfield' => 'name'
))
# or die('Unable to get host groups: ' . print_r(ZabbixAPI::getLastError(), true));

// Now, iterate over these groups to filter them according to specific criteria. $filtered_host_groups = []; foreach ($host_groups as $group) { // Make an API call to fetch hosts by group that have graphs. Adjust this part according to your implementation. $hosts = ZabbixAPI::fetch_array('host', 'get', array( 'output' => 'extend', 'groupids' => $group['groupid'], 'with_graphs' => true // Assuming this filter can be applied here. Adjust as needed. ));

// If hosts with graphs were found in the group, then include the group in the filtered list.
if (!empty($hosts)) {
    $filtered_host_groups[] = $group;
}

}

// Reassign the filtered groups back to $host_groups so the rest of the code is unaffected. $host_groups = $filtered_host_groups;

This workaround fetches and filters host groups to include only those that contain hosts with graphs. Please adjust the API call parameters ('output' => 'extend', 'with_graphs' => true) as per your Zabbix setup and API version compatibility.

Hope this helps!

martinm76 commented 6 months ago

Hmm... Interesting. I'll try to have a look in the next few days. Are you able to make a pull request or will I have to copy/paste this? A bit strange if they have changed this behaviour all of a sudden, but then again, they changed user/username a while back...

mdlr01 commented 5 months ago

Hello,

Sorry for the delay in responding. I really appreciate your willingness to look into this. I currently do not have experience with making pull requests on Git, but you are welcome to use and copy the code as needed. Thank you very much for your contributions and your help.

martinm76 commented 4 months ago

I decided to spend some time on this today. I believe I have put in a slightly modified version of your code with a fallback to the previous behaviour for older versions. Committing from a cloud VM seems to elude me, but I could luckily edit the files directly on GitHub, so hopefully everything is still as it should be. Can you give it a try? You must set $zabbix_vesion in config.inc.php to at least 6.2 for this change to take effect.