Example #1
0
     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 \n\t\t\ttcheck=1, lastread='{$currentval}',\n\t\t\tlasttime='" . date('Y-m-d H:i:s', $currenttime) . "',\n\t\t\toldvalue='" . $item[$thold_data['name']] . "'\n\t\t\tWHERE id = " . $thold_data['thold_id']);
 }
 /* check all thresholds */
 $sql_query = "SELECT td.*\n\t\tFROM plugin_thold_daemon_data AS tdd\n\t\tINNER JOIN thold_data AS td\n\t\tON td.id = tdd.id\n\t\tLEFT JOIN data_template_rrd AS dtr\n\t\tON dtr.id = td.data_template_rrd_id\n\t\tWHERE tdd.pid = '{$pid}' \n\t\tAND td.thold_enabled='on' \n\t\tAND td.tcheck=1";
 $tholds = do_hook_function('thold_get_live_hosts', db_fetch_assoc($sql_query));
 $total_tholds = sizeof($tholds);
 foreach ($tholds as $thold) {
Example #2
0
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,
			thold_data.percent_ds, thold_data.expression,
			thold_data.data_type, thold_data.cdef, thold_data.rra_id,
			thold_data.data_id, thold_data.lastread,
			UNIX_TIMESTAMP(thold_data.lasttime) AS lasttime, thold_data.oldvalue,
			data_template_rrd.data_source_name as name,
			data_template_rrd.data_source_type_id, data_template_data.rrd_step,
			data_template_rrd.rrd_maximum
			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 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',
			lasttime='" . date("Y-m-d H:i:s", $currenttime) . "',
			oldvalue='" . $item[$t_item['name']] . "'
			WHERE rra_id = " . $t_item['rra_id'] . "
			AND data_id = " . $t_item['data_id']);
	}
	}

	return $rrd_update_array;
}
Example #3
0
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;
}