} else { $m_disabled[] = $module; } } if (count($m_disabled)) { print_message('Disabled poller modules:'); print_message(' ' . implode("\n ", $m_disabled)); } exit; } } if (!isset($options['q'])) { print_cli_banner(); $latest['version'] = get_obs_attrib('latest_ver'); $latest['revision'] = get_obs_attrib('latest_rev'); $latest['date'] = get_obs_attrib('latest_rev_date'); if ($latest['revision'] > OBSERVIUM_REV) { print_message("%GThere is a newer revision of Observium available!%n", 'color'); print_message("%GVersion %r" . $latest['version'] . "%G (" . format_unixtime(datetime_to_unixtime($latest['date']), 'jS F Y') . ") is %r" . ($latest['revision'] - OBSERVIUM_REV) . "%G revisions ahead.%n\n", 'color'); } // print_message("%g".OBSERVIUM_PRODUCT." ".OBSERVIUM_VERSION."\n%WPoller%n\n", 'color'); if (OBS_DEBUG) { print_versions(); } } if ($options['h'] == "odd") { $options['n'] = "1"; $options['i'] = "2"; } elseif ($options['h'] == "even") { $options['n'] = "0"; $options['i'] = "2";
function get_http_request($request, $context = array(), $rate_limit = FALSE) { global $config; $ok = TRUE; if (defined('OBS_HTTP_REQUEST') && OBS_HTTP_REQUEST === FALSE) { print_debug("HTTP requests skipped since previous request exit with timeout"); $ok = FALSE; $GLOBALS['response_headers'] = array('code' => '408', 'status' => 'Request Timeout'); } else { if (!ini_get('allow_url_fopen')) { print_debug('HTTP requests disabled, since PHP config option "allow_url_fopen" set to off. Please enable this option in your PHP config.'); $ok = FALSE; $GLOBALS['response_headers'] = array('code' => '501', 'status' => 'Not Implemented'); } else { if (preg_match('/^https/i', $request) && !check_extension_exists('openssl')) { // Check if Secure requests allowed, but ssl extensin not exist print_debug(__FUNCTION__ . '() wants to connect with https but https is not enabled on this server. Please check your PHP settings, the openssl extension must exist and be enabled.'); logfile(__FUNCTION__ . '() wants to connect with https but https is not enabled on this server. Please check your PHP settings, the openssl extension must exist and be enabled.'); $ok = FALSE; $GLOBALS['response_headers'] = array('code' => '501', 'status' => 'HTTPS Method Not Implemented'); } } } if ($ok && $rate_limit && is_numeric($rate_limit) && $rate_limit >= 0) { // Check limit rates to this domain (per/day) if (preg_match('/^https?:\\/\\/([\\w\\.]+[\\w\\-\\.]*(:\\d+)?)/i', $request, $matches)) { $date = format_unixtime($config['time']['now'], 'Y-m-d'); $domain = $matches[0]; // base domain (with http(s)): https://test-me.com/ -> https://test-me.com $rate_db = json_decode(get_obs_attrib('http_rate_' . $domain), TRUE); //print_vars($date); print_vars($rate_db); if (is_array($rate_db) && isset($rate_db[$date])) { $rate_count = $rate_db[$date]; } else { $rate_count = 0; } $rate_count++; set_obs_attrib('http_rate_' . $domain, json_encode(array($date => $rate_count))); if ($rate_count > $rate_limit) { print_debug("HTTP requests skipped because the rate limit {$rate_limit}/day for domain '{$domain}' is exceeded (count: {$rate_count})"); $GLOBALS['response_headers'] = array('code' => '429', 'status' => 'Too Many Requests'); $ok = FALSE; } else { if (OBS_DEBUG > 1) { print_debug("HTTP rate count for domain '{$domain}': {$rate_count} ({$rate_limit}/day)"); } } } else { $rate_limit = FALSE; } } if (OBS_DEBUG > 0) { $debug_request = $request; if (OBS_DEBUG < 2 && strpos($request, 'update.observium.org')) { $debug_request = preg_replace('/&stats=.+/', '&stats=***', $debug_request); } $debug_msg = PHP_EOL . 'REQUEST[%y' . $debug_request . '%n]'; } if (!$ok) { if (OBS_DEBUG > 0) { print_message($debug_msg . PHP_EOL . 'REQUEST STATUS[' . $GLOBALS['response_headers']['code'] . ' ' . $GLOBALS['response_headers']['status'] . ']', 'console'); } return FALSE; } $response = ''; if (!is_array($context)) { $context = array(); } // Fix context if not array passed $opts = array('http' => $context); $opts['http']['timeout'] = '15'; // User agent (required for some type of queries, ie geocoding) if (!isset($opts['http']['header'])) { $opts['http']['header'] = ''; } // Avoid 'undefined index' when concatting below $opts['http']['header'] .= 'User-Agent: ' . OBSERVIUM_PRODUCT . '/' . OBSERVIUM_VERSION . '\\r\\n'; if (isset($config['http_proxy']) && $config['http_proxy']) { $opts['http']['proxy'] = 'tcp://' . $config['http_proxy']; $opts['http']['request_fulluri'] = TRUE; } // Basic proxy auth if (isset($config['proxy_user']) && $config['proxy_user'] && isset($config['proxy_password'])) { $auth = base64_encode($config['proxy_user'] . ':' . $config['proxy_password']); $opts['http']['header'] .= 'Proxy-Authorization: Basic ' . $auth . '\\r\\n'; } $start = utime(); $context = stream_context_create($opts); $response = file_get_contents($request, FALSE, $context); $runtime = utime() - $start; // Parse response headers $head = array(); foreach ($http_response_header as $k => $v) { $t = explode(':', $v, 2); if (isset($t[1])) { $head[trim($t[0])] = trim($t[1]); } else { if (preg_match("!HTTP/([\\d\\.]+)\\s+(\\d+)(.*)!", $v, $matches)) { $head['http'] = $matches[1]; $head['code'] = intval($matches[2]); $head['status'] = trim($matches[3]); } else { $head[] = $v; } } } $GLOBALS['response_headers'] = $head; if (OBS_DEBUG > 0) { if (OBS_DEBUG < 2 && strpos($request, 'update.observium.org')) { $request = preg_replace('/&stats=.+/', '&stats=***', $request); } print_message($debug_msg . PHP_EOL . 'REQUEST STATUS[' . $head['code'] . ' ' . $head['status'] . ']' . PHP_EOL . 'REQUEST RUNTIME[' . ($runtime > 3 ? '%r' : '%g') . round($runtime, 4) . 's%n]', 'console'); if (OBS_DEBUG > 1) { print_message("RESPONSE[\n" . $response . "\n]", 'console', FALSE); print_vars($http_response_header); print_vars($opts); } } // Set OBS_HTTP_REQUEST for skip all other requests if (!defined('OBS_HTTP_REQUEST')) { if ($response === FALSE && empty($http_response_header)) { $GLOBALS['response_headers'] = array('code' => '408', 'status' => 'Request Timeout'); // Timeout error, only if not received responce headers define('OBS_HTTP_REQUEST', FALSE); print_debug(__FUNCTION__ . '() exit with timeout. Access to outside localnet is blocked by firewall or network problems. Check proxy settings.'); logfile(__FUNCTION__ . '() exit with timeout. Access to outside localnet is blocked by firewall or network problems. Check proxy settings.'); } else { define('OBS_HTTP_REQUEST', TRUE); } } // FIXME. what if first request fine, but second broken? //else if ($response === FALSE) //{ // if (function_exists('runkit_constant_redefine')) { runkit_constant_redefine('OBS_HTTP_REQUEST', FALSE); } //} return $response; }
print_error("DB schema not installed, first install it."); die; } } else { // Disable STRICT mode for DB session (we not fully support them) $db_modes = explode(',', dbFetchCell("SELECT @@SESSION.sql_mode;")); $db_mode_exclude = 'STRICT_TRANS_TABLES'; if (in_array($db_mode_exclude, $db_modes)) { $db_modes = array_diff($db_modes, array($db_mode_exclude)); dbQuery('SET SESSION `sql_mode` = ?', array(implode(',', $db_modes))); print_debug('DB STRICT mode disabled'); } //register_shutdown_function('dbClose'); // Maybe better in another place, but at least here it runs always; keep track of what svn revision we last saw, and eventlog the upgrade versions. // We have versions here from the includes above, and we just connected to the DB. $rev_old = @get_obs_attrib('current_rev'); if ($rev_old < OBSERVIUM_REV || !is_numeric($rev_old)) { set_obs_attrib('current_rev', OBSERVIUM_REV); log_event("Observium updated: {$rev_old} -> " . OBSERVIUM_REV); // FIXME log_event currently REQUIRES a device, the SQL query will fail. } } } // Load SQL configuration into $config variable load_sqlconfig($config); /** * OHMYGOD, this is very dangerous, because this is secure hole for override static definitions, * now already defined configs skipped in load_sqlconfig(). * // Reload configuration file into $config variable to make sure it overrules all SQL-supplied and default settings // Not the greatest hack, but array_merge was unfit for the job, unfortunately.
if (isset($config['syslog']['fifo']) && $config['syslog']['fifo'] !== FALSE) { // FIFO configured, try to grab logs from it #echo 'Opening FIFO: '.$config['syslog']['fifo'].PHP_EOL; //No any echo to STDOUT/STDERR! $s = fopen($config['syslog']['fifo'], 'r'); } else { // No FIFO configured, take logs from stdin #echo 'Opening STDIN'.PHP_EOL; //No any echo to STDOUT/STDERR! $s = fopen('php://stdin', 'r'); } while ($line = fgets($s)) { if (isset($config['syslog']['debug']) && $config['syslog']['debug']) { // Store RAW syslog line into debug.log logfile('debug.log', $line); } // Update syslog ruleset if they've changed. (this query should be cheap). $new_rules = get_obs_attrib('syslog_rules_changed'); if ($new_rules > $cur_rules) { $cur_rules = $new_rules; $rules = cache_syslog_rules(); $device_rules = cache_syslog_rules_assoc(); $maint = cache_alert_maintenance(); // logfile('debug.log', "Rules updated: ".$new_rules); } // host || facility || priority || level || tag || timestamp || msg || program $entry = array(); // Init!!! list($entry['host'], $entry['facility'], $entry['priority'], $entry['level'], $entry['tag'], $entry['timestamp'], $entry['msg'], $entry['program']) = explode("||", trim($line)); process_syslog($entry, 1); unset($entry, $line); $i++; if ($i > 10) {