getContact() public method

Get port opposite port ID if linked item
public getContact ( $ID ) : ID
$ID networking port ID
return ID of the NetworkPort found, false if not found
コード例 #1
0
 function getNetworkEquipment($id, $line, $a_links = array())
 {
     $networkPort = new NetworkPort();
     $a_networkports = $networkPort->find("`itemtype`='NetworkEquipment'\n         AND `items_id`='" . $id . "'");
     $i = 0;
     foreach ($a_networkports as $data_n) {
         $networkports_id = $networkPort->getContact($data_n['id']);
         if ($networkports_id) {
             $networkPort->getFromDB($networkports_id);
             switch ($networkPort->fields['itemtype']) {
                 case 'NetworkEquipment':
                     if ($i > 0) {
                         $this->line++;
                         $this->routes[$this->line] = $this->routes[$line];
                         $line = $this->line;
                     }
                     array_push($this->routes[$line], $networkPort->fields['items_id']);
                     if (!isset($this->devices[$networkPort->fields['items_id']])) {
                         $this->devices[$networkPort->fields['items_id']] = $networkPort->fields['items_id'];
                         $this->getNetworkEquipment($networkPort->fields['items_id'], $line);
                     }
                     $i++;
                     break;
             }
         }
     }
 }
コード例 #2
0
ファイル: canvas.class.php プロジェクト: geldarr/hack-space
 function getNetworkEquipments($networkequipments_id)
 {
     $networkPort = new NetworkPort();
     $a_networkports = $networkPort->find("`itemtype`='NetworkEquipment'\n         AND `items_id`='" . $networkequipments_id . "'");
     foreach ($a_networkports as $data_n) {
         $networkports_id = $networkPort->getContact($data_n['id']);
         if ($networkports_id) {
             $networkPort->getFromDB($networkports_id);
             switch ($networkPort->fields['itemtype']) {
                 case 'NetworkEquipment':
                     $this->a_devices_link['NetworkEquipment-' . $networkPort->fields['items_id']]['NetworkEquipment-' . $networkequipments_id] = 1;
                     if (!isset($this->a_devices['NetworkEquipment-' . $networkPort->fields['items_id']])) {
                         $this->a_devices['NetworkEquipment-' . $networkPort->fields['items_id']] = $this->getState('NetworkEquipment', $networkPort->fields['items_id']);
                         $this->getNetworkEquipments($networkPort->fields['items_id']);
                     }
                     break;
                 case 'Computer':
                 case 'Printer':
                     $this->a_devices_link['NetworkEquipment-' . $networkequipments_id][$networkPort->fields['itemtype'] . '-' . $networkPort->fields['items_id']] = 1;
                     if (!isset($this->a_devices[$networkPort->fields['itemtype'] . '-' . $networkPort->fields['items_id']])) {
                         $this->a_devices[$networkPort->fields['itemtype'] . '-' . $networkPort->fields['items_id']] = $this->getState($networkPort->fields['itemtype'], $networkPort->fields['items_id']);
                     }
                     break;
             }
         }
     }
 }
コード例 #3
0
   function generateHostsCfg($file=0, $tag='') {
      global $DB;

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

               $a_fields = array();

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

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

               $a_fields = $pmComponent->fields;

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

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

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


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

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

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

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

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

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

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


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

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

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

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

               $i++;
            // }
         }
      }


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

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

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


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

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

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

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

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

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

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

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

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

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

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


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

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

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

      } else {
         return $a_hosts;
      }
   }
