function showCustomArguments($services_id) {

      $pmComponent = new PluginMonitoringComponent();
      $pmCommand = new PluginMonitoringCommand();
      $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();

      $this->getFromDB($services_id);

      $options = array();
      $options['target'] = str_replace("service.form.php", "servicearg.form.php", $this->getFormURL());

      $this->showFormHeader($options);

      $pmComponentscatalog_Host->getFromDB($this->fields['plugin_monitoring_componentscatalogs_hosts_id']);
      $itemtype = $pmComponentscatalog_Host->fields['itemtype'];
      $item = new $itemtype();
      $item->getFromDB($pmComponentscatalog_Host->fields['items_id']);
      echo "<tr>";
      echo "<td>";
      echo $item->getTypeName()." :";
      echo "</td>";
      echo "<td>";
      echo $item->getLink();
      echo "</td>";
      echo "<td colspan='2'></td>";
      echo "</tr>";

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

      $array = array();
      $a_displayarg = array();
      if (isset($pmCommand->fields['command_line'])) {
         preg_match_all("/\\$(ARG\d+)\\$/", $pmCommand->fields['command_line'], $array);
         $a_arguments = importArrayFromDB($pmComponent->fields['arguments']);
         foreach ($array[0] as $arg) {
            if (strstr($arg, "ARG")) {
               $arg = str_replace('$', '', $arg);
               if (!isset($a_arguments[$arg])) {
                  $a_arguments[$arg] = '';
               }
               $a_displayarg[$arg] = $a_arguments[$arg];
            }
         }
      }
      if (count($a_displayarg) > 0) {
         $a_tags = $pmComponent->tagsAvailable();
         array_shift($a_tags);
         $a_argtext = importArrayFromDB($pmCommand->fields['arguments']);
         echo "<tr>";
         echo "<th colspan='2'>".__('Component arguments', 'monitoring')."</th>";
         echo "<th colspan='2'>".__('List of tags available', 'monitoring')."&nbsp;</th>";
         echo "</tr>";

         foreach ($a_displayarg as $key=>$value) {
         echo "<tr>";
         echo "<td>";
            if (isset($a_argtext[$key])
                    AND $a_argtext[$key] != '') {
               echo nl2br($a_argtext[$key])."&nbsp;:";
            } else {
               echo __('Argument', 'monitoring')." (".$key.")&nbsp;:";
            }
            echo "</td>";
            echo "<td>";
            echo $value."<br/>";
            echo "</td>";
            if (count($a_tags) > 0) {
               foreach ($a_tags as $key=>$value) {
                  echo "<td class='tab_bg_3'>";
                  echo "<strong>".$key."</strong>&nbsp;:";
                  echo "</td>";
                  echo "<td class='tab_bg_3'>";
                  echo $value;
                  echo "</td>";
                  unset($a_tags[$key]);
                  break;
               }
            } else {
               echo "<td colspan='2'></td>";
            }
            echo "</tr>";
         }
         foreach ($a_tags as $key=>$value) {
            echo "<tr>";
            echo "<td colspan='2'></td>";
            echo "<td class='tab_bg_3'>";
            echo "<strong>".$key."</strong>&nbsp;:";
            echo "</td>";
            echo "<td class='tab_bg_3'>";
            echo $value;
            echo "</td>";
            echo "</tr>";
         }
      }

      // customized arguments
      echo "<tr>";
      echo "<th colspan='4'>".__('Custom arguments for this resource (empty : inherit)', 'monitoring')."</th>";
      echo "</tr>";
      $array = array();
      $a_displayarg = array();
      if (isset($pmCommand->fields['command_line'])) {
         preg_match_all("/\\$(ARG\d+)\\$/", $pmCommand->fields['command_line'], $array);
         $a_arguments = importArrayFromDB($this->fields['arguments']);
         foreach ($array[0] as $arg) {
            if (strstr($arg, "ARG")) {
               $arg = str_replace('$', '', $arg);
               if (!isset($a_arguments[$arg])) {
                  $a_arguments[$arg] = '';
               }
               $a_displayarg[$arg] = $a_arguments[$arg];
            }
         }
      }
      $a_argtext = importArrayFromDB($pmCommand->fields['arguments']);
      foreach ($a_displayarg as $key=>$value) {
         echo "<tr>";
         echo "<td>";
         if (isset($a_argtext[$key])
                 AND $a_argtext[$key] != '') {
            echo nl2br($a_argtext[$key])."&nbsp;:";
         } else {
            echo __('Argument', 'monitoring')." (".$key.")&nbsp;:";
         }
         echo "</td>";
         echo "<td>";
         echo "<input type='text' name='arg[".$key."]' value='".$value."'/><br/>";
         echo "</td>";
         echo "<td colspan='2'></td>";
         echo "</tr>";
      }

      $this->showFormButtons($options);

   }
   function generateServicesCfg($file=0, $tag='') {
      global $DB;

      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         "Starting generateServicesCfg services ($tag) ...\n"
      );
      $pMonitoringCommand      = new PluginMonitoringCommand();
      $pmEventhandler          = new PluginMonitoringEventhandler();
      $pMonitoringCheck        = new PluginMonitoringCheck();
      $pmComponent             = new PluginMonitoringComponent();
      $pmEntity                = new PluginMonitoringEntity();
      $pmContact_Item          = new PluginMonitoringContact_Item();
      $networkPort             = new NetworkPort();
      $pmService               = new PluginMonitoringService();
      $pmComponentscatalog     = new PluginMonitoringComponentscatalog();
      $pmHostconfig            = new PluginMonitoringHostconfig();
      $calendar                = new Calendar();
      $user                    = new User();
      $profile_User = new Profile_User();

      $a_services = array();
      $i=0;

      // TODO: only contacts in allowed entities ...
      // Prepare individual 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;
      }
      // Prepare groups contacts
      $group = new Group();
      $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'
         AND `groups_id`>0");
      foreach ($a_list_contact as $data) {
         $group->getFromDB($data['groups_id']);
         if ($group->fields['is_recursive'] == 1) {
            $contactentities = getSonsOf('glpi_entities', $group->fields['entities_id']);
         } else {
            $contactentities = array($group->fields['entities_id'] => $group->fields['entities_id']);
         }
         $queryg = "SELECT * FROM `glpi_groups_users`
            WHERE `groups_id`='".$data['groups_id']."'";
         $resultg = $DB->query($queryg);
         while ($datag=$DB->fetch_array($resultg)) {
            if (isset($a_contacts_entities[$data['items_id']][$datag['users_id']])) {
               $contactentities = array_merge($contactentities, $a_contacts_entities[$data['items_id']][$datag['users_id']]);
            }
            $a_contacts_entities[$data['items_id']][$datag['users_id']] = $contactentities;
         }
      }



      $a_entities_allowed = $pmEntity->getEntitiesByTag($tag);
      // Toolbox::logInFile("pm-shinken", " Allowed entities:\n");
      $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_plugin_monitoring_services", '', $a_entities_list);
      }

      // --------------------------------------------------
      // "Normal" services ....
      $query = "SELECT * FROM `glpi_plugin_monitoring_services` $where";
      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         "Services: $query\n"
      );
      $result = $DB->query($query);
      while ($data=$DB->fetch_array($result)) {
         // Toolbox::logInFile("pm-shinken", " - fetch service ".$data['id']."\n");

         // if (isset($a_entities_allowed['-1'])
                 // OR isset($a_entities_allowed[$item->fields['entities_id']])) {
            $notadd = 0;
            $notadddescription = '';
            $a_component = current($pmComponent->find("`id`='".$data['plugin_monitoring_components_id']."'", "", 1));
            if (empty($a_component)) {
               continue;
            }
            $a_hostname = array();
            $a_hostname_type = array();
            $a_hostname_id = array();
            $queryh = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`
               WHERE `id` = '".$data['plugin_monitoring_componentscatalogs_hosts_id']."'
               LIMIT 1";
            $resulth = $DB->query($queryh);
            $hostname = '';
            $plugin_monitoring_componentscatalogs_id = 0;
            while ($datah=$DB->fetch_array($resulth)) {
               $itemtype = $datah['itemtype'];
               $item = new $itemtype();
               if ($item->getFromDB($datah['items_id'])) {
                  // if (isset($a_entities_allowed['-1'])
                          // OR isset($a_entities_allowed[$item->fields['entities_id']])) {

                     // Fix if hostname is not defined ...
                     if (! empty($item->fields['name'])) {
                        $a_hostname[] = preg_replace("/[^A-Za-z0-9\-_]/","",$item->fields['name']);
                        $a_hostname_type[] = $datah['itemtype'];
                        $a_hostname_id[] = $datah['items_id'];
                        $hostname = $item->fields['name'];
                        $plugin_monitoring_componentscatalogs_id = $datah['plugin_monitoring_componentscalalog_id'];
                     }
                  // }
               }
            }
            if (count($a_hostname) > 0) {
               if (isset($_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']])) {
                  $a_services[$i]['use'] = $_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']];
               }
               $a_services[$i]['host_name'] = implode(",", array_unique($a_hostname));
               $a_services[$i]['_HOSTITEMSID'] = implode(",", array_unique($a_hostname_id));
               $a_services[$i]['_HOSTITEMTYPE'] = implode(",", array_unique($a_hostname_type));

               // Define display_name / service_description
               $a_services[$i]['service_description'] = (! empty($a_component['description'])) ? $a_component['description'] : preg_replace("/[^A-Za-z0-9\-_]/","",$a_component['name']);
               // In case have multiple networkt port, may have description different, else be dropped by shinken
               if ($data['networkports_id'] > 0) {
                  $networkPort->getFromDB($data['networkports_id']);
                  $a_services[$i]['service_description'] .= '-'.preg_replace("/[^A-Za-z0-9\-_]/", "", $networkPort->fields['name']);
               }
               $a_services[$i]['display_name'] = $a_component['name'];
               // $a_services[$i]['_ENTITIESID'] = $item->fields['entities_id'];
               // $a_services[$i]['_ITEMSID'] = $data['id'];
               // $a_services[$i]['_ITEMTYPE'] = 'Service';
               PluginMonitoringToolbox::logIfExtradebug(
                  'pm-shinken',
                  " - add service ".$a_services[$i]['service_description']." on ".$a_services[$i]['host_name']."\n"
               );

               if (isset(self::$shinkenParameters['glpi']['entityId'])) {
                  $a_services[$i][self::$shinkenParameters['glpi']['entityId']] =
                     $item->fields['entities_id'];
               }
               if (isset(self::$shinkenParameters['glpi']['itemType'])) {
                  $a_services[$i][self::$shinkenParameters['glpi']['itemType']] =
                     'Service';
               }
               if (isset(self::$shinkenParameters['glpi']['itemId'])) {
                  $a_services[$i][self::$shinkenParameters['glpi']['itemId']] =
                     $data['id'];
               }

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

               $pMonitoringCommand->getFromDB($a_component['plugin_monitoring_commands_id']);
               // Manage arguments
               $array = array();
               preg_match_all("/\\$(ARG\d+)\\$/", $pMonitoringCommand->fields['command_line'], $array);
               sort($array[0]);
               $a_arguments = importArrayFromDB($a_component['arguments']);
               $a_argumentscustom = importArrayFromDB($data['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 ($a_services[$i]['_HOSTITEMTYPE'] == 'Computer') {
                              // Get networkportname of networkcard defined
                              $pmHostaddress = new PluginMonitoringHostaddress();
                              $a_hostaddresses = $pmHostaddress->find("`itemtype`='Computer'"
                                      . " AND  `items_id`='".$a_services[$i]['_HOSTITEMSID']."'", '', 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]." do not have value";
                        }
                        $args .= '!'.$a_arguments[$arg];
                        if ($a_arguments[$arg] == ''
                                AND $a_component['alias_command'] != '') {
                           $args .= $a_component['alias_command'];
                        }
                     }
                  }
               }
               // End manage arguments
               if ($a_component['remotesystem'] == 'nrpe') {
                  if ($a_component['alias_command'] != '') {
                     $alias_command = $a_component['alias_command'];
                     if (strstr($alias_command, '[[IP]]')) {
                        $split = explode('-', current($a_hostname));
                        $ip = PluginMonitoringHostaddress::getIp($split[1], $split[0], '');
                        $alias_command = str_replace("[[IP]]", $ip, $alias_command);
                     }
                     $a_services[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . "check_nrpe!".$alias_command;
                  } else {
                     $a_services[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . "check_nrpe!".$pMonitoringCommand->fields['command_name'];
                  }
               } else {
                  $a_services[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . $pMonitoringCommand->fields['command_name'].$args;
               }

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

               if (! empty(self::$shinkenParameters['shinken']['services']['process_perf_data'])) $a_services[$i]['process_perf_data'] = self::$shinkenParameters['shinken']['services']['process_perf_data'];

               if (! empty(self::$shinkenParameters['shinken']['services']['notes'])) $a_services[$i]['notes'] = self::$shinkenParameters['shinken']['services']['notes'];
               if (! empty(self::$shinkenParameters['shinken']['services']['notes_url'])) $a_services[$i]['notes_url'] = self::$shinkenParameters['shinken']['services']['notes_url'];
               if (! empty(self::$shinkenParameters['shinken']['services']['action_url'])) $a_services[$i]['action_url'] = self::$shinkenParameters['shinken']['services']['action_url'];
               if (! empty(self::$shinkenParameters['shinken']['services']['icon_image'])) $a_services[$i]['icon_image'] = self::$shinkenParameters['shinken']['services']['icon_image'];
               if (! empty(self::$shinkenParameters['shinken']['services']['icon_image_alt'])) $a_services[$i]['icon_image_alt'] = self::$shinkenParameters['shinken']['services']['icon_image_alt'];

               // * Contacts
               $a_contacts = array();
               $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'
                  AND `items_id`='".$plugin_monitoring_componentscatalogs_id."'");
               foreach ($a_list_contact as $data_contact) {
                  if ($data_contact['users_id'] > 0) {
                     if (isset($a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$data_contact['users_id']])) {
                        if (in_array($data['entities_id'], $a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$data_contact['users_id']])) {
                           $user->getFromDB($data_contact['users_id']);
                           $a_contacts[] = $user->fields['name'];
                        }
                     }
                  } else if ($data_contact['groups_id'] > 0) {
                     $queryg = "SELECT * FROM `glpi_groups_users`
                        WHERE `groups_id`='".$data_contact['groups_id']."'";
                     $resultg = $DB->query($queryg);
                     while ($datag=$DB->fetch_array($resultg)) {
                        if (in_array($data['entities_id'], $a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$datag['users_id']])) {
                           $user->getFromDB($datag['users_id']);
                           $a_contacts[] = $user->fields['name'];
                        }
                     }
                  }
               }

               $a_contacts_unique = array_unique($a_contacts);
               $a_services[$i]['contacts'] = implode(',', $a_contacts_unique);

               // ** If shinken not use templates or template not defined :
               if (!isset($_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']])) {
                  $pMonitoringCheck->getFromDB($a_component['plugin_monitoring_checks_id']);
                  $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval'];
                  $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval'];
                  $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts'];
                  if ($calendar->getFromDB($a_component['calendars_id'])) {
                     $a_services[$i]['check_period'] = $calendar->fields['name'];
                  }
                  $a_services[$i]['notification_interval'] = '30';
                  $a_services[$i]['notification_period'] = "24x7";
                  $a_services[$i]['notification_options'] = 'w,u,c,r,f,s';
                  $a_services[$i]['process_perf_data'] = '1';
                  $a_services[$i]['active_checks_enabled'] = '1';
                  $a_services[$i]['passive_checks_enabled'] = '1';
                  $a_services[$i]['parallelize_check'] = '1';
                  $a_services[$i]['obsess_over_service'] = '1';
                  $a_services[$i]['check_freshness'] = '1';
                  $a_services[$i]['freshness_threshold'] = '3600';
                  $a_services[$i]['notifications_enabled'] = '1';

                  if (isset($a_services[$i]['event_handler'])) {
                     $a_services[$i]['event_handler_enabled'] = '1';
                  } else {
                     $a_services[$i]['event_handler_enabled'] = '0';
                     // $a_services[$i]['event_handler_enabled'] = '';
                  }
                  $a_services[$i]['flap_detection_enabled'] = '1';
                  $a_services[$i]['failure_prediction_enabled'] = '1';
                  $a_services[$i]['retain_status_information'] = '1';
                  $a_services[$i]['retain_nonstatus_information'] = '1';
                  $a_services[$i]['is_volatile'] = '0';
                  // $a_services[$i]['_httpstink'] = 'NO';
               } else {
                  // Notification options
                  $a_services[$i]['notification_interval'] = '30';
                  $pmComponentscatalog->getFromDB($plugin_monitoring_componentscatalogs_id);
                  if ($pmComponentscatalog->fields['notification_interval'] != '30') {
                     $a_services[$i]['notification_interval'] = $pmComponentscatalog->fields['notification_interval'];
                  }
                  $a_services[$i]['notification_period'] = '24x7';
                  $a_services[$i]['check_period'] = '24x7';
                  if ($calendar->getFromDB($a_component['calendars_id'])) {
                     $a_services[$i]['check_period'] = $calendar->fields['name'];
                  }
               }

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

               if ($notadd == '1') {
                  unset($a_services[$i]);
                  $input = array();
                  $input['id'] = $data['id'];
                  $input['event'] = $notadddescription;
                  $input['state'] = "CRITICAL";
                  $input['state_type'] = "HARD";
                  $pmService->update($input);
               } else {
                  $i++;
               }
            }
         // }
      }

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

      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         "Starting generateServicesCfg business rules ...\n"
      );

      // --------------------------------------------------
      // Business rules services ...
      $pmService = new PluginMonitoringService();
      $pmServicescatalog = new PluginMonitoringServicescatalog();
      $pmBusinessrulegroup = new PluginMonitoringBusinessrulegroup();
      $pmBusinessrule = new PluginMonitoringBusinessrule();
      $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
      $pmBusinessrule_component = new PluginMonitoringBusinessrule_component();
      // Prepare individual contacts
      $a_contacts_entities = array();
      $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringServicescatalog'
         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;
      }
      // Prepare groups contacts
      $group = new Group();
      $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringServicescatalog'
         AND `groups_id`>0");
      foreach ($a_list_contact as $data) {
         $group->getFromDB($data['groups_id']);
         if ($group->fields['is_recursive'] == 1) {
            $contactentities = getSonsOf('glpi_entities', $group->fields['entities_id']);
         } else {
            $contactentities = array($group->fields['entities_id'] => $group->fields['entities_id']);
         }
         $queryg = "SELECT * FROM `glpi_groups_users`
            WHERE `groups_id`='".$data['groups_id']."'";
         $resultg = $DB->query($queryg);
         while ($datag=$DB->fetch_array($resultg)) {
            if (isset($a_contacts_entities[$data['items_id']][$datag['users_id']])) {
               $contactentities = array_merge($contactentities, $a_contacts_entities[$data['items_id']][$datag['users_id']]);
            }
            $a_contacts_entities[$data['items_id']][$datag['users_id']] = $contactentities;
         }
      }

      // Services catalogs
      $a_listBA = $pmServicescatalog->find("`is_generic`='0'");
      foreach ($a_listBA as $dataBA) {

         if (isset($a_entities_allowed['-1'])
                 OR isset($a_entities_allowed[$dataBA['entities_id']])) {

            $a_grouplist = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='".$dataBA['id']."'");
            $a_group = array();
            foreach ($a_grouplist as $gdata) {

               $pmBusinessrule_component->replayDynamicServices($gdata['id']);
               $a_listBR = $pmBusinessrule->find(
                       "`plugin_monitoring_businessrulegroups_id`='".$gdata['id']."'");
               foreach ($a_listBR as $dataBR) {
                  if ($pmService->getFromDB($dataBR['plugin_monitoring_services_id'])) {
                     if ($pmService->getHostName() != '') {
                        $hostname = preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getHostName());

                        if ($gdata['operator'] == 'and'
                                OR $gdata['operator'] == 'or'
                                OR strstr($gdata['operator'], ' of:')) {

                           $operator = '|';
                           if ($gdata['operator'] == 'and') {
                              $operator = '&';
                           }
                           if (!isset($a_group[$gdata['id']])) {
                              $a_group[$gdata['id']] = '';
                              if (strstr($gdata['operator'], ' of:')) {
                                 $a_group[$gdata['id']] = $gdata['operator'];
                              }
                              $a_group[$gdata['id']] .= $hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getName(array('shinken'=>true)));
                           } else {
                              $a_group[$gdata['id']] .= $operator.$hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getName(array('shinken'=>true)));
                           }
                        } else {
                           $a_group[$gdata['id']] = $gdata['operator']." ".$hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$item->getName());
                        }
                     }
                  }
                  PluginMonitoringToolbox::logIfExtradebug(
                     'pm-shinken',
                     "   - SC group : ".$a_group[$gdata['id']]."\n"
                  );
               }
            }
            if (count($a_group) > 0) {
               $pMonitoringCheck->getFromDB($dataBA['plugin_monitoring_checks_id']);
               $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval'];
               $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval'];
               $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts'];
               if ($calendar->getFromDB($dataBA['calendars_id'])) {
                  $a_services[$i]['check_period'] = $calendar->fields['name'];
               }
               $a_services[$i]['host_name'] = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . self::$shinkenParameters['shinken']['fake_hosts']['bp_host'];
               $a_services[$i]['business_impact'] = $dataBA['business_priority'];
               $a_services[$i]['service_description'] = preg_replace("/[^A-Za-z0-9\-_]/","",$dataBA['name']);
               $a_services[$i]['_ENTITIESID'] = $dataBA['id'];
               $a_services[$i]['_ITEMSID'] = $dataBA['id'];
               $a_services[$i]['_ITEMTYPE'] = 'ServiceCatalog';
               $command = "bp_rule!";

               foreach ($a_group as $key=>$value) {
                  if (!strstr($value, "&")
                          AND !strstr($value, "|")) {
                     $a_group[$key] = trim($value);
                  } else {
                     $a_group[$key] = "(".trim($value).")";
                  }
               }
               $a_services[$i]['check_command'] = $command.implode("&", $a_group);
               if ($dataBA['notification_interval'] != '30') {
                  $a_services[$i]['notification_interval'] = $dataBA['notification_interval'];
               } else {
                  $a_services[$i]['notification_interval'] = '30';
               }
               $a_services[$i]['notification_period'] = "24x7";
               $a_services[$i]['notification_options'] = 'w,u,c,r,f,s';
               $a_services[$i]['active_checks_enabled'] = '1';
               $a_services[$i]['process_perf_data'] = '1';
               $a_services[$i]['active_checks_enabled'] = '1';
               $a_services[$i]['passive_checks_enabled'] = '1';
               $a_services[$i]['parallelize_check'] = '1';
               $a_services[$i]['obsess_over_service'] = '1';
               $a_services[$i]['check_freshness'] = '1';
               $a_services[$i]['freshness_threshold'] = '3600';
               $a_services[$i]['notifications_enabled'] = '1';
               $a_services[$i]['event_handler_enabled'] = '0';
               //$a_services[$i]['event_handler'] = 'super_event_kill_everyone!DIE';
               $a_services[$i]['flap_detection_enabled'] = '1';
               $a_services[$i]['failure_prediction_enabled'] = '1';
               $a_services[$i]['retain_status_information'] = '1';
               $a_services[$i]['retain_nonstatus_information'] = '1';
               $a_services[$i]['is_volatile'] = '0';
               // $a_services[$i]['_httpstink'] = 'NO';

               // * Contacts
               $a_contacts = array();
               $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringServicescatalog'
                  AND `items_id`='".$dataBA['id']."'");
               foreach ($a_list_contact as $data_contact) {
                  if ($data_contact['users_id'] > 0) {
                     if (isset($a_contacts_entities[$dataBA['id']][$data_contact['users_id']])) {
                        if (in_array($data['entities_id'], $a_contacts_entities[$dataBA['id']][$data_contact['users_id']])) {
                           $user->getFromDB($data_contact['users_id']);
                           $a_contacts[] = $user->fields['name'];
                        }
                     }
                  } else if ($data_contact['groups_id'] > 0) {
                     $queryg = "SELECT * FROM `glpi_groups_users`
                        WHERE `groups_id`='".$data_contact['groups_id']."'";
                     $resultg = $DB->query($queryg);
                     while ($datag=$DB->fetch_array($resultg)) {
                        if (in_array($data['entities_id'], $a_contacts_entities[$dataBA['id']][$datag['users_id']])) {
                           $user->getFromDB($datag['users_id']);
                           $a_contacts[] = $user->fields['name'];
                        }
                     }
                  }
               }

               $a_contacts_unique = array_unique($a_contacts);
               $a_services[$i]['contacts'] = implode(',', $a_contacts_unique);
               $i++;
            }
         }
      }

      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         "End generateServicesCfg business rules\n"
      );

      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         "Starting generateServicesCfg business rules templates ...\n"
      );

      // Services catalogs templates
      // TODO : correctly test and improve it !
      $a_listBA = $pmServicescatalog->find("`is_generic`='1'");
      foreach ($a_listBA as $dataBA) {
         PluginMonitoringToolbox::logIfExtradebug(
            'pm-shinken',
            "   - SC : ".$dataBA['id']."\n"
         );

         if (isset($a_entities_allowed['-1'])
                 OR isset($a_entities_allowed[$dataBA['entities_id']])) {

            $pmServicescatalog->getFromDB($dataBA['id']);

            $a_entitiesServices = $pmServicescatalog->getGenericServicesEntities();
            foreach ($a_entitiesServices as $idEntity=>$a_entityServices) {
               // New entity ... so new business rule !
               PluginMonitoringToolbox::logIfExtradebug(
                  'pm-shinken',
                  "   - SC templated services for an entity : ".$idEntity."\n"
               );

               $pmDerivatedSC = new PluginMonitoringServicescatalog();
               $a_derivatedSC = $pmDerivatedSC->find("`entities_id`='$idEntity' AND `name` LIKE '".$dataBA['name']."%'");
               foreach ($a_derivatedSC as $a_derivated) {
                  PluginMonitoringToolbox::logIfExtradebug(
                     'pm-shinken',
                     "   - a_derivated : ".$a_derivated['name']."\n"
                  );
                  $a_derivatedSC = $a_derivated;
               }

               $a_group = array();
               foreach ($a_entityServices as $services) {
                  if ($pmService->getFromDB($services['serviceId'])) {
                     // Toolbox::logInFile("pm-shinken", "   - SC templated service entity : ".$services['entityId'].", service :  ".$pmService->getName(true)." on ".$pmService->getHostName()."\n");
                     if ($pmService->getHostName() != '') {
                        $hostname = preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getHostName());

                        $serviceFakeId = $services['entityId'];

                        $pmBusinessrulegroup->getFromDB($services['BRgroupId']);
                        $BRoperator = $pmBusinessrulegroup->getField('operator');
                        if ($BRoperator == 'and'
                                OR $BRoperator == 'or'
                                OR strstr($BRoperator, ' of:')) {

                           $operator = '|';
                           if ($BRoperator == 'and') {
                              $operator = '&';
                           }
                           if (!isset($a_group[$serviceFakeId])) {
                              $a_group[$serviceFakeId] = '';
                              if (strstr($BRoperator, ' of:')) {
                                 $a_group[$serviceFakeId] = $BRoperator;
                              }
                              $a_group[$serviceFakeId] .= $hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getName(array('shinken'=>true)));
                           } else {
                              $a_group[$serviceFakeId] .= $operator.$hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getName(array('shinken'=>true)));
                           }
                        } else {
                           $a_group[$serviceFakeId] = $BRoperator." ".$hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getHostName());
                        }
                        // Toolbox::logInFile("pm-shinken", "   - SCT group : ".$a_group[$serviceFakeId]."\n");
                     }
                  }
               }
               if (count($a_group) > 0) {
                  $pMonitoringCheck->getFromDB($a_derivatedSC['plugin_monitoring_checks_id']);
                  $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval'];
                  $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval'];
                  $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts'];
                  if ($calendar->getFromDB($a_derivatedSC['calendars_id'])) {
                     $a_services[$i]['check_period'] = $calendar->fields['name'];
                  }
                  $a_services[$i]['host_name'] = preg_replace("/[^A-Za-z0-9\-_]/","",$a_derivatedSC['name']);
                  $a_services[$i]['host_name'] = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . self::$shinkenParameters['shinken']['fake_hosts']['bp_host'];
                  $a_services[$i]['business_impact'] = $a_derivatedSC['business_priority'];
                  $a_services[$i]['service_description'] = preg_replace("/[^A-Za-z0-9\-_]/","",$a_derivatedSC['name']);
                  $a_services[$i]['_ENTITIESID'] = $a_derivatedSC['entities_id'];
                  $a_services[$i]['_ITEMSID'] = $a_derivatedSC['id'];
                  $a_services[$i]['_ITEMTYPE'] = 'ServiceCatalog';
                  $command = "bp_rule!";

                  foreach ($a_group as $key=>$value) {
                     if (!strstr($value, "&")
                             AND !strstr($value, "|")) {
                        $a_group[$key] = trim($value);
                     } else {
                        $a_group[$key] = "(".trim($value).")";
                     }
                  }
                  $a_services[$i]['check_command'] = $command.implode("&", $a_group);
                  if ($a_derivatedSC['notification_interval'] != '30') {
                     $a_services[$i]['notification_interval'] = $a_derivatedSC['notification_interval'];
                  } else {
                     $a_services[$i]['notification_interval'] = '30';
                  }
                  $a_services[$i]['notification_period'] = "24x7";
                  $a_services[$i]['notification_options'] = 'w,u,c,r,f,s';
                  $a_services[$i]['active_checks_enabled'] = '1';
                  $a_services[$i]['process_perf_data'] = '1';
                  $a_services[$i]['active_checks_enabled'] = '1';
                  $a_services[$i]['passive_checks_enabled'] = '1';
                  $a_services[$i]['parallelize_check'] = '1';
                  $a_services[$i]['obsess_over_service'] = '1';
                  $a_services[$i]['check_freshness'] = '1';
                  $a_services[$i]['freshness_threshold'] = '3600';
                  $a_services[$i]['notifications_enabled'] = '1';
                  $a_services[$i]['event_handler_enabled'] = '0';
                  //$a_services[$i]['event_handler'] = 'super_event_kill_everyone!DIE';
                  $a_services[$i]['flap_detection_enabled'] = '1';
                  $a_services[$i]['failure_prediction_enabled'] = '1';
                  $a_services[$i]['retain_status_information'] = '1';
                  $a_services[$i]['retain_nonstatus_information'] = '1';
                  $a_services[$i]['is_volatile'] = '0';
                  // $a_services[$i]['_httpstink'] = 'NO';

                  // * Contacts
                  $a_contacts = array();
                  $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringServicescatalog'
                     AND `items_id`='".$dataBA['id']."'");
                  foreach ($a_list_contact as $data_contact) {
                     if ($data_contact['users_id'] > 0) {
                        if (isset($a_contacts_entities[$dataBA['id']][$data_contact['users_id']])) {
                           if (in_array($data['entities_id'], $a_contacts_entities[$dataBA['id']][$data_contact['users_id']])) {
                              $user->getFromDB($data_contact['users_id']);
                              $a_contacts[] = $user->fields['name'];
                           }
                        }
                     } else if ($data_contact['groups_id'] > 0) {
                        $queryg = "SELECT * FROM `glpi_groups_users`
                           WHERE `groups_id`='".$data_contact['groups_id']."'";
                        $resultg = $DB->query($queryg);
                        while ($datag=$DB->fetch_array($resultg)) {
                           if (in_array($data['entities_id'], $a_contacts_entities[$dataBA['id']][$datag['users_id']])) {
                              $user->getFromDB($datag['users_id']);
                              $a_contacts[] = $user->fields['name'];
                           }
                        }
                     }
                  }

                  $a_contacts_unique = array_unique($a_contacts);
                  $a_services[$i]['contacts'] = implode(',', $a_contacts_unique);
                  $i++;
               }
            }
         }
      }

      PluginMonitoringToolbox::logIfExtradebug(
         'pm-shinken',
         "End generateServicesCfg business rules templates\n"
      );

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

         foreach ($a_services as $data) {
            $config .= $this->writeFile("service", $data);
         }
         return array('services.cfg', $config);

      } else {
         return $a_services;
      }
   }
 /**
  * Display form for service configuration
  *
  * @param $items_id integer ID
  * @param $options array
  *
  *@return bool true if form is ok
  *
  **/
 function showForm($items_id, $options = array())
 {
     $pMonitoringCommand = new PluginMonitoringCommand();
     if ($items_id == '0') {
         $this->getEmpty();
     } else {
         $this->getFromDB($items_id);
     }
     $this->showFormHeader($options);
     echo "<tr>";
     echo "<td>";
     echo __('Template name') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     echo "<input type='hidden' name='is_template' value='1' />";
     $objectName = autoName($this->fields["name"], "name", 1, $this->getType());
     Html::autocompletionTextField($this, 'name', array('value' => $objectName));
     echo "</td>";
     // * commande
     echo "<td>";
     echo __('Command', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td align='center'>";
     if ($this->fields['is_template'] == '1') {
         $pMonitoringCommand->getFromDB($this->fields['plugin_monitoring_commands_id']);
         echo $pMonitoringCommand->getLink(1);
     } else {
         $pMonitoringCommand->getFromDB($this->fields['plugin_monitoring_commands_id']);
         Dropdown::show("PluginMonitoringCommand", array('name' => 'plugin_monitoring_commands_id', 'value' => $this->fields['plugin_monitoring_commands_id']));
     }
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     // * checks
     echo "<td>" . __('Check definition', 'monitoring') . "&nbsp;:</td>";
     echo "<td align='center'>";
     if ($this->fields['is_template'] == '1') {
         $pMonitoringCheck = new PluginMonitoringCheck();
         $pMonitoringCheck->getFromDB($this->fields['plugin_monitoring_checks_id']);
         echo $pMonitoringCheck->getLink(1);
     } else {
         Dropdown::show("PluginMonitoringCheck", array('name' => 'plugin_monitoring_checks_id', 'value' => $this->fields['plugin_monitoring_checks_id']));
     }
     echo "</td>";
     // * active check
     echo "<td>";
     echo __('Active checks enable', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td align='center'>";
     if ($this->fields['is_template'] == '1') {
         echo Dropdown::getYesNo($this->fields['active_checks_enabled']);
     } else {
         Dropdown::showYesNo("active_checks_enabled", $this->fields['active_checks_enabled']);
     }
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     // * passive check
     echo "<td>";
     echo __('Passive checks enable', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td align='center'>";
     if ($this->fields['is_template'] == '1') {
         echo Dropdown::getYesNo($this->fields['passive_checks_enabled']);
     } else {
         Dropdown::showYesNo("passive_checks_enabled", $this->fields['passive_checks_enabled']);
     }
     echo "</td>";
     // * calendar
     echo "<td>" . __('Check period', 'monitoring') . "&nbsp;:</td>";
     echo "<td align='center'>";
     if ($this->fields['is_template'] == '1') {
         $calendar = new Calendar();
         $calendar->getFromDB($this->fields['calendars_id']);
         echo $calendar->getLink(1);
     } else {
         dropdown::show("Calendar", array('name' => 'calendars_id', 'value' => $this->fields['calendars_id']));
     }
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<th colspan='4'>" . __('Remote check', 'monitoring') . "</th>";
     echo "</tr>";
     echo "<tr>";
     // * remotesystem
     echo "<td>";
     echo __('Utility used for remote check', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     $input = array();
     $input[''] = '------';
     $input['byssh'] = __('By ssh', 'monitoring');
     $input['nrpe'] = 'nrpe';
     $input['nsca'] = 'nsca';
     if ($this->fields['is_template'] == '1') {
         echo $input[$this->fields['remotesystem']];
     } else {
         Dropdown::showFromArray("remotesystem", $input, array('value' => $this->fields['remotesystem']));
     }
     echo "</td>";
     // * is_argument
     echo "<td>";
     echo __('Use arguments (Only for NRPE)', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     if ($this->fields['is_template'] == '1') {
         echo Dropdown::getYesNo($this->fields['is_arguments']);
     } else {
         Dropdown::showYesNo("is_arguments", $this->fields['is_arguments']);
     }
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     // alias command
     echo "<td>";
     echo __('Alias command if required (Only for NRPE)', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     if ($this->fields['is_template'] == '1') {
         echo "<input type='text' name='alias_commandservice' value='" . $this->fields['alias_command'] . "' />";
     } else {
         echo "<input type='text' name='alias_command' value='" . $this->fields['alias_command'] . "' />";
     }
     echo "</td>";
     echo "<td>";
     echo __('Command link (used for graphs generation)', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     if ($this->fields['is_template'] == '1') {
         $pMonitoringCommand->getFromDB($this->fields['aliasperfdata_commands_id']);
         echo $pMonitoringCommand->getLink(1);
     } else {
         $pMonitoringCommand->getFromDB($this->fields['aliasperfdata_commands_id']);
         Dropdown::show("PluginMonitoringCommand", array('name' => 'aliasperfdata_commands_id', 'value' => $this->fields['aliasperfdata_commands_id']));
     }
     echo "</td>";
     echo "</tr>";
     // * Manage arguments
     $array = array();
     $a_displayarg = array();
     if (isset($pMonitoringCommand->fields['command_line'])) {
         preg_match_all("/\\\$(ARG\\d+)\\\$/", $pMonitoringCommand->fields['command_line'], $array);
         $a_arguments = importArrayFromDB($this->fields['arguments']);
         foreach ($array[0] as $arg) {
             if (strstr($arg, "ARG")) {
                 $arg = str_replace('$', '', $arg);
                 if (!isset($a_arguments[$arg])) {
                     $a_arguments[$arg] = '';
                 }
                 $a_displayarg[$arg] = $a_arguments[$arg];
             }
         }
     }
     if (count($a_displayarg) > 0) {
         $a_argtext = importArrayFromDB($pMonitoringCommand->fields['arguments']);
         echo "<tr>";
         echo "<th colspan='4'>" . __('Arguments', 'monitoring') . "&nbsp;</th>";
         echo "</tr>";
         foreach ($a_displayarg as $key => $value) {
             echo "<tr>";
             echo "<th>" . $key . "</th>";
             echo "<td colspan='2'>";
             if (isset($a_argtext[$key])) {
                 echo nl2br($a_argtext[$key]) . "&nbsp;:";
             } else {
                 echo __('Argument', 'monitoring') . "&nbsp;:";
             }
             echo "</td>";
             echo "<td>";
             echo "<input type='text' name='arg[" . $key . "]' value='" . $value . "'/><br/>";
             echo "</td>";
             echo "</tr>";
         }
     }
     $this->showFormButtons($options);
     return true;
 }
 /**
  *
  * @param $items_id integer ID 
  * @param $options array
  *
  *@return bool true if form is ok
  *
  **/
 function showForm($items_id, $itemtype, $options = array())
 {
     global $DB, $CFG_GLPI;
     $pmCommand = new PluginMonitoringCommand();
     $pmCheck = new PluginMonitoringCheck();
     $calendar = new Calendar();
     $pmRealm = new PluginMonitoringRealm();
     $entities_id = 0;
     if ($itemtype == "Entity") {
         $entities_id = $items_id;
     } else {
         $item = new $itemtype();
         $item->getFromDB($items_id);
         $entities_id = $item->fields['entities_id'];
     }
     $query = "SELECT * FROM `" . $this->getTable() . "`\n         WHERE `items_id`='" . $items_id . "'\n            AND `itemtype`='" . $itemtype . "'\n         LIMIT 1";
     $result = $DB->query($query);
     if ($DB->numrows($result) == '0') {
         $this->getEmpty();
         if ($entities_id != '0' or $itemtype != 'Entity') {
             $this->fields['plugin_monitoring_commands_id'] = -1;
             $this->fields['plugin_monitoring_checks_id'] = -1;
             $this->fields['calendars_id'] = -1;
             $this->fields['plugin_monitoring_realms_id'] = -1;
         }
     } else {
         $data = $DB->fetch_assoc($result);
         $this->getFromDB($data['id']);
     }
     echo "<form name='form' method='post' \n         action='" . $CFG_GLPI['root_doc'] . "/plugins/monitoring/front/hostconfig.form.php'>";
     echo "<table class='tab_cadre_fixe'";
     echo "<tr class='tab_bg_1'>";
     echo "<th colspan='4'>";
     echo __('Hosts configuration', 'monitoring');
     echo "</th>";
     echo "</tr>";
     echo "<tr class='tab_bg_1'>";
     echo "<td>";
     echo __('Command', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     $input = array();
     if ($entities_id != '0' or $itemtype != 'Entity') {
         $input["-1"] = __('Inheritance of the parent entity');
     }
     $query = "SELECT * FROM `" . getTableForItemType("PluginMonitoringCommand") . "`\n         ORDER BY `name`";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $input[$data['id']] = $data['name'];
     }
     Dropdown::showFromArray('plugin_monitoring_commands_id', $input, array('value' => $this->fields['plugin_monitoring_commands_id']));
     echo "</td>";
     echo "<td>" . __('Check definition', 'monitoring') . "&nbsp;:</td>";
     echo "<td>";
     $input = array();
     if ($entities_id != '0' or $itemtype != 'Entity') {
         $input["-1"] = __('Inheritance of the parent entity');
     }
     $query = "SELECT * FROM `" . getTableForItemType("PluginMonitoringCheck") . "`\n         ORDER BY `name`";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $input[$data['id']] = $data['name'];
     }
     Dropdown::showFromArray('plugin_monitoring_checks_id', $input, array('value' => $this->fields['plugin_monitoring_checks_id']));
     echo "</td>";
     echo "</tr>";
     // Inheritance
     if ($this->fields['plugin_monitoring_commands_id'] == '-1' or $this->fields['plugin_monitoring_checks_id'] == '-1') {
         echo "<tr class='tab_bg_1'>";
         if ($this->fields['plugin_monitoring_commands_id'] == '-1') {
             echo "<td colspan='2' class='green center'>";
             echo __('Inheritance of the parent entity') . "&nbsp;:&nbsp;";
             $pmCommand->getFromDB($this->getValueAncestor("plugin_monitoring_commands_id", $entities_id));
             echo $pmCommand->fields['name'];
             echo "</td>";
         } else {
             echo "<td colspan='2'>";
             echo "</td>";
         }
         if ($this->fields['plugin_monitoring_checks_id'] == '-1') {
             echo "<td colspan='2' class='green center'>";
             echo __('Inheritance of the parent entity') . "&nbsp;:&nbsp;";
             $pmCheck->getFromDB($this->getValueAncestor("plugin_monitoring_checks_id", $entities_id));
             echo $pmCheck->fields['name'];
             echo "</td>";
         } else {
             echo "<td colspan='2'>";
             echo "</td>";
         }
         echo "</tr>";
     }
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . __('Reaml', 'monitoring') . "&nbsp;:</td>";
     echo "<td>";
     $input = array();
     if ($entities_id != '0' or $itemtype != 'Entity') {
         $input["-1"] = __('Inheritance of the parent entity');
     }
     $query = "SELECT * FROM `" . getTableForItemType("PluginMonitoringRealm") . "`\n         ORDER BY `name`";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $input[$data['id']] = $data['name'];
     }
     Dropdown::showFromArray('plugin_monitoring_realms_id', $input, array('value' => $this->fields['plugin_monitoring_realms_id']));
     echo "</td>";
     echo "<td>" . __('Check period', 'monitoring') . "&nbsp;:</td>";
     echo "<td>";
     if (Session::isMultiEntitiesMode()) {
         $input = array();
         if ($entities_id != '0' or $itemtype != 'Entity') {
             $input["-1"] = __('Inheritance of the parent entity');
         }
         $entities_ancestors = getAncestorsOf("glpi_entities", $entities_id);
         if (!isset($entities_ancestors[$entities_id])) {
             $entities_ancestors[$entities_id] = $entities_id;
         }
         $query = "SELECT * FROM `" . getTableForItemType("Calendar") . "`\n            WHERE `entities_id` IN ('" . implode(",", $entities_ancestors) . "') AND `is_recursive`='1'\n            ORDER BY `name`";
         $result = $DB->query($query);
         while ($data = $DB->fetch_array($result)) {
             $input[$data['id']] = $data['name'];
         }
         Dropdown::showFromArray('calendars_id', $input, array('value' => $this->fields['calendars_id']));
     } else {
         Dropdown::show("Calendar", array('value' => $this->fields['calendars_id']));
     }
     echo "</td>";
     echo "</tr>";
     // Inheritance
     if ($this->fields['calendars_id'] == '-1' or $this->fields['plugin_monitoring_realms_id'] == '-1') {
         echo "<tr class='tab_bg_1'>";
         if ($this->fields['plugin_monitoring_realms_id'] == '-1') {
             echo "<td colspan='2' class='green center'>";
             echo __('Inheritance of the parent entity') . "&nbsp;:&nbsp;";
             $pmRealm->getFromDB($this->getValueAncestor("plugin_monitoring_realms_id", $entities_id));
             echo $pmRealm->fields['name'];
             echo "</td>";
         } else {
             echo "<td colspan='2'>";
             echo "</td>";
         }
         if ($this->fields['calendars_id'] == '-1') {
             echo "<td colspan='2' class='green center'>";
             echo __('Inheritance of the parent entity') . "&nbsp;:&nbsp;";
             $calendar->getFromDB($this->getValueAncestor("calendars_id", $entities_id));
             echo $calendar->fields['name'];
             echo "</td>";
         } else {
             echo "<td colspan='2'>";
             echo "</td>";
         }
         echo "</tr>";
     }
     if ($itemtype == 'Entity' and $items_id == '0') {
         echo "<tr class='tab_bg_1'>";
         echo "<td>";
         echo __('Shinken Server', 'monitoring') . "&nbsp;:";
         echo "</td>";
         echo "<td>";
         Dropdown::show("Computer", array('name' => 'computers_id', 'value' => $this->fields['computers_id']));
         echo "</td>";
         echo "<td colspan='2'></td>";
         echo "</tr>";
     }
     echo "<tr class='tab_bg_1'>";
     echo "<td colspan='4' align='center'>";
     if (isset($this->fields['id']) and $this->fields['id'] != '') {
         echo "<input type='hidden' name='id' value='" . $this->fields['id'] . "'/>";
     }
     echo "<input type='hidden' name='itemtype' value='" . $itemtype . "'/>";
     echo "<input type='hidden' name='items_id' value='" . $items_id . "'/>";
     echo "<input type='submit' name='update' value=\"" . __('Save') . "\" class='submit'>";
     echo "</td>";
     echo "</tr>";
     echo "</table>";
     Html::closeForm();
     return true;
 }
 function showComponents($componentscatalogs_id)
 {
     global $DB, $CFG_GLPI;
     $this->addComponent($componentscatalogs_id);
     $rand = mt_rand();
     $pmComponent = new PluginMonitoringComponent();
     $pmCommand = new PluginMonitoringCommand();
     $pmCheck = new PluginMonitoringCheck();
     $calendar = new Calendar();
     echo "<form method='post' name='componentscatalog_component_form{$rand}' id='componentscatalog_component_form{$rand}' action=\"" . $CFG_GLPI["root_doc"] . "/plugins/monitoring/front/componentscatalog_component.form.php\">";
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr>";
     echo "<th>";
     echo __('Associated components', 'monitoring');
     echo "</th>";
     echo "</tr>";
     echo "</table>";
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr>";
     echo "<th width='10'>&nbsp;</th>";
     echo "<th>" . __('Name') . "</th>";
     echo "<th>" . __('Command name', 'monitoring') . "</th>";
     echo "<th>" . __('Check definition', 'monitoring') . "</th>";
     echo "<th>" . __('Check period', 'monitoring') . "</th>";
     echo "<th>" . __('Remote check', 'monitoring') . "</th>";
     echo "</tr>";
     $used = array();
     $query = "SELECT * FROM `" . $this->getTable() . "`\n         WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscatalogs_id . "'";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $used[] = $data['plugin_monitoring_components_id'];
         $pmComponent->getFromDB($data['plugin_monitoring_components_id']);
         echo "<tr>";
         echo "<td>";
         echo "<input type='checkbox' name='item[" . $data["id"] . "]' value='1'>";
         echo "</td>";
         echo "<td class='center'>";
         echo $pmComponent->getLink(1);
         echo "</td>";
         echo "<td class='center'>";
         $pmCommand->getFromDB($pmComponent->fields['plugin_monitoring_commands_id']);
         echo $pmCommand->getLink();
         echo "</td>";
         echo "<td class='center'>";
         $pmCheck->getFromDB($pmComponent->fields['plugin_monitoring_checks_id']);
         echo $pmCheck->getLink();
         echo "</td>";
         echo "<td class='center'>";
         $calendar->getFromDB($pmComponent->fields['calendars_id']);
         echo $calendar->getLink();
         echo "</td>";
         echo "<td class='center'>";
         if ($pmComponent->fields['remotesystem'] == '') {
             echo "-";
         } else {
             echo $pmComponent->fields['remotesystem'];
         }
         echo "</td>";
         echo "</tr>";
     }
     Html::openArrowMassives("componentscatalog_host_form{$rand}", true);
     Html::closeArrowMassives(array('deleteitem' => _sx('button', 'Delete permanently')));
     Html::closeForm();
     echo "</table>";
 }
 function showComponents($componentscatalogs_id)
 {
     global $DB, $LANG, $CFG_GLPI;
     $this->addComponent($componentscatalogs_id);
     $rand = mt_rand();
     $pmComponent = new PluginMonitoringComponent();
     $pmCommand = new PluginMonitoringCommand();
     $pmCheck = new PluginMonitoringCheck();
     $calendar = new Calendar();
     echo "<form method='post' name='componentscatalog_component_form{$rand}' id='componentscatalog_component_form{$rand}' action=\"" . $CFG_GLPI["root_doc"] . "/plugins/monitoring/front/componentscatalog_component.form.php\">";
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr>";
     echo "<th>";
     echo $LANG['plugin_monitoring']['component'][2];
     echo "</th>";
     echo "</tr>";
     echo "</table>";
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr>";
     echo "<th width='10'>&nbsp;</th>";
     echo "<th>" . $LANG['common'][16] . "</th>";
     echo "<th>" . $LANG['plugin_monitoring']['command'][2] . "</th>";
     echo "<th>" . $LANG['plugin_monitoring']['check'][0] . "</th>";
     echo "<th>" . $LANG['plugin_monitoring']['host'][9] . "</th>";
     echo "<th>" . $LANG['plugin_monitoring']['service'][8] . "</th>";
     echo "</tr>";
     $used = array();
     $query = "SELECT * FROM `" . $this->getTable() . "`\n         WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscatalogs_id . "'";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $used[] = $data['plugin_monitoring_components_id'];
         $pmComponent->getFromDB($data['plugin_monitoring_components_id']);
         echo "<tr>";
         echo "<td>";
         echo "<input type='checkbox' name='item[" . $data["id"] . "]' value='1'>";
         echo "</td>";
         echo "<td class='center'>";
         echo $pmComponent->getLink(1);
         echo "</td>";
         echo "<td class='center'>";
         $pmCommand->getFromDB($pmComponent->fields['plugin_monitoring_commands_id']);
         echo $pmCommand->getLink();
         echo "</td>";
         echo "<td class='center'>";
         $pmCheck->getFromDB($pmComponent->fields['plugin_monitoring_checks_id']);
         echo $pmCheck->getLink();
         echo "</td>";
         echo "<td class='center'>";
         $calendar->getFromDB($pmComponent->fields['calendars_id']);
         echo $calendar->getLink();
         echo "</td>";
         echo "<td class='center'>";
         if ($pmComponent->fields['remotesystem'] == '') {
             echo "-";
         } else {
             echo $pmComponent->fields['remotesystem'];
         }
         echo "</td>";
         echo "</tr>";
     }
     openArrowMassive("componentscatalog_host_form{$rand}", true);
     closeArrowMassive('deleteitem', $LANG['buttons'][6]);
     echo "</table>";
 }
}
if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']])) {
    echo json_encode(array());
    //   echo __('No data ...', 'monitoring');
    exit;
}
if (isset($_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']][''])) {
    unset($_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']]['']);
}
$a_ret = $pmServicegraph->generateData($_GET['rrdtool_template'], $_GET['itemtype'], $_GET['items_id'], $_GET['timezone'], $_GET['time'], $enddate, $_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']]);
$mydatat = $a_ret[0];
$a_labels = $a_ret[1];
$pmComponent = new PluginMonitoringComponent();
$pmCommand = new PluginMonitoringCommand();
$pmComponent->getFromDB($_GET['components_id']);
$pmCommand->getFromDB($pmComponent->fields['plugin_monitoring_commands_id']);
$a_data = array();
$a_values = array();
$lab = '';
$num = 1;
$a_names = array();
$formaty = ".0f";
$max = 0;
foreach ($mydatat as $name => $data) {
    if (!isset($a_names[$name])) {
        $a_names[$name] = $num;
        $num++;
    }
    $display = "checked";
    if (isset($_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']])) {
        $display = "";
 /**
  * Display form for service configuration
  *
  * @param $items_id integer ID
  * @param $options array
  *
  *@return bool true if form is ok
  *
  **/
 function showForm($items_id, $options = array(), $copy = array())
 {
     global $DB, $CFG_GLPI;
     $pMonitoringCommand = new PluginMonitoringCommand();
     if (count($copy) > 0) {
         foreach ($copy as $key => $value) {
             $this->fields[$key] = stripslashes($value);
         }
     }
     $this->initForm($items_id, $options);
     if ($this->fields['id'] == 0) {
         $this->fields['active_checks_enabled'] = 1;
         $this->fields['passive_checks_enabled'] = 1;
     }
     $this->showFormHeader($options);
     if (isset($_SESSION['plugin_monitoring_components'])) {
         $this->fields = $_SESSION['plugin_monitoring_components'];
         if (!isset($this->fields["id"])) {
             $this->fields["id"] = '';
         }
         if (!isset($this->fields["arguments"])) {
             $this->fields["arguments"] = '';
         }
         unset($_SESSION['plugin_monitoring_components']);
     }
     echo "<tr>";
     echo "<td>";
     echo __('Name') . "<font class='red'>*</font>&nbsp;:";
     echo "</td>";
     echo "<td>";
     echo "<input type='hidden' name='is_template' value='1' />";
     $objectName = autoName($this->fields["name"], "name", 1, $this->getType());
     Html::autocompletionTextField($this, 'name', array('value' => $objectName));
     echo "</td>";
     // * checks
     echo "<td>" . __('Check definition', 'monitoring') . "<font class='red'>*</font>&nbsp;:</td>";
     echo "<td>";
     Dropdown::show("PluginMonitoringCheck", array('name' => 'plugin_monitoring_checks_id', 'value' => $this->fields['plugin_monitoring_checks_id']));
     echo "</td>";
     echo "</tr>";
     // * Link
     echo "<tr>";
     echo "<td>";
     echo __('Alias (Shinken service_description)', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     $objectDescription = autoName($this->fields["description"], "name", 1, $this->getType());
     Html::autocompletionTextField($this, 'description', array('value' => $objectDescription));
     echo "</td>";
     /*
           echo "<td>";
     //      echo "Type of template&nbsp;:";
           echo "</td>";
           echo "<td>";
     //      $a_types = array();
     //      $a_types[''] = Dropdown::EMPTY_VALUE;
     //      $a_types['partition'] = "Partition";
     //      $a_types['processor'] = "Processor";
     //      Dropdown::showFromArray("link", $a_types, array('value'=>$this->fields['link']));
           echo "</td>";
     */
     // * active check
     echo "<td>";
     echo __('Active check', 'monitoring') . "<font class='red'>*</font>&nbsp;:";
     echo "</td>";
     echo "<td>";
     Dropdown::showYesNo("active_checks_enabled", $this->fields['active_checks_enabled']);
     echo "</td>";
     echo "</tr>";
     // * command
     echo "<tr>";
     echo "<td>";
     echo __('Command', 'monitoring') . "<font class='red'>*</font>&nbsp;:";
     echo "</td>";
     echo "<td>";
     $pMonitoringCommand->getFromDB($this->fields['plugin_monitoring_commands_id']);
     Dropdown::show("PluginMonitoringCommand", array('name' => 'plugin_monitoring_commands_id', 'value' => $this->fields['plugin_monitoring_commands_id']));
     echo "</td>";
     // * passive check
     echo "<td>";
     echo __('Passive check', 'monitoring') . "<font class='red'>*</font>&nbsp;:";
     echo "</td>";
     echo "<td>";
     Dropdown::showYesNo("passive_checks_enabled", $this->fields['passive_checks_enabled']);
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<td>";
     echo __('Template (for graphs generation)', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     Dropdown::show('PluginMonitoringPerfdata', array('name' => 'graph_template', 'value' => $this->fields['graph_template']));
     echo "</td>";
     // * freshness
     echo "<td>" . __('Freshness (for passive mode)', 'monitoring') . "&nbsp;:</td>";
     echo "<td>";
     if ($this->fields['freshness_count'] == '') {
         $this->fields['freshness_count'] = 0;
     }
     Dropdown::showNumber("freshness_count", array('value' => $this->fields['freshness_count'], 'min' => 0, 'max' => 300));
     $a_time = array();
     $a_time['seconds'] = __('Second(s)', 'monitoring');
     $a_time['minutes'] = __('Minute(s)', 'monitoring');
     $a_time['hours'] = __('Hour(s)', 'monitoring');
     $a_time['days'] = __('Day(s)', 'monitoring');
     Dropdown::showFromArray("freshness_type", $a_time, array('value' => $this->fields['freshness_type']));
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<td>";
     echo __('Event handler', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     dropdown::show("PluginMonitoringEventhandler", array('name' => 'plugin_monitoring_eventhandlers_id', 'value' => $this->fields['plugin_monitoring_eventhandlers_id']));
     echo "</td>";
     // * calendar
     echo "<td>" . __('Check period', 'monitoring') . "<font class='red'>*</font>&nbsp;:</td>";
     echo "<td>";
     dropdown::show("Calendar", array('name' => 'calendars_id', 'value' => $this->fields['calendars_id']));
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<th colspan='4'>" . __('Remote check', 'monitoring') . "</th>";
     echo "</tr>";
     echo "<tr>";
     // * remotesystem
     echo "<td>";
     echo __('Utility used for remote check', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     $input = array();
     $input[''] = '------';
     $input['byssh'] = 'byssh';
     $input['nrpe'] = 'nrpe';
     $input['nsca'] = 'nsca';
     Dropdown::showFromArray("remotesystem", $input, array('value' => $this->fields['remotesystem']));
     echo "</td>";
     // * is_argument
     echo "<td>";
     echo __('Use arguments (NRPE only)', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     Dropdown::showYesNo("is_arguments", $this->fields['is_arguments']);
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     // alias command
     echo "<td>";
     echo __('Alias command if required (NRPE only)', 'monitoring') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     echo "<input type='text' name='alias_command' value='" . $this->fields['alias_command'] . "' size='35' />";
     echo "</td>";
     echo "<td colspan='2'></td>";
     echo "</tr>";
     // * Manage arguments
     $array = array();
     $a_displayarg = array();
     if (isset($pMonitoringCommand->fields['command_line'])) {
         preg_match_all("/\\\$(ARG\\d+)\\\$/", $pMonitoringCommand->fields['command_line'], $array);
         $a_arguments = importArrayFromDB($this->fields['arguments']);
         foreach ($array[0] as $arg) {
             if (strstr($arg, "ARG")) {
                 $arg = str_replace('$', '', $arg);
                 if (!isset($a_arguments[$arg])) {
                     $a_arguments[$arg] = '';
                 }
                 $a_displayarg[$arg] = $a_arguments[$arg];
             }
         }
     }
     if (count($a_displayarg) > 0) {
         $a_tags = $this->tagsAvailable();
         $a_argtext = importArrayFromDB($pMonitoringCommand->fields['arguments']);
         echo "<tr>";
         echo "<th colspan='4'>" . __('Arguments', 'monitoring') . "&nbsp;</th>";
         echo "</tr>";
         foreach ($a_displayarg as $key => $value) {
             echo "<tr>";
             echo "<td>";
             if (isset($a_argtext[$key]) and $a_argtext[$key] != '') {
                 echo nl2br($a_argtext[$key]) . "&nbsp;:";
             } else {
                 echo __('Argument', 'monitoring') . " (" . $key . ")&nbsp;:";
             }
             echo "</td>";
             echo "<td>";
             echo "<input type='text' name='arg[" . $key . "]' value='" . $value . "' size='35' /><br/>";
             echo "</td>";
             if (count($a_tags) > 0) {
                 foreach ($a_tags as $key => $value) {
                     echo "<td class='tab_bg_3'>";
                     echo "<strong>" . $key . "</strong>&nbsp;:";
                     echo "</td>";
                     echo "<td class='tab_bg_3'>";
                     echo $value;
                     echo "</td>";
                     unset($a_tags[$key]);
                     break;
                 }
             } else {
                 echo "<td colspan='2'></td>";
             }
             echo "</tr>";
         }
         foreach ($a_tags as $key => $value) {
             echo "<tr>";
             echo "<td colspan='2'></td>";
             echo "<td class='tab_bg_3'>";
             echo "<strong>" . $key . "</strong>&nbsp;:";
             echo "</td>";
             echo "<td class='tab_bg_3'>";
             echo $value;
             echo "</td>";
             echo "</tr>";
         }
     }
     if (Session::haveRight("plugin_monitoring_weathermap", READ)) {
         echo "<tr>";
         echo "<th colspan='4'>" . __('Weathermap', 'monitoring') . "&nbsp;</th>";
         echo "</tr>";
         echo "<tr>";
         echo "<td>";
         echo __('Use this component for Weathermap', 'monitoring') . "&nbsp;:";
         echo "</td>";
         echo "<td>";
         Dropdown::showYesNo("is_weathermap", $this->fields['is_weathermap']);
         echo "</td>";
         echo "<td>";
         $tooltip = __('Example', 'monitoring') . " :<br/><br/>";
         $tooltip .= "perfdata : <i>inUsage=0.00%;85;98 outUsage=0.00%;85;98 inBandwidth=<strong>789944</strong>.00bps outBandwidth=486006.00bps inAbsolut=0 outAbsolut=12665653</i><br/><br/>";
         $tooltip .= __('Regex bandwidth input', 'monitoring') . " : <i><strong>(?:.*)inBandwidth=(\\d+)(?:.*)</strong></i><br/><br/>";
         $tooltip .= __('Assign the value from regular expression') . " : <strong>789944</strong>";
         echo __('Regex bandwidth input', 'monitoring') . "&nbsp;";
         Html::showToolTip($tooltip, array('autoclose' => false));
         echo "&nbsp;:";
         echo "</td>";
         echo "<td>";
         echo "<input type='text' name='weathermap_regex_in' value='" . $this->fields['weathermap_regex_in'] . "' size='40' />";
         echo "</td>";
         echo "</tr>";
         echo "<tr>";
         echo "<td colspan='2'>";
         echo "</td>";
         echo "<td>";
         $tooltip = __('Example', 'monitoring') . " :<br/><br/>";
         $tooltip .= "perfdata : <i>inUsage=0.00%;85;98 outUsage=0.00%;85;98 inBandwidth=789944.00bps outBandwidth=<strong>486006</strong>.00bps inAbsolut=0 outAbsolut=12665653</i><br/><br/>";
         $tooltip .= __('Regex bandwidth output', 'monitoring') . " : <i><strong>(?:.*)outBandwidth=(\\d+)(?:.*)</strong></i><br/><br/>";
         $tooltip .= __('Assign the value from regular expression') . " : <strong>789944</strong>";
         echo __('Regex bandwidth output', 'monitoring') . "&nbsp;";
         Html::showToolTip($tooltip, array('autoclose' => false));
         echo "&nbsp;:";
         echo "</td>";
         echo "<td>";
         echo "<input type='text' name='weathermap_regex_out' value='" . $this->fields['weathermap_regex_out'] . "' size='40' />";
         echo "</td>";
         echo "</tr>";
     }
     $this->showFormButtons($options);
     return true;
 }
 function generateServicesCfg($file = 0, $tag = '')
 {
     global $DB;
     $pMonitoringCommand = new PluginMonitoringCommand();
     $pMonitoringCheck = new PluginMonitoringCheck();
     $pmComponent = new PluginMonitoringComponent();
     $pmEntity = new PluginMonitoringEntity();
     $pmContact_Item = new PluginMonitoringContact_Item();
     $pmService = new PluginMonitoringService();
     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
     $calendar = new Calendar();
     $user = new User();
     $pmLog = new PluginMonitoringLog();
     $profile_User = new Profile_User();
     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");
         $pmLog->add($input);
     }
     $hostnamebp = '';
     $a_services = array();
     $i = 0;
     // * Prepare contacts
     $a_contacts_entities = array();
     $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'\n         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;
     }
     $a_entities_allowed = $pmEntity->getEntitiesByTag($tag);
     $query = "SELECT * FROM `glpi_plugin_monitoring_services`";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $notadd = 0;
         $notadddescription = '';
         $a_component = current($pmComponent->find("`id`='" . $data['plugin_monitoring_components_id'] . "'", "", 1));
         $a_hostname = array();
         $queryh = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts` \n            WHERE `id` = '" . $data['plugin_monitoring_componentscatalogs_hosts_id'] . "'\n            LIMIT 1";
         $resulth = $DB->query($queryh);
         $hostname = '';
         $plugin_monitoring_componentscatalogs_id = 0;
         while ($datah = $DB->fetch_array($resulth)) {
             $itemtype = $datah['itemtype'];
             $item = new $itemtype();
             if ($item->getFromDB($datah['items_id'])) {
                 if (isset($a_entities_allowed['-1']) or isset($a_entities_allowed[$item->fields['entities_id']])) {
                     $a_hostname[] = $itemtype . "-" . $datah['items_id'] . "-" . preg_replace("/[^A-Za-z0-9]/", "", $item->fields['name']);
                     $hostname = $item->fields['name'];
                     $plugin_monitoring_componentscatalogs_id = $datah['plugin_monitoring_componentscalalog_id'];
                 }
             }
         }
         if (count($a_hostname) > 0) {
             if (isset($_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']])) {
                 $a_services[$i]['use'] = $_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']];
             }
             $a_services[$i]['host_name'] = implode(",", array_unique($a_hostname));
             $hostnamebp = $a_services[$i]['host_name'];
             // For business rules
             $a_services[$i]['service_description'] = preg_replace("/[^A-Za-z0-9]/", "", $a_component['name']) . "-" . $data['id'];
             $pMonitoringCommand->getFromDB($a_component['plugin_monitoring_commands_id']);
             // Manage arguments
             $array = array();
             preg_match_all("/\\\$(ARG\\d+)\\\$/", $pMonitoringCommand->fields['command_line'], $array);
             sort($array[0]);
             $a_arguments = importArrayFromDB($a_component['arguments']);
             $a_argumentscustom = importArrayFromDB($data['arguments']);
             foreach ($a_argumentscustom as $key => $value) {
                 $a_arguments[$key] = $value;
             }
             $args = '';
             foreach ($array[0] as $arg) {
                 if ($arg != '$PLUGINSDIR$' 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("PluginFusinvsnmpNetworkPort")) {
                                 $pfNetworkPort = new PluginFusinvsnmpNetworkPort();
                                 $pfNetworkPort->loadNetworkport($data['networkports_id']);
                                 $descr = '';
                                 $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]]")) {
                             $networkPort = new NetworkPort();
                             $networkPort->getFromDB($data['networkports_id']);
                             $portname = $pfNetworkPort->fields['name'];
                             $a_arguments[$arg] = str_replace("[[NETWORKPORTNAME]]", $portname, $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'];
                         }
                     }
                 }
             }
             // End manage arguments
             if ($a_component['remotesystem'] == 'nrpe') {
                 if ($a_component['alias_command'] != '') {
                     $a_services[$i]['check_command'] = "check_nrpe!" . $a_component['alias_command'];
                 } else {
                     $a_services[$i]['check_command'] = "check_nrpe!" . $pMonitoringCommand->fields['command_name'];
                 }
             } else {
                 $a_services[$i]['check_command'] = $pMonitoringCommand->fields['command_name'] . $args;
             }
             // * Contacts
             $a_contacts = array();
             $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'\n                  AND `items_id`='" . $plugin_monitoring_componentscatalogs_id . "'");
             foreach ($a_list_contact as $data_contact) {
                 if (isset($a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$data_contact['users_id']])) {
                     if (in_array($data['entities_id'], $a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$data_contact['users_id']])) {
                         //                  $pmContact->getFromDB($data_contact['plugin_monitoring_contacts_id']);
                         $user->getFromDB($data_contact['users_id']);
                         $a_contacts[] = $user->fields['name'];
                     }
                 }
             }
             $a_contacts_unique = array_unique($a_contacts);
             $a_services[$i]['contacts'] = implode(',', $a_contacts_unique);
             // ** If shinken not use templates or template not defined :
             if (!isset($_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']])) {
                 $pMonitoringCheck->getFromDB($a_component['plugin_monitoring_checks_id']);
                 $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval'];
                 $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval'];
                 $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts'];
                 if ($calendar->getFromDB($a_component['calendars_id'])) {
                     $a_services[$i]['check_period'] = $calendar->fields['name'];
                 }
                 $a_services[$i]['notification_interval'] = '30';
                 if ($calendar->getFromDB($a_component['calendars_id'])) {
                     $a_services[$i]['notification_period'] = $calendar->fields['name'];
                 } else {
                     $a_services[$i]['notification_period'] = "24x7";
                 }
                 $a_services[$i]['notification_options'] = 'w,c,r';
                 $a_services[$i]['active_checks_enabled'] = '1';
                 $a_services[$i]['process_perf_data'] = '1';
                 $a_services[$i]['active_checks_enabled'] = '1';
                 $a_services[$i]['passive_checks_enabled'] = '1';
                 $a_services[$i]['parallelize_check'] = '1';
                 $a_services[$i]['obsess_over_service'] = '1';
                 $a_services[$i]['check_freshness'] = '1';
                 $a_services[$i]['freshness_threshold'] = '1';
                 $a_services[$i]['notifications_enabled'] = '1';
                 $a_services[$i]['event_handler_enabled'] = '0';
                 //$a_services[$i]['event_handler'] = 'super_event_kill_everyone!DIE';
                 $a_services[$i]['flap_detection_enabled'] = '1';
                 $a_services[$i]['failure_prediction_enabled'] = '1';
                 $a_services[$i]['retain_status_information'] = '1';
                 $a_services[$i]['retain_nonstatus_information'] = '1';
                 $a_services[$i]['is_volatile'] = '0';
                 $a_services[$i]['_httpstink'] = 'NO';
             }
             $pmComponentscatalog->getFromDB($plugin_monitoring_componentscatalogs_id);
             if ($pmComponentscatalog->fields['notification_interval'] != '30') {
                 $a_services[$i]['notification_interval'] = $pmComponentscatalog->fields['notification_interval'];
             }
             if ($notadd == '1') {
                 unset($a_services[$i]);
                 $input = array();
                 $input['id'] = $data['id'];
                 $input['event'] = $notadddescription;
                 $input['state'] = "CRITICAL";
                 $input['state_type'] = "HARD";
                 $pmService->update($input);
             } else {
                 $i++;
             }
         }
     }
     //      // Business rules....
     $pmService = new PluginMonitoringService();
     $pmServicescatalog = new PluginMonitoringServicescatalog();
     $pMonitoringBusinessrulegroup = new PluginMonitoringBusinessrulegroup();
     $pmBusinessrule = new PluginMonitoringBusinessrule();
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     $a_listBA = $pmServicescatalog->find();
     foreach ($a_listBA as $dataBA) {
         if (isset($a_entities_allowed['-1']) or isset($a_entities_allowed[$dataBA['entities_id']])) {
             $a_grouplist = $pMonitoringBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='" . $dataBA['id'] . "'");
             $a_group = array();
             foreach ($a_grouplist as $gdata) {
                 $a_listBR = $pmBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $gdata['id'] . "'");
                 foreach ($a_listBR as $dataBR) {
                     if ($pmService->getFromDB($dataBR['plugin_monitoring_services_id'])) {
                         $pmComponentscatalog_Host->getFromDB($pmService->fields['plugin_monitoring_componentscatalogs_hosts_id']);
                         $itemtype = $pmComponentscatalog_Host->fields['itemtype'];
                         $item = new $itemtype();
                         if ($item->getFromDB($pmComponentscatalog_Host->fields['items_id'])) {
                             $hostname = $itemtype . "-" . $pmComponentscatalog_Host->fields['items_id'] . "-" . preg_replace("/[^A-Za-z0-9]/", "", $item->fields['name']);
                             if ($gdata['operator'] == 'and' or $gdata['operator'] == 'or' or strstr($gdata['operator'], ' of:')) {
                                 $operator = '|';
                                 if ($gdata['operator'] == 'and') {
                                     $operator = '&';
                                 }
                                 if (!isset($a_group[$gdata['id']])) {
                                     $a_group[$gdata['id']] = '';
                                     if (strstr($gdata['operator'], ' of:')) {
                                         $a_group[$gdata['id']] = $gdata['operator'];
                                     }
                                     $a_group[$gdata['id']] .= $hostname . "," . preg_replace("/[^A-Za-z0-9]/", "", $pmService->fields['name']) . "-" . $pmService->fields['id'];
                                 } else {
                                     $a_group[$gdata['id']] .= $operator . $hostname . "," . preg_replace("/[^A-Za-z0-9]/", "", $pmService->fields['name']) . "-" . $pmService->fields['id'];
                                 }
                             } else {
                                 $a_group[$gdata['id']] = $gdata['operator'] . " " . $hostname . "," . preg_replace("/[^A-Za-z0-9]/", "", $item->getName()) . "-" . $item->fields['id'];
                             }
                         }
                     }
                 }
             }
             if (count($a_group) > 0) {
                 $pMonitoringCheck->getFromDB($dataBA['plugin_monitoring_checks_id']);
                 $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval'];
                 $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval'];
                 $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts'];
                 if ($calendar->getFromDB($dataBA['calendars_id'])) {
                     $a_services[$i]['check_period'] = $calendar->fields['name'];
                 }
                 $a_services[$i]['host_name'] = $hostnamebp;
                 $a_services[$i]['service_description'] = preg_replace("/[^A-Za-z0-9]/", "", $dataBA['name']) . "-" . $dataBA['id'] . "-businessrules";
                 $command = "bp_rule!";
                 foreach ($a_group as $key => $value) {
                     if (!strstr($value, "&") and !strstr($value, "|")) {
                         $a_group[$key] = trim($value);
                     } else {
                         $a_group[$key] = "(" . trim($value) . ")";
                     }
                 }
                 $a_services[$i]['check_command'] = $command . implode("&", $a_group);
                 $a_services[$i]['notification_interval'] = '30';
                 if ($calendar->getFromDB($dataBA['calendars_id'])) {
                     $a_services[$i]['notification_period'] = $calendar->fields['name'];
                 } else {
                     $a_services[$i]['notification_period'] = "24x7";
                 }
                 $a_services[$i]['notification_options'] = 'w,c,r';
                 $a_services[$i]['active_checks_enabled'] = '1';
                 $a_services[$i]['process_perf_data'] = '1';
                 $a_services[$i]['active_checks_enabled'] = '1';
                 $a_services[$i]['passive_checks_enabled'] = '1';
                 $a_services[$i]['parallelize_check'] = '1';
                 $a_services[$i]['obsess_over_service'] = '1';
                 $a_services[$i]['check_freshness'] = '1';
                 $a_services[$i]['freshness_threshold'] = '1';
                 $a_services[$i]['notifications_enabled'] = '1';
                 $a_services[$i]['event_handler_enabled'] = '0';
                 //$a_services[$i]['event_handler'] = 'super_event_kill_everyone!DIE';
                 $a_services[$i]['flap_detection_enabled'] = '1';
                 $a_services[$i]['failure_prediction_enabled'] = '1';
                 $a_services[$i]['retain_status_information'] = '1';
                 $a_services[$i]['retain_nonstatus_information'] = '1';
                 $a_services[$i]['is_volatile'] = '0';
                 $a_services[$i]['_httpstink'] = 'NO';
                 $a_services[$i]['contacts'] = '';
                 $i++;
             }
         }
     }
     if ($file == "1") {
         $config = "# Generated by plugin monitoring for GLPI\n# on " . date("Y-m-d H:i:s") . "\n\n";
         foreach ($a_services as $data) {
             $config .= $this->constructFile("service", $data);
         }
         return array('services.cfg', $config);
     } else {
         return $a_services;
     }
 }
 function parseToRrdtool($plugin_monitoring_services_id)
 {
     global $DB;
     $pmRrdtool = new PluginMonitoringRrdtool();
     $pmCommand = new PluginMonitoringCommand();
     $pmService = new PluginMonitoringService();
     $pmComponent = new PluginMonitoringComponent();
     if ($pmService->getFromDB($plugin_monitoring_services_id)) {
         $pmComponent->getFromDB($pmService->fields['plugin_monitoring_components_id']);
         if (!isset($pmComponent->fields['plugin_monitoring_commands_id'])) {
             return;
         }
         if (is_null($pmComponent->fields['graph_template'])) {
             return;
         }
         $pmCommand->getFromDB($pmComponent->fields['plugin_monitoring_commands_id']);
         $pmUnavaibility = new PluginMonitoringUnavaibility();
         $pmUnavaibility->runUnavaibility($plugin_monitoring_services_id);
         $query = "SELECT * FROM `" . $this->getTable() . "`\n            WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n            ORDER BY `date`";
         $result = $DB->query($query);
         $i = 0;
         $nb_rows = $DB->numrows($result);
         $rrdtool_value = '';
         $last_date = '';
         while ($edata = $DB->fetch_array($result)) {
             $i++;
             if ($edata['unavailability'] == '0') {
                 if ($last_date != '') {
                     $pmRrdtool->addData($pmComponent->fields['graph_template'], $plugin_monitoring_services_id, 0, '', $rrdtool_value, 1);
                 }
                 break;
             }
             $perf_data = $edata['perf_data'];
             if ($edata['perf_data'] == '') {
                 $perf_data = $edata['output'];
             }
             if ($edata['unavailability'] != '2' and $i < $nb_rows) {
                 $rrdtool_value = $pmRrdtool->addData($pmComponent->fields['graph_template'], $plugin_monitoring_services_id, $this->convert_datetime_timestamp($edata['date']), $perf_data, $rrdtool_value, 0);
             }
             $last_date = $edata['date'];
             if ($i == $nb_rows) {
                 if ($edata['unavailability'] != '2') {
                     $input = array();
                     $input['id'] = $edata['id'];
                     $input['unavailability'] = 2;
                     $this->update($input);
                     $pmRrdtool->addData($pmComponent->fields['graph_template'], $plugin_monitoring_services_id, $this->convert_datetime_timestamp($edata['date']), $perf_data, $rrdtool_value, 1);
                     $queryd = "DELETE FROM `" . $this->getTable() . "`\n                     WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n                        AND `date`<'" . $edata['date'] . "'";
                     $DB->query($queryd);
                 }
             }
         }
         $a_list = array();
         $a_list[] = "2h";
         $a_list[] = "12h";
         $a_list[] = "1d";
         $a_list[] = "1w";
         $a_list[] = "1m";
         $a_list[] = "0y6m";
         $a_list[] = "1y";
         $pmConfig = new PluginMonitoringConfig();
         $pmConfig->getFromDB(1);
         $a_timezones = importArrayFromDB($pmConfig->fields['timezones']);
         foreach ($a_list as $time) {
             foreach ($a_timezones as $timezone) {
                 $pmRrdtool->displayGLPIGraph($pmComponent->fields['graph_template'], "PluginMonitoringService", $plugin_monitoring_services_id, $timezone, $time);
             }
         }
     } else {
         $query = "DELETE FROM `" . $this->getTable() . "`\n            WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'";
         $DB->query($query);
     }
 }
 /**
  * Display form for service configuration
  *
  * @param $items_id integer ID 
  * @param $options array
  *
  *@return bool true if form is ok
  *
  **/
 function showForm($items_id, $options = array(), $copy = array())
 {
     global $DB, $CFG_GLPI, $LANG;
     $pMonitoringCommand = new PluginMonitoringCommand();
     if ($items_id == '0') {
         $this->getEmpty();
         $this->fields['active_checks_enabled'] = 1;
         $this->fields['passive_checks_enabled'] = 1;
     } else {
         $this->getFromDB($items_id);
     }
     if (count($copy) > 0) {
         foreach ($copy as $key => $value) {
             $this->fields[$key] = stripslashes($value);
         }
     }
     $this->showTabs($options);
     $this->showFormHeader($options);
     if (isset($_SESSION['plugin_monitoring_components'])) {
         $this->fields = $_SESSION['plugin_monitoring_components'];
         if (!isset($this->fields["id"])) {
             $this->fields["id"] = '';
         }
         if (!isset($this->fields["arguments"])) {
             $this->fields["arguments"] = '';
         }
         unset($_SESSION['plugin_monitoring_components']);
     }
     echo "<tr>";
     echo "<td>";
     echo $LANG['common'][16] . "<font class='red'>*</font>&nbsp;:";
     echo "</td>";
     echo "<td>";
     echo "<input type='hidden' name='is_template' value='1' />";
     $objectName = autoName($this->fields["name"], "name", 1, $this->getType());
     autocompletionTextField($this, 'name', array('value' => $objectName));
     echo "</td>";
     // * checks
     echo "<td>" . $LANG['plugin_monitoring']['check'][0] . "<font class='red'>*</font>&nbsp;:</td>";
     echo "<td>";
     Dropdown::show("PluginMonitoringCheck", array('name' => 'plugin_monitoring_checks_id', 'value' => $this->fields['plugin_monitoring_checks_id']));
     echo "</td>";
     echo "</tr>";
     // * Link
     echo "<tr>";
     echo "<td>";
     //      echo "Type of template&nbsp;:";
     echo "</td>";
     echo "<td>";
     //      $a_types = array();
     //      $a_types[''] = DROPDOWN_EMPTY_VALUE;
     //      $a_types['partition'] = "Partition";
     //      $a_types['processor'] = "Processor";
     //      Dropdown::showFromArray("link", $a_types, array('value'=>$this->fields['link']));
     echo "</td>";
     // * active check
     echo "<td>";
     echo $LANG['plugin_monitoring']['host'][5] . "<font class='red'>*</font>&nbsp;:";
     echo "</td>";
     echo "<td>";
     Dropdown::showYesNo("active_checks_enabled", $this->fields['active_checks_enabled']);
     echo "</td>";
     echo "</tr>";
     // * command
     echo "<tr>";
     echo "<td>";
     echo $LANG['plugin_monitoring']['service'][5] . "<font class='red'>*</font>&nbsp;:";
     echo "</td>";
     echo "<td>";
     $pMonitoringCommand->getFromDB($this->fields['plugin_monitoring_commands_id']);
     Dropdown::show("PluginMonitoringCommand", array('name' => 'plugin_monitoring_commands_id', 'value' => $this->fields['plugin_monitoring_commands_id']));
     echo "</td>";
     // * passive check
     echo "<td>";
     echo $LANG['plugin_monitoring']['service'][7] . "<font class='red'>*</font>&nbsp;:";
     echo "</td>";
     echo "<td>";
     Dropdown::showYesNo("passive_checks_enabled", $this->fields['passive_checks_enabled']);
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<td>";
     echo $LANG['plugin_monitoring']['service'][12] . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     $a_templates = array();
     $a_templates[''] = DROPDOWN_EMPTY_VALUE;
     if ($handle = opendir(GLPI_PLUGIN_DOC_DIR . "/monitoring/templates/")) {
         while (false !== ($entry = readdir($handle))) {
             if ($entry != "." && $entry != "..") {
                 if (strstr($entry, "_graph.json")) {
                     $entry = str_replace("_graph.json", "", $entry);
                     $a_templates[$entry] = $entry;
                 }
             }
         }
         closedir($handle);
     }
     Dropdown::showFromArray("graph_template", $a_templates, array('value' => $this->fields['graph_template']));
     echo "</td>";
     // * calendar
     echo "<td>" . $LANG['plugin_monitoring']['host'][9] . "<font class='red'>*</font>&nbsp;:</td>";
     echo "<td>";
     dropdown::show("Calendar", array('name' => 'calendars_id', 'value' => $this->fields['calendars_id']));
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<th colspan='4'>" . $LANG['plugin_monitoring']['service'][8] . "</th>";
     echo "</tr>";
     echo "<tr>";
     // * remotesystem
     echo "<td>";
     echo $LANG['plugin_monitoring']['service'][9] . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     $input = array();
     $input[''] = '------';
     $input['byssh'] = 'byssh';
     $input['nrpe'] = 'nrpe';
     $input['nsca'] = 'nsca';
     Dropdown::showFromArray("remotesystem", $input, array('value' => $this->fields['remotesystem']));
     echo "</td>";
     // * is_argument
     echo "<td>";
     echo $LANG['plugin_monitoring']['service'][10] . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     Dropdown::showYesNo("is_arguments", $this->fields['is_arguments']);
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     // alias command
     echo "<td>";
     echo $LANG['plugin_monitoring']['service'][11] . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     echo "<input type='text' name='alias_command' value='" . $this->fields['alias_command'] . "' />";
     echo "</td>";
     echo "<td colspan='2'></td>";
     echo "</tr>";
     // * Manage arguments
     $array = array();
     $a_displayarg = array();
     if (isset($pMonitoringCommand->fields['command_line'])) {
         preg_match_all("/\\\$(ARG\\d+)\\\$/", $pMonitoringCommand->fields['command_line'], $array);
         $a_arguments = importArrayFromDB($this->fields['arguments']);
         foreach ($array[0] as $arg) {
             if (strstr($arg, "ARG")) {
                 $arg = str_replace('$', '', $arg);
                 if (!isset($a_arguments[$arg])) {
                     $a_arguments[$arg] = '';
                 }
                 $a_displayarg[$arg] = $a_arguments[$arg];
             }
         }
     }
     if (count($a_displayarg) > 0) {
         $a_tags = $this->tagsAvailable();
         $a_argtext = importArrayFromDB($pMonitoringCommand->fields['arguments']);
         echo "<tr>";
         echo "<th colspan='4'>" . $LANG['plugin_monitoring']['service'][4] . "&nbsp;</th>";
         echo "</tr>";
         foreach ($a_displayarg as $key => $value) {
             echo "<tr>";
             echo "<td>";
             if (isset($a_argtext[$key]) and $a_argtext[$key] != '') {
                 echo nl2br($a_argtext[$key]) . "&nbsp;:";
             } else {
                 echo $LANG['plugin_monitoring']['service'][14] . " (" . $key . ")&nbsp;:";
             }
             echo "</td>";
             echo "<td>";
             echo "<input type='text' name='arg[" . $key . "]' value='" . $value . "'/><br/>";
             echo "</td>";
             if (count($a_tags) > 0) {
                 foreach ($a_tags as $key => $value) {
                     echo "<td class='tab_bg_3'>";
                     echo "<strong>" . $key . "</strong>&nbsp;:";
                     echo "</td>";
                     echo "<td class='tab_bg_3'>";
                     echo $value;
                     echo "</td>";
                     unset($a_tags[$key]);
                     break;
                 }
             } else {
                 echo "<td colspan='2'></td>";
             }
             echo "</tr>";
         }
         foreach ($a_tags as $key => $value) {
             echo "<tr>";
             echo "<td colspan='2'></td>";
             echo "<td class='tab_bg_3'>";
             echo "<strong>" . $key . "</strong>&nbsp;:";
             echo "</td>";
             echo "<td class='tab_bg_3'>";
             echo $value;
             echo "</td>";
             echo "</tr>";
         }
     }
     echo "<tr>";
     echo "<th colspan='4'>" . $LANG['plugin_monitoring']['weathermap'][0] . "&nbsp;</th>";
     echo "</tr>";
     echo "<tr>";
     echo "<td>";
     echo $LANG['plugin_monitoring']['weathermap'][1] . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     Dropdown::showYesNo("is_weathermap", $this->fields['is_weathermap']);
     echo "</td>";
     echo "<td>";
     $tooltip = $LANG['plugin_monitoring']['component'][15] . " :<br/><br/>";
     $tooltip .= "perfdata : <i>inUsage=0.00%;85;98 outUsage=0.00%;85;98 inBandwidth=<strong>789944</strong>.00bps outBandwidth=486006.00bps inAbsolut=0 outAbsolut=12665653</i><br/><br/>";
     $tooltip .= $LANG['plugin_monitoring']['weathermap'][18] . " : <i><strong>(?:.*)inBandwidth=(\\d+)(?:.*)</strong></i><br/><br/>";
     $tooltip .= $LANG['rulesengine'][85] . " : <strong>789944</strong>";
     echo $LANG['plugin_monitoring']['weathermap'][18] . "&nbsp;";
     showToolTip($tooltip, array('autoclose' => false));
     echo "&nbsp;:";
     echo "</td>";
     echo "<td>";
     echo "<input type='text' name='weathermap_regex_in' value='" . $this->fields['weathermap_regex_in'] . "' size='40' />";
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<td colspan='2'>";
     echo "</td>";
     echo "<td>";
     $tooltip = $LANG['plugin_monitoring']['component'][15] . " :<br/><br/>";
     $tooltip .= "perfdata : <i>inUsage=0.00%;85;98 outUsage=0.00%;85;98 inBandwidth=789944.00bps outBandwidth=<strong>486006</strong>.00bps inAbsolut=0 outAbsolut=12665653</i><br/><br/>";
     $tooltip .= $LANG['plugin_monitoring']['weathermap'][19] . " : <i><strong>(?:.*)outBandwidth=(\\d+)(?:.*)</strong></i><br/><br/>";
     $tooltip .= $LANG['rulesengine'][85] . " : <strong>789944</strong>";
     echo $LANG['plugin_monitoring']['weathermap'][19] . "&nbsp;";
     showToolTip($tooltip, array('autoclose' => false));
     echo "&nbsp;:";
     echo "</td>";
     echo "<td>";
     echo "<input type='text' name='weathermap_regex_out' value='" . $this->fields['weathermap_regex_out'] . "' size='40' />";
     echo "</td>";
     echo "</tr>";
     $this->showFormButtons($options);
     // Add form for copy item
     if ($items_id != '') {
         $this->fields['id'] = 0;
         $this->showFormHeader($options);
         echo "<tr class='tab_bg_1'>";
         echo "<td colspan='4' class='center'>";
         foreach ($this->fields as $key => $value) {
             if ($key != 'id') {
                 echo "<input type='hidden' name='" . $key . "' value='" . $value . "'/>";
             }
         }
         echo "<input type='submit' name='copy' value=\"" . $LANG['setup'][283] . "\" class='submit'>";
         echo "</td>";
         echo "</tr>";
         echo "</table>";
         echo "</form>";
     }
     return true;
 }