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);
     }
 }
 function getData($result, $perfdatas_id, $start_date, $end_date, $ret = array(), $timecomplete = 0, $todisplay = array())
 {
     global $DB;
     // Toolbox::logInFile("pm", "serviceevent, getData : $perfdatas_id, from $start_date to $end_date\n");
     if (empty($ret)) {
         $ret = $this->getRef($perfdatas_id);
     }
     $a_ref = $ret[0];
     $a_convert = $ret[1];
     $mydatat = array();
     $a_labels = array();
     $a_perfdata_name = array();
     $a_perf = PluginMonitoringPerfdata::getArrayPerfdata($perfdatas_id);
     $previous_timestamp = strtotime($start_date);
     $query_data = array();
     $cnt = 0;
     if (gettype($result) == 'object') {
         while ($edata = $DB->fetch_array($result)) {
             $current_timestamp = strtotime($edata['date']);
             $cnt++;
             // Timeup = time between 2 checks + 20%
             $timeup = $_SESSION['plugin_monitoring_checkinterval'] * 1.2;
             while ($previous_timestamp + $timeup < $current_timestamp) {
                 $previous_timestamp += $_SESSION['plugin_monitoring_checkinterval'];
                 if ($previous_timestamp < $current_timestamp) {
                     $query_data[] = array('date' => date('Y-m-d H:i:s', $previous_timestamp), 'perf_data' => '');
                 }
             }
             $previous_timestamp = $current_timestamp;
             $query_data[] = $edata;
         }
     } else {
         foreach ($result as $edata) {
             // Toolbox::logInFile("pm", "serviceevent, getData : ".$edata['id']."\n");
             $current_timestamp = strtotime($edata['date']);
             $cnt++;
             // Timeup = time between 2 checks + 20%
             $timeup = $_SESSION['plugin_monitoring_checkinterval'] * 1.2;
             while ($previous_timestamp + $timeup < $current_timestamp) {
                 $previous_timestamp += $_SESSION['plugin_monitoring_checkinterval'];
                 if ($previous_timestamp < $current_timestamp) {
                     $query_data[] = array('date' => date('Y-m-d H:i:s', $previous_timestamp), 'perf_data' => '');
                 }
             }
             $previous_timestamp = $current_timestamp;
             $query_data[] = $edata;
         }
     }
     $timeup = $_SESSION['plugin_monitoring_checkinterval'] * 1.2;
     $current_timestamp = strtotime($end_date);
     while ($previous_timestamp + $timeup < $current_timestamp) {
         $previous_timestamp += $_SESSION['plugin_monitoring_checkinterval'];
         if ($previous_timestamp < $current_timestamp) {
             $query_data[] = array('date' => date('Y-m-d H:i:s', $previous_timestamp), 'perf_data' => '');
         }
     }
     foreach ($query_data as $edata) {
         $current_timestamp = strtotime($edata['date']);
         if ('' == $previous_timestamp) {
             $previous_timestamp = $current_timestamp;
         }
         $a_perfdata = PluginMonitoringPerfdata::splitPerfdata($edata['perf_data']);
         $a_labels[] = $current_timestamp;
         //         $a_time = explode(" ", $edata['date']);
         //         $a_time2 = explode(":", $a_time[1]);
         //         if ($timecomplete == 1) {
         //            $a_labels[] = $a_time[0]." ".$a_time2[0].":".$a_time2[1];
         //         } else {
         //            $day = explode("-", $a_time[0]);
         //            if ($timecomplete == 2) {
         //               $a_labels[] = $day[1]."-".$day[2]." ".$a_time2[0].":".$a_time2[1];
         //            } else {
         //               $a_labels[] = "(".$day[2].")".$a_time2[0].":".$a_time2[1];
         //            }
         //         }
         foreach ($a_perf['parseperfdata'] as $num => $data) {
             if (isset($a_perfdata[$num])) {
                 $a_perfdata[$num] = trim($a_perfdata[$num], ", ");
                 $a_a_perfdata = explode("=", $a_perfdata[$num]);
                 $a_a_perfdata[0] = trim($a_a_perfdata[0], "'");
                 $regex = 0;
                 if (strstr($data['name'], "*")) {
                     $datanameregex = str_replace("*", "(.*)", $data['name']);
                     $regex = 1;
                 }
                 if (($a_a_perfdata[0] == $data['name'] or '' == $data['name'] or $regex == 1 and preg_match("/" . $datanameregex . "/", $data['name'])) and isset($a_a_perfdata[1])) {
                     $a_perfdata_final = explode(";", $a_a_perfdata[1]);
                     // New perfdata row, no unit knew.
                     $unity = '';
                     foreach ($a_perfdata_final as $nb_val => $val) {
                         if (count($todisplay) == 0 || isset($data['DS'][$nb_val]) && isset($todisplay[$data['DS'][$nb_val]['dsname']])) {
                             //No value, no graph
                             if ('' == $val) {
                                 if ($nb_val >= count($a_perfdata_final) - 1) {
                                     continue;
                                 } else {
                                     $val = 0;
                                 }
                             }
                             $toreplace = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".");
                             $not_number = str_replace($toreplace, '', $val);
                             if ($not_number) {
                                 $unity = $not_number;
                             }
                             //Maintain for a same perfdata row, unity data. If set it's normally a new perfdata row.
                             if ($unity == '') {
                                 $val = round($val + 0, 2);
                             } else {
                                 $val = str_replace($unity, '', $val);
                                 switch ($unity) {
                                     case 'ms':
                                     case 'bps':
                                     case 'B':
                                     case "Bits/s":
                                         $val = round($val, 0);
                                         break;
                                     case '%':
                                         $val = round($val, 2);
                                         break;
                                     case 'KB':
                                         $val = $val * 1000;
                                         // Have in B
                                         break;
                                     case 'MB':
                                         $val = $val * 1000000;
                                         // Have in B
                                         break;
                                     case 'TB':
                                         $val = $val * 1000000000;
                                         // Have in B
                                         break;
                                     case 's':
                                         $val = round($val * 1000, 0);
                                         break;
                                     case 'timeout':
                                         if ($val > 2) {
                                             $val = round($val);
                                         } else {
                                             $val = round($val, 2);
                                         }
                                         break;
                                     default:
                                         $val = round($val + 0, 2);
                                 }
                             }
                             $a_perfdata_name[$data['name']] = $data['DS'][$nb_val]['dsname'];
                             if (!isset($mydatat[$data['DS'][$nb_val]['dsname']])) {
                                 $mydatat[$data['DS'][$nb_val]['dsname']] = array();
                             }
                             array_push($mydatat[$data['DS'][$nb_val]['dsname']], $val);
                             if ($data['incremental'][$nb_val] == 1) {
                                 if (!isset($mydatat[$data['DS'][$nb_val]['dsname'] . " | diff"])) {
                                     $mydatat[$data['DS'][$nb_val]['dsname'] . " | diff"] = array();
                                 }
                                 array_push($mydatat[$data['DS'][$nb_val]['dsname'] . " | diff"], $val);
                             }
                         }
                     }
                 } else {
                     $nb_DS = count($data['DS']);
                     for ($nb_val = 0; $nb_val < $nb_DS; $nb_val++) {
                         if (count($todisplay) == 0 || isset($todisplay[$data['DS'][$nb_val]['dsname']])) {
                             $a_perfdata_name[$data['name']] = $data['DS'][$nb_val]['dsname'];
                             if (!isset($mydatat[$data['DS'][$nb_val]['dsname']])) {
                                 $mydatat[$data['DS'][$nb_val]['dsname']] = array();
                             }
                             array_push($mydatat[$data['DS'][$nb_val]['dsname']], 0);
                             if ($data['incremental'][$nb_val] == 1) {
                                 if (!isset($mydatat[$data['DS'][$nb_val]['dsname'] . " | diff"])) {
                                     $mydatat[$data['DS'][$nb_val]['dsname'] . " | diff"] = array();
                                 }
                                 array_push($mydatat[$data['DS'][$nb_val]['dsname'] . " | diff"], 0);
                             }
                         }
                     }
                 }
             } else {
                 $nb_DS = count($data['DS']);
                 for ($nb_val = 0; $nb_val < $nb_DS; $nb_val++) {
                     if (count($todisplay) == 0 || isset($todisplay[$data['DS'][$nb_val]['dsname']])) {
                         $a_perfdata_name[$data['name']] = $data['DS'][$nb_val]['dsname'];
                         if (!isset($mydatat[$data['DS'][$nb_val]['dsname']])) {
                             $mydatat[$data['DS'][$nb_val]['dsname']] = array();
                         }
                         array_push($mydatat[$data['DS'][$nb_val]['dsname']], 0);
                         if ($data['incremental'][$nb_val] == 1) {
                             if (!isset($mydatat[$data['DS'][$nb_val]['dsname'] . " | diff"])) {
                                 $mydatat[$data['DS'][$nb_val]['dsname'] . " | diff"] = array();
                             }
                             array_push($mydatat[$data['DS'][$nb_val]['dsname'] . " | diff"], 0);
                         }
                     }
                 }
             }
         }
     }
     $a_incremental = array();
     foreach ($a_perf['parseperfdata'] as $data) {
         foreach ($data['DS'] as $num => $data1) {
             if ($data['incremental'][$num] == 1) {
                 $a_incremental[$data1['dsname']] = 1;
             }
         }
     }
     foreach ($mydatat as $name => $data) {
         if (strstr($name, " | diff")) {
             $old_val = -1;
             foreach ($data as $num => $val) {
                 if ($old_val == -1) {
                     $data[$num] = '###';
                 } else {
                     if ($val < $old_val) {
                         $data[$num] = 0;
                     } else {
                         $data[$num] = $val - $old_val;
                     }
                 }
                 if ($data[0] == '###') {
                     $data[0] = $data[$num];
                 }
                 $old_val = $val;
             }
             $mydatat[$name] = $data;
         } else {
             if (isset($a_incremental[$name])) {
                 $old_val = 0;
                 foreach ($data as $num => $val) {
                     if ($val == 0) {
                         $data[$num] = $old_val;
                     } else {
                         $old_val = $val;
                     }
                 }
                 $mydatat[$name] = $data;
             }
         }
     }
     $a_perfdata_name = array_unique($a_perfdata_name);
     // Toolbox::logInFile("pm", "a_perfdata_name : ".serialize($a_perfdata_name)."\n");
     // Toolbox::logInFile("pm", "mydatat : ".serialize($mydatat)."\n");
     return array($mydatat, $a_labels, $a_ref, $a_convert, $a_perfdata_name);
 }