static function updateDetailForPerfdata($perfdata, $perfdatas_id)
 {
     $a_lines = array();
     $a_perfdata = PluginMonitoringPerfdata::splitPerfdata($perfdata);
     $i = 1;
     foreach ($a_perfdata as $data) {
         $data = trim($data, ", ");
         $a_a_perfdata = explode("=", $data);
         $a_a_perfdata[0] = trim($a_a_perfdata[0], "'");
         if (!isset($a_a_perfdata[1])) {
             return;
         }
         //$a_a_perfdata[1] = trim($a_a_perfdata[1], ";");
         $a_lines[$i] = array('name' => $a_a_perfdata[0]);
         $a_perfdata_final = explode(";", $a_a_perfdata[1]);
         $num = 1;
         foreach ($a_perfdata_final as $nb_val => $val) {
             if ($val == '') {
                 if ($nb_val < count($a_perfdata_final) - 1) {
                     $a_lines[$i]['values'][$num] = '';
                 }
             } else {
                 $a_lines[$i]['values'][$num] = '';
             }
             $num++;
         }
         $i++;
     }
     // Add/update perfdatadetails in DB
     $pmPerfdataDetail = new PluginMonitoringPerfdataDetail();
     $a_perfdatadetails = $pmPerfdataDetail->find("`plugin_monitoring_perfdatas_id`='" . $perfdatas_id . "'", "position");
     foreach ($a_perfdatadetails as $data) {
         $find = 0;
         foreach ($a_lines as $key => $a_line) {
             if ($a_line['name'] == $data['name'] && $data['position'] == $key && !$find) {
                 $find = 1;
                 $countfind = count($a_line['values']);
                 $input = array();
                 $input['id'] = $data['id'];
                 $input['dsname_num'] = $countfind;
                 for ($i = 1; $i <= $countfind; $i++) {
                     if ($data['dsname' . $i] == '') {
                         $input['dsname' . $i] = 'value' . $data['position'] . '.' . $i;
                     }
                 }
                 for ($i = $countfind + 1; $i < 9; $i++) {
                     $input['dsname' . $i] = '';
                 }
                 $pmPerfdataDetail->update($input);
                 unset($a_lines[$key]);
             }
         }
         if (!$find) {
             $pmPerfdataDetail->delete($data);
         }
     }
     foreach ($a_lines as $position => $data) {
         $input = array();
         $input['name'] = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($data['name']));
         $input['plugin_monitoring_perfdatas_id'] = $perfdatas_id;
         $input['position'] = $position;
         $input['dsname_num'] = count($data['values']);
         for ($i = 1; $i <= $input['dsname_num']; $i++) {
             $input['dsname' . $i] = 'value' . $position . '.' . $i;
         }
         $pmPerfdataDetail->add($input);
     }
 }
 static function getArrayPerfdata($perfdatas_id)
 {
     $pmPerfdata = new PluginMonitoringPerfdata();
     $pmPerfdataDetail = new PluginMonitoringPerfdataDetail();
     $data = array();
     $data['parseperfdata'] = array();
     if (!$pmPerfdata->getFromDB($perfdatas_id)) {
         $data['command'] = '';
         return $data;
     }
     $data['command'] = $pmPerfdata->fields['name'];
     $a_perfdatadetails = $pmPerfdataDetail->find("`plugin_monitoring_perfdatas_id`='" . $perfdatas_id . "'", "position");
     foreach ($a_perfdatadetails as $a_perfdatadetail) {
         $ds = array();
         $a_incremental = array();
         for ($i = 1; $i <= $a_perfdatadetail['dsname_num']; $i++) {
             $ds[] = array('dsname' => $a_perfdatadetail['dsname' . $i]);
             $a_incremental[] = $a_perfdatadetail['dsnameincr' . $i];
         }
         $name = $a_perfdatadetail['name'];
         if ($a_perfdatadetail['dynamic_name']) {
             $name = "*";
         }
         $data['parseperfdata'][] = array('name' => $name, 'DS' => $ds, 'incremental' => $a_incremental);
     }
     return $data;
 }
 function type_other($type = 'average')
 {
     global $DB;
     $pmService = new PluginMonitoringService();
     $pmServiceevent = new PluginMonitoringServiceevent();
     $pmComponent = new PluginMonitoringComponent();
     $pmPerfdataDetail = new PluginMonitoringPerfdataDetail();
     $a_date = PluginMonitoringCustomitem_Common::getTimeRange($this->fields);
     $val = 0;
     $a_val = array();
     $nb_val = 0;
     $a_tocheck = array('warn' => 0, 'crit' => 0, 'limit' => 0);
     $a_types = array('warn', 'crit', 'limit');
     for ($i = 0; $i < count($a_types); $i++) {
         if (is_numeric($this->fields['aggregate_' . $a_types[$i]])) {
             $a_ret[$a_types[$i]] = $this->fields['aggregate_' . $a_types[$i]];
         } else {
             $a_ret[$a_types[$i]] = 0;
             $a_tocheck[$a_types[$i]] = 1;
         }
     }
     $items = importArrayFromDB($this->fields['aggregate_items']);
     foreach ($items as $itemtype => $data) {
         switch ($itemtype) {
             case 'PluginMonitoringService':
                 foreach ($data as $items_id => $data2) {
                     $pmService->getFromDB($items_id);
                     $_SESSION['plugin_monitoring_checkinterval'] = PluginMonitoringComponent::getTimeBetween2Checks($pmService->fields['plugin_monitoring_components_id']);
                     $pmComponent->getFromDB($pmService->fields['plugin_monitoring_components_id']);
                     $query = "SELECT * FROM `glpi_plugin_monitoring_serviceevents`\n                     WHERE `plugin_monitoring_services_id`='" . $items_id . "'\n                        AND `date` >= '" . $a_date['begin'] . "'\n                     ORDER BY `date`";
                     $result = $DB->query($query);
                     $ret = $pmServiceevent->getData($result, $pmComponent->fields['graph_template'], $a_date['begin'], $a_date['end']);
                     foreach ($data2 as $a_perfdatadetails) {
                         $pmPerfdataDetail->getFromDB($a_perfdatadetails['perfdatadetails_id']);
                         $nb_val += count($ret[0][$pmPerfdataDetail->fields['dsname' . $a_perfdatadetails['perfdatadetails_dsname']]]);
                         $val += array_sum($ret[0][$pmPerfdataDetail->fields['dsname' . $a_perfdatadetails['perfdatadetails_dsname']]]);
                         $a_val = array_merge($a_val, $ret[0][$pmPerfdataDetail->fields['dsname' . $a_perfdatadetails['perfdatadetails_dsname']]]);
                     }
                     // for manage warn, crit and limit
                     foreach ($a_tocheck as $other_type => $num_type) {
                         if ($num_type == 1) {
                             $other_items = importArrayFromDB($this->fields['aggregate_' . $other_type]);
                             foreach ($other_items[$itemtype][$items_id] as $a_perfdatadetails) {
                                 $pmPerfdataDetail->getFromDB($a_perfdatadetails['perfdatadetails_id']);
                                 $a_ret[$other_type] += array_sum($ret[0][$pmPerfdataDetail->fields['dsname' . $a_perfdatadetails['perfdatadetails_dsname']]]);
                             }
                         }
                     }
                 }
                 break;
             case 'PluginMonitoringComponentscatalog':
                 $pmComponentscatalog = new PluginMonitoringComponentscatalog();
                 foreach ($data as $items_id => $data2) {
                     $ret = $pmComponentscatalog->getInfoOfCatalog(str_replace('id', '', $items_id));
                     $a_hosts = $ret[6];
                     foreach ($data2['PluginMonitoringComponent'] as $items_id_components => $data4) {
                         $query = "SELECT * FROM `glpi_plugin_monitoring_services`\n                        WHERE `plugin_monitoring_components_id`='" . str_replace('id', '', $items_id_components) . "'\n                           AND `plugin_monitoring_componentscatalogs_hosts_id` IN\n                              ('" . implode("','", $a_hosts) . "')\n                           AND `entities_id` IN (" . $_SESSION['glpiactiveentities_string'] . ")";
                         $result = $DB->query($query);
                         while ($dataq = $DB->fetch_array($result)) {
                             $pmService->getFromDB($dataq['id']);
                             $_SESSION['plugin_monitoring_checkinterval'] = PluginMonitoringComponent::getTimeBetween2Checks($pmService->fields['plugin_monitoring_components_id']);
                             $pmComponent->getFromDB($dataq['plugin_monitoring_components_id']);
                             $query = "SELECT * FROM `glpi_plugin_monitoring_serviceevents`\n                           WHERE `plugin_monitoring_services_id`='" . $dataq['id'] . "'\n                              AND `date` >= '" . $a_date['begin'] . "'\n                           ORDER BY `date`";
                             $result = $DB->query($query);
                             $ret = $pmServiceevent->getData($result, $pmComponent->fields['graph_template'], $a_date['begin'], $a_date['end']);
                             foreach ($data4 as $a_perfdatadetails) {
                                 $pmPerfdataDetail->getFromDB($a_perfdatadetails['perfdatadetails_id']);
                                 $nb_val += count($ret[0][$pmPerfdataDetail->fields['dsname' . $a_perfdatadetails['perfdatadetails_dsname']]]);
                                 $val += array_sum($ret[0][$pmPerfdataDetail->fields['dsname' . $a_perfdatadetails['perfdatadetails_dsname']]]);
                                 $a_val = array_merge($a_val, $ret[0][$pmPerfdataDetail->fields['dsname' . $a_perfdatadetails['perfdatadetails_dsname']]]);
                             }
                             // for manage warn, crit and limit
                             foreach ($a_tocheck as $other_type => $num_type) {
                                 if ($num_type == 1) {
                                     $other_items = importArrayFromDB($this->fields['aggregate_' . $other_type]);
                                     foreach ($other_items[$itemtype][$items_id]['PluginMonitoringComponent'][$items_id_components] as $a_perfdatadetails) {
                                         $pmPerfdataDetail->getFromDB($a_perfdatadetails['perfdatadetails_id']);
                                         $a_ret[$other_type] += array_sum($ret[0][$pmPerfdataDetail->fields['dsname' . $a_perfdatadetails['perfdatadetails_dsname']]]);
                                     }
                                 }
                             }
                         }
                     }
                 }
                 break;
         }
     }
     if ($nb_val != 0) {
         if ($type == 'average') {
             $val = $val / $nb_val;
         } else {
             if ($type == 'median') {
                 sort($a_val);
                 $count = count($a_val);
                 //total numbers in array
                 $middleval = floor(($count - 1) / 2);
                 // find the middle value, or the lowest middle value
                 if ($count % 2) {
                     // odd number, middle is the median
                     $median = $a_val[$middleval];
                 } else {
                     // even number, calculate avg of 2 medians
                     $low = $arr[$middleval];
                     $high = $arr[$middleval + 1];
                     $median = ($low + $high) / 2;
                 }
                 $val = $median;
             }
         }
     }
     foreach ($a_tocheck as $other_type => $num_type) {
         if ($num_type == 1) {
             $a_ret[$other_type] = $a_ret[$other_type] / $nb_val;
         }
     }
     $a_ret['val'] = $val;
     return $a_ret;
 }
  ------------------------------------------------------------------------

  @package   Plugin Monitoring for GLPI
  @author    David Durieux
  @co-author
  @comment
  @copyright Copyright (c) 2011-2014 Plugin Monitoring for GLPI team
  @license   AGPL License 3.0 or (at your option) any later version
             http://www.gnu.org/licenses/agpl-3.0-standalone.html
  @link      https://forge.indepnet.net/projects/monitoring/
  @since     2013

  ------------------------------------------------------------------------
*/
include "../../../inc/includes.php";
Session::checkRight("plugin_monitoring_perfdata", READ);
$pmPerfdataDetail = new PluginMonitoringPerfdataDetail();
if (isset($_POST["update"])) {
    foreach ($_POST as $name => $value) {
        if (strstr($name, 'dsnameincr') && $value == 'on') {
            $_POST[$name] = 1;
        }
    }
    for ($i = 1; $i <= 15; $i++) {
        if (!isset($_POST['dsnameincr' . $i])) {
            $_POST['dsnameincr' . $i] = 0;
        }
    }
    $pmPerfdataDetail->update($_POST);
    Html::back();
}