コード例 #4
0
   function configureNodesLinks($weathermaps_id) {
      global $DB,$CFG_GLPI;

      $networkPort = new NetworkPort();

      $this->getFromDB($weathermaps_id);

      $style = '';
      if ($this->fields['width'] > 950) {
         $style = ";position:relative;left:-".(($this->fields['width'] - 950) / 2)."px";
      }


      echo "<table class='tab_cadre' style='width:".
              $this->fields['width']."px;height:".$this->fields['height']."px".
              $style."'>";
      echo "<tr class='tab_bg_1'>";
      echo "<th colspan='2'>";
      echo __('Nodes and links', 'monitoring');
      echo "</th>";
      echo "</tr>";

      echo "<tr class='tab_bg_1'>";
      echo "<td valign='top'>";
      echo "<div>";
      PluginMonitoringToolbox::loadLib();
      $this->drawMap($weathermaps_id, 100, 1);
      echo "</div>";
      echo "</td>";
      echo "<td valign='top'>";

      echo "<div style='position: fixed;top: 30px;right: 0;z-index:999;' >";
      echo "<table class='tab_cadre' width='100%'>";
      echo "<tr>";
      echo "<td>";
      echo "<a onClick='Ext.get(\"weathermapform\").toggle();'>
      <img src='".$CFG_GLPI["root_doc"]."/pics/deplier_down.png' />&nbsp;
         ".__('Display weathermap form', 'monitoring')."
      &nbsp;<img src='".$CFG_GLPI["root_doc"]."/pics/deplier_down.png' /></a>";
      echo "</td>";
      echo "</tr>";
      echo"</table>";
      echo "</div>";

      echo "<div style='position: fixed;top: 50px;right: 0;z-index:1000;' id='weathermapform' >";
      echo '<form name="pointform" method="post" action="'.$CFG_GLPI['root_doc'].'/plugins/monitoring/front/weathermapnode.form.php">';
      echo "<table>";
      echo "<tr>";
      echo "<td>";

         echo "<table class='tab_cadre' width='100%'>";
         echo "<tr>";
         echo "<th colspan='2'>";
         echo "x : ";
         echo '<input type="text" name="x" size="4" value="50" />';
         echo " ";
         echo "y : ";
         echo '<input type="text" name="y" size="4" value="50"/>';
         echo "</th>";
         echo "</tr>";

         // * Add node
         echo "<tr>";
         echo "<th colspan='2'>";
         echo "<input type='hidden' name='plugin_monitoring_weathermaps_id' value='".$weathermaps_id."' />";
         echo __('Add a node', 'monitoring');
         echo "</th>";
         echo "</tr>";

         echo "<tr>";
         echo "<td>";
         echo __('Node', 'monitoring')."&nbsp;:";
         echo "</td>";
         echo "<td>";
         Dropdown::showAllItems("items_id");
         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td>";
         echo __('Name')."&nbsp;:";
         echo "</td>";
         echo "<td>";
         echo "<input type='text' name='name' value='' />";
         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td>";
         echo __('Position of label', 'monitoring')." :";
         echo "</td>";
         echo "<td>";
         $positions = array(
             'middle' => __('Center', 'monitoring'),
             'start' => __('Right', 'monitoring'),
             'end' => __('Left', 'monitoring')
         );
         Dropdown::showFromArray('position', $positions);
         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td align='center' colspan='2'>";
         echo "<input type='submit' name='add' value=\"".__('Add')."\" class='submit'>";
         echo "</td>";
         echo "</tr>";


         // * Change node position
         echo "<tr>";
         echo "<th colspan='2'>";
         echo __('Edit a node', 'monitoring');
         echo "</th>";
         echo "</tr>";

         echo "<tr>";
         echo "<td colspan='2' align='center'>";

         $query = "SELECT * FROM `".getTableForItemType("PluginMonitoringWeathermapnode")."`
            WHERE `plugin_monitoring_weathermaps_id`='".$weathermaps_id."'
            ORDER BY `name`";
         $result = $DB->query($query);
         $elements = array();
         $elements[0] = Dropdown::EMPTY_VALUE;
         $result = $DB->query($query);
         while ($data=$DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            if ($itemtype == '0') {
               $pmWeathermapnode->delete($data);
            } else {
               $item = new $itemtype();
               $item->getFromDB($data['items_id']);
               $name = $data['name'];
               if ($name == '') {
                  $name = $item->getName();
               }
               $elements[$data['id']] = $name;
            }
         }
         $rand = Dropdown::showFromArray('id_update', $elements);

         $params = array('items_id'        => '__VALUE__',
                         'rand'            => $rand);

         Ajax::updateItemOnSelectEvent("dropdown_id_update$rand", "show_updatenode$rand",
                                     $CFG_GLPI["root_doc"]."/plugins/monitoring/ajax/dropdownWnode.php",
                                     $params, TRUE);

         echo "<span id='show_updatenode$rand'></span>\n";

         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td align='center' colspan='2'>";
         echo "<input type='submit' name='update' value=\"".__('Save')."\" class='submit'>";
         echo "</td>";
         echo "</tr>";


         // * Delete node
         echo "<tr>";
         echo "<th colspan='2'>";
         echo __('Delete a node', 'monitoring');
         echo "</th>";
         echo "</tr>";

         echo "<tr>";
         echo "<td>";
         echo "</td>";
         echo "<td>";
         $query = "SELECT * FROM `".getTableForItemType("PluginMonitoringWeathermapnode")."`
            WHERE `plugin_monitoring_weathermaps_id`='".$weathermaps_id."'
            ORDER BY `name`";
         $result = $DB->query($query);
         $elements = array();
         $elements[0] = Dropdown::EMPTY_VALUE;
         $result = $DB->query($query);
         while ($data=$DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($data['items_id']);
            $name = $data['name'];
            if ($name == '') {
               $name = $item->getName();
            }
            $elements[$data['id']] = $name;
         }
         Dropdown::showFromArray('id', $elements);
         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td align='center' colspan='2'>";
         echo "<input type='submit' name='purge' value=\"".__('Delete permanently')."\" class='submit'>";
         echo "</td>";
         echo "</tr>";

         echo "</table>";
         Html::closeForm();

      echo "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td>";

         echo '<form name="formlink" method="post" action="'.$CFG_GLPI['root_doc'].'/plugins/monitoring/front/weathermaplink.form.php">';
         echo "<table class='tab_cadre' width='100%'>";
         // *Add Link
         echo "<tr>";
         echo "<th colspan='2'>";
         echo __('Add a link', 'monitoring');
         echo "</th>";
         echo "</tr>";

         echo "<tr>";
         echo "<td>";
         echo __('Source', 'monitoring')."*&nbsp;:";
         echo "</td>";
         echo "<td>";

         $query = "SELECT `glpi_plugin_monitoring_weathermapnodes`.`id` as `id`,
               `glpi_plugin_monitoring_weathermapnodes`.`name` as `name`,
               `glpi_plugin_monitoring_componentscatalogs_hosts`.`itemtype`,
               `glpi_plugin_monitoring_componentscatalogs_hosts`.`items_id`,
               `glpi_plugin_monitoring_services`.`id` as `services_id`,
               `glpi_plugin_monitoring_components`.`name` as `components_name`,
               `plugin_monitoring_commands_id`, `glpi_plugin_monitoring_components`.`arguments`,
               `glpi_plugin_monitoring_services`.`networkports_id`
            FROM `glpi_plugin_monitoring_weathermapnodes`

            LEFT JOIN `glpi_plugin_monitoring_componentscatalogs_hosts`
               ON (`glpi_plugin_monitoring_weathermapnodes`.`items_id`=`glpi_plugin_monitoring_componentscatalogs_hosts`.`items_id`
                  AND `glpi_plugin_monitoring_weathermapnodes`.`itemtype`=`glpi_plugin_monitoring_componentscatalogs_hosts`.`itemtype`)

            LEFT JOIN `glpi_plugin_monitoring_services`
               ON `plugin_monitoring_componentscatalogs_hosts_id`= `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`

            LEFT JOIN `glpi_plugin_monitoring_components`
               ON `plugin_monitoring_components_id` = `glpi_plugin_monitoring_components`.`id`


            WHERE `is_weathermap` = '1'
               AND `plugin_monitoring_weathermaps_id`='".$weathermaps_id."'
            ORDER BY `itemtype`,`items_id`,`glpi_plugin_monitoring_components`.`name`";
         $elements = array();
         $elements[0] = Dropdown::EMPTY_VALUE;
         $elements2 = array();
         $result = $DB->query($query);
         while ($data=$DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($data['items_id']);
            $name = $data['name'];
            if ($name == '') {
               $name = $item->getName();
            }
            // Try to get device/node connected on this port
            $device_connected = '';
            if ($data['arguments'] != '') {
               $arguments = importArrayFromDB($data['arguments']);
               foreach ($arguments as $argument) {
                  if (!is_numeric($argument)) {
                     if (strstr($argument, "[[NETWORKPORTDESCR]]")){
                        if (class_exists("PluginFusinvsnmpNetworkPort")) {
                           $pfNetworkPort = new PluginFusinvsnmpNetworkPort();
                           $pfNetworkPort->loadNetworkport($data['networkports_id']);
                           $argument = $pfNetworkPort->getValue("ifdescr");
                        }
                     } elseif (strstr($argument, "[[NETWORKPORTNUM]]")){
                        $networkPort = new NetworkPort();
                        $networkPort->getFromDB($data['networkports_id']);
                        $argument = $pfNetworkPort->fields['logical_number'];
                     } elseif (strstr($argument, "[[NETWORKPORTNAME]]")){
                        $networkPort = new NetworkPort();
                        $networkPort->getFromDB($data['networkports_id']);
                        $argument = $pfNetworkPort->fields['name'];
                     }


                     // Search networkport have this name or description
                     $a_ports = $networkPort->find("`itemtype`='".$itemtype."'
                        AND `items_id`='".$data['items_id']."'
                        AND `name`='".$argument."'");
                     foreach ($a_ports as $pdata) {
                        if ($device_connected == '') {
                           $oppositeports_id = $networkPort->getContact($pdata['id']);
                           if ($oppositeports_id) {
                              $networkPort->getFromDB($oppositeports_id);
                              $a_nodes = $pmWeathermapnode->find("
                                 `plugin_monitoring_weathermaps_id`='".$weathermaps_id."'
                                 AND `itemtype`='".$networkPort->fields['itemtype']."'
                                 AND `items_id`='".$networkPort->fields['items_id']."'", "", 1);
                              if (count($a_nodes) > 0) {
                                 $a_node = current($a_nodes);
                                 $device_connected = $pmWeathermapnode->getNodeName($a_node['id']);
                              }
                           }
                        }
                     }
                     if ($device_connected == ''
                             AND class_exists("PluginFusinvsnmpNetworkPort")) {
                        $queryn = "SELECT `glpi_networkports`.`id` FROM `glpi_plugin_fusinvsnmp_networkports`

                           LEFT JOIN `glpi_networkports`
                              ON `glpi_networkports`.`id`=`networkports_id`

                           WHERE `itemtype`='".$itemtype."'
                           AND `items_id`='".$data['items_id']."'
                           AND `ifdescr`='".$argument."'";
                        $resultn = $DB->query($queryn);
                        while ($pdata=$DB->fetch_array($resultn)) {
                           if ($device_connected == '') {
                              $oppositeports_id = $networkPort->getContact($pdata['id']);
                              if ($oppositeports_id) {
                                 $networkPort->getFromDB($oppositeports_id);
                                 $a_nodes = $pmWeathermapnode->find("
                                    `plugin_monitoring_weathermaps_id`='".$weathermaps_id."'
                                    AND `itemtype`='".$networkPort->fields['itemtype']."'
                                    AND `items_id`='".$networkPort->fields['items_id']."'", "", 1);
                                 if (count($a_nodes) > 0) {
                                    $a_node = current($a_nodes);

                                    $queryl = "SELECT `plugin_monitoring_weathermapnodes_id_1`
                                       FROM `glpi_plugin_monitoring_weathermaplinks`

                                       LEFT JOIN `glpi_plugin_monitoring_weathermapnodes`
                                          ON `glpi_plugin_monitoring_weathermapnodes`.`id` = `plugin_monitoring_weathermapnodes_id_1`

                                       WHERE ((`plugin_monitoring_weathermapnodes_id_1`='".$data['id']."'
                                                   AND `plugin_monitoring_weathermapnodes_id_2`='".$a_node['id']."')
                                                OR (`plugin_monitoring_weathermapnodes_id_1`='".$a_node['id']."'
                                                   AND `plugin_monitoring_weathermapnodes_id_2`='".$data['id']."'))
                                          AND `plugin_monitoring_weathermaps_id` = '".$weathermaps_id."'";
                                    $resultl = $DB->query($queryl);
                                    if ($DB->numrows($resultl) == '0') {
                                       $device_connected = $pmWeathermapnode->getNodeName($a_node['id']);
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
            if ($device_connected == '') {
               $networkPort->getFromDB($data['networkports_id']);
               $elements2[$data['id']."-".$data['services_id']] = $name." [".$networkPort->getfield('name')."] (".$data['components_name'].")";
            } else {
               $networkPort->getFromDB($data['networkports_id']);
               $elements[$data['id']."-".$data['services_id']] = $name." [".$networkPort->getfield('name')."] (".$data['components_name'].") > ".$device_connected;
            }
         }
         if (count($elements) > 1
                 AND count($elements2) > 0) {

            $elements = array_merge($elements,array('0'=>Dropdown::EMPTY_VALUE));
            $elements = array_merge($elements, $elements2);

         } else {
            $elements = array_merge($elements, $elements2);
         }

         Dropdown::showFromArray('linksource', $elements);

         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td>";
         echo __('Destination', 'monitoring')."&nbsp;:";
         echo "</td>";
         echo "<td>";

         echo "<div id='nodedestination'>";

         $query = "SELECT * FROM `".getTableForItemType("PluginMonitoringWeathermapnode")."`
            WHERE `plugin_monitoring_weathermaps_id`='".$weathermaps_id."'
            ORDER BY `name`";
         $result = $DB->query($query);
         $elements = array();
         $elements[0] = Dropdown::EMPTY_VALUE;
         $result = $DB->query($query);
         while ($data=$DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($data['items_id']);
            $name = $data['name'];
            if ($name == '') {
               $name = $item->getName();
            }
            $elements[$data['id']] = $name;
         }
         Dropdown::showFromArray('plugin_monitoring_weathermapnodes_id_2', $elements);
         echo "</div>";
         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td>";
         echo __('Max bandwidth input', 'monitoring')."&nbsp;:";
         echo "</td>";
         echo "<td>";
         echo "<input type='text' name='bandwidth_in' value=''/>";
         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td>";
         echo __('Max bandwidth output', 'monitoring')."&nbsp;:";
         echo "</td>";
         echo "<td>";
         echo "<input type='text' name='bandwidth_out' value=''/>";
         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td align='center' colspan='2'>";
         echo "<input type='submit' name='add' value=\"".__('Add')."\" class='submit'>";
         echo "</td>";
         echo "</tr>";

         // * Edit link
         echo "<tr>";
         echo "<th colspan='2'>";
         echo __('Edit a link', 'monitoring');
         echo "</th>";
         echo "</tr>";
         echo "<tr>";
         echo "<td colspan='2' align='center'>";
         $pmWeathermapnode = new PluginMonitoringWeathermapnode();
         $query = "SELECT `glpi_plugin_monitoring_weathermaplinks`.`id` as `id`,
               `itemtype`, `items_id`, `name`, `plugin_monitoring_weathermapnodes_id_2`
            FROM `glpi_plugin_monitoring_weathermaplinks`

            LEFT JOIN `glpi_plugin_monitoring_weathermapnodes`
               ON `glpi_plugin_monitoring_weathermapnodes`.`id` = `plugin_monitoring_weathermapnodes_id_1`

            WHERE `plugin_monitoring_weathermaps_id` = '".$weathermaps_id."'";
         $elements = array();
         $elements[0] = Dropdown::EMPTY_VALUE;
         $result = $DB->query($query);
         while ($data=$DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($data['items_id']);
            $name1 = $data['name'];
            if ($name1 == '') {
               $name1 = $item->getName();
            }
            $pmWeathermapnode->getFromDB($data['plugin_monitoring_weathermapnodes_id_2']);
            $itemtype = $pmWeathermapnode->fields['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($pmWeathermapnode->fields['items_id']);
            $name2 = $pmWeathermapnode->fields['name'];
            if ($name2 == '') {
               $name2 = $item->getName();
            }

            $elements[$data['id']] = $name1." - ".$name2;
         }
         $rand = Dropdown::showFromArray('id_update', $elements);

         $params = array('items_id'        => '__VALUE__',
                         'rand'            => $rand);

         Ajax::updateItemOnSelectEvent("dropdown_id_update$rand", "show_updatelink$rand",
                                     $CFG_GLPI["root_doc"]."/plugins/monitoring/ajax/dropdownWlink.php",
                                     $params, TRUE);
         echo "<span id='show_updatelink$rand'></span>\n";
         echo "</td>";
         echo "</tr>";


         // * Delete link
         echo "<tr>";
         echo "<th colspan='2'>";
         echo __('Delete a link', 'monitoring');
         echo "</th>";
         echo "</tr>";

         echo "<tr>";
         echo "<td>";
         echo __('Link', 'monitoring')." :";
         echo "</td>";
         echo "<td>";
         Dropdown::showFromArray('id', $elements);
         echo "</td>";
         echo "</tr>";

         echo "<tr>";
         echo "<td align='center' colspan='2'>";
         echo "<input type='submit' name='purge' value=\"".__('Delete permanently')."\" class='submit'>";
         echo "</td>";
         echo "</tr>";

         echo "</table>";
         Html::closeForm();

      echo "</td>";
      echo "</tr>";
      echo "</table>";
      echo "</div>";

      echo "</td>";
      echo "</tr>";

      echo "</table>";
   }
コード例 #5
0
    function configureNodesLinks($weathermaps_id)
    {
        global $DB, $CFG_GLPI;
        $networkPort = new NetworkPort();
        $pmWeathermapnode = new PluginMonitoringWeathermapnode();
        $this->getFromDB($weathermaps_id);
        echo "<table class='tab_cadre_fixe'>";
        echo "<tr class='tab_bg_1'>";
        echo "<th colspan='2'>";
        echo __('Nodes and links', 'monitoring');
        echo "</th>";
        echo "</tr>";
        $this->generateWeathermap($weathermaps_id, 1, 1);
        $map = "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/monitoring/front/send.php?file=weathermap-" . $weathermaps_id . ".png'/>";
        echo "<tr class='tab_bg_1'>";
        echo "<td valign='top' width='10'>";
        if ($this->fields['background'] == '') {
            echo '<script language="JavaScript" type="text/JavaScript">

         function FindPosition(oElement) {
            if(typeof( oElement.offsetParent ) != "undefined") {
               for(var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
                  posX += oElement.offsetLeft;
                  posY += oElement.offsetTop;
               }
               return [ posX, posY ];
            } else {
               return [ oElement.x, oElement.y ];
            }
         }

         function GetCoordinates(e) {
            var PosX = 0;
            var PosY = 0;
            var ImgPos;
            ImgPos = FindPosition(myImg);
            if (!e) var e = window.event;
            if (e.pageX || e.pageY) {
               PosX = e.pageX;
               PosY = e.pageY;
            } else if (e.clientX || e.clientY) {
               PosX = e.clientX + document.body.scrollLeft
                 + document.documentElement.scrollLeft;
               PosY = e.clientY + document.body.scrollTop
                 + document.documentElement.scrollTop;
            }
            PosX = PosX - ImgPos[0];
            PosY = PosY - ImgPos[1];

            document.pointform.x.value = PosX;
            document.pointform.y.value = PosY;
         }

         var myImg = document.getElementById("myImgId");
         myImg.onmousedown = GetCoordinates;

         </script>';
            echo "<div><img src='" . $CFG_GLPI['root_doc'] . "/plugins/monitoring/front/send.php?file=weathermap-" . $weathermaps_id . ".png'/>";
            echo "<div style='position: absolute; top:40px;' id='myImgId' >\n            <table class='gridweathermap' width='" . $this->fields['width'] . "' \n               height='" . $this->fields['height'] . "'>";
            $line = '';
            $nbcol = ceil($this->fields['width'] / 15);
            for ($num = 0; $num < $nbcol; $num++) {
                $line .= "<td></td>";
            }
            $line = '<tr>' . $line . '</tr>';
            $nbline = ceil($this->fields['height'] / 15);
            for ($num = 0; $num < $nbline; $num++) {
                echo $line;
            }
            echo "</table></div></div>";
        } else {
            echo '<div id="pointer_div" onclick="point_it(event)" style = "background-image:url(\'' . $this->fields['background'] . '\');">
            <img id="cross" style="position:relative;visibility:hidden;z-index:2;">
            ' . $map . '</div>';
            echo '<script language="JavaScript">
         function point_it(event){
            pos_x = event.offsetX?(event.offsetX):event.pageX;
            pos_y = event.offsetY?(event.offsetY):event.pageY;
            document.getElementById("cross").style.left = (pos_x-1) ;
            document.getElementById("cross").style.top = (pos_y-15) ;

            var topValue= 0;
            var leftValue= 0;
            var obj = document.getElementById("pointer_div");
            while(obj){
               leftValue+= obj.offsetLeft;
               topValue+= obj.offsetTop;
               obj= obj.offsetParent;
            }


            document.pointform.x.value = pos_x-leftValue;
            document.pointform.y.value = pos_y-topValue;

         }
         </script>';
        }
        echo "</td>";
        echo "<td valign='top'>";
        echo "<div style='position: fixed;top: 30px;right: 0;z-index:999;' >";
        echo "<table class='tab_cadre' width='100%'>";
        echo "<tr>";
        echo "<td>";
        echo "<a onClick='Ext.get(\"weathermapform\").toggle();'>\n      <img src='" . $CFG_GLPI["root_doc"] . "/pics/deplier_down.png' />&nbsp;\n         " . __('Display weathermap form', 'monitoring') . "\n      &nbsp;<img src='" . $CFG_GLPI["root_doc"] . "/pics/deplier_down.png' /></a>";
        echo "</td>";
        echo "</tr>";
        echo "</table>";
        echo "</div>";
        echo "<div style='position: fixed;top: 50px;right: 0;z-index:1000;' id='weathermapform' >";
        echo '<form name="pointform" method="post" action="' . $CFG_GLPI['root_doc'] . '/plugins/monitoring/front/weathermapnode.form.php">';
        echo "<table>";
        echo "<tr>";
        echo "<td>";
        echo "<table class='tab_cadre' width='100%'>";
        echo "<tr>";
        echo "<th colspan='2'>";
        echo "x : ";
        echo '<input type="text" name="x" size="4" />';
        echo " ";
        echo "y : ";
        echo '<input type="text" name="y" size="4" />';
        echo "</th>";
        echo "</tr>";
        // * Add node
        echo "<tr>";
        echo "<th colspan='2'>";
        echo "<input type='hidden' name='plugin_monitoring_weathermaps_id' value='" . $weathermaps_id . "' />";
        echo __('Add a node', 'monitoring');
        echo "</th>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo __('Node', 'monitoring') . "&nbsp;:";
        echo "</td>";
        echo "<td>";
        Dropdown::showAllItems("items_id");
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo __('Name') . "&nbsp;:";
        echo "</td>";
        echo "<td>";
        echo "<input type='text' name='name' value='' />";
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td align='center' colspan='2'>";
        echo "<input type='submit' name='add' value=\"" . __('Add') . "\" class='submit'>";
        echo "</td>";
        echo "</tr>";
        // * Change node position
        echo "<tr>";
        echo "<th colspan='2'>";
        echo __('Edit a node', 'monitoring');
        echo "</th>";
        echo "</tr>";
        echo "<tr>";
        echo "<td colspan='2' align='center'>";
        $query = "SELECT * FROM `" . getTableForItemType("PluginMonitoringWeathermapnode") . "`\n            WHERE `plugin_monitoring_weathermaps_id`='" . $weathermaps_id . "'\n            ORDER BY `name`";
        $result = $DB->query($query);
        $elements = array();
        $elements[0] = Dropdown::EMPTY_VALUE;
        $result = $DB->query($query);
        while ($data = $DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            if ($itemtype == '0') {
                $pmWeathermapnode->delete($data);
            } else {
                $item = new $itemtype();
                $item->getFromDB($data['items_id']);
                $name = $data['name'];
                if ($name == '') {
                    $name = $item->getName();
                }
                $elements[$data['id']] = $name;
            }
        }
        $rand = Dropdown::showFromArray('id_update', $elements);
        $params = array('items_id' => '__VALUE__', 'rand' => $rand);
        Ajax::updateItemOnSelectEvent("dropdown_id_update{$rand}", "show_updatenode{$rand}", $CFG_GLPI["root_doc"] . "/plugins/monitoring/ajax/dropdownWnode.php", $params, false);
        echo "<span id='show_updatenode{$rand}'></span>\n";
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td align='center' colspan='2'>";
        echo "<input type='submit' name='update' value=\"" . __('Save') . "\" class='submit'>";
        echo "</td>";
        echo "</tr>";
        // * Delete node
        echo "<tr>";
        echo "<th colspan='2'>";
        echo __('Delete a node', 'monitoring');
        echo "</th>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo "</td>";
        echo "<td>";
        $query = "SELECT * FROM `" . getTableForItemType("PluginMonitoringWeathermapnode") . "`\n            WHERE `plugin_monitoring_weathermaps_id`='" . $weathermaps_id . "'\n            ORDER BY `name`";
        $result = $DB->query($query);
        $elements = array();
        $elements[0] = Dropdown::EMPTY_VALUE;
        $result = $DB->query($query);
        while ($data = $DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($data['items_id']);
            $name = $data['name'];
            if ($name == '') {
                $name = $item->getName();
            }
            $elements[$data['id']] = $name;
        }
        Dropdown::showFromArray('id', $elements);
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td align='center' colspan='2'>";
        echo "<input type='submit' name='purge' value=\"" . __('Delete permanently') . "\" class='submit'>";
        echo "</td>";
        echo "</tr>";
        echo "</table>";
        Html::closeForm();
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo '<form name="formlink" method="post" action="' . $CFG_GLPI['root_doc'] . '/plugins/monitoring/front/weathermaplink.form.php">';
        echo "<table class='tab_cadre' width='100%'>";
        // *Add Link
        echo "<tr>";
        echo "<th colspan='2'>";
        echo __('Add a link', 'monitoring');
        echo "</th>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo __('Source', 'monitoring') . "*&nbsp;:";
        echo "</td>";
        echo "<td>";
        $query = "SELECT `glpi_plugin_monitoring_weathermapnodes`.`id` as `id`,\n               `glpi_plugin_monitoring_weathermapnodes`.`name` as `name`,\n               `glpi_plugin_monitoring_componentscatalogs_hosts`.`itemtype`, \n               `glpi_plugin_monitoring_componentscatalogs_hosts`.`items_id`,\n               `glpi_plugin_monitoring_services`.`id` as `services_id`,\n               `glpi_plugin_monitoring_components`.`name` as `components_name`,\n               `plugin_monitoring_commands_id`, `glpi_plugin_monitoring_components`.`arguments`,\n               `glpi_plugin_monitoring_services`.`networkports_id`\n            FROM `glpi_plugin_monitoring_weathermapnodes`\n            \n            LEFT JOIN `glpi_plugin_monitoring_componentscatalogs_hosts`\n               ON (`glpi_plugin_monitoring_weathermapnodes`.`items_id`=`glpi_plugin_monitoring_componentscatalogs_hosts`.`items_id`\n                  AND `glpi_plugin_monitoring_weathermapnodes`.`itemtype`=`glpi_plugin_monitoring_componentscatalogs_hosts`.`itemtype`)\n            \n            LEFT JOIN `glpi_plugin_monitoring_services` \n               ON `plugin_monitoring_componentscatalogs_hosts_id`= `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n\n            LEFT JOIN `glpi_plugin_monitoring_components` \n               ON `plugin_monitoring_components_id` = `glpi_plugin_monitoring_components`.`id`\n            \n\n            WHERE `is_weathermap` = '1'\n               AND `plugin_monitoring_weathermaps_id`='" . $weathermaps_id . "'\n            ORDER BY `itemtype`,`items_id`,`glpi_plugin_monitoring_components`.`name`";
        $elements = array();
        $elements[0] = Dropdown::EMPTY_VALUE;
        $elements2 = array();
        $result = $DB->query($query);
        while ($data = $DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($data['items_id']);
            $name = $data['name'];
            if ($name == '') {
                $name = $item->getName();
            }
            // Try to get device/node connected on this port
            $device_connected = '';
            if ($data['arguments'] != '') {
                $arguments = importArrayFromDB($data['arguments']);
                foreach ($arguments as $argument) {
                    if (!is_numeric($argument)) {
                        if (strstr($argument, "[[NETWORKPORTDESCR]]")) {
                            if (class_exists("PluginFusinvsnmpNetworkPort")) {
                                $pfNetworkPort = new PluginFusinvsnmpNetworkPort();
                                $pfNetworkPort->loadNetworkport($data['networkports_id']);
                                $argument = $pfNetworkPort->getValue("ifdescr");
                            }
                        } elseif (strstr($argument, "[[NETWORKPORTNUM]]")) {
                            $networkPort = new NetworkPort();
                            $networkPort->getFromDB($data['networkports_id']);
                            $argument = $pfNetworkPort->fields['logical_number'];
                        } elseif (strstr($argument, "[[NETWORKPORTNAME]]")) {
                            $networkPort = new NetworkPort();
                            $networkPort->getFromDB($data['networkports_id']);
                            $argument = $pfNetworkPort->fields['name'];
                        }
                        // Search networkport have this name or description
                        $a_ports = $networkPort->find("`itemtype`='" . $itemtype . "'\n                        AND `items_id`='" . $data['items_id'] . "'\n                        AND `name`='" . $argument . "'");
                        foreach ($a_ports as $pdata) {
                            if ($device_connected == '') {
                                $oppositeports_id = $networkPort->getContact($pdata['id']);
                                if ($oppositeports_id) {
                                    $networkPort->getFromDB($oppositeports_id);
                                    $a_nodes = $pmWeathermapnode->find("\n                                 `plugin_monitoring_weathermaps_id`='" . $weathermaps_id . "'\n                                 AND `itemtype`='" . $networkPort->fields['itemtype'] . "'\n                                 AND `items_id`='" . $networkPort->fields['items_id'] . "'", "", 1);
                                    if (count($a_nodes) > 0) {
                                        $a_node = current($a_nodes);
                                        $device_connected = $pmWeathermapnode->getNodeName($a_node['id']);
                                    }
                                }
                            }
                        }
                        if ($device_connected == '' and class_exists("PluginFusinvsnmpNetworkPort")) {
                            $queryn = "SELECT `glpi_networkports`.`id` FROM `glpi_plugin_fusinvsnmp_networkports`\n                           \n                           LEFT JOIN `glpi_networkports`\n                              ON `glpi_networkports`.`id`=`networkports_id`\n                              \n                           WHERE `itemtype`='" . $itemtype . "'\n                           AND `items_id`='" . $data['items_id'] . "'\n                           AND `ifdescr`='" . $argument . "'";
                            $resultn = $DB->query($queryn);
                            while ($pdata = $DB->fetch_array($resultn)) {
                                if ($device_connected == '') {
                                    $oppositeports_id = $networkPort->getContact($pdata['id']);
                                    if ($oppositeports_id) {
                                        $networkPort->getFromDB($oppositeports_id);
                                        $a_nodes = $pmWeathermapnode->find("\n                                    `plugin_monitoring_weathermaps_id`='" . $weathermaps_id . "'\n                                    AND `itemtype`='" . $networkPort->fields['itemtype'] . "'\n                                    AND `items_id`='" . $networkPort->fields['items_id'] . "'", "", 1);
                                        if (count($a_nodes) > 0) {
                                            $a_node = current($a_nodes);
                                            $queryl = "SELECT `plugin_monitoring_weathermapnodes_id_1`\n                                       FROM `glpi_plugin_monitoring_weathermaplinks`\n\n                                       LEFT JOIN `glpi_plugin_monitoring_weathermapnodes`\n                                          ON `glpi_plugin_monitoring_weathermapnodes`.`id` = `plugin_monitoring_weathermapnodes_id_1`\n\n                                       WHERE ((`plugin_monitoring_weathermapnodes_id_1`='" . $data['id'] . "'\n                                                   AND `plugin_monitoring_weathermapnodes_id_2`='" . $a_node['id'] . "')\n                                                OR (`plugin_monitoring_weathermapnodes_id_1`='" . $a_node['id'] . "'\n                                                   AND `plugin_monitoring_weathermapnodes_id_2`='" . $data['id'] . "'))\n                                          AND `plugin_monitoring_weathermaps_id` = '" . $weathermaps_id . "'";
                                            $resultl = $DB->query($queryl);
                                            if ($DB->numrows($resultl) == '0') {
                                                $device_connected = $pmWeathermapnode->getNodeName($a_node['id']);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if ($device_connected == '') {
                $networkPort->getFromDB($data['networkports_id']);
                $elements2[$data['id'] . "-" . $data['services_id']] = $name . " [" . $networkPort->getfield('name') . "] (" . $data['components_name'] . ")";
            } else {
                $networkPort->getFromDB($data['networkports_id']);
                $elements[$data['id'] . "-" . $data['services_id']] = $name . " [" . $networkPort->getfield('name') . "] (" . $data['components_name'] . ") > " . $device_connected;
            }
        }
        if (count($elements) > 1 and count($elements2) > 0) {
            $elements = array_merge($elements, array('0' => Dropdown::EMPTY_VALUE));
            $elements = array_merge($elements, $elements2);
        } else {
            $elements = array_merge($elements, $elements2);
        }
        Dropdown::showFromArray('linksource', $elements);
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo __('Destination', 'monitoring') . "&nbsp;:";
        echo "</td>";
        echo "<td>";
        echo "<div id='nodedestination'>";
        $query = "SELECT * FROM `" . getTableForItemType("PluginMonitoringWeathermapnode") . "`\n            WHERE `plugin_monitoring_weathermaps_id`='" . $weathermaps_id . "'\n            ORDER BY `name`";
        $result = $DB->query($query);
        $elements = array();
        $elements[0] = Dropdown::EMPTY_VALUE;
        $result = $DB->query($query);
        while ($data = $DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($data['items_id']);
            $name = $data['name'];
            if ($name == '') {
                $name = $item->getName();
            }
            $elements[$data['id']] = $name;
        }
        Dropdown::showFromArray('plugin_monitoring_weathermapnodes_id_2', $elements);
        echo "</div>";
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo __('Max bandwidth input', 'monitoring') . "&nbsp;:";
        echo "</td>";
        echo "<td>";
        echo "<input type='text' name='bandwidth_in' value=''/>";
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo __('Max bandwidth output', 'monitoring') . "&nbsp;:";
        echo "</td>";
        echo "<td>";
        echo "<input type='text' name='bandwidth_out' value=''/>";
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td align='center' colspan='2'>";
        echo "<input type='submit' name='add' value=\"" . __('Add') . "\" class='submit'>";
        echo "</td>";
        echo "</tr>";
        // * Edit link
        echo "<tr>";
        echo "<th colspan='2'>";
        echo __('Edit a link', 'monitoring');
        echo "</th>";
        echo "</tr>";
        echo "<tr>";
        echo "<td colspan='2' align='center'>";
        $pmWeathermapnode = new PluginMonitoringWeathermapnode();
        $query = "SELECT `glpi_plugin_monitoring_weathermaplinks`.`id` as `id`,\n               `itemtype`, `items_id`, `name`, `plugin_monitoring_weathermapnodes_id_2`\n            FROM `glpi_plugin_monitoring_weathermaplinks`\n            \n            LEFT JOIN `glpi_plugin_monitoring_weathermapnodes`\n               ON `glpi_plugin_monitoring_weathermapnodes`.`id` = `plugin_monitoring_weathermapnodes_id_1`\n\n            WHERE `plugin_monitoring_weathermaps_id` = '" . $weathermaps_id . "'";
        $elements = array();
        $elements[0] = Dropdown::EMPTY_VALUE;
        $result = $DB->query($query);
        while ($data = $DB->fetch_array($result)) {
            $itemtype = $data['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($data['items_id']);
            $name1 = $data['name'];
            if ($name1 == '') {
                $name1 = $item->getName();
            }
            $pmWeathermapnode->getFromDB($data['plugin_monitoring_weathermapnodes_id_2']);
            $itemtype = $pmWeathermapnode->fields['itemtype'];
            $item = new $itemtype();
            $item->getFromDB($pmWeathermapnode->fields['items_id']);
            $name2 = $pmWeathermapnode->fields['name'];
            if ($name2 == '') {
                $name2 = $item->getName();
            }
            $elements[$data['id']] = $name1 . " - " . $name2;
        }
        $rand = Dropdown::showFromArray('id_update', $elements);
        $params = array('items_id' => '__VALUE__', 'rand' => $rand);
        Ajax::updateItemOnSelectEvent("dropdown_id_update{$rand}", "show_updatelink{$rand}", $CFG_GLPI["root_doc"] . "/plugins/monitoring/ajax/dropdownWlink.php", $params, false);
        echo "<span id='show_updatelink{$rand}'></span>\n";
        echo "</td>";
        echo "</tr>";
        // * Delete link
        echo "<tr>";
        echo "<th colspan='2'>";
        echo __('Delete a link', 'monitoring');
        echo "</th>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo "Link :";
        echo "</td>";
        echo "<td>";
        Dropdown::showFromArray('id', $elements);
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td align='center' colspan='2'>";
        echo "<input type='submit' name='purge' value=\"" . __('Delete permanently') . "\" class='submit'>";
        echo "</td>";
        echo "</tr>";
        echo "</table>";
        Html::closeForm();
        echo "</td>";
        echo "</tr>";
        echo "</table>";
        echo "</div>";
        echo "</td>";
        echo "</tr>";
    }
コード例 #6
0
 function assignVlan($port, $vlan)
 {
     global $DB;
     $query = "INSERT INTO\n                `glpi_networkports_vlans` (`networkports_id`,`vlans_id`)\n                VALUES ('{$port}','{$vlan}')";
     $DB->query($query);
     $np = new NetworkPort();
     if ($contact_id = $np->getContact($port)) {
         if ($np->getFromDB($contact_id)) {
             $vlans = self::getVlansForNetworkPort($port);
             if (!in_array($vlan, $vlans)) {
                 $query = "INSERT INTO\n                        `glpi_networkports_vlans` (`networkports_id`,`vlans_id`)\n                        VALUES ('{$contact_id}','{$vlan}')";
                 $DB->query($query);
             }
         }
     }
 }
コード例 #7
0
 function displayHubConnections($items_id, $background_img)
 {
     $NetworkPort = new NetworkPort();
     $a_ports = $NetworkPort->find("`itemtype`='PluginFusioninventoryUnmanaged'\n                                    AND `items_id`='" . $items_id . "'");
     echo "<table width='100%' class='tab_cadre' cellpadding='5'>";
     foreach ($a_ports as $a_port) {
         if ($a_port['name'] != "Link") {
             $id = $NetworkPort->getContact($a_port['id']);
             if ($id) {
                 $NetworkPort->getFromDB($id);
                 $link = '';
                 $link1 = '';
                 $link2 = '';
                 if ($NetworkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                     $classname = $NetworkPort->fields['itemtype'];
                     $item = new $classname();
                     $item->getFromDB($NetworkPort->fields['items_id']);
                     $link1 = $item->getLink(1);
                     $link = str_replace($item->getName(0), $NetworkPort->fields["mac"], $item->getLink());
                     // Get ips
                     $a_ips = PluginFusioninventoryToolbox::getIPforDevice('PluginFusioninventoryUnmanaged', $item->getID());
                     $link2 = str_replace($item->getName(0), implode(", ", $a_ips), $item->getLink());
                     $icon = $this->getItemtypeIcon($item->fields["item_type"]);
                     if ($item->fields['accepted'] == 1) {
                         echo "<tr>";
                         echo "<td align='center'  style='background:#bfec75'\n                                              class='tab_bg_1_2'>" . $icon . $item->getLink(1);
                     } else {
                         echo "<tr>";
                         echo "<td align='center' style='background:#cf9b9b'\n                                              class='tab_bg_1_2'>" . $icon . $item->getLink(1);
                     }
                     if (!empty($link)) {
                         echo "<br/>" . $link;
                     }
                     if (!empty($link2)) {
                         echo "<br/>" . $link2;
                     }
                     echo "</td>";
                     echo "</tr>";
                 } else {
                     $classname = $NetworkPort->fields['itemtype'];
                     $item = new $classname();
                     $item->getFromDB($NetworkPort->fields['items_id']);
                     $link1 = $item->getLink(1);
                     $link = str_replace($item->getName(0), $NetworkPort->fields["mac"], $item->getLink());
                     //                  $link2 = str_replace($item->getName(0), $NetworkPort->fields["ip"],
                     //                                       $item->getLink());
                     echo "<tr>";
                     $icon = $this->getItemtypeIcon($classname);
                     echo "<td align='center'  " . $background_img . "\n                                           class='tab_bg_1_2'>" . $icon . $item->getLink(1);
                     if (!empty($link)) {
                         echo "<br/>" . $link;
                     }
                     if (!empty($link2)) {
                         echo "<br/>" . $link2;
                     }
                     echo "</td>";
                     echo "</tr>";
                 }
             }
         }
     }
     echo "</table>";
 }
コード例 #8
0
 static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
 {
     global $DB;
     $ID = $item->getField('id');
     $type = get_class($item);
     $query = "SELECT `glpi_networkports`.`id`\n                FROM `glpi_networkports`\n                WHERE `items_id` = '" . $ID . "'\n                      AND `itemtype` = '" . $type . "'\n                ORDER BY `name`, `logical_number`";
     $pdf->setColumnsSize(100);
     if ($result = $DB->query($query)) {
         $nb_connect = $DB->numrows($result);
         if (!$nb_connect) {
             $pdf->displayTitle('<b>0 ' . __('No network port found') . '</b>');
         } else {
             $pdf->displayTitle('<b>' . sprintf(__('%1$s: %2$d'), _n('Network port', 'Network ports', $nb_connect), $nb_connect . "</b>"));
             while ($devid = $DB->fetch_row($result)) {
                 $netport = new NetworkPort();
                 $netport->getfromDB(current($devid));
                 $instantiation_type = $netport->fields["instantiation_type"];
                 $instname = call_user_func(array($instantiation_type, 'getTypeName'));
                 $pdf->displayTitle('<b>' . $instname . '</b>');
                 $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), '#</b>', $netport->fields["logical_number"]));
                 $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Name') . '</b>', $netport->fields["name"]));
                 $contact = new NetworkPort();
                 $netport2 = new NetworkPort();
                 $add = __('Not connected.');
                 if ($cid = $contact->getContact($netport->fields["id"])) {
                     if ($netport2->getFromDB($cid) && ($device2 = getItemForItemtype($netport2->fields["itemtype"]))) {
                         if ($device2->getFromDB($netport2->fields["items_id"])) {
                             $add = $netport2->getName() . ' ' . __('on') . ' ' . $device2->getName() . ' (' . $device2->getTypeName() . ')';
                         }
                     }
                 }
                 if ($instantiation_type == 'NetworkPortEthernet') {
                     $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Connected to') . '</b>', $add));
                     $netportethernet = new NetworkPortEthernet();
                     $speed = $type = '';
                     if ($netportethernet->getFromDB($netport->fields['id'])) {
                         $speed = NetworkPortEthernet::getPortSpeed($netportethernet->fields['speed']);
                         $type = NetworkPortEthernet::getPortTypeName($netportethernet->fields['type']);
                     }
                     $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Ethernet port speed') . '</b>', $speed));
                     $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Ethernet port type') . '</b>', $type));
                     $netpoint = new Netpoint();
                     $outlet = '';
                     if ($netpoint->getFromDB($netportethernet->fields['netpoints_id'])) {
                         $outlet = $netpoint->fields['name'];
                     }
                     $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Network outlet') . '</b>', $outlet));
                 }
                 $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('MAC') . '</b>', $netport->fields["mac"]));
                 $sqlip = "LEFT JOIN `glpi_networknames`\n                           ON (`glpi_ipaddresses`.`items_id` = `glpi_networknames`.`id`\n                               AND `glpi_ipaddresses`.`entities_id`\n                                    = '" . $_SESSION['glpiactive_entity'] . "')\n                         WHERE `glpi_networknames`.`items_id` = '" . $netport->fields["id"] . "'";
                 $ipname = '';
                 $ip = new IPAddress();
                 if ($ip->getFromDBByQuery($sqlip)) {
                     $ipname = $ip->fields['name'];
                     $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('ip') . '</b>', $ipname));
                     $sql = "SELECT `glpi_ipaddresses_ipnetworks`.`ipnetworks_id`\n                      FROM `glpi_ipaddresses_ipnetworks`\n                      LEFT JOIN `glpi_ipnetworks`\n                           ON (`glpi_ipaddresses_ipnetworks`.`ipnetworks_id` = `glpi_ipnetworks`.`id`)\n                      WHERE `glpi_ipaddresses_ipnetworks`.`ipaddresses_id` = '" . $ip->getID() . "'";
                     $res = $DB->query($sql);
                     if ($res) {
                         while ($row = $DB->fetch_assoc($res)) {
                             $ipnetwork = new IPNetwork();
                             if ($ipnetwork->getFromDB($row['ipnetworks_id'])) {
                                 $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('IP network') . '</b>', $ipnetwork->fields['address']));
                                 $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Subnet mask') . '</b>', $ipnetwork->fields['netmask']));
                                 $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Gateway') . '</b>', $ipnetwork->fields['gateway']));
                             }
                         }
                     }
                 }
             }
             // each port
         }
         // Found
     }
     // Query
     $pdf->displaySpace();
 }
コード例 #9
0
function plugin_item_purge_fusioninventory($parm)
{
    switch (get_class($parm)) {
        case 'NetworkPort_NetworkPort':
            // If remove connection of a hub port (unknown device), we must delete this port too
            $NetworkPort = new NetworkPort();
            $NetworkPort_Vlan = new NetworkPort_Vlan();
            $pfUnmanaged = new PluginFusioninventoryUnmanaged();
            $networkPort_NetworkPort = new NetworkPort_NetworkPort();
            $a_hubs = array();
            $port_id = $NetworkPort->getContact($parm->getField('networkports_id_1'));
            $NetworkPort->getFromDB($parm->getField('networkports_id_1'));
            if ($NetworkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                $pfUnmanaged->getFromDB($NetworkPort->fields['items_id']);
                if ($pfUnmanaged->fields['hub'] == '1') {
                    $a_hubs[$NetworkPort->fields['items_id']] = 1;
                    $NetworkPort->delete($NetworkPort->fields);
                }
            }
            $NetworkPort->getFromDB($port_id);
            if ($port_id) {
                if ($NetworkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                    $pfUnmanaged->getFromDB($NetworkPort->fields['items_id']);
                    if ($pfUnmanaged->fields['hub'] == '1') {
                        $a_hubs[$NetworkPort->fields['items_id']] = 1;
                    }
                }
            }
            $port_id = $NetworkPort->getContact($parm->getField('networkports_id_2'));
            $NetworkPort->getFromDB($parm->getField('networkports_id_2'));
            if ($NetworkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                if ($pfUnmanaged->getFromDB($NetworkPort->fields['items_id'])) {
                    if ($pfUnmanaged->fields['hub'] == '1') {
                        $a_vlans = $NetworkPort_Vlan->getVlansForNetworkPort($NetworkPort->fields['id']);
                        foreach ($a_vlans as $vlan_id) {
                            $NetworkPort_Vlan->unassignVlan($NetworkPort->fields['id'], $vlan_id);
                        }
                        $a_hubs[$NetworkPort->fields['items_id']] = 1;
                        $NetworkPort->delete($NetworkPort->fields);
                    }
                }
            }
            if ($port_id) {
                $NetworkPort->getFromDB($port_id);
                if ($NetworkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                    $pfUnmanaged->getFromDB($NetworkPort->fields['items_id']);
                    if ($pfUnmanaged->fields['hub'] == '1') {
                        $a_hubs[$NetworkPort->fields['items_id']] = 1;
                    }
                }
            }
            // If hub have no port, delete it
            foreach (array_keys($a_hubs) as $unkowndevice_id) {
                $a_networkports = $NetworkPort->find("`itemtype`='PluginFusioninventoryUnmanaged'\n               AND `items_id`='" . $unkowndevice_id . "' ");
                if (count($a_networkports) < 2) {
                    $pfUnmanaged->delete(array('id' => $unkowndevice_id), 1);
                } else {
                    if (count($a_networkports) == '2') {
                        $switchPorts_id = 0;
                        $otherPorts_id = 0;
                        foreach ($a_networkports as $data) {
                            if ($data['name'] == 'Link') {
                                $switchPorts_id = $NetworkPort->getContact($data['id']);
                            } else {
                                if ($otherPorts_id == '0') {
                                    $otherPorts_id = $NetworkPort->getContact($data['id']);
                                } else {
                                    $switchPorts_id = $NetworkPort->getContact($data['id']);
                                }
                            }
                        }
                        $pfUnmanaged->disconnectDB($switchPorts_id);
                        // disconnect this port
                        $pfUnmanaged->disconnectDB($otherPorts_id);
                        // disconnect destination port
                        $networkPort_NetworkPort->add(array('networkports_id_1' => $switchPorts_id, 'networkports_id_2' => $otherPorts_id));
                    }
                }
            }
            break;
        case 'NetworkEquipment':
            // Delete all ports
            $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_networkequipments`\n                          WHERE `networkequipments_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            $query_select = "SELECT `glpi_plugin_fusioninventory_networkports`.`id`,\n                              `glpi_networkports`.`id` as nid\n                          FROM `glpi_plugin_fusioninventory_networkports`\n                               LEFT JOIN `glpi_networkports`\n                                         ON `glpi_networkports`.`id` = `networkports_id`\n                          WHERE `items_id`='" . $parm->fields["id"] . "'\n                                AND `itemtype`='NetworkEquipment';";
            $result = $DB->query($query_select);
            while ($data = $DB->fetch_array($result)) {
                $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_networkports`\n                             WHERE `id`='" . $data["id"] . "';";
                $DB->query($query_delete);
                $query_delete = "DELETE FROM `glpi_plugin_fusinvsnmp_networkportlogs`\n                           WHERE `networkports_id`='" . $data['nid'] . "'";
                $DB->query($query_delete);
            }
            break;
        case "Printer":
            $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_printers`\n                          WHERE `printers_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_printercartridges`\n                          WHERE `printers_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_printerlogs`\n                          WHERE `printers_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            break;
        case 'PluginFusioninventoryUnmanaged':
            $query_delete = "DELETE FROM `glpi_plugin_fusinvsnmp_unmanageds`\n                          WHERE `plugin_fusioninventory_unmanageds_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            break;
    }
    return $parm;
}
コード例 #10
0
 function generateHostsCfg($file = 0, $tag = '')
 {
     global $DB;
     $pmCommand = new PluginMonitoringCommand();
     $pmCheck = new PluginMonitoringCheck();
     $pmComponent = new PluginMonitoringComponent();
     $pmEntity = new PluginMonitoringEntity();
     $pmHostconfig = new PluginMonitoringHostconfig();
     $pmHost = new PluginMonitoringHost();
     $calendar = new Calendar();
     $pmRealm = new PluginMonitoringRealm();
     $networkEquipment = new NetworkEquipment();
     $pmContact_Item = new PluginMonitoringContact_Item();
     $profile_User = new Profile_User();
     $user = new User();
     $a_hosts = array();
     $i = 0;
     $a_parents_found = array();
     $a_hosts_found = array();
     $a_entities_allowed = $pmEntity->getEntitiesByTag($tag);
     // * 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;
     }
     $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n         GROUP BY `itemtype`, `items_id`";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $classname = $data['itemtype'];
         $class = new $classname();
         if ($class->getFromDB($data['items_id'])) {
             if (isset($a_entities_allowed['-1']) or isset($a_entities_allowed[$class->fields['entities_id']])) {
                 $a_hosts[$i]['host_name'] = $classname . "-" . $data['items_id'] . "-" . preg_replace("/[^A-Za-z0-9]/", "", $class->fields['name']);
                 $a_hosts_found[$a_hosts[$i]['host_name']] = 1;
                 $a_hosts[$i]['alias'] = preg_replace("/[^A-Za-z0-9]/", "", $class->fields['name']) . " / " . $classname . "-" . $data['items_id'];
                 if (isset($class->fields['networkequipmenttypes_id'])) {
                     if ($class->fields['networkequipmenttypes_id'] > 0) {
                         $a_hosts[$i]['alias'] .= " (" . Dropdown::getDropdownName("glpi_networkequipmenttypes", $class->fields['networkequipmenttypes_id']) . ")";
                     }
                 } else {
                     if (isset($class->fields['computertypes_id'])) {
                         if ($class->fields['computertypes_id'] > 0) {
                             $a_hosts[$i]['alias'] .= " (" . Dropdown::getDropdownName("glpi_computertypes", $class->fields['computertypes_id']) . ")";
                         }
                     } else {
                         if (isset($class->fields['printertypes_id'])) {
                             if ($class->fields['printertypes_id'] > 0) {
                                 $a_hosts[$i]['alias'] .= " (" . Dropdown::getDropdownName("glpi_printertypes", $class->fields['printertypes_id']) . ")";
                             }
                         }
                     }
                 }
                 $ip = PluginMonitoringHostaddress::getIp($data['items_id'], $data['itemtype'], $class->fields['name']);
                 $a_hosts[$i]['address'] = $ip;
                 // Manage dependencies
                 $parent = '';
                 if ($data['itemtype'] != 'NetworkEquipment') {
                     $networkPort = new NetworkPort();
                     $a_networkports = $networkPort->find("`itemtype`='" . $data['itemtype'] . "'\n                        AND `items_id`='" . $data['items_id'] . "'");
                     foreach ($a_networkports as $data_n) {
                         $networkports_id = $networkPort->getContact($data_n['id']);
                         if ($networkports_id) {
                             $networkPort->getFromDB($networkports_id);
                             if ($networkPort->fields['itemtype'] == 'NetworkEquipment') {
                                 $networkEquipment->getFromDB($networkPort->fields['items_id']);
                                 $parent = 'NetworkEquipment-' . $networkPort->fields['items_id'] . '-' . preg_replace("/[^A-Za-z0-9]/", "", $networkEquipment->fields['name']);
                                 $a_parents_found[$parent] = 1;
                                 $pmHost->updateDependencies($classname, $data['items_id'], 'NetworkEquipment-' . $networkPort->fields['items_id']);
                             }
                         }
                     }
                 }
                 $a_hosts[$i]['parents'] = $parent;
                 $a_fields = array();
                 $pmCommand->getFromDB($pmHostconfig->getValueAncestor('plugin_monitoring_commands_id', $class->fields['entities_id'], $classname, $class->getID()));
                 $a_component = current($pmComponent->find("`plugin_monitoring_commands_id`='" . $pmCommand->fields['id'] . "'", "", 1));
                 $a_fields = $a_component;
                 $a_hosts[$i]['check_command'] = $pmCommand->fields['command_name'];
                 $pmCheck->getFromDB($pmHostconfig->getValueAncestor('plugin_monitoring_checks_id', $class->fields['entities_id'], $classname, $class->getID()));
                 $a_hosts[$i]['check_interval'] = $pmCheck->fields['check_interval'];
                 $a_hosts[$i]['retry_interval'] = $pmCheck->fields['retry_interval'];
                 $a_hosts[$i]['max_check_attempts'] = $pmCheck->fields['max_check_attempts'];
                 if ($calendar->getFromDB($pmHostconfig->getValueAncestor('calendars_id', $class->fields['entities_id'], $classname, $class->getID()))) {
                     $a_hosts[$i]['check_period'] = $calendar->fields['name'];
                 } else {
                     $a_hosts[$i]['check_period'] = "24x7";
                 }
                 $pmRealm->getFromDB($pmHostconfig->getValueAncestor('plugin_monitoring_realms_id', $class->fields['entities_id'], $classname, $class->getID()));
                 $a_hosts[$i]['realm'] = $pmRealm->fields['name'];
                 $a_hosts[$i]['process_perf_data'] = '1';
                 $a_hosts[$i]['notification_interval'] = '30';
                 // For contact check if a service with this component
                 $a_hosts[$i]['contacts'] = '';
                 $querycont = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n                     LEFT JOIN `glpi_plugin_monitoring_services`\n                        ON `plugin_monitoring_componentscatalogs_hosts_id`\n                           = `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n                     WHERE `plugin_monitoring_components_id`='" . $a_component['id'] . "'\n                        AND `items_id`='" . $data['items_id'] . "'\n                        AND `itemtype`='" . $data['itemtype'] . "'\n                        LIMIT 1";
                 $resultcont = $DB->query($querycont);
                 if ($DB->numrows($resultcont) != 0) {
                     $a_componentscatalogs_hosts = $DB->fetch_assoc($resultcont);
                     // Notification interval
                     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
                     $pmComponentscatalog->getFromDB($a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']);
                     $a_hosts[$i]['notification_interval'] = $pmComponentscatalog->fields['notification_interval'];
                     $a_contacts = array();
                     $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'\n                        AND `items_id`='" . $a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id'] . "'");
                     foreach ($a_list_contact as $data_contact) {
                         if (isset($a_contacts_entities[$a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']][$data_contact['users_id']])) {
                             if (in_array($class->fields['entities_id'], $a_contacts_entities[$a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']][$data_contact['users_id']])) {
                                 $user->getFromDB($data_contact['users_id']);
                                 $a_contacts[] = $user->fields['name'];
                             }
                         }
                     }
                     if (count($a_contacts) > 0) {
                         $a_contacts_unique = array_unique($a_contacts);
                         $a_hosts[$i]['contacts'] = implode(',', $a_contacts_unique);
                     }
                 }
                 if ($calendar->getFromDB($a_fields['calendars_id'])) {
                     $a_hosts[$i]['notification_period'] = $calendar->fields['name'];
                 } else {
                     $a_hosts[$i]['notification_period'] = "24x7";
                 }
                 $a_hosts[$i]['notification_options'] = 'd,u,r';
                 $i++;
             }
         }
     }
     // Check if parents all exist in hosts config
     foreach ($a_parents_found as $host => $num) {
         if (!isset($a_hosts_found[$host])) {
             // Delete parents not added in hosts config
             foreach ($a_hosts as $id => $data) {
                 if ($data['parents'] == $host) {
                     $a_hosts[$id]['parents'] = '';
                 }
             }
         }
     }
     if ($file == "1") {
         $config = "# Generated by plugin monitoring for GLPI\n# on " . date("Y-m-d H:i:s") . "\n\n";
         foreach ($a_hosts as $data) {
             $config .= $this->constructFile("host", $data);
         }
         return array('hosts.cfg', $config);
     } else {
         return $a_hosts;
     }
 }
 function importConnectionMac($a_portconnection, $networkports_id)
 {
     $wire = new NetworkPort_NetworkPort();
     $networkPort = new NetworkPort();
     $pfNetworkPort = new PluginFusioninventoryNetworkPort();
     $pfUnmanaged = new PluginFusioninventoryUnmanaged();
     $a_snmpports = current($pfNetworkPort->find("`networkports_id`='" . $networkports_id . "'", "", 1));
     $pfNetworkPort->getFromDB($a_snmpports['id']);
     $count = count($a_portconnection);
     $pfNetworkPort->loadNetworkport($networkports_id);
     if ($pfNetworkPort->getValue('trunk') != '1') {
         if ($count == '2') {
             // detect if phone IP is one of the 2 devices
             $phonecase = 0;
             $macNotPhone_id = 0;
             $macNotPhone = '';
             $phonePort_id = 0;
             foreach ($a_portconnection as $ifmac) {
                 $a_ports = $networkPort->find("`mac`='" . $ifmac . "'", "", 1);
                 $a_port = current($a_ports);
                 if ($a_port['itemtype'] == 'Phone') {
                     // Connect phone on switch port and other (computer..) in this phone
                     $phonePort_id = $a_port['id'];
                     $phonecase++;
                 } else {
                     $macNotPhone_id = $a_port['id'];
                     $macNotPhone = $ifmac;
                 }
             }
             if ($phonecase == '1') {
                 $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $phonePort_id));
                 $networkPort->getFromDB($phonePort_id);
                 $Phone = new Phone();
                 $Phone->getFromDB($networkPort->fields['items_id']);
                 $a_portsPhone = $networkPort->find("`items_id`='" . $networkPort->fields['items_id'] . "'\n                                                AND `itemtype`='Phone'\n                                                AND `name`='Link'", '', 1);
                 $portLink_id = 0;
                 if (count($a_portsPhone) == '1') {
                     $a_portPhone = current($a_portsPhone);
                     $portLink_id = $a_portPhone['id'];
                 } else {
                     // Create Port Link
                     $input = array();
                     $input['name'] = 'Link';
                     $input['itemtype'] = 'Phone';
                     $input['items_id'] = $Phone->fields['id'];
                     $input['entities_id'] = $Phone->fields['entities_id'];
                     $portLink_id = $networkPort->add($input);
                 }
                 $opposite_id = FALSE;
                 if ($opposite_id == $wire->getOppositeContact($portLink_id)) {
                     if ($opposite_id != $macNotPhone_id) {
                         $pfNetworkPort->disconnectDB($portLink_id);
                         // disconnect this port
                         $pfNetworkPort->disconnectDB($macNotPhone_id);
                         // disconnect destination port
                     }
                 }
                 if (!isset($macNotPhone_id)) {
                     // Create unmanaged ports
                     $unmanagedn_infos = array();
                     $unmanagedn_infos["name"] = '';
                     if (isset($_SESSION["plugin_fusioninventory_entity"])) {
                         $input['entities_id'] = $_SESSION["plugin_fusioninventory_entity"];
                     }
                     $newID = $pfUnmanaged->add($unmanagedn_infos);
                     // Add networking_port
                     $port_add = array();
                     $port_add["items_id"] = $newID;
                     $port_add["itemtype"] = 'PluginFusioninventoryUnmanaged';
                     $port_add['mac'] = $macNotPhone;
                     $port_add['instantiation_type'] = "NetworkPortEthernet";
                     $macNotPhone_id = $networkPort->add($port_add);
                 }
                 $wire->add(array('networkports_id_1' => $portLink_id, 'networkports_id_2' => $macNotPhone_id));
             } else {
                 $pfUnmanaged->hubNetwork($pfNetworkPort, $a_portconnection);
             }
         } else {
             if ($count > 1) {
                 // MultipleMac
                 $pfUnmanaged->hubNetwork($pfNetworkPort, $a_portconnection);
             } else {
                 // One mac on port
                 foreach ($a_portconnection as $ifmac) {
                     //Only 1 time
                     $a_ports = $networkPort->find("`mac`='" . $ifmac . "' AND `logical_number`='1'", "", 1);
                     if (count($a_ports) == 0) {
                         $a_ports = $networkPort->find("`mac`='" . $ifmac . "'", "", 1);
                     }
                     if (count($a_ports) > 0) {
                         $a_port = current($a_ports);
                         $hub = 0;
                         $id = $networkPort->getContact($a_port['id']);
                         if ($id and $networkPort->getFromDB($id)) {
                             if ($networkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                                 $pfUnmanaged->getFromDB($networkPort->fields['items_id']);
                                 if ($pfUnmanaged->fields['hub'] == '1') {
                                     $hub = 1;
                                 }
                             }
                         }
                         $direct_id = $networkPort->getContact($networkports_id);
                         if ($id and $id != $networkports_id and $hub == '0') {
                             $directconnect = 0;
                             if (!$direct_id) {
                                 $directconnect = 1;
                             } else {
                                 $networkPort->getFromDB($direct_id);
                                 if ($networkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                                     // 1. Hub connected to this switch port
                                     $pfUnmanaged->connectPortToHub(array($a_port), $networkPort->fields['items_id']);
                                 } else {
                                     // 2. direct connection
                                     $directconnect = 1;
                                 }
                             }
                             if ($directconnect == '1') {
                                 $pfNetworkPort->disconnectDB($networkports_id);
                                 // disconnect this port
                                 $pfNetworkPort->disconnectDB($a_port['id']);
                                 // disconnect destination port
                                 $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $a_port['id']));
                             }
                         } else {
                             if ($id and $hub == '1') {
                                 $directconnect = 0;
                                 if (!$direct_id) {
                                     $directconnect = 1;
                                 } else {
                                     $networkPort->getFromDB($direct_id);
                                     $ddirect = $networkPort->fields;
                                     $networkPort->getFromDB($id);
                                     if ($ddirect['items_id'] == $networkPort->fields['items_id'] and $ddirect['itemtype'] == $networkPort->fields['itemtype']) {
                                         // 1.The hub where this device is connected is yet connected
                                         // to this switch port
                                         // => Do nothing
                                     } else {
                                         // 2. The hub where this device is connected to is not connected
                                         // to this switch port
                                         if ($ddirect['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                                             // b. We have a hub connected to the switch port
                                             $pfUnmanaged->connectPortToHub(array($a_port), $ddirect['items_id']);
                                         } else {
                                             // a. We have a direct connexion to another device
                                             // (on the switch port)
                                             $directconnect = 1;
                                         }
                                     }
                                 }
                                 if ($directconnect == '1') {
                                     $pfNetworkPort->disconnectDB($networkports_id);
                                     // disconnect this port
                                     $pfNetworkPort->disconnectDB($a_port['id']);
                                     // disconnect destination port
                                     $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $a_port['id']));
                                 }
                             } else {
                                 if ($id) {
                                     // Yet connected
                                 } else {
                                     // Not connected
                                     $pfNetworkPort->disconnectDB($networkports_id);
                                     // disconnect this port
                                     $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $a_port['id']));
                                 }
                             }
                         }
                     } else {
                         // Create unmanaged device
                         $pfUnmanaged = new PluginFusioninventoryUnmanaged();
                         $input = array();
                         $manufacturer = PluginFusioninventoryInventoryExternalDB::getManufacturerWithMAC($ifmac);
                         $input['name'] = $manufacturer;
                         if (isset($_SESSION["plugin_fusioninventory_entity"])) {
                             $input['entities_id'] = $_SESSION["plugin_fusioninventory_entity"];
                         }
                         $newID = $pfUnmanaged->add($input);
                         $input['itemtype'] = "PluginFusioninventoryUnmanaged";
                         $input['items_id'] = $newID;
                         $input['mac'] = $ifmac;
                         $input['instantiation_type'] = "NetworkPortEthernet";
                         $newPortID = $networkPort->add($input);
                         $pfNetworkPort->disconnectDB($networkports_id);
                         // disconnect this port
                         $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $newPortID));
                     }
                 }
             }
         }
     }
 }