function thold_check_baseline($rra_id, $ds, $current_value, &$item) { global $debug; $now = time(); // See if we have a valid cached thold_high and thold_low value if ($item['bl_thold_valid'] && $now < $item['bl_thold_valid']) { if ($item['thold_hi'] && $current_value > $item['thold_hi']) { $failed = 2; } elseif ($item['thold_low'] && $current_value < $item['thold_low']) { $failed = 1; } else { $failed = 0; } } else { $midnight = gmmktime(0, 0, 0); $t0 = $midnight + floor(($now - $midnight) / $item['bl_ref_time_range']) * $item['bl_ref_time_range']; $ref_values = thold_get_ref_value($rra_id, $ds, $t0, $item['bl_ref_time_range']); if (!is_array($ref_values) || sizeof($ref_values) == 0) { $item['thold_low'] = ''; $item['thold_hi'] = ''; $item['bl_thold_valid'] = $now; $returnvalue = -1; return $returnvalue; // Baseline reference value not yet established } $ref_value = $ref_values[0]; if ($item['cdef'] != 0) { $ref_value = thold_build_cdef($item['cdef'], $ref_value, $item['rra_id'], $item['data_id']); } $blt_low = ''; $blt_high = ''; if ($item['bl_pct_down'] != '') { $blt_low = round($ref_value - abs($ref_value * $item['bl_pct_down'] / 100), 2); } if ($item['bl_pct_up'] != '') { $blt_high = round($ref_value + abs($ref_value * $item['bl_pct_up'] / 100), 2); } // Cache the calculated or empty values $item['thold_low'] = $blt_low; $item['thold_hi'] = $blt_high; $item['bl_thold_valid'] = $t0 + $item['bl_ref_time_range']; $failed = 0; // Check low boundary if ($blt_low != '' && $current_value < $blt_low) { $failed = 1; } // Check up boundary if ($failed == 0 && $blt_high != '' && $current_value > $blt_high) { $failed = 2; } } if ($debug) { echo "RRA: {$rra_id} : {$ds}\n"; echo 'Ref. values count: ' . (isset($ref_values) ? count($ref_values) : "N/A") . "\n"; echo "Ref. value (min): " . (isset($ref_value_min) ? $ref_value_min : "N/A") . "\n"; echo "Ref. value (max): " . (isset($ref_value_max) ? $ref_value_max : "N/A") . "\n"; echo "Cur. value: {$current_value}\n"; echo "Low bl thresh: " . (isset($blt_low) ? $blt_low : "N/A") . "\n"; echo "High bl thresh: " . (isset($blt_high) ? $blt_high : "N/A") . "\n"; echo 'Check against baseline: '; switch ($failed) { case 0: echo 'OK'; break; case 1: echo 'FAIL: Below baseline threshold!'; break; case 2: echo 'FAIL: Above baseline threshold!'; break; } echo "\n"; echo "------------------\n"; } return $failed; }
function thold_check_baseline($local_data_id, $data_template_rrd_id, $current_value, &$thold_data) { global $debug; $now = time(); // See if we have a valid cached thold_high and thold_low value if ($thold_data['bl_thold_valid'] && $now < $thold_data['bl_thold_valid']) { if ($thold_data['thold_hi'] && $current_value > $thold_data['thold_hi']) { $failed = 2; } elseif ($thold_data['thold_low'] && $current_value < $thold_data['thold_low']) { $failed = 1; } else { $failed = 0; } } else { $midnight = gmmktime(0, 0, 0); $t0 = $midnight + floor(($now - $midnight) / $thold_data['bl_ref_time_range']) * $thold_data['bl_ref_time_range']; $ref_values = thold_get_ref_value($local_data_id, $data_template_rrd_id, $t0, $thold_data['bl_ref_time_range']); $ref_value_min = min($ref_values); $ref_value_max = max($ref_values); if (!is_array($ref_values) || sizeof($ref_values) == 0) { $thold_data['thold_low'] = ''; $thold_data['thold_hi'] = ''; $thold_data['bl_thold_valid'] = $now; $returnvalue = -1; return $returnvalue; // Baseline reference value not yet established } $ref_value = $ref_values[0]; if ($thold_data['cdef'] != 0) { $ref_value = thold_build_cdef($thold_data['cdef'], $ref_value, $thold_data['local_data_id'], $thold_data['data_template_rrd_id']); } $blt_low = ''; $blt_high = ''; if ($thold_data['bl_pct_down'] != '') { $blt_low = $ref_value_min - $ref_value_min * $thold_data['bl_pct_down'] / 100; } if ($thold_data['bl_pct_up'] != '') { $blt_high = $ref_value_max + $ref_value_max * $thold_data['bl_pct_up'] / 100; } // Cache the calculated or empty values $thold_data['thold_low'] = $blt_low; $thold_data['thold_hi'] = $blt_high; $thold_data['bl_thold_valid'] = $t0 + $thold_data['bl_ref_time_range']; $failed = 0; // Check low boundary if ($blt_low != '' && $current_value < $blt_low) { $failed = 1; } // Check up boundary if ($failed == 0 && $blt_high != '' && $current_value > $blt_high) { $failed = 2; } } if ($debug) { echo 'Local Data Id: ' . $local_data_id . ':' . $data_template_rrd_id . "\n"; echo 'Ref. values count: ' . (isset($ref_values) ? count($ref_values) : "N/A") . "\n"; echo 'Ref. value (min): ' . (isset($ref_value_min) ? $ref_value_min : 'N/A') . "\n"; echo 'Ref. value (max): ' . (isset($ref_value_max) ? $ref_value_max : 'N/A') . "\n"; echo 'Cur. value: ' . $current_value . "\n"; echo 'Low bl thresh: ' . (isset($blt_low) ? $blt_low : 'N/A') . "\n"; echo 'High bl thresh: ' . (isset($blt_high) ? $blt_high : 'N/A') . "\n"; echo 'Check against baseline: '; switch ($failed) { case 0: echo 'OK'; break; case 1: echo 'FAIL: Below baseline threshold!'; break; case 2: echo 'FAIL: Above baseline threshold!'; break; } echo "\n"; echo "------------------\n"; } return $failed; }