Beispiel #1
0
   /**
    * Get comments for host
    * $id, host id
    *    default is current host instance
    *
    */
   function getComments($id=-1) {
      global $CFG_GLPI;

      if ($id == -1) {
         $pm_Host = $this;
      } else {
         $pm_Host = new PluginMonitoringHost();
         $pm_Host->getFromDB($id);
      }

      // Toolbox::logInFile("pm", "Host getcomments : $id : ".$pm_Host->getID()."\n");
      $comment = "";
      $toadd   = array();

      // associated computer ...
      $item = new $pm_Host->fields['itemtype'];
      $item->getFromDB($pm_Host->fields['items_id']);

      if ($pm_Host->getField('itemtype') == 'Computer') {
         if ($item->isField('completename')) {
            $toadd[] = array('name'  => __('Complete name'),
                             'value' => nl2br($item->getField('completename')));
         }

         $type = new ComputerType();
         if ($item->getField("computertypes_id")) {
            $type->getFromDB($item->getField("computertypes_id"));
            $type = $type->getName();
            if (! empty($type)) {
               $toadd[] = array('name'  => __('Type'),
                                'value' => nl2br($type));
            }
         } else {
            return $comment;
         }

         $model = new ComputerModel();
         if ($item->getField("computermodels_id")) {
            $model->getFromDB($item->getField("computermodels_id"));
            $model = $model->getName();
            if (! empty($model)) {
               $toadd[] = array('name'  => __('Model'),
                                'value' => nl2br($model));
            }
         }

         $state = new State();
         $state->getFromDB($item->fields["states_id"]);
         $state = $state->getName();
         if (! empty($state)) {
            $toadd[] = array('name'  => __('State'),
                             'value' => nl2br($state));
         }

         $entity = new Entity();
         $entity->getFromDB($item->fields["entities_id"]);
         $entity = $entity->getName();
         if (! empty($entity)) {
            $toadd[] = array('name'  => __('Entity'),
                             'value' => nl2br($entity));
         }

         $location = new Location();
         $location->getFromDB($item->fields["locations_id"]);
         $location = $location->getName(array('complete'  => true));
         if (! empty($location)) {
            $toadd[] = array('name'  => __('Location'),
                             'value' => nl2br($location));
         }

         if (! empty($item->fields["serial"])) {
            $toadd[] = array('name'  => __('Serial'),
                             'value' => nl2br($item->fields["serial"]));
         }
         if (! empty($item->fields["otherserial"])) {
            $toadd[] = array('name'  => __('Inventory number'),
                             'value' => nl2br($item->fields["otherserial"]));
         }

         if (($pm_Host instanceof CommonDropdown)
             && $pm_Host->isField('comment')) {
            $toadd[] = array('name'  => __('Comments'),
                             'value' => nl2br($pm_Host->getField('comment')));
         }

         if (count($toadd)) {
            foreach ($toadd as $data) {
               $comment .= sprintf(__('%1$s: %2$s')."<br>",
                                   "<span class='b'>".$data['name'], "</span>".$data['value']);
            }
         }
      } else {
         $toadd[] = array('name'  => __('Host type'),
                          'value' => nl2br($item->getTypeName()));

         if ($item->isField('completename')) {
            $toadd[] = array('name'  => __('Complete name'),
                             'value' => nl2br($item->getField('completename')));
         }
      }

      if (!empty($comment)) {
         return Html::showToolTip($comment, array('display' => false));
      }
   }
   function generateHostsCfg($file=0, $tag='') {
      global $DB;

      // Log Shinken restart event with Tag information ...
      $pmLog = new PluginMonitoringLog();
      if (isset($_SERVER['HTTP_USER_AGENT'])
              AND strstr($_SERVER['HTTP_USER_AGENT'], 'xmlrpclib.py')) {
         if (!isset($_SESSION['glpi_currenttime'])) {
            $_SESSION['glpi_currenttime'] = date("Y-m-d H:i:s");
         }
         $input = array();
         $input['user_name'] = "Shinken";
         $input['action'] = "restart";
         $input['date_mod'] = date("Y-m-d H:i:s");
         $input['value'] = $tag;
         $pmLog->add($input);
      }

      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         "Starting generateHostsCfg ($tag) ...\n"
      );
      $pmCommand     = new PluginMonitoringCommand();
      $pmCheck       = new PluginMonitoringCheck();
      $pmComponent   = new PluginMonitoringComponent();
      $pmEntity      = new PluginMonitoringEntity();
      $pmHostconfig  = new PluginMonitoringHostconfig();
      $pmHost        = new PluginMonitoringHost();
      $calendar      = new Calendar();
      $pmRealm       = new PluginMonitoringRealm();
      $networkEquipment = new NetworkEquipment();
      $pmContact_Item   = new PluginMonitoringContact_Item();
      $profile_User     = new Profile_User();
      $pmEventhandler = new PluginMonitoringEventhandler();
      $user = new User();

      $a_hosts = array();
      $i=0;
      $a_parents_found = array();
      $a_hosts_found = array();

      $a_entities_allowed = $pmEntity->getEntitiesByTag($tag);
      $a_entities_list = array();
      foreach ($a_entities_allowed as $entity) {
         $a_entities_list = getSonsOf("glpi_entities", $entity);
      }
      $where = '';
      if (! isset($a_entities_allowed['-1'])) {
         $where = getEntitiesRestrictRequest("WHERE", "glpi_entities", '', $a_entities_list);
      }


      // * Prepare contacts
      $a_contacts_entities = array();
      $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'
         AND `users_id`>0");
      foreach ($a_list_contact as $data) {
         $contactentities = getSonsOf('glpi_entities', $data['entities_id']);
         if (isset($a_contacts_entities[$data['items_id']][$data['users_id']])) {
            $contactentities = array_merge($contactentities, $a_contacts_entities[$data['items_id']][$data['users_id']]);
         }
         $a_contacts_entities[$data['items_id']][$data['users_id']] = $contactentities;
      }

      // $query = "SELECT *
         // FROM `glpi_plugin_monitoring_componentscatalogs_hosts`
         // GROUP BY `itemtype`, `items_id`";
      $query = "SELECT
         `glpi_plugin_monitoring_componentscatalogs_hosts`.*,
         `glpi_computers`.`id`,
         `glpi_entities`.`id` AS entityId, `glpi_entities`.`name` AS entityName, `glpi_entities`.`completename` AS entityFullName,
         `glpi_locations`.`id`, `glpi_locations`.`completename` AS locationName,
         `glpi_locations`.`comment` AS locationComment, `glpi_locations`.`building` AS locationGPS,
         `glpi_plugin_monitoring_services`.`networkports_id`
         FROM `glpi_plugin_monitoring_componentscatalogs_hosts`
         LEFT JOIN `glpi_computers` ON `glpi_computers`.`id` = `glpi_plugin_monitoring_componentscatalogs_hosts`.`items_id`
         LEFT JOIN `glpi_entities` ON `glpi_computers`.`entities_id` = `glpi_entities`.`id`
         LEFT JOIN `glpi_locations` ON `glpi_locations`.`id` = `glpi_computers`.`locations_id`
         LEFT JOIN `glpi_plugin_monitoring_services`
            ON `glpi_plugin_monitoring_services`.`plugin_monitoring_componentscatalogs_hosts_id`
               = `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`
         $where
         GROUP BY `itemtype`, `items_id`";
      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         "Hosts: $query\n"
      );
      $result = $DB->query($query);
      while ($data=$DB->fetch_array($result)) {
         // Toolbox::logInFile("pm-shinken", " - fetch host ".$data['itemtype']." / ".$data['items_id']."\n");

         $classname = $data['itemtype'];
         $class = new $classname;
         if ($class->getFromDB($data['items_id'])) {

            // if (isset($a_entities_allowed['-1'])
                    // OR isset($a_entities_allowed[$class->fields['entities_id']])) {

               $pmHost->getFromDBByQuery("WHERE `glpi_plugin_monitoring_hosts`.`itemtype` = '" . $data['itemtype'] . "' AND `glpi_plugin_monitoring_hosts`.`items_id` = '" . $data['items_id'] . "' LIMIT 1");

               $a_hosts[$i]['host_name'] = preg_replace("/[^A-Za-z0-9\-_]/","",$class->fields['name']);
               // Fix if hostname is not defined ...
               if (empty($a_hosts[$i]['host_name'])) {
                  continue;
               }
               $a_hosts_found[$a_hosts[$i]['host_name']] = 1;
               PluginMonitoringToolbox::logIfExtradebug(
                  'pm-shinken',
                  " - add host ".$a_hosts[$i]['host_name']."\n"
               );

               $a_hosts[$i]['_HOSTID'] =
                  $pmHost->getField('id');
               if (isset(self::$shinkenParameters['glpi']['entityId'])) {
                  $a_hosts[$i][self::$shinkenParameters['glpi']['entityId']] =
                     $data['entityId'];
               }
               if (isset(self::$shinkenParameters['glpi']['itemType'])) {
                  $a_hosts[$i][self::$shinkenParameters['glpi']['itemType']] =
                     $classname;
               }
               if (isset(self::$shinkenParameters['glpi']['itemId'])) {
                  $a_hosts[$i][self::$shinkenParameters['glpi']['itemId']] =
                     $data['items_id'];
               }

               if (isset(self::$shinkenParameters['glpi']['entityName'])) {
                  $a_hosts[$i][self::$shinkenParameters['glpi']['entityName']] =
                     strtolower(preg_replace("/[^A-Za-z0-9\-_]/","",$data['entityName']));
               }

               $data['entityFullName'] = preg_replace("/ > /","#",$data['entityFullName']);
               $data['entityFullName'] = preg_replace("/". self::$shinkenParameters['glpi']['rootEntity'] ."#/","",$data['entityFullName']);
               $data['entityFullName'] = preg_replace("/#/","_",$data['entityFullName']);
               if (isset(self::$shinkenParameters['glpi']['entityComplete'])) {
                  $a_hosts[$i][self::$shinkenParameters['glpi']['entityComplete']] =
                     strtolower(preg_replace("/[^A-Za-z0-9\-_]/","",$data['entityFullName']));
               }
               $data['entityFullName'] = preg_replace("/_/",".",$data['entityFullName']);

               if (isset(self::$shinkenParameters['glpi']['location'])) {
                  if (! empty($data['locationName'])) {
                     // Toolbox::logInFile("pm-shinken", " - location: ".$data['locationName']."\n");
                     $string = utf8_decode(strip_tags(trim($data['locationName'])));
                     // $string = Toolbox::decodeFromUtf8($data['locationName']);
                     $string = preg_replace("/[\r\n]/",".",$data['locationName']);
                     $string = preg_replace("/[^A-Za-z0-9\-_ <>\',;.:!?%*()éèàù]/",'',$string);
                     // Toolbox::logInFile("pm-shinken", " - location: ".$string."\n");
                     $a_hosts[$i][self::$shinkenParameters['glpi']['location']] =
                        $string;
                  }
               }

               if (isset(self::$shinkenParameters['graphite']['prefix']['name'])) {
                  $a_hosts[$i][self::$shinkenParameters['graphite']['prefix']['name']] =
                     strtolower(self::$shinkenParameters['graphite']['prefix']['value'] . preg_replace("/[^A-Za-z0-9\-_.]/","",$data['entityFullName']));
               }

               if (isset(self::$shinkenParameters['glpi']['lat'])) {
                  if (! empty($data['locationGPS'])) {
                     $split = explode(',', $data['locationGPS']);
                     if (count($split) > 2) {
                        // At least 3 elements, let us consider as GPS coordinates with altitude ...
                        $a_hosts[$i][self::$shinkenParameters['glpi']['lat']] = $split[0];
                        $a_hosts[$i][self::$shinkenParameters['glpi']['lng']] = $split[1];
                        $a_hosts[$i][self::$shinkenParameters['glpi']['alt']] = $split[2];
                     } else if (count($split) > 1) {
                        // At least 2 elements, let us consider as GPS coordinates ...
                        $a_hosts[$i][self::$shinkenParameters['glpi']['lat']] = $split[0];
                        $a_hosts[$i][self::$shinkenParameters['glpi']['lng']] = $split[1];
                     // } else {
                        // $a_hosts[$i]['_LOC_BUILDING'] = preg_replace("/[\r\n]/",".",$data['locationGPS']);
                        // $a_hosts[$i]['_LOC_BUILDING'] = preg_replace("/[^A-Za-z0-9\-_]/"," / ",$a_hosts[$i]['_LOC_BUILDING']);
                     }
                  }
               }

               // Hostgroup name
               $a_hosts[$i]['hostgroups'] = strtolower(preg_replace("/[^A-Za-z0-9\-_ ]/","",$data['entityName']));
               $a_hosts[$i]['hostgroups'] = preg_replace("/[ ]/","_",$a_hosts[$i]['hostgroups']);

               // Alias
               $a_hosts[$i]['alias'] = $data['entityName']." / ". $a_hosts[$i]['host_name'];
               if (isset($class->fields['networkequipmenttypes_id'])) {
                  if ($class->fields['networkequipmenttypes_id'] > 0) {
                     $a_hosts[$i]['alias'] .= " (".Dropdown::getDropdownName("glpi_networkequipmenttypes", $class->fields['networkequipmenttypes_id']).")";
                  }
               } else if (isset($class->fields['computertypes_id'])) {
                  if ($class->fields['computertypes_id'] > 0) {
                     $a_hosts[$i]['alias'] .= " (".Dropdown::getDropdownName("glpi_computertypes", $class->fields['computertypes_id']).")";
                  }
               } else if (isset($class->fields['printertypes_id'])) {
                  if ($class->fields['printertypes_id'] > 0) {
                     $a_hosts[$i]['alias'] .= " (".Dropdown::getDropdownName("glpi_printertypes", $class->fields['printertypes_id']).")";
                  }
               }

               // WebUI user interface ...
               if (isset(self::$shinkenParameters['webui']['hostIcons']['name'])) {
                  $a_hosts[$i][self::$shinkenParameters['webui']['hostIcons']['name']] =
                     self::$shinkenParameters['webui']['hostIcons']['value'];
               }
               if (isset(self::$shinkenParameters['webui']['hostView']['name'])) {
                  $a_hosts[$i][self::$shinkenParameters['webui']['hostView']['name']] =
                     self::$shinkenParameters['webui']['hostView']['value'];
               }

               // IP address
               $ip = PluginMonitoringHostaddress::getIp($data['items_id'], $data['itemtype'], $class->fields['name']);
               $a_hosts[$i]['address'] = $ip;

               // Manage dependencies
               $parent = '';
               if ($data['itemtype'] != 'NetworkEquipment') {
                  $networkPort = new NetworkPort();
                  $a_networkports = $networkPort->find("`itemtype`='".$data['itemtype']."'
                     AND `items_id`='".$data['items_id']."'");
                  foreach ($a_networkports as $data_n) {
                     $networkports_id = $networkPort->getContact($data_n['id']);
                     if ($networkports_id) {
                        $networkPort->getFromDB($networkports_id);
                        if ($networkPort->fields['itemtype'] == 'NetworkEquipment') {
                           $networkEquipment->getFromDB($networkPort->fields['items_id']);
                           $parent = preg_replace("/[^A-Za-z0-9\-_]/","",$networkEquipment->fields['name']);
                           $a_parents_found[$parent] = 1;
                           $pmHost->updateDependencies($classname, $data['items_id'], 'NetworkEquipment-'.$networkPort->fields['items_id']);
                        }
                     }
                  }

                  if (empty($parent)) {
                     if (self::$shinkenParameters['shinken']['hosts']['parents'] == 'entity') {
                        $parent = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . $a_hosts[$i]['hostgroups'];
                     } else {
                        $parent = self::$shinkenParameters['shinken']['hosts']['parents'];
                     }
                  }
               }
               $a_hosts[$i]['parents'] = $parent;

               $a_fields = array();

               $pmComponent->getFromDB($pmHostconfig->getValueAncestor('plugin_monitoring_components_id',
                                                                        $class->fields['entities_id'],
                                                                        $classname,
                                                                        $class->getID()));

               $pmCommand->getFromDB($pmComponent->fields['plugin_monitoring_commands_id']);

               $a_fields = $pmComponent->fields;

               // Manage host check_command arguments
               // Toolbox::logInFile("pm-shinken", "Command line : ".$pmCommand->fields['command_line']."\n");
               // Toolbox::logInFile("pm-shinken", "Arguments : ".$a_fields['arguments']."\n");
               // Toolbox::logInFile("pm-shinken", "Arguments : ".$pmCommand->fields['arguments']."\n");

               $array = array();
               preg_match_all("/\\$(ARG\d+)\\$/", $pmCommand->fields['command_line'], $array);
               sort($array[0]);
               $a_arguments = importArrayFromDB($pmCommand->fields['arguments']);
               $a_argumentscustom = importArrayFromDB($pmComponent->fields['arguments']);
               foreach ($a_argumentscustom as $key=>$value) {
                  $a_arguments[$key] = $value;
               }
               foreach ($a_arguments as $key=>$value) {
                  $a_arguments[$key] = str_replace('!', '\!', html_entity_decode($value));
               }
               $args = '';
               foreach ($array[0] as $arg) {
                  if ($arg != '$PLUGINSDIR$'
                          AND $arg != '$NAGIOSPLUGINSDIR$'
                          AND $arg != '$HOSTADDRESS$'
                          AND $arg != '$MYSQLUSER$'
                          AND $arg != '$MYSQLPASSWORD$') {
                     $arg = str_replace('$', '', $arg);
                     if (!isset($a_arguments[$arg])) {
                        $args .= '!';
                     } else {
                        if (strstr($a_arguments[$arg], "[[HOSTNAME]]")) {
                           $a_arguments[$arg] = str_replace("[[HOSTNAME]]", $hostname, $a_arguments[$arg]);
                        } elseif (strstr($a_arguments[$arg], "[[NETWORKPORTDESCR]]")){
                           if (class_exists("PluginFusioninventoryNetworkPort")) {
                              $pfNetworkPort = new PluginFusioninventoryNetworkPort();
                              $pfNetworkPort->loadNetworkport($data['networkports_id']);
                              $descr = $pfNetworkPort->getValue("ifdescr");
                              $a_arguments[$arg] = str_replace("[[NETWORKPORTDESCR]]", $descr, $a_arguments[$arg]);
                           }
                        } elseif (strstr($a_arguments[$arg], "[[NETWORKPORTNUM]]")){
                           $networkPort = new NetworkPort();
                           $networkPort->getFromDB($data['networkports_id']);
                           $logicalnum = $pfNetworkPort->fields['logical_number'];
                           $a_arguments[$arg] = str_replace("[[NETWORKPORTNUM]]", $logicalnum, $a_arguments[$arg]);
                        } elseif (strstr($a_arguments[$arg], "[[NETWORKPORTNAME]]")) {
                           if (isset($data['networkports_id'])
                                   && $data['networkports_id'] > 0) {
                              $networkPort = new NetworkPort();
                              $networkPort->getFromDB($data['networkports_id']);
                              $portname = $pfNetworkPort->fields['name'];
                              $a_arguments[$arg] = str_replace("[[NETWORKPORTNAME]]", $portname, $a_arguments[$arg]);
                           } else if ($classname == 'Computer') {
                              // Get networkportname of networkcard defined
                              $pmHostaddress = new PluginMonitoringHostaddress();
                              $a_hostaddresses = $pmHostaddress->find("`itemtype`='Computer'"
                                      . " AND  `items_id`='".$class->fields['id']."'", '', 1);
                              if (count($a_hostaddresses) == 1) {
                                 $a_hostaddress = current($a_hostaddresses);
                                 if ($a_hostaddress['networkports_id'] > 0) {
                                    $networkPort = new NetworkPort();
                                    $networkPort->getFromDB($a_hostaddress['networkports_id']);
                                    $a_arguments[$arg] = str_replace("[[NETWORKPORTNAME]]", $networkPort->fields['name'], $a_arguments[$arg]);
                                 }
                              }
                           }
                        } else if (strstr($a_arguments[$arg], "[")) {
                           $a_arguments[$arg] = PluginMonitoringService::convertArgument($data['id'], $a_arguments[$arg]);
                        }
                        if ($a_arguments == '') {
                           $notadd = 1;
                           if ($notadddescription != '') {
                              $notadddescription .= ", ";
                           }
                           $notadddescription .= "Argument ".$a_arguments[$arg]." Not have value";
                        }
                        $args .= '!'.$a_arguments[$arg];
                        if ($a_arguments[$arg] == ''
                                AND $a_component['alias_command'] != '') {
                           $args .= $a_component['alias_command'];
                        }
                     }
                  }
               }

               $a_hosts[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . $pmCommand->fields['command_name'].$args;
               // Toolbox::logInFile("pm", "check_command : ".$a_hosts[$i]['check_command']."\n");


               $pmCheck->getFromDB($pmComponent->fields['plugin_monitoring_checks_id']);
               $a_hosts[$i]['check_interval'] = $pmCheck->fields['check_interval'];
               $a_hosts[$i]['retry_interval'] = $pmCheck->fields['retry_interval'];
               $a_hosts[$i]['max_check_attempts'] = $pmCheck->fields['max_check_attempts'];
               if ($calendar->getFromDB($pmComponent->fields['calendars_id'])) {
                  $a_hosts[$i]['check_period'] = $calendar->fields['name'];
               } else {
                  $a_hosts[$i]['check_period'] = self::$shinkenParameters['shinken']['hosts']['check_period'];
               }
               $a_hosts[$i]['active_checks_enabled'] = $a_fields['active_checks_enabled'];
               $a_hosts[$i]['passive_checks_enabled'] = $a_fields['passive_checks_enabled'];

               // Manage freshness
               if ($a_fields['freshness_count'] == 0) {
                  $a_hosts[$i]['check_freshness'] = '0';
                  $a_hosts[$i]['freshness_threshold'] = '0';
               } else {
                  $multiple = 1;
                  if ($a_fields['freshness_type'] == 'seconds') {
                     $multiple = 1;
                  } else if ($a_fields['freshness_type'] == 'minutes') {
                     $multiple = 60;
                  } else if ($a_fields['freshness_type'] == 'hours') {
                     $multiple = 3600;
                  } else if ($a_fields['freshness_type'] == 'days') {
                     $multiple = 86400;
                  }
                  $a_hosts[$i]['check_freshness'] = '1';
                  $a_hosts[$i]['freshness_threshold'] = (string)($a_fields['freshness_count'] * $multiple);
               }

               // Manage event handler
               if ($a_fields['plugin_monitoring_eventhandlers_id'] > 0) {
                  if ($a_fields->getFromDB($a_fields['plugin_monitoring_eventhandlers_id'])) {
                     $a_hosts[$i]['event_handler'] = $pmEventhandler->fields['command_name'];
                  }
               }

               // Realm
               $pmRealm->getFromDB($pmHostconfig->getValueAncestor('plugin_monitoring_realms_id',
                                                                                    $class->fields['entities_id'],
                                                                                    $classname,
                                                                                    $class->getID()));
               $a_hosts[$i]['realm'] = $pmRealm->fields['name'];

               if (! empty(self::$shinkenParameters['shinken']['hosts']['use'])) $a_hosts[$i]['use'] = self::$shinkenParameters['shinken']['hosts']['use'];

               if (! empty(self::$shinkenParameters['shinken']['hosts']['process_perf_data'])) $a_hosts[$i]['process_perf_data'] = self::$shinkenParameters['shinken']['hosts']['process_perf_data'];
               if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_period'])) $a_hosts[$i]['notification_period'] = self::$shinkenParameters['shinken']['hosts']['notification_period'];
               if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_options'])) $a_hosts[$i]['notification_options'] = self::$shinkenParameters['shinken']['hosts']['notification_options'];
               if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_interval'])) $a_hosts[$i]['notification_interval'] = self::$shinkenParameters['shinken']['hosts']['notification_interval'];

               if (! empty(self::$shinkenParameters['shinken']['hosts']['notes'])) $a_hosts[$i]['notes'] = self::$shinkenParameters['shinken']['hosts']['notes'];
               if (! empty(self::$shinkenParameters['shinken']['hosts']['notes_url'])) $a_hosts[$i]['notes_url'] = self::$shinkenParameters['shinken']['hosts']['notes_url'];
               if (! empty(self::$shinkenParameters['shinken']['hosts']['action_url'])) $a_hosts[$i]['action_url'] = self::$shinkenParameters['shinken']['hosts']['action_url'];
               if (! empty(self::$shinkenParameters['shinken']['hosts']['icon_image'])) $a_hosts[$i]['icon_image'] = self::$shinkenParameters['shinken']['hosts']['icon_image'];
               if (! empty(self::$shinkenParameters['shinken']['hosts']['icon_image_alt'])) $a_hosts[$i]['icon_image_alt'] = self::$shinkenParameters['shinken']['hosts']['icon_image_alt'];
               if (! empty(self::$shinkenParameters['shinken']['hosts']['vrml_image'])) $a_hosts[$i]['vrml_image'] = self::$shinkenParameters['shinken']['hosts']['vrml_image'];
               if (! empty(self::$shinkenParameters['shinken']['hosts']['statusmap_image'])) $a_hosts[$i]['statusmap_image'] = self::$shinkenParameters['shinken']['hosts']['statusmap_image'];


               // For contacts, check if a component catalog contains the host associated component ...
               $a_hosts[$i]['contacts'] = '';

               if (($a_fields['passive_checks_enabled'] == '1') and ($a_fields['active_checks_enabled'] == '0')) {
                  // Specific query if host is only passively checked ...
                  // Find the first component catalog in which the host is used ...
                  $querycont = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`
                     LEFT JOIN `glpi_plugin_monitoring_services`
                        ON `plugin_monitoring_componentscatalogs_hosts_id`
                           = `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`
                     WHERE `items_id`='".$data['items_id']."' AND `itemtype`='".$data['itemtype']."'
                     LIMIT 1";
               } else {
                  // Find component catalog which contains the host associated component ...
                  $querycont = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`
                     LEFT JOIN `glpi_plugin_monitoring_services`
                        ON `plugin_monitoring_componentscatalogs_hosts_id`
                           = `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`
                     WHERE `plugin_monitoring_components_id`='".$pmComponent->fields['id']."' AND
                        `items_id`='".$data['items_id']."' AND `itemtype`='".$data['itemtype']."'
                     LIMIT 1";
               }

               $resultcont = $DB->query($querycont);
               if ($DB->numrows($resultcont) != 0) {
                  $a_componentscatalogs_hosts = $DB->fetch_assoc($resultcont);
                  // Notification interval
                  $pmComponentscatalog = new PluginMonitoringComponentscatalog();
                  $pmComponentscatalog->getFromDB($a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']);
                  $a_hosts[$i]['notification_interval'] = $pmComponentscatalog->fields['notification_interval'];

                  $a_contacts = array();
                  $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'
                     AND `items_id`='".$a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']."'");
                  foreach ($a_list_contact as $data_contact) {
                     if (isset($a_contacts_entities[$a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']][$data_contact['users_id']])) {
                        if (in_array($class->fields['entities_id'], $a_contacts_entities[$a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']][$data_contact['users_id']])) {
                           $user->getFromDB($data_contact['users_id']);
                           $a_contacts[] = $user->fields['name'];
                        }
                     }
                  }
                  if (count($a_contacts) > 0) {
                     $a_contacts_unique = array_unique($a_contacts);
                     $a_hosts[$i]['contacts'] = implode(',', $a_contacts_unique);
                  }
               }

               $i++;
            // }
         }
      }


      // Fake host for business rules
      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         " - add host_for_bp\n"
      );
      $a_hosts[$i]['host_name'] = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . self::$shinkenParameters['shinken']['fake_hosts']['bp_host'];
      $a_hosts[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . 'check_dummy!0';
      $a_hosts[$i]['alias'] = 'Fake host for business rules';
      $a_hosts[$i]['_HOSTID'] = '0';
      $a_hosts[$i]['_ITEMSID'] = '0';
      $a_hosts[$i]['_ITEMTYPE'] = 'Computer';
      $a_hosts[$i]['address'] = '127.0.0.1';
      $a_hosts[$i]['parents'] = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . self::$shinkenParameters['shinken']['fake_hosts']['root_parent'];
      $a_hosts[$i]['hostgroups'] = self::$shinkenParameters['shinken']['fake_hosts']['hostgroup_name'];
      $a_hosts[$i]['check_interval'] = '60';
      $a_hosts[$i]['retry_interval'] = '1';
      $a_hosts[$i]['max_check_attempts'] = '1';
      $a_hosts[$i]['check_period'] = '24x7';
      if (self::$shinkenParameters['shinken']['fake_contacts']['build']) {
         $a_hosts[$i]['contacts'] = self::$shinkenParameters['shinken']['fake_contacts']['contact_name'];
      } else {
         $a_hosts[$i]['contacts'] = '';
      }
      if (! empty(self::$shinkenParameters['shinken']['fake_hosts']['use'])) $a_hosts[$i]['use'] = self::$shinkenParameters['shinken']['fake_hosts']['use'];

      if (! empty(self::$shinkenParameters['shinken']['hosts']['process_perf_data'])) $a_hosts[$i]['process_perf_data'] = self::$shinkenParameters['shinken']['hosts']['process_perf_data'];
      if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_period'])) $a_hosts[$i]['notification_period'] = self::$shinkenParameters['shinken']['hosts']['notification_period'];
      if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_options'])) $a_hosts[$i]['notification_options'] = self::$shinkenParameters['shinken']['hosts']['notification_options'];
      if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_interval'])) $a_hosts[$i]['notification_interval'] = self::$shinkenParameters['shinken']['hosts']['notification_interval'];

      if (! empty(self::$shinkenParameters['shinken']['hosts']['notes'])) $a_hosts[$i]['notes'] = self::$shinkenParameters['shinken']['hosts']['notes'];
      if (! empty(self::$shinkenParameters['shinken']['hosts']['notes_url'])) $a_hosts[$i]['notes_url'] = self::$shinkenParameters['shinken']['hosts']['notes_url'];
      if (! empty(self::$shinkenParameters['shinken']['hosts']['action_url'])) $a_hosts[$i]['action_url'] = self::$shinkenParameters['shinken']['hosts']['action_url'];
      if (! empty(self::$shinkenParameters['shinken']['hosts']['icon_image'])) $a_hosts[$i]['icon_image'] = self::$shinkenParameters['shinken']['hosts']['icon_image'];
      if (! empty(self::$shinkenParameters['shinken']['hosts']['icon_image_alt'])) $a_hosts[$i]['icon_image_alt'] = self::$shinkenParameters['shinken']['hosts']['icon_image_alt'];
      if (! empty(self::$shinkenParameters['shinken']['hosts']['vrml_image'])) $a_hosts[$i]['vrml_image'] = self::$shinkenParameters['shinken']['hosts']['vrml_image'];
      if (! empty(self::$shinkenParameters['shinken']['hosts']['statusmap_image'])) $a_hosts[$i]['statusmap_image'] = self::$shinkenParameters['shinken']['hosts']['statusmap_image'];
      $i++;


      // Add one fake host for each entity
      if (self::$shinkenParameters['shinken']['fake_hosts']['build']) {
         PluginMonitoringToolbox::logIfExtradebug(
            'pm-shinken',
            " - add fake hosts for parents relationship\n"
         );

         // Main root parent
         $a_hosts[$i]['host_name'] = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . self::$shinkenParameters['shinken']['fake_hosts']['root_parent'];
         $a_hosts[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . 'check_dummy!0';
         $a_hosts[$i]['alias'] = 'Main root parent';
         $a_hosts[$i]['_HOSTID'] = '0';
         $a_hosts[$i]['_ITEMSID'] = '0';
         $a_hosts[$i]['_ITEMTYPE'] = 'Computer';
         $a_hosts[$i]['address'] = '127.0.0.1';
         $a_hosts[$i]['parents'] = '';
         $a_hosts[$i]['hostgroups'] = self::$shinkenParameters['shinken']['fake_hosts']['hostgroup_name'];
         $a_hosts[$i]['check_interval'] = '60';
         $a_hosts[$i]['retry_interval'] = '1';
         $a_hosts[$i]['max_check_attempts'] = '1';
         $a_hosts[$i]['check_period'] = '24x7';
         if (self::$shinkenParameters['shinken']['fake_contacts']['build']) {
            $a_hosts[$i]['contacts'] = self::$shinkenParameters['shinken']['fake_contacts']['contact_name'];
         } else {
            $a_hosts[$i]['contacts'] = '';
         }
         if (! empty(self::$shinkenParameters['shinken']['fake_hosts']['use'])) $a_hosts[$i]['use'] = self::$shinkenParameters['shinken']['fake_hosts']['use'];

         if (! empty(self::$shinkenParameters['shinken']['hosts']['process_perf_data'])) $a_hosts[$i]['process_perf_data'] = self::$shinkenParameters['shinken']['hosts']['process_perf_data'];
         if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_period'])) $a_hosts[$i]['notification_period'] = self::$shinkenParameters['shinken']['hosts']['notification_period'];
         if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_options'])) $a_hosts[$i]['notification_options'] = self::$shinkenParameters['shinken']['hosts']['notification_options'];
         if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_interval'])) $a_hosts[$i]['notification_interval'] = self::$shinkenParameters['shinken']['hosts']['notification_interval'];

         if (! empty(self::$shinkenParameters['shinken']['hosts']['notes'])) $a_hosts[$i]['notes'] = self::$shinkenParameters['shinken']['hosts']['notes'];
         if (! empty(self::$shinkenParameters['shinken']['hosts']['notes_url'])) $a_hosts[$i]['notes_url'] = self::$shinkenParameters['shinken']['hosts']['notes_url'];
         if (! empty(self::$shinkenParameters['shinken']['hosts']['action_url'])) $a_hosts[$i]['action_url'] = self::$shinkenParameters['shinken']['hosts']['action_url'];
         if (! empty(self::$shinkenParameters['shinken']['hosts']['icon_image'])) $a_hosts[$i]['icon_image'] = self::$shinkenParameters['shinken']['hosts']['icon_image'];
         if (! empty(self::$shinkenParameters['shinken']['hosts']['icon_image_alt'])) $a_hosts[$i]['icon_image_alt'] = self::$shinkenParameters['shinken']['hosts']['icon_image_alt'];
         if (! empty(self::$shinkenParameters['shinken']['hosts']['vrml_image'])) $a_hosts[$i]['vrml_image'] = self::$shinkenParameters['shinken']['hosts']['vrml_image'];
         if (! empty(self::$shinkenParameters['shinken']['hosts']['statusmap_image'])) $a_hosts[$i]['statusmap_image'] = self::$shinkenParameters['shinken']['hosts']['statusmap_image'];
         $i++;

         $a_entities_allowed = $pmEntity->getEntitiesByTag($tag);
         $a_entities_list = array();
         foreach ($a_entities_allowed as $entity) {
            $a_entities_list = getSonsOf("glpi_entities", $entity);
         }
         $where = '';
         if (! isset($a_entities_allowed['-1'])) {
            $where = getEntitiesRestrictRequest("WHERE", "glpi_entities", '', $a_entities_list);
         }

         $query = "SELECT
            `glpi_entities`.`id` AS entityId, `glpi_entities`.`name` AS entityName
            FROM `glpi_entities` $where";
         $result = $DB->query($query);
         while ($dataEntity=$DB->fetch_array($result)) {
            // Hostgroup name : used as host name for parents ...
            $fake_host_name = strtolower(preg_replace("/[^A-Za-z0-9\-_ ]/","",$dataEntity['entityName']));
            $fake_host_name = preg_replace("/[ ]/","_",$fake_host_name);

            $a_hosts[$i]['host_name'] = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . $fake_host_name;
            $a_hosts[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . 'check_dummy!0';
            $a_hosts[$i]['alias'] = $dataEntity['entityName'];
            $a_hosts[$i]['_HOSTID'] = '0';
            $a_hosts[$i]['_ITEMSID'] = '0';
            $a_hosts[$i]['_ITEMTYPE'] = 'Computer';
            $a_hosts[$i]['address'] = '127.0.0.1';
            $a_hosts[$i]['parents'] = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . self::$shinkenParameters['shinken']['fake_hosts']['root_parent'];
            $a_hosts[$i]['hostgroups'] = self::$shinkenParameters['shinken']['fake_hosts']['hostgroup_name'];
            $a_hosts[$i]['check_interval'] = '60';
            $a_hosts[$i]['retry_interval'] = '1';
            $a_hosts[$i]['max_check_attempts'] = '1';
            $a_hosts[$i]['check_period'] = '24x7';
            if (self::$shinkenParameters['shinken']['fake_contacts']['build']) {
               $a_hosts[$i]['contacts'] = self::$shinkenParameters['shinken']['fake_contacts']['contact_name'];
            } else {
               $a_hosts[$i]['contacts'] = '';
            }
            if (! empty(self::$shinkenParameters['shinken']['fake_hosts']['use'])) $a_hosts[$i]['use'] = self::$shinkenParameters['shinken']['fake_hosts']['use'];

            if (! empty(self::$shinkenParameters['shinken']['hosts']['process_perf_data'])) $a_hosts[$i]['process_perf_data'] = self::$shinkenParameters['shinken']['hosts']['process_perf_data'];
            if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_period'])) $a_hosts[$i]['notification_period'] = self::$shinkenParameters['shinken']['hosts']['notification_period'];
            if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_options'])) $a_hosts[$i]['notification_options'] = self::$shinkenParameters['shinken']['hosts']['notification_options'];
            if (! empty(self::$shinkenParameters['shinken']['hosts']['notification_interval'])) $a_hosts[$i]['notification_interval'] = self::$shinkenParameters['shinken']['hosts']['notification_interval'];

            if (! empty(self::$shinkenParameters['shinken']['hosts']['notes'])) $a_hosts[$i]['notes'] = self::$shinkenParameters['shinken']['hosts']['notes'];
            if (! empty(self::$shinkenParameters['shinken']['hosts']['notes_url'])) $a_hosts[$i]['notes_url'] = self::$shinkenParameters['shinken']['hosts']['notes_url'];
            if (! empty(self::$shinkenParameters['shinken']['hosts']['action_url'])) $a_hosts[$i]['action_url'] = self::$shinkenParameters['shinken']['hosts']['action_url'];
            if (! empty(self::$shinkenParameters['shinken']['hosts']['icon_image'])) $a_hosts[$i]['icon_image'] = self::$shinkenParameters['shinken']['hosts']['icon_image'];
            if (! empty(self::$shinkenParameters['shinken']['hosts']['icon_image_alt'])) $a_hosts[$i]['icon_image_alt'] = self::$shinkenParameters['shinken']['hosts']['icon_image_alt'];
            if (! empty(self::$shinkenParameters['shinken']['hosts']['vrml_image'])) $a_hosts[$i]['vrml_image'] = self::$shinkenParameters['shinken']['hosts']['vrml_image'];
            if (! empty(self::$shinkenParameters['shinken']['hosts']['statusmap_image'])) $a_hosts[$i]['statusmap_image'] = self::$shinkenParameters['shinken']['hosts']['statusmap_image'];

            $i++;
         }
         PluginMonitoringToolbox::logIfExtradebug(
            'pm-shinken',
            "End generateHostgroupsCfg\n"
         );
      }

      // Check if parents all exist in hosts config
      foreach ($a_parents_found as $host => $num) {
         if (!isset($a_hosts_found[$host])) {
            // Delete parents not added in hosts config
            foreach ($a_hosts as $id=>$data) {
               if ($data['parents'] == $host) {
                  $a_hosts[$id]['parents'] = '';
               }
            }
         }
      }


      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         "End generateHostsCfg\n"
      );

      if ($file == "1") {
         $config = "# Generated by plugin monitoring for GLPI\n# on ".date("Y-m-d H:i:s")."\n\n";

         foreach ($a_hosts as $data) {
            $config .= $this->writeFile("host", $data);
         }
         return array('hosts.cfg', $config);

      } else {
         return $a_hosts;
      }
   }
 /**
  *
  *
  * @param $items_id integer ID
  *
  * @param $host_id integer associated host ID
  * @param $options array
  *
  *@return bool true if form is ok
  *
  **/
 function showForm($items_id = -1, $options = array())
 {
     global $DB, $CFG_GLPI;
     $host_id = -1;
     if (isset($_GET['host_id'])) {
         $host_id = $_GET['host_id'];
     }
     if ($host_id == -1 && $items_id == -1) {
         return false;
     }
     $createDowntime = false;
     $pmHost = new PluginMonitoringHost();
     if ($host_id != -1) {
         $pmHost->getFromDB($host_id);
         if ($pmHost->isInScheduledDowntime()) {
             // If host already in scheduled downtime, show current downtime ...
             $pmDowntime = new PluginMonitoringDowntime();
             $pmDowntime->getFromDBByQuery("WHERE `" . $pmDowntime->getTable() . "`.`plugin_monitoring_hosts_id` = '" . $host_id . "' LIMIT 1");
             $items_id = $pmDowntime->getID();
             $this->getFromDB($items_id);
         } else {
             // .. else create new downtime
             $createDowntime = true;
             $this->getEmpty();
             $this->setDefaultContent($host_id);
         }
     } else {
         $this->getFromDB($items_id);
     }
     // Now ...
     $nowDate = date('Y-m-d');
     $nowTime = date('H:i:s');
     $this->showFormHeader(array('colspan' => '4'));
     $this->isExpired();
     $pmHost = new PluginMonitoringHost();
     $pmHost->getFromDB($this->fields["plugin_monitoring_hosts_id"]);
     $itemtype = $pmHost->getField("itemtype");
     $item = new $itemtype();
     $item->getFromDB($pmHost->getField("items_id"));
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . $item->getTypeName() . "</td>";
     echo "<td>";
     echo "<input type='hidden' name='plugin_monitoring_hosts_id' value='" . $this->fields['plugin_monitoring_hosts_id'] . "' />";
     echo $item->getLink() . "&nbsp;" . $pmHost->getComments();
     echo "</td>";
     echo "<td></td>";
     echo "<td></td>";
     echo "</tr>";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . __('Start time', 'monitoring') . "</td>";
     echo "<td>";
     $date = $this->fields["start_time"];
     Html::showDateTimeField("start_time", array('value' => $date, 'timestep' => 10, 'maybeempty' => false, 'canedit' => $createDowntime, 'mindate' => $nowDate, 'mintime' => $nowTime));
     echo "</td>";
     echo "<td>" . __('Flexible ?', 'monitoring') . "</td>";
     echo "<td>";
     if ($createDowntime) {
         Dropdown::showYesNo('flexible', $this->fields['flexible']);
     } else {
         echo Dropdown::getYesNo($this->fields['flexible']);
     }
     echo "</td>";
     echo "</tr>";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . __('End time', 'monitoring') . "</td>";
     echo "<td>";
     $date = $this->fields["end_time"];
     Html::showDateTimeField("end_time", array('value' => $date, 'timestep' => 10, 'maybeempty' => false, 'canedit' => $createDowntime, 'mindate' => $nowDate, 'mintime' => $nowTime));
     echo "</td>";
     echo "<td>" . __('Duration', 'monitoring') . "</td>";
     echo "<td>";
     if ($createDowntime) {
         Dropdown::showNumber("duration", array('value' => $this->fields['duration'], 'min' => 1, 'max' => 300));
     } else {
         echo $this->fields['duration'];
     }
     $a_duration_type = array();
     $a_duration_type['seconds'] = __('Second(s)', 'monitoring');
     $a_duration_type['minutes'] = __('Minute(s)', 'monitoring');
     $a_duration_type['hours'] = __('Hour(s)', 'monitoring');
     $a_duration_type['days'] = __('Day(s)', 'monitoring');
     if ($createDowntime) {
         Dropdown::showFromArray("duration_type", $a_duration_type, array('value' => $this->fields['duration_type']));
     } else {
         echo "&nbsp;" . $this->fields['duration_type'];
     }
     echo "</td>";
     echo "</tr>";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . __('Comment', 'monitoring') . "</td>";
     echo "<td >";
     if ($createDowntime) {
         echo "<textarea cols='80' rows='4' name='comment' >" . $this->fields['comment'] . "</textarea>";
     } else {
         echo "<textarea cols='80' rows='4' name='comment' readonly='1' disabled='1' >" . $this->fields['comment'] . "</textarea>";
     }
     echo "</td>";
     echo "</tr>";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . __('User', 'monitoring') . "</td>";
     echo "<td>";
     echo "<input type='hidden' name='users_id' value='" . $this->fields['users_id'] . "' />";
     echo $this->getUsername();
     echo "</td>";
     echo "<td>" . __('Expired ?', 'monitoring') . "</td>";
     echo "<td>";
     echo Dropdown::getYesNo($this->fields['expired']);
     echo "</td>";
     echo "</tr>";
     if (Ticket::canView()) {
         echo "<tr class='tab_bg_1'>";
         echo "<td colspan='4'>&nbsp;</td>";
         echo "</tr>";
         if ($this->isAssociatedTicket()) {
             echo "<tr class='tab_bg_3'>";
             echo "<td colspan='4'>" . __('Downtime associated ticket', 'monitoring') . "</td>";
             echo "</tr>";
             // Find ticket in DB ...
             $track = new Ticket();
             $track->getFromDB($this->getField("tickets_id"));
             // Display ticket id, name and tracking ...
             $bgcolor = $_SESSION["glpipriority_" . $track->fields["priority"]];
             echo "<tr class='tab_bg_2'>";
             echo "<td class='center' bgcolor='{$bgcolor}'>" . sprintf(__('%1$s: %2$s'), __('ID'), $track->fields["id"]) . "</td>";
             echo "<td class='center'>";
             $showprivate = Session::haveRight("show_full_ticket", 1);
             $link = "<a id='ticket" . $track->fields["id"] . "' href='" . $CFG_GLPI["root_doc"] . "/front/ticket.form.php?id=" . $track->fields["id"];
             $link .= "'>";
             $link .= "<span class='b'>" . $track->getNameID() . "</span></a>";
             $link = sprintf(__('%1$s (%2$s)'), $link, sprintf(__('%1$s - %2$s'), $track->numberOfFollowups($showprivate), $track->numberOfTasks($showprivate)));
             $link = printf(__('%1$s %2$s'), $link, Html::showToolTip($track->fields['content'], array('applyto' => 'ticket' . $track->fields["id"], 'display' => false)));
             echo "</td>";
             echo "</tr>";
         } else {
             if ($createDowntime && Ticket::canCreate()) {
                 echo "<tr class='tab_bg_3'>";
                 echo "<td colspan='4'>" . __('Associated ticket (no declared category implies no ticket created):', 'monitoring') . "</td>";
                 echo "</tr>";
                 echo "<input type='hidden' name='redirect' value='" . $CFG_GLPI["root_doc"] . "/front/ticket.form.php' />";
                 echo "<input type='hidden' name='itemtype' value='" . $pmHost->getField("itemtype") . "' />";
                 echo "<input type='hidden' name='items_id' value='" . $pmHost->getField("items_id") . "' />";
                 echo '<input type="hidden" name="entities_id" value="' . $item->fields['entities_id'] . '" />';
                 $item = new $itemtype();
                 $item->getFromDB($pmHost->getField("items_id"));
                 echo "<input type='hidden' name='locations_id' value='" . $item->getField("locations_id") . "' />";
                 /*
                             // Find SLA ...
                             $sla = new Sla();
                             $slas = current($sla->find("`name` LIKE '%proactive%' LIMIT 1"));
                             $sla_id = isset($slas['id']) ? $slas['id'] : 0;
                 
                             echo "<tr class='tab_bg_3'>";
                             echo "<td>".__('Ticket SLA:', 'monitoring')."</td>";
                             echo "<td colspan='3'>";
                             Sla::dropdown(array('value'  => $sla_id));
                             echo "</td>";
                             echo "</tr>";
                 */
                 // Ticket type ...
                 echo "<tr class='tab_bg_3'>";
                 echo "<td>" . __('Ticket type:', 'monitoring') . "</td>";
                 echo "<td colspan='3'>";
                 Ticket::dropdownType("type", array('value' => Ticket::INCIDENT_TYPE));
                 echo "</td>";
                 echo "</tr>";
                 // Find category ...
                 $category = new ITILCategory();
                 $categories = current($category->find("`name` LIKE '%incident%' LIMIT 1"));
                 $category_id = isset($categories['id']) ? $categories['id'] : 0;
                 /*
                             echo "
                             <script>
                             function changeCategory() {
                                alert(document.getElementById('dropdown_itilcategories_idcategory'));
                                alert($('#dropdown_itilcategories_idcategory').val());
                             }
                             </script>
                             ";
                 */
                 echo "<tr class='tab_bg_3'>";
                 echo "<td>" . __('Ticket category:', 'monitoring') . "</td>";
                 echo "<td colspan='3'>";
                 ITILCategory::dropdown(array('value' => $category_id));
                 /*
                             ITILCategory::dropdown(array(
                                'value'     => $category_id,
                                'rand'      => 'category',
                                'on_change' => 'changeCategory();'
                             ));
                 */
                 echo "</td>";
                 echo "</tr>";
             } else {
                 echo "<tr class='tab_bg_3'>";
                 echo "<td colspan='4'>" . __('No associated ticket for this downtime', 'monitoring') . "</td>";
                 echo "</tr>";
             }
         }
     }
     $this->showFormButtons(array('canedit' => $createDowntime));
     return true;
 }