Пример #1
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,\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;
}
Пример #2
0
function thold_calculate_expression($thold, $currentval, &$rrd_reindexed, &$rrd_time_reindexed)
{
    global $rpn_error;
    /* set an rpn error flag */
    $rpn_error = false;
    /* operators to support */
    $math = array('+', '-', '*', '/', '%', '^', 'ADDNAN', 'SIN', 'COS', 'LOG', 'EXP', 'SQRT', 'ATAN', 'ATAN2', 'FLOOR', 'CEIL', 'DEG2RAD', 'RAD2DEG', 'ABS');
    $boolean = array('LT', 'LE', 'GT', 'GE', 'EQ', 'NE', 'UN', 'ISNF', 'IF', 'AND', 'OR');
    $comparison = array('MIN', 'MAX', 'LIMIT');
    $setops = array('SORT', 'REV', 'AVG');
    $specvals = array('UNKN', 'INF', 'NEGINF', 'PREV', 'COUNT');
    $stackops = array('DUP', 'POP', 'EXC');
    $time = array('NOW', 'TIME', 'LTIME');
    $spectypes = array('CURRENT_DATA_SOURCE', 'CURRENT_GRAPH_MINIMUM_VALUE', 'CURRENT_GRAPH_MINIMUM_VALUE', 'CURRENT_DS_MINIMUM_VALUE', 'CURRENT_DS_MAXIMUM_VALUE', 'VALUE_OF_HDD_TOTAL', 'ALL_DATA_SOURCES_NODUPS', 'ALL_DATA_SOURCES_DUPS');
    /* our expression array */
    $expression = explode(',', $thold['expression']);
    /* out current data sources */
    $data_sources = $rrd_reindexed[$thold['rra_id']];
    if (sizeof($data_sources)) {
        foreach ($data_sources as $key => $value) {
            $key = strtolower($key);
            $nds[$key] = $value;
        }
        $data_sources = $nds;
    }
    /* replace all data tabs in the rpn with values */
    if (sizeof($expression)) {
        foreach ($expression as $key => $item) {
            if (substr_count($item, "|ds:")) {
                $dsname = strtolower(trim(str_replace("|ds:", "", $item), " |\n\r"));
                $thold_item = db_fetch_row("SELECT thold_data.id, thold_data.graph_id,\r\n\t\t\t\tthold_data.percent_ds, thold_data.expression,\r\n\t\t\t\tthold_data.data_type, thold_data.cdef, thold_data.rra_id,\r\n\t\t\t\tthold_data.data_id, thold_data.lastread,\r\n\t\t\t\tUNIX_TIMESTAMP(thold_data.lasttime) AS lasttime, thold_data.oldvalue,\r\n\t\t\t\tdata_template_rrd.data_source_name as name,\r\n\t\t\t\tdata_template_rrd.data_source_type_id, data_template_data.rrd_step,\r\n\t\t\t\tdata_template_rrd.rrd_maximum\r\n\t\t\t\tFROM thold_data\r\n\t\t\t\tLEFT JOIN data_template_rrd\r\n\t\t\t\tON (data_template_rrd.id = thold_data.data_id)\r\n\t\t\t\tLEFT JOIN data_template_data\r\n\t\t\t\tON (data_template_data.local_data_id=thold_data.rra_id)\r\n\t\t\t\tWHERE data_template_rrd.data_source_name='{$dsname}'\r\n\t\t\t\tAND thold_data.rra_id=" . $thold['rra_id'], false);
                if (sizeof($thold_item)) {
                    $item = array();
                    $currenttime = 0;
                    $expression[$key] = thold_get_currentval($thold_item, $rrd_reindexed, $rrd_time_reindexed, $item, $currenttime);
                } else {
                    $value = '';
                    if (api_plugin_is_enabled('dsstats') && read_config_option("dsstats_enable") == "on") {
                        $value = db_fetch_cell("SELECT calculated\r\n\t\t\t\t\t\tFROM data_source_stats_hourly_last\r\n\t\t\t\t\t\tWHERE local_data_id=" . $thold['rrd_id'] . "\r\n\t\t\t\t\t\tAND rrd_name='{$dsname}'");
                    }
                    if (empty($value) || $value == '-90909090909') {
                        $expression[$key] = get_current_value($thold['rra_id'], $dsname);
                    } else {
                        $expression[$key] = $value;
                    }
                    cacti_log($expression[$key]);
                }
                if ($expression[$key] == '') {
                    $expression[$key] = '0';
                }
            } elseif (substr_count($item, "|")) {
                $gl = db_fetch_row("SELECT * FROM graph_local WHERE id=" . $thold["graph_id"]);
                if (sizeof($gl)) {
                    $expression[$key] = thold_expand_title($thold, $gl["host_id"], $gl["snmp_query_id"], $gl["snmp_index"], $item);
                } else {
                    $expression[$key] = '0';
                    cacti_log("WARNING: Query Replacement for '{$item}' Does Not Exist");
                }
                if ($expression[$key] == '') {
                    $expression[$key] = '0';
                }
            } else {
                /* normal operator */
            }
        }
    }
    //cacti_log(implode(",", array_keys($data_sources)));
    //cacti_log(implode(",", $data_sources));
    //cacti_log(implode(",", $expression));
    /* now let's process the RPN stack */
    $x = count($expression);
    if ($x == 0) {
        return $currentval;
    }
    /* operation stack for RPN */
    $stack = array();
    /* current pointer in the RPN operations list */
    $cursor = 0;
    while ($cursor < $x) {
        $operator = strtoupper(trim($expression[$cursor]));
        /* is the operator a data source */
        if (is_numeric($operator)) {
            //cacti_log("NOTE: Numeric '$operator'", false, "THOLD");
            array_push($stack, $operator);
        } elseif (array_key_exists($operator, $data_sources)) {
            //cacti_log("NOTE: DS Value '$operator'", false, "THOLD");
            thold_expression_ds_value($operator, $stack, $data_sources);
        } elseif (in_array($operator, $comparison)) {
            //cacti_log("NOTE: Compare '$operator'", false, "THOLD");
            thold_expression_compare_rpn($operator, $stack);
        } elseif (in_array($operator, $boolean)) {
            //cacti_log("NOTE: Boolean '$operator'", false, "THOLD");
            thold_expression_boolean_rpn($operator, $stack);
        } elseif (in_array($operator, $math)) {
            //cacti_log("NOTE: Math '$operator'", false, "THOLD");
            thold_expression_math_rpn($operator, $stack);
        } elseif (in_array($operator, $setops)) {
            //cacti_log("NOTE: SetOps '$operator'", false, "THOLD");
            thold_expression_setops_rpn($operator, $stack);
        } elseif (in_array($operator, $specvals)) {
            //cacti_log("NOTE: SpecVals '$operator'", false, "THOLD");
            thold_expression_specvals_rpn($operator, $stack, $cursor + 2);
        } elseif (in_array($operator, $stackops)) {
            //cacti_log("NOTE: StackOps '$operator'", false, "THOLD");
            thold_expression_stackops_rpn($operator, $stack);
        } elseif (in_array($operator, $time)) {
            //cacti_log("NOTE: Time '$operator'", false, "THOLD");
            thold_expression_time_rpn($operator, $stack);
        } elseif (in_array($operator, $spectypes)) {
            //cacti_log("NOTE: SpecialTypes '$operator'", false, "THOLD");
            thold_expression_specialtype_rpn($operator, $stack, $thold['rra_id'], $currentval);
        } else {
            cacti_log("WARNING: Unsupported Field '{$operator}'", false, "THOLD");
            $rpn_error = true;
        }
        $cursor++;
        if ($rpn_error) {
            cacti_log("ERROR: RPN Expression is invalid! THold:'" . $thold['thold_name'] . "', Value:'" . $currentval . "', Expression:'" . $thold['expression'] . "'", false, 'THOLD');
            return 0;
        }
    }
    return $stack[0];
}
Пример #3
0
 $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);
 $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'] != '') {
Пример #4
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;
}