context === 'cacti') { if (false === function_exists('db_fetch_row')) { wm_debug( "ReadData CactiTHold: Cacti database library not found. [THOLD001]\n"); return (false); } $thold_present = false; if (true === function_exists('api_plugin_is_enabled')) { if (true === api_plugin_is_enabled('thold')) { $thold_present = true; } } if ( (true === isset($plugins)) && (true === in_array('thold', $plugins))) { $thold_present = true; } if (false === $thold_present) { wm_debug("ReadData CactiTHold: THold plugin not enabled. [THOLD002]\n"); } $sql = 'show tables'; $result = db_fetch_assoc($sql) or die(mysql_error()); $tables = array (); foreach ($result as $index => $arr) { foreach ($arr as $t) { $tables[] = $t; } } if (false === in_array('thold_data', $tables)) { wm_debug( 'ReadData CactiTHold: thold_data database table not found. [THOLD003]\n'); return (false); } return (true); } else { wm_debug( "ReadData CactiTHold: Can only run from Cacti environment. [THOLD004]\n"); } return (false); } function Recognise($targetstring) { if (1 === preg_match('/^cacti(thold|monitor):(\d+)$/', $targetstring, $matches)) { return true; } elseif (1 === preg_match('/^cactithold:(\d+):(\d+)$/', $targetstring, $matches)) { return false; } else { return false; } } function ReadData($targetstring, &$map, &$item) { $data[IN] = null; $data[OUT] = null; $data_time = 0; if (1 === preg_match('/^cactithold:(\d+):(\d+)$/', $targetstring, $matches)) { // Returns 0 if threshold is not breached, 1 if it is. // use target aggregation to build these up into a 'badness' percentage // takes the same two values that are visible in thold's own URLs (the actual thold ID isn't shown anywhere) $rra_id = intval($matches[1]); $data_id = intval($matches[2]); $SQL2 = sprintf("select thold_alert from thold_data where rra_id=%d and data_id=%d and thold_enabled='on'", $rra_id, $data_id); $result = db_fetch_row($SQL2); if (true === isset($result)) { if ($result['thold_alert'] > 0) { $data[IN] = 1; } else { $data[IN] = 0; } $data[OUT] = 0; } } elseif (1 === preg_match('/^cacti(thold|monitor):(\d+)$/', $targetstring, $matches)) { $type = $matches[1]; $id = intval($matches[2]); if ($type === 'thold') { // VERY simple. Returns 0 if threshold is not breached, 1 if it is. // use target aggregation to build these up into a 'badness' percentage $SQL2 = 'select thold_alert from thold_data where id='.$id." and thold_enabled='on'"; $result = db_fetch_row($SQL2); if (true === isset($result)) { if ($result['thold_alert'] > 0) { $data[IN] = 1; } else { $data[IN] = 0; } $data[OUT] = 0; } } if ($type === 'monitor') { wm_debug('CactiTHold ReadData: Getting cacti basic state for host '.$id."\n"); $SQL = 'select * from host where id='.$id; // 0=disabled // 1=down // 2=recovering // 3=up // 4=tholdbreached // 5=unknown $state = -1; $statename = ''; $result = db_fetch_row($SQL); if (true === isset($result)) { // create a note, which can be used in icon filenames or labels more nicely if ($result['status'] === 1) { $state = 1; $statename = 'down'; } if ($result['status'] === 2) { $state = 2; $statename = 'recovering'; } if ($result['status'] === 3) { $state = 3; $statename = 'up'; } if ($result['disabled'] === true) { $state = 0; $statename = 'disabled'; } if ($result['status'] == 5) { $state = 5; $statename = 'unknown'; } $data[IN] = $state; $data[OUT] = 0; $item->add_note('state', $statename); $item->add_note('cacti_description', $result['description']); $item->add_note('cacti_hostname', $result['hostname']); $item->add_note('cacti_curtime', $result['cur_time']); $item->add_note('cacti_avgtime', $result['avg_time']); $item->add_note('cacti_mintime', $result['min_time']); $item->add_note('cacti_maxtime', $result['max_time']); $item->add_note('cacti_availability', $result['availability']); $item->add_note('cacti_faildate', $result['status_fail_date']); $item->add_note('cacti_recdate', $result['status_rec_date']); } wm_debug(sprintf( "CactiTHold ReadData: Basic state for host %d is %s/%s\n", $id, $state, $statename)); wm_debug('CactiTHold ReadData: Checking threshold states for host '.$id."\n"); $numthresh = 0; $numfailing = 0; $SQL2 = sprintf("select rra_id, data_id, thold_alert from thold_data,data_local where thold_data.rra_id=data_local.id and data_local.host_id=%d and thold_enabled='on'", $id); $queryrows = db_fetch_assoc($SQL2); if (true === is_array($queryrows)) { foreach ($queryrows as $th) { $desc = $th['rra_id'] . '/' . $th['data_id']; $v = $th['thold_alert']; $numthresh++; if (intval($th['thold_alert']) > 0) { wm_debug( 'CactiTHold ReadData: Seen threshold '.$desc.' failing ('.$v.')for host '.$id."\n"); $numfailing++; } else { wm_debug( 'CactiTHold ReadData: Seen threshold '.$desc.' OK ('.$v.') for host '.$id."\n"); } } } else { wm_debug('CactiTHold ReadData: Failed to get thold info for host '.$id."\n"); } wm_debug( 'CactiTHold ReadData: Checked '.$numthresh.' and found '.$numfailing." failing\n"); if (($numfailing > 0) && ($numthresh > 0) && ($state === 3)) { $state = 4; $statename = 'tholdbreached'; $item->add_note('state', $statename); $item->add_note('thold_failcount', $numfailing); $item->add_note('thold_failpercent', ($numfailing / $numthresh) * 100); $data[IN] = $state; $data[OUT] = $numfailing; wm_debug('CactiTHold ReadData: State is '.$state.'/'.$statename."\n"); } elseif ($numthresh > 0) { $item->add_note('thold_failcount', 0); $item->add_note('thold_failpercent', 0); wm_debug('CactiTHold ReadData: Leaving state as '.$state."\n"); } } } wm_debug( sprintf("CactiThold ReadData: Returning (%s, %s, %s)\n", string_or_null($data[IN]), string_or_null($data[OUT]), $data_time )); return (array ( $data[IN], $data[OUT], $data_time )); } } // vim:ts=4:sw=4: ?>