function thold_poller_output($rrd_update_array) { global $config, $debug; include_once $config['base_path'] . '/plugins/thold/thold_functions.php'; include_once $config['library_path'] . '/snmp.php'; $rrd_reindexed = array(); $rrd_time_reindexed = array(); $rra_ids = ''; $x = 0; foreach ($rrd_update_array as $item) { if (isset($item['times'][key($item['times'])])) { if ($x) { $rra_ids .= ','; } $rra_ids .= $item['local_data_id']; $rrd_reindexed[$item['local_data_id']] = $item['times'][key($item['times'])]; $rrd_time_reindexed[$item['local_data_id']] = key($item['times']); $x++; } } if ($rra_ids != '') { $thold_items = db_fetch_assoc("SELECT thold_data.id, thold_data.name AS thold_name, thold_data.graph_id,\r\n\t\t\tthold_data.percent_ds, thold_data.expression,\r\n\t\t\tthold_data.data_type, thold_data.cdef, thold_data.rra_id,\r\n\t\t\tthold_data.data_id, thold_data.lastread,\r\n\t\t\tUNIX_TIMESTAMP(thold_data.lasttime) AS lasttime, thold_data.oldvalue,\r\n\t\t\tdata_template_rrd.data_source_name as name,\r\n\t\t\tdata_template_rrd.data_source_type_id, data_template_data.rrd_step,\r\n\t\t\tdata_template_rrd.rrd_maximum\r\n\t\t\tFROM thold_data\r\n\t\t\tLEFT JOIN data_template_rrd\r\n\t\t\tON (data_template_rrd.id = thold_data.data_id)\r\n\t\t\tLEFT JOIN data_template_data\r\n\t\t\tON ( data_template_data.local_data_id = thold_data.rra_id )\r\n\t\t\tWHERE data_template_rrd.data_source_name!=''\r\n\t\t\tAND thold_data.rra_id IN({$rra_ids})", false); } else { return $rrd_update_array; } if (sizeof($thold_items)) { foreach ($thold_items as $t_item) { thold_debug("Checking Threshold:'" . $t_item["thold_name"] . "', Graph:'" . $t_item["graph_id"] . "'"); $item = array(); $currenttime = 0; $currentval = thold_get_currentval($t_item, $rrd_reindexed, $rrd_time_reindexed, $item, $currenttime); switch ($t_item['data_type']) { case 0: break; case 1: if ($t_item['cdef'] != 0) { $currentval = thold_build_cdef($t_item['cdef'], $currentval, $t_item['rra_id'], $t_item['data_id']); } break; case 2: if ($t_item['percent_ds'] != '') { $currentval = thold_calculate_percent($t_item, $currentval, $rrd_reindexed); } break; case 3: if ($t_item['expression'] != '') { $currentval = thold_calculate_expression($t_item, $currentval, $rrd_reindexed, $rrd_time_reindexed); } break; } if (is_numeric($currentval)) { $currentval = round($currentval, 4); } else { $currentval = ''; } db_execute("UPDATE thold_data SET tcheck=1, lastread='{$currentval}',\r\n\t\t\tlasttime='" . date("Y-m-d H:i:s", $currenttime) . "',\r\n\t\t\toldvalue='" . $item[$t_item['name']] . "'\r\n\t\t\tWHERE rra_id = " . $t_item['rra_id'] . "\r\n\t\t\tAND data_id = " . $t_item['data_id']); } } return $rrd_update_array; }
function plgexample_poller_output ($rrd_update_array) { global $config; include_once($config['base_path'] . '/plugins/thold/thold_functions.php'); $rrd_update_array_reindexed = array(); $rra_ids = ''; $x = 0; foreach($rrd_update_array as $item) { if (isset($item['times'][key($item['times'])])) { if ($x) { $rra_ids .= ' OR '; } $rra_ids .= 'thold_data.rra_id = ' . $item['local_data_id']; $rrd_update_array_reindexed[$item['local_data_id']] = $item['times'][key($item['times'])]; $x++; } } if ($rra_ids != '') { $thold_items = db_fetch_assoc("SELECT thold_data.percent_ds, thold_data.data_type, thold_data.cdef, thold_data.rra_id, thold_data.data_id, thold_data.lastread, thold_data.oldvalue, data_template_rrd.data_source_name as name, data_template_rrd.data_source_type_id, data_template_data.rrd_step FROM thold_data LEFT JOIN data_template_rrd on (data_template_rrd.id = thold_data.data_id) LEFT JOIN data_template_data ON ( data_template_data.local_data_id = thold_data.rra_id ) WHERE data_template_rrd.data_source_name != '' AND $rra_ids", false); } else { return $rrd_update_array; } if (sizeof($thold_items)) { foreach ($thold_items as $t_item) { $polling_interval = $t_item['rrd_step']; if (isset($rrd_update_array_reindexed[$t_item['rra_id']])) { $item = $rrd_update_array_reindexed[$t_item['rra_id']]; if (isset($item[$t_item['name']])) { switch ($t_item['data_source_type_id']) { case 2: // COUNTER if ($item[$t_item['name']] >= $t_item['oldvalue']) { // Everything is normal $currentval = $item[$t_item['name']] - $t_item['oldvalue']; } else { // Possible overflow, see if its 32bit or 64bit if ($t_item['oldvalue'] > 4294967295) { $currentval = (18446744073709551615 - $t_item['oldvalue']) + $item[$t_item['name']]; } else { $currentval = (4294967295 - $t_item['oldvalue']) + $item[$t_item['name']]; } } $currentval = $currentval / $polling_interval; break; case 3: // DERIVE $currentval = ($item[$t_item['name']] - $t_item['oldvalue']) / $polling_interval; break; case 4: // ABSOLUTE $currentval = $item[$t_item['name']] / $polling_interval; break; case 1: // GAUGE default: $currentval = $item[$t_item['name']]; break; } switch ($t_item['data_type']) { case 0: $currentval = round($currentval, 4); break; case 1: if ($t_item['cdef'] != 0) { $currentval = thold_build_cdef($t_item['cdef'], $currentval, $t_item['rra_id'], $t_item['data_id']); } $currentval = round($currentval, 4); break; case 2: if ($t_item['percent_ds'] != '') { $currentval = plgexample_calculate_percent($t_item, $currentval, $rrd_update_array_reindexed); } $currentval = round($currentval, 4); break; } # db_execute("UPDATE thold_data SET tcheck = 1, lastread = '$currentval', oldvalue = '" . $item[$t_item['name']] . "' WHERE rra_id = " . $t_item['rra_id'] . " AND data_id = " . $t_item['data_id']); } } } } return $rrd_update_array; }
unset($cdefs_tmp); $rrd_reindexed = array(); $rrd_time_reindexed = array(); foreach ($tholds as $thold_data) { thold_debug("Checking Threshold Name: '" . $thold_data['thold_name'] . "', Graph: '" . $thold_data['local_graph_id'] . "'"); $item = array(); $rrd_reindexed[$thold_data['local_data_id']] = unserialize($thold_data['thold_server_rrd_reindexed']); $rrd_time_reindexed[$thold_data['local_data_id']] = $thold_data['thold_server_rrd_time_reindexed']; $currenttime = 0; $currentval = thold_get_currentval($thold_data, $rrd_reindexed, $rrd_time_reindexed, $item, $currenttime); switch ($thold_data['data_type']) { case 0: break; case 1: if ($thold_data['cdef'] != 0) { $currentval = thold_build_cdef($cdefs[$thold_data['cdef']], $currentval, $thold_data['local_data_id'], $thold_data['data_template_rrd_id']); } break; case 2: if ($thold_data['percent_ds'] != '') { $currentval = thold_calculate_percent($thold_data, $currentval, $rrd_reindexed); } break; case 3: if ($thold_data['expression'] != '') { $currentval = thold_calculate_expression($thold_data, $currentval, $rrd_reindexed, $rrd_time_reindexed); } break; } if (is_numeric($currentval)) { $currentval = round($currentval, 4);
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_poller_output(&$rrd_update_array) { global $config, $debug; include_once $config['base_path'] . '/plugins/thold/thold_functions.php'; include_once $config['library_path'] . '/snmp.php'; $rrd_reindexed = array(); $rrd_time_reindexed = array(); $local_data_ids = ''; $x = 0; foreach ($rrd_update_array as $item) { if (isset($item['times'][key($item['times'])])) { if ($x) { $local_data_ids .= ','; } $local_data_ids .= $item['local_data_id']; $rrd_reindexed[$item['local_data_id']] = $item['times'][key($item['times'])]; $rrd_time_reindexed[$item['local_data_id']] = key($item['times']); $x++; } } if ($local_data_ids != '') { if (read_config_option('thold_daemon_enable')) { /* assign a new process id */ $thold_pid = time() . '_' . rand(); $thold_items = db_fetch_assoc("SELECT id, local_data_id FROM thold_data WHERE thold_daemon_pid = '' AND thold_data.local_data_id IN ({$local_data_ids})"); if ($thold_items) { /* avoid that concurrent processes will work on the same thold items */ db_execute("UPDATE thold_data SET thold_data.thold_daemon_pid = '{$thold_pid}' WHERE thold_daemon_pid = '' AND thold_data.local_data_id IN ({$local_data_ids});"); /* cache required polling data. prefer bulk inserts for performance reasons - start with chunks of 1000 items*/ $sql_max_inserts = 1000; $thold_items = array_chunk($thold_items, $sql_max_inserts); $sql_insert = 'INSERT INTO `plugin_thold_daemon_data` ( id, pid, rrd_reindexed, rrd_time_reindexed ) VALUES '; $sql_values = ''; foreach ($thold_items as $packet) { foreach ($packet as $thold_item) { $sql_values .= "('" . $thold_item['id'] . "','" . $thold_pid . "','" . serialize($rrd_reindexed[$thold_item['local_data_id']]) . "','" . $rrd_time_reindexed[$thold_item['local_data_id']] . "'),"; } db_execute($sql_insert . substr($sql_values, 0, -1)); $sql_values = ''; } /* queue a new thold process */ db_execute("INSERT INTO `plugin_thold_daemon_processes` ( pid ) VALUES('{$thold_pid}')"); } return $rrd_update_array; } /* hold data of all CDEFs in memory to reduce the number of SQL queries to minimum */ $cdefs = array(); $cdefs_tmp = db_fetch_assoc('SELECT cdef_id, sequence, type, value FROM cdef_items ORDER BY cdef_id, sequence'); if ($cdefs_tmp & sizeof($cdefs_tmp) > 0) { foreach ($cdefs_tmp as $cdef_tmp) { $cdefs[$cdef_tmp['cdef_id']][] = $cdef_tmp; } } unset($cdefs_tmp); $tholds = db_fetch_assoc("SELECT td.id, \n\t\t\ttd.name AS thold_name, td.local_graph_id,\n\t\t\ttd.percent_ds, td.expression,\n\t\t\ttd.data_type, td.cdef, td.local_data_id,\n\t\t\ttd.data_template_rrd_id, td.lastread,\n\t\t\tUNIX_TIMESTAMP(td.lasttime) AS lasttime, td.oldvalue,\n\t\t\tdtr.data_source_name as name,\n\t\t\tdtr.data_source_type_id, dtd.rrd_step,\n\t\t\tdtr.rrd_maximum\n\t\t\tFROM thold_data AS td\n\t\t\tLEFT JOIN data_template_rrd AS dtr\n\t\t\tON (dtr.id = td.data_template_rrd_id)\n\t\t\tLEFT JOIN data_template_data AS dtd\n\t\t\tON dtd.local_data_id = td.local_data_id\n\t\t\tWHERE dtr.data_source_name!=''\n\t\t\tAND td.local_data_id IN({$local_data_ids})", false); } else { return $rrd_update_array; } if (sizeof($tholds)) { foreach ($tholds as $thold_data) { thold_debug("Checking Threshold: Name: '" . $thold_data['thold_name'] . "', Graph: '" . $thold_data['local_graph_id'] . "'"); $item = array(); $currenttime = 0; $currentval = thold_get_currentval($thold_data, $rrd_reindexed, $rrd_time_reindexed, $item, $currenttime); switch ($thold_data['data_type']) { case 0: break; case 1: if ($thold_data['cdef'] != 0) { $currentval = thold_build_cdef($cdefs[$thold_data['cdef']], $currentval, $thold_data['local_data_id'], $thold_data['data_template_rrd_id']); } break; case 2: if ($thold_data['percent_ds'] != '') { $currentval = thold_calculate_percent($thold_data, $currentval, $rrd_reindexed); } break; case 3: if ($thold_data['expression'] != '') { $currentval = thold_calculate_expression($thold_data, $currentval, $rrd_reindexed, $rrd_time_reindexed); } break; } if (is_numeric($currentval)) { $currentval = round($currentval, 4); } else { $currentval = ''; } db_execute("UPDATE thold_data SET tcheck=1, lastread='{$currentval}',\n\t\t\t\tlasttime='" . date('Y-m-d H:i:s', $currenttime) . "',\n\t\t\t\toldvalue='" . (isset($item[$thold_data['name']]) ? $item[$thold_data['name']] : '') . "'\n\t\t\t\tWHERE local_data_id = " . $thold_data['local_data_id'] . "\n\t\t\t\tAND data_template_rrd_id = " . $thold_data['data_template_rrd_id']); } } return $rrd_update_array; }
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; }