Zabbix Multi Tenant IT Services Working on 2.2.1
Autor: Jonathan Araujo | Publicado: 2013-12-14T21:29:00-02:00

After a long time, here's another (short) post. WH1 is eating up my time. I decided to post just in English from now on, as it will broaden the public.

&nbsp;Zabbix is a very nice tool but I won't talk about it. If you don't know, check it at: www.zabbix.com.

Using Zabbix in a multi tenant server, feel the lack of avoiding one user to see the Services of each other. It won't see any trigger from other group, but top level SLA is still visible.

&nbsp;Check this post for the complete problem description:
https://www.zabbix.com/forum/showthread.php?t=23482&nbsp;

&nbsp;Someone sugested a solution, but it didn't work for 2.2.1 version I'm using.



With a bit of hack, I managed to get it working with 2.2.1 easily. Sorry for the messy code. At last it works nice for me.

Steps:

1) &nbsp;In the include/services.inc.php, locate and change the "if" block bellow. This is the one right after the  comment:

// hard dependencies and dependencies for the "root" node



if (!$dependency || $dependency['soft'] == 0) { 

               
                        $tree[$serviceNode['id']] = $serviceNode;

                        foreach ($service['dependencies'] as $dependency) {

                                $childService = $services[$dependency['servicedownid']];

                                createServiceMonitoringTree($services, $slaData, $period, $tree, $service, $childService, $dependency);
                        }
                
        }

          

Change it to:

if (!$dependency || $dependency['soft'] == 0) { 

                // Show logic
                if( checkServiceVisibility($serviceNode['id'],$services)){
                        $tree[$serviceNode['id']] = $serviceNode;

                        foreach ($service['dependencies'] as $dependency) {

                                $childService = $services[$dependency['servicedownid']];

                                createServiceMonitoringTree($services, $slaData, $period, $tree, $service, $childService, $dependency);
                        }
                }
        }




Keep the else block as it is.


2) Include the following function somewhere in the same file.


/**
&nbsp;* Check if a service should be visible or not.
&nbsp;* If it do not have a last one children service that is a trigger, do not show
&nbsp;* It's recursive function to go deep into the tree.
&nbsp;*
&nbsp;* @param array $services &nbsp; &nbsp; &nbsp; an array of services to display in the tree
&nbsp;* @param $serviceid
&nbsp;*
&nbsp;* @return bool
&nbsp;*/
function checkServiceVisibility($id,$services){
&nbsp; &nbsp; &nbsp; &nbsp; $r = false;
&nbsp; &nbsp; &nbsp; &nbsp; // root is always shown
&nbsp; &nbsp; &nbsp; &nbsp; if($id == 0 ){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; // If &nbsp;$service has a trigger, it should be show
&nbsp; &nbsp; &nbsp; &nbsp; if(count($services[$id]['trigger']) &gt; 0){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; // It doesn't have a trigger, let's check at last one of the children have
&nbsp; &nbsp; &nbsp; &nbsp; else{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( $services[$id]['dependencies'] as $children ){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($children['servicedownid']);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( checkServiceVisibility($children['servicedownid'],$services)){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;
&nbsp; &nbsp; &nbsp; &nbsp; }

}



&nbsp;Probably there's a better way to do it, but I'm too lazy to go deep on how the code works. It would be nice if Zabbix guys could implement this on future versions, so I can upgrade easily.