function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, $divisor = 1, $multiplier = 1, $low_limit = NULL, $low_warn_limit = NULL, $warn_limit = NULL, $high_limit = NULL, $current = NULL, $poller_type = 'snmp', $entPhysicalIndex = NULL, $entPhysicalIndex_measured = NULL, $measured_class = NULL, $measured_entity = NULL)
{
    global $config;
    print_debug("Discover sensor: {$class}, " . $device['hostname'] . ", {$oid}, {$index}, {$type}, {$descr}, {$divisor}, {$multiplier}, {$low_limit}, {$low_warn_limit}, {$warn_limit}, {$high_limit}, {$current}, {$poller_type}, {$entPhysicalIndex}, {$entPhysicalIndex_measured}, {$measured_class}, {$measured_entity}");
    // Reset empty strings to NULL
    if (!is_numeric($low_limit)) {
        $low_limit = NULL;
    }
    if (!is_numeric($high_limit)) {
        $high_limit = NULL;
    }
    if (!is_numeric($low_warn_limit)) {
        $low_warn_limit = NULL;
    }
    if (!is_numeric($warn_limit)) {
        $warn_limit = NULL;
    }
    if (!is_null($low_warn_limit) && !is_null($warn_limit) && $low_warn_limit > $warn_limit) {
        // Fix high/low thresholds (i.e. on negative numbers)
        list($warn_limit, $low_warn_limit) = array($low_warn_limit, $warn_limit);
    }
    if (dbFetchCell('SELECT COUNT(`sensor_id`) FROM `sensors` WHERE `poller_type`= ? AND `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?', array($poller_type, $class, $device['device_id'], $type, $index)) == '0') {
        if (!isset($config['sensor_states'][$type])) {
            if (!$high_limit) {
                $high_limit = sensor_limit($class, $current);
            }
            if (!$low_limit) {
                $low_limit = sensor_low_limit($class, $current);
            }
            if (!is_null($low_limit) && !is_null($high_limit) && $low_limit > $high_limit) {
                // Fix high/low thresholds (i.e. on negative numbers)
                list($high_limit, $low_limit) = array($low_limit, $high_limit);
            }
        } else {
            // For state sensors limits always is NULL
            $high_limit = NULL;
            $low_limit = NULL;
            $warn_limit = NULL;
            $low_warn_limit = NULL;
        }
        foreach (array('high_limit', 'warn_limit', 'low_warn_limit', 'low_limit') as $v) {
            // Convert strings/numbers to (float) or to array('NULL')
            ${$v} = ${$v} === NULL ? array('NULL') : (double) ${$v};
        }
        $sensor_insert = array('poller_type' => $poller_type, 'sensor_class' => $class, 'device_id' => $device['device_id'], 'sensor_oid' => $oid, 'sensor_index' => $index, 'sensor_type' => $type, 'sensor_descr' => $descr, 'sensor_divisor' => $divisor, 'sensor_multiplier' => $multiplier, 'sensor_limit' => $high_limit, 'sensor_limit_warn' => $warn_limit, 'sensor_limit_low' => $low_limit, 'sensor_limit_low_warn' => $low_warn_limit, 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured, 'measured_class' => $measured_class, 'measured_entity' => $measured_entity);
        $sensor_id = dbInsert($sensor_insert, 'sensors');
        $state_insert = array('sensor_id' => $sensor_id, 'sensor_value' => $current, 'sensor_polled' => 'NOW()');
        dbInsert($state_insert, 'sensors-state');
        print_debug("( {$sensor_id} inserted )");
        echo "+";
        log_event("Sensor added: {$class} {$type} {$index} {$descr}", $device, 'sensor', $sensor_id);
    } else {
        $sensor_entry = dbFetchRow("SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?", array($class, $device['device_id'], $type, $index));
        // Limits
        if (!$sensor_entry['sensor_custom_limit']) {
            if (!isset($config['sensor_states'][$type])) {
                if (!isset($high_limit)) {
                    if (!$sensor_entry['sensor_limit']) {
                        // Calculate a reasonable limit
                        $high_limit = sensor_limit($class, $current);
                    } else {
                        // Use existing limit
                        $high_limit = $sensor_entry['sensor_limit'];
                    }
                }
                if (!isset($low_limit)) {
                    if (!$sensor_entry['sensor_limit_low']) {
                        // Calculate a reasonable limit
                        $low_limit = sensor_low_limit($class, $current);
                    } else {
                        // Use existing limit
                        $low_limit = $sensor_entry['sensor_limit_low'];
                    }
                }
                // Fix high/low thresholds (i.e. on negative numbers)
                if (!is_null($low_limit) && !is_null($high_limit) && $low_limit > $high_limit) {
                    list($high_limit, $low_limit) = array($low_limit, $high_limit);
                }
            } else {
                // For state sensors limits always is NULL
                $high_limit = NULL;
                $low_limit = NULL;
                $warn_limit = NULL;
                $low_warn_limit = NULL;
            }
            // Update limits
            $update = array();
            foreach (array('high_limit' => 'sensor_limit', 'warn_limit' => 'sensor_limit_warn', 'low_warn_limit' => 'sensor_limit_low_warn', 'low_limit' => 'sensor_limit_low') as $v => $k) {
                //convert strings/numbers to identical type (float) or to array('NULL') for correct comparison
                ${$v} = ${$v} === NULL ? array('NULL') : (double) ${$v};
                $sensor_entry[$k] = $sensor_entry[$k] === NULL ? array('NULL') : (double) $sensor_entry[$k];
                if (${$v} !== $sensor_entry[$k]) {
                    $update[$k] = ${$v};
                }
            }
            if (count($update)) {
                echo "L";
                $msg = 'Sensor Limits updated (discovery): ' . $class . ' ' . $type . ' ' . $index . ' ' . $descr . ' ';
                foreach (array('L' => 'sensor_limit_low', 'Lw' => 'sensor_limit_low_warn', 'Hw' => 'sensor_limit_warn', 'H' => 'sensor_limit') as $v => $k) {
                    if (isset($update[$k])) {
                        $msg .= is_array($update[$k]) ? "[{$v}: " . $update[$k][0] . "]" : "[{$v}: " . $update[$k] . "]";
                    }
                }
                log_event($msg, $device, 'sensor', $sensor_entry['sensor_id']);
                $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
                print_debug("( {$updated} updated )");
            }
        }
        if ($oid == $sensor_entry['sensor_oid'] && $descr == $sensor_entry['sensor_descr'] && $multiplier == $sensor_entry['sensor_multiplier'] && $divisor == $sensor_entry['sensor_divisor'] && $entPhysicalIndex_measured == $sensor_entry['entPhysicalIndex_measured'] && $entPhysicalIndex == $sensor_entry['entPhysicalIndex'] && $sensor_entry['measured_class'] == $measured_class && $sensor_entry['measured_entity'] == $measured_entity) {
            echo ".";
        } else {
            $update = array('sensor_oid' => $oid, 'sensor_descr' => $descr, 'sensor_multiplier' => $multiplier, 'sensor_divisor' => $divisor, 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured, 'measured_class' => $measured_class, 'measured_entity' => $measured_entity);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            echo "U";
            log_event("Sensor updated: {$class} {$type} {$index} {$descr}", $device, 'sensor', $sensor_entry['sensor_id']);
            print_debug("( {$updated} updated )");
        }
    }
    $valid[$class][$type][$index] = 1;
}
Exemple #2
0
function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, $divisor = '1', $multiplier = '1', $low_limit = null, $low_warn_limit = null, $warn_limit = null, $high_limit = null, $current = null, $poller_type = 'snmp', $entPhysicalIndex = null, $entPhysicalIndex_measured = null)
{
    d_echo("Discover sensor: {$oid}, {$index}, {$type}, {$descr}, {$poller_type}, {$precision}, {$entPhysicalIndex}\n");
    if (is_null($low_warn_limit) && !is_null($warn_limit)) {
        // Warn limits only make sense when we have both a high and a low limit
        $low_warn_limit = null;
        $warn_limit = null;
    } elseif ($low_warn_limit > $warn_limit) {
        // Fix high/low thresholds (i.e. on negative numbers)
        list($warn_limit, $low_warn_limit) = array($low_warn_limit, $warn_limit);
    }
    if (dbFetchCell('SELECT COUNT(sensor_id) FROM `sensors` WHERE `poller_type`= ? AND `sensor_class` = ? AND `device_id` = ? AND sensor_type = ? AND `sensor_index` = ?', array($poller_type, $class, $device['device_id'], $type, $index)) == '0') {
        if (!$high_limit) {
            $high_limit = sensor_limit($class, $current);
        }
        if (!$low_limit) {
            $low_limit = sensor_low_limit($class, $current);
        }
        if ($low_limit > $high_limit) {
            // Fix high/low thresholds (i.e. on negative numbers)
            list($high_limit, $low_limit) = array($low_limit, $high_limit);
        }
        $insert = array('poller_type' => $poller_type, 'sensor_class' => $class, 'device_id' => $device['device_id'], 'sensor_oid' => $oid, 'sensor_index' => $index, 'sensor_type' => $type, 'sensor_descr' => $descr, 'sensor_divisor' => $divisor, 'sensor_multiplier' => $multiplier, 'sensor_limit' => $high_limit, 'sensor_limit_warn' => $warn_limit, 'sensor_limit_low' => $low_limit, 'sensor_limit_low_warn' => $low_warn_limit, 'sensor_current' => $current, 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured);
        foreach ($insert as $key => $val_check) {
            if (!isset($val_check)) {
                unset($insert[$key]);
            }
        }
        $inserted = dbInsert($insert, 'sensors');
        d_echo("( {$inserted} inserted )\n");
        echo '+';
        log_event('Sensor Added: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr), $device, 'sensor', $inserted);
    } else {
        $sensor_entry = dbFetchRow('SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?', array($class, $device['device_id'], $type, $index));
        if (!isset($high_limit)) {
            if (!$sensor_entry['sensor_limit']) {
                // Calculate a reasonable limit
                $high_limit = sensor_limit($class, $current);
            } else {
                // Use existing limit
                $high_limit = $sensor_entry['sensor_limit'];
            }
        }
        if (!isset($low_limit)) {
            if (!$sensor_entry['sensor_limit_low']) {
                // Calculate a reasonable limit
                $low_limit = sensor_low_limit($class, $current);
            } else {
                // Use existing limit
                $low_limit = $sensor_entry['sensor_limit_low'];
            }
        }
        // Fix high/low thresholds (i.e. on negative numbers)
        if ($low_limit > $high_limit) {
            list($high_limit, $low_limit) = array($low_limit, $high_limit);
        }
        if ($high_limit != $sensor_entry['sensor_limit'] && $sensor_entry['sensor_custom'] == 'No') {
            $update = array('sensor_limit' => $high_limit == null ? array('NULL') : $high_limit);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            d_echo("( {$updated} updated )\n");
            echo 'H';
            log_event('Sensor High Limit Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr) . ' (' . $high_limit . ')', $device, 'sensor', $sensor_id);
        }
        if ($sensor_entry['sensor_limit_low'] != $low_limit && $sensor_entry['sensor_custom'] == 'No') {
            $update = array('sensor_limit_low' => $low_limit == null ? array('NULL') : $low_limit);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            d_echo("( {$updated} updated )\n");
            echo 'L';
            log_event('Sensor Low Limit Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr) . ' (' . $low_limit . ')', $device, 'sensor', $sensor_id);
        }
        if ($warn_limit != $sensor_entry['sensor_limit_warn'] && $sensor_entry['sensor_custom'] == 'No') {
            $update = array('sensor_limit_warn' => $warn_limit == null ? array('NULL') : $warn_limit);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            d_echo("( {$updated} updated )\n");
            echo 'WH';
            log_event('Sensor Warn High Limit Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr) . ' (' . $warn_limit . ')', $device, 'sensor', $sensor_id);
        }
        if ($sensor_entry['sensor_limit_low_warn'] != $low_warn_limit && $sensor_entry['sensor_custom'] == 'No') {
            $update = array('sensor_limit_low_warn' => $low_warn_limit == null ? array('NULL') : $low_warn_limit);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            d_echo("( {$updated} updated )\n");
            echo 'WL';
            log_event('Sensor Warn Low Limit Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr) . ' (' . $low_warn_limit . ')', $device, 'sensor', $sensor_id);
        }
        if ($oid == $sensor_entry['sensor_oid'] && $descr == $sensor_entry['sensor_descr'] && $multiplier == $sensor_entry['sensor_multiplier'] && $divisor == $sensor_entry['sensor_divisor'] && $entPhysicalIndex_measured == $sensor_entry['entPhysicalIndex_measured'] && $entPhysicalIndex == $sensor_entry['entPhysicalIndex']) {
            echo '.';
        } else {
            $update = array('sensor_oid' => $oid, 'sensor_descr' => $descr, 'sensor_multiplier' => $multiplier, 'sensor_divisor' => $divisor, 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            echo 'U';
            log_event('Sensor Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr), $device, 'sensor', $sensor_id);
            d_echo("( {$updated} updated )\n");
        }
    }
    //end if
    $valid[$class][$type][$index] = 1;
}
function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, $divisor = '1', $multiplier = '1', $low_limit = NULL, $low_warn_limit = NULL, $warn_limit = NULL, $high_limit = NULL, $current = NULL, $poller_type = 'snmp', $entPhysicalIndex = NULL, $entPhysicalIndex_measured = NULL, $measured_class = NULL, $measured_entity = NULL)
{
    global $config, $debug;
    if ($debug) {
        echo "Discover sensor: {$class}, {$device}, {$oid}, {$index}, {$type}, {$descr},  {$divisor} , {$multiplier}, {$low_limit}, {$low_warn_limit}, {$warn_limit}, {$high_limit}, {$current}, {$poller_type}, {$entPhysicalIndex}, {$entPhysicalIndex_measured}, {$measured_class}, {$measured_entity} \n";
    }
    if (empty($low_warn_limit) || empty($warn_limit)) {
        // Warn limits only make sense when we have both a high and a low limit
        $low_warn_limit = NULL;
        $warn_limit = NULL;
    } elseif ($low_warn_limit > $warn_limit) {
        // Fix high/low thresholds (i.e. on negative numbers)
        list($warn_limit, $low_warn_limit) = array($low_warn_limit, $warn_limit);
    }
    if (dbFetchCell("SELECT COUNT(sensor_id) FROM `sensors` WHERE `poller_type`= ? AND `sensor_class` = ? AND `device_id` = ? AND sensor_type = ? AND `sensor_index` = ?", array($poller_type, $class, $device['device_id'], $type, $index)) == '0') {
        if (!$high_limit) {
            $high_limit = sensor_limit($class, $current);
        }
        if (!$low_limit) {
            $low_limit = sensor_low_limit($class, $current);
        }
        if ($low_limit > $high_limit) {
            // Fix high/low thresholds (i.e. on negative numbers)
            list($high_limit, $low_limit) = array($low_limit, $high_limit);
        }
        $insert = array('poller_type' => $poller_type, 'sensor_class' => $class, 'device_id' => $device['device_id'], 'sensor_oid' => $oid, 'sensor_index' => $index, 'sensor_type' => $type, 'sensor_descr' => $descr, 'sensor_divisor' => $divisor, 'sensor_multiplier' => $multiplier, 'sensor_limit' => $high_limit, 'sensor_limit_warn' => $warn_limit, 'sensor_limit_low' => $low_limit, 'sensor_limit_low_warn' => $low_warn_limit, 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured, 'measured_class' => $measured_class, 'measured_entity' => $measured_entity);
        $inserted = dbInsert($insert, 'sensors');
        if ($debug) {
            echo "( {$inserted} inserted )\n";
        }
        echo "+";
        log_event("Sensor Added: " . mres($class) . " " . mres($type) . " " . mres($index) . " " . mres($descr), $device, 'sensor', mysql_insert_id());
    } else {
        $sensor_entry = dbFetchRow("SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?", array($class, $device['device_id'], $type, $index));
        if (!isset($high_limit)) {
            if (!$sensor_entry['sensor_limit']) {
                // Calculate a reasonable limit
                $high_limit = sensor_limit($class, $current);
            } else {
                // Use existing limit
                $high_limit = $sensor_entry['sensor_limit'];
            }
        }
        if (!isset($low_limit)) {
            if (!$sensor_entry['sensor_limit_low']) {
                // Calculate a reasonable limit
                $low_limit = sensor_low_limit($class, $current);
            } else {
                // Use existing limit
                $low_limit = $sensor_entry['sensor_limit_low'];
            }
        }
        // Fix high/low thresholds (i.e. on negative numbers)
        if ($low_limit > $high_limit) {
            list($high_limit, $low_limit) = array($low_limit, $high_limit);
        }
        if ($high_limit != $sensor_entry['sensor_limit']) {
            $update = array('sensor_limit' => $high_limit == NULL ? array('NULL') : $high_limit);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            if ($debug) {
                echo "( {$updated} updated )\n";
            }
            echo "H";
            log_event("Sensor High Limit Updated: " . mres($class) . " " . mres($type) . " " . mres($index) . " " . mres($descr) . " (" . $high_limit . ")", $device, 'sensor', $sensor_id);
        }
        if ($sensor_entry['sensor_limit_low'] != $low_limit) {
            $update = array('sensor_limit_low' => $low_limit == NULL ? array('NULL') : $low_limit);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            if ($debug) {
                echo "( {$updated} updated )\n";
            }
            echo "L";
            log_event("Sensor Low Limit Updated: " . mres($class) . " " . mres($type) . " " . mres($index) . " " . mres($descr) . " (" . $low_limit . ")", $device, 'sensor', $sensor_id);
        }
        if ($warn_limit != $sensor_entry['sensor_limit_warn']) {
            $update = array('sensor_limit_warn' => $warn_limit == NULL ? array('NULL') : $warn_limit);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            if ($debug) {
                echo "( {$updated} updated )\n";
            }
            echo "WH";
            log_event("Sensor Warn High Limit Updated: " . mres($class) . " " . mres($type) . " " . mres($index) . " " . mres($descr) . " (" . $warn_limit . ")", $device, 'sensor', $sensor_id);
        }
        if ($sensor_entry['sensor_limit_low_warn'] != $low_warn_limit) {
            $update = array('sensor_limit_low_warn' => $low_warn_limit == NULL ? array('NULL') : $low_warn_limit);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            if ($debug) {
                echo "( {$updated} updated )\n";
            }
            echo "WL";
            log_event("Sensor Warn Low Limit Updated: " . mres($class) . " " . mres($type) . " " . mres($index) . " " . mres($descr) . " (" . $low_warn_limit . ")", $device, 'sensor', $sensor_id);
        }
        if ($oid == $sensor_entry['sensor_oid'] && $descr == $sensor_entry['sensor_descr'] && $multiplier == $sensor_entry['sensor_multiplier'] && $divisor == $sensor_entry['sensor_divisor'] && $entPhysicalIndex_measured == $sensor_entry['entPhysicalIndex_measured'] && $entPhysicalIndex == $sensor_entry['entPhysicalIndex'] && $sensor_entry['measured_class'] == $measured_class && $sensor_entry['measured_entity'] == $measured_entity) {
            echo ".";
        } else {
            $update = array('sensor_oid' => $oid, 'sensor_descr' => $descr, 'sensor_multiplier' => $multiplier, 'sensor_divisor' => $divisor, 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured, 'measured_class' => $measured_class, 'measured_entity' => $measured_entity);
            $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id']));
            echo "U";
            log_event("Sensor Updated: " . mres($class) . " " . mres($type) . " " . mres($index) . " " . mres($descr), $device, 'sensor', $sensor_id);
            if ($debug) {
                echo "( {$updated} updated )\n";
            }
        }
    }
    $valid[$class][$type][$index] = 1;
}