Пример #1
0
function RSSReader_activate()
{
    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 1);
    require_once "{$GLOBALS['PROJECT_BASEDIR']}/libs/TempStore.php";
    // abstracts data storage
    // returns MySQL valid timestamp
    function mysql_time($t)
    {
        return date('Y-m-d H:i:s', $t);
    }
    // prints out fetched key=>val pairs
    function printarr($arr)
    {
        foreach ($arr as $k => $v) {
            if (is_array($v)) {
                printarr($v);
            } else {
                echo "<tr>\n\t<td>&laquo;{$k}&raquo;</td>\n\t<td>&laquo;" . htmlspecialchars($v, ENT_COMPAT, 'UTF-8') . "&raquo;</td>\n</tr>\n";
            }
        }
    }
    // CONSTANTS AND HANDLES
    $shortwait = 10;
    // time in seconds between each check for new content on well reporting servers (10*60 is a good value here)
    $longwait = 30;
    // time in seconds between each redownload of content on servers not reporting content information (30*60 is a good value here)
    $sql =& new DBXStore($GLOBALS['DB_HOST'], $GLOBALS['DB_USERNAME'], $GLOBALS['DB_PASSWORD'], $GLOBALS['DB_DATABASE']);
    // data storage handle
    $feeds = $sql->read('feeds', array('id', 'url', 'timer', 'lastmodified', 'etag', 'reports'));
    // cache of all relevant information from data storage
    $ett = null;
    // I have to get the ETag we got from the server to the data writing bit somehow...
    $lmm = null;
    // I have to get the Last-Modified timestamp we got from the server to the data writing bit somehow...
    $update = false;
    // This also needs a default value to avoid notices--I don't want ANY errors. ;)
    //________________________________________________________________
    //================================================================
    //
    //      MAIN LOOP
    //________________________________________________________________
    //================================================================
    foreach ($feeds->data as $feed) {
        // Check to see if it's time to update this feed
        $checkhead = false;
        $ett = null;
        // clear out old ETag availability value
        $lmm = null;
        // clear out old Last-Modified availability value
        if ($feed['reports'] == 'complete' || $feed['reports'] == 'etagonly' || $feed['reports'] == 'lmonly') {
            // Server reports some usable information, we can make HEAD requests after a short wait time
            if ($feed['timer'] == 0 || empty($feed['timer']) || $feed['timer'] == null || strtotime($feed['timer']) + $shortwait < time()) {
                // Time since last update longer than required short wait time, or not set timestamp
                $checkhead = true;
            }
        } else {
            // Either reports 'none' (bad server!) or not tested, only check after long wait time has passed
            if ($feed['timer'] == 0 || empty($feed['timer']) || $feed['timer'] == null || strtotime($feed['timer']) + $longwait < time()) {
                // Time since last update longer than required long wait time, or not set timestamp
                $checkhead = true;
            }
        }
        if ($checkhead == true) {
            // about time to make a new HEAD request
            if ($feed['reports'] == 'complete' || $feed['reports'] == 'etagonly') {
                // etag checking here
                $resp = SimpleHEAD::HEADrequest($feed['url']);
                if (!isset($feed['etag']) || empty($feed['etag']) || $feed['etag'] == null || !isset($resp['et']) || empty($resp['et']) || $resp['et'] == null) {
                    echo "Got no ETag to compare with; proceeding with download.\n";
                    $update = true;
                } elseif (isset($feed['etag']) && isset($resp['et']) && !empty($resp['et']) && $resp['et'] != null && $feed['etag'] == $resp['et']) {
                    echo "Cool, these ETags are equal! No need to update.\n";
                    $update = false;
                    // we can't check ALL the time!
                    $sql->write('feeds', array('timer' => mysql_time(time())), $feed['id']);
                    $ett = $resp['et'];
                    // I have to get this to the data writing bit somehow...
                } elseif (isset($feed['etag']) && isset($resp['et']) && !empty($resp['et']) && $feed['etag'] != null && $feed['etag'] != $resp['et']) {
                    echo "These ETags are different; proceeding with download.\n";
                    echo $feed['etag'] . '   -   ' . $resp['et'] . "\n";
                    $update = true;
                    $ett = $resp['et'];
                    // I have to get this to the data writing bit somehow...
                } else {
                    // Something I failed to think about?
                    die("This sure is an interesting error. (Check the ETag code of " . __FILE__ . " at " . __LINE__ . ".\n");
                }
                if ($feed['reports'] == 'complete') {
                    $lmm = $resp['lm'];
                    // don't forget to store this if we have it
                }
            } elseif ($feed['reports'] == 'lmonly') {
                // lm checking here
                $resp = SimpleHEAD::HEADrequest($feed['url']);
                if (isset($resp['lm']) && !empty($resp['lm'])) {
                    $lmm = strtotime($resp['lm']);
                }
                if (!isset($feed['lastmodified']) || strtotime($feed['lastmodified']) == 0 || !isset($resp['lm']) || empty($resp['lm'])) {
                    echo "Got no Last-Modified timestamp to compare with; proceeding with download.\n";
                    $update = true;
                } elseif (isset($feed['lastmodified']) && $lmm != 0 && strtotime($feed['lastmodified']) == $lmm) {
                    echo "Cool, these Last-Modified timestamps are equal! No need to update.\n";
                    $update = false;
                    // we can't check ALL the time!
                    $sql->write('feeds', array('timer' => mysql_time(time())), $feed['id']);
                } elseif (isset($feed['lastmodified']) && $lmm != 0 && strtotime($feed['lastmodified']) != $lmm) {
                    echo "These timestamps are different; proceeding with download.\n";
                    echo strtotime($feed['lastmodified']) . '   -   ' . $lmm . "\n";
                    $update = true;
                } else {
                    // Something I failed to think about?
                    die("This sure is an interesting error. (Check the Last-Modified code of " . __FILE__ . " at " . __LINE__ . ".\n");
                }
            } elseif ($feed['reports'] == 'none' || $feed['reports'] == null) {
                if ($feed['reports'] == null) {
                    ServerTestPackage::ServerReportingTest($feed['url'], $feed['id'], $sql);
                }
                $ett = null;
                // reset these before writing
                $lmm = null;
                // reset these before writing
                // no way to check if anything's new... we'll just have to download the whole thing
                $update = true;
            } else {
                // ambiguous database value for 'reports'
                die("Ambiguous database value for 'reports': feed['reports'] = {$feed['reports']}.\n");
            }
        } else {
            // from if($checkhead)
            // too soon to update, we'll just give out a little notice
            if ($feed['reports'] == 'complete' || $feed['reports'] == 'etagonly' || $feed['reports'] == 'lmonly') {
                // short wait time
                echo "LOL! Too soon d00d.\n" . Duration::toString(time() - strtotime($feed['timer'])) . " since last update. You just gotta wait " . Duration::toString(strtotime($feed['timer']) + $shortwait - time()) . " more.\n";
            } else {
                // long wait time
                echo "OMG! Too soon d00d.\n" . Duration::toString(time() - strtotime($feed['timer'])) . " since last update. You just gotta wait " . Duration::toString(strtotime($feed['timer']) + $longwait - time()) . " more.\n";
            }
        }
        // end if($checkhead)
        if ($update) {
            $rss =& new XML_RSS($feed['url']);
            $rss->parse();
            echo '<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset="utf-8" />
		<title>Test</title>
	</head>
	<body>
		<table>';
            printarr($rss->getChannelInfo());
            echo '		</table>
	</body>
</html>			
';
            // we've updated, now write a new timestamp
            if (isset($ett) && !empty($ett) && isset($lmm) && !empty($lmm)) {
                // Got both ETag and Last-Modified from server (bravo!)
                // write the current time, the ETag we got, and the Last-Modified timestamp we got
                $sql->write('feeds', array('timer' => mysql_time(time()), 'etag' => $ett, 'lastmodified' => mysql_time($lmm)), $feed['id']);
            } elseif (isset($ett) && !empty($ett)) {
                // Only got ETag from server
                // write the current time and the ETag we got
                $sql->write('feeds', array('timer' => mysql_time(time()), 'etag' => $ett), $feed['id']);
            } elseif (isset($lmm) && !empty($lmm)) {
                // Only got Last-Modified from server
                // write the current time and the Last-Modified timestamp we got
                $sql->write('feeds', array('timer' => mysql_time(time()), 'lastmodified' => mysql_time($lmm)), $feed['id']);
            } else {
                // Got no content information from server (bad server!)
                // just write the current time
                $sql->write('feeds', array('timer' => mysql_time(time())), $feed['id']);
            }
        }
    }
    // end main loop
}
Пример #2
0
function handle_service($buffer)
{
    global $hlist;
    global $services_ok;
    global $services_warning;
    global $services_pending;
    global $services_critical;
    global $services_unknown;
    global $services_ack;
    $nothing = strtok($buffer, "\n");
    $host_name = substr(strtok("\n"), 11);
    $description = substr(strtok("\n"), 21);
    $modified_attributes = substr(strtok("\n"), 21);
    $check_command = substr(strtok("\n"), 15);
    $check_period = substr(strtok("\n"), 13);
    $notification_period = substr(strtok("\n"), 20);
    $check_interval = substr(strtok("\n"), 15);
    $retry_interval = substr(strtok("\n"), 15);
    $event_handler = substr(strtok("\n"), 15);
    $has_been_checked = substr(strtok("\n"), 18);
    $should_be_scheduled = substr(strtok("\n"), 21);
    $execution_time = substr(strtok("\n"), 22);
    $latency = substr(strtok("\n"), 15);
    $check_type = substr(strtok("\n"), 12);
    $status = substr(strtok("\n"), 15);
    $last_hard_state = substr(strtok("\n"), 17);
    $last_event_id = substr(strtok("\n"), 14);
    $current_event_id = substr(strtok("\n"), 18);
    $current_problem_id = substr(strtok("\n"), 20);
    $last_problem_id = substr(strtok("\n"), 17);
    $current_attempt = substr(strtok("\n"), 17);
    $max_attempts = substr(strtok("\n"), 14);
    # If you have a slightly older version of Nagios you'll need to uncomment these. They were in the file twice in older versions.
    #		$last_event_id = substr(strtok("\n"),14);
    #		$current_event_id = substr(strtok("\n"),18);
    $state_type = substr(strtok("\n"), 12);
    $last_state_change = substr(strtok("\n"), 19);
    $last_hard_state_change = substr(strtok("\n"), 24);
    $time_ok = substr(strtok("\n"), 14);
    $time_warning = substr(strtok("\n"), 19);
    $time_unknown = substr(strtok("\n"), 19);
    $time_critical = substr(strtok("\n"), 20);
    $plugin_output = substr(strtok("\n"), 15);
    $long_plugin_output = substr(strtok("\n"), 20);
    $performance_data = substr(strtok("\n"), 18);
    $last_check = strftime("%b %d %Y %H:%M:%S", substr(strtok("\n"), 12));
    $next_check = substr(strtok("\n"), 12);
    $check_options = substr(strtok("\n"), 15);
    $current_notification_number = substr(strtok("\n"), 29);
    $current_notification_id = substr(strtok("\n"), 24);
    $last_notification = strftime("%b %d %Y %H:%M:%S", substr(strtok("\n"), 19));
    $next_notification = substr(strtok("\n"), 19);
    $no_more_notifications = substr(strtok("\n"), 23);
    $notifications_enabled = substr(strtok("\n"), 23);
    $checks_enabled = substr(strtok("\n"), 23);
    $accept_passive_service_checks = substr(strtok("\n"), 24);
    $event_handler_enabled = substr(strtok("\n"), 23);
    $problem_has_been_acknowledged = substr(strtok("\n"), 31);
    $acknowledgement_type = substr(strtok("\n"), 22);
    $flap_detection_enabled = substr(strtok("\n"), 24);
    $failure_prediction_enabled = substr(strtok("\n"), 28);
    $process_performance_data = substr(strtok("\n"), 26);
    $obsess_over_service = substr(strtok("\n"), 21);
    $last_update = strftime("%b %d %Y %H:%M:%S", substr(strtok("\n"), 13));
    $is_flapping = substr(strtok("\n"), 13);
    $percent_state_change = substr(strtok("\n"), 22);
    $scheduled_downtime_depth = substr(strtok("\n"), 26);
    $real_dur = time(0) - $last_state_change;
    $duration = Duration::toString($real_dur);
    $last_state_change = strftime("%b %d %Y %H:%M:%S", $last_state_change);
    // Populate the wonderful array with all the details about this service. Also, increment the counters for the service status.
    if ($status == "0") {
        $status = "OK";
        $services_ok++;
    } else {
        if ($status == "1") {
            $services_warning++;
            $status = "WARNING";
        } else {
            if ($status == "4") {
                $services_pending++;
            } else {
                if ($status == "2") {
                    //the table status thing ignores critial for critical hosts, so why does the counter?
                    //it doesn't anymore!
                    if ($hlist[$host_name]["host"]["status"] != "1") {
                        if ($problem_has_been_acknowledged != "1" && $notifications_enabled == "1") {
                            $services_critical++;
                        } else {
                            $services_ack++;
                        }
                    }
                    $status = "CRITICAL";
                } else {
                    if ($status == "3") {
                        $services_unknown++;
                        $status = "Unknown";
                    } else {
                        $services_unknown++;
                        $status = "Unknown";
                    }
                }
            }
        }
    }
    if ($status != "OK" && $status != "PENDING") {
        // Don't store the lengthy details for the OK or Pending services because that seems unneccesary
        $hlist[$host_name]["service"][$description]["last_update"] = $last_update;
        $hlist[$host_name]["service"][$description]["host_name"] = $host_name;
        $hlist[$host_name]["service"][$description]["description"] = $description;
        $hlist[$host_name]["service"][$description]["status"] = $status;
        $hlist[$host_name]["service"][$description]["current_attempt"] = $current_attempt;
        $hlist[$host_name]["service"][$description]["max_attempts"] = $max_attempts;
        $hlist[$host_name]["service"][$description]["state_type"] = $state_type;
        $hlist[$host_name]["service"][$description]["last_check"] = $last_check;
        $hlist[$host_name]["service"][$description]["next_check"] = $next_check;
        $hlist[$host_name]["service"][$description]["check_type"] = $check_type;
        $hlist[$host_name]["service"][$description]["checks_enabled"] = $checks_enabled;
        $hlist[$host_name]["service"][$description]["accept_passive_service_checks"] = $accept_passive_service_checks;
        $hlist[$host_name]["service"][$description]["event_handler_enabled"] = $event_handler_enabled;
        $hlist[$host_name]["service"][$description]["last_state_change"] = $last_state_change;
        $hlist[$host_name]["service"][$description]["problem_has_been_acknowledged"] = $problem_has_been_acknowledged;
        $hlist[$host_name]["service"][$description]["last_hard_state"] = $last_hard_state;
        $hlist[$host_name]["service"][$description]["time_ok"] = $time_ok;
        $hlist[$host_name]["service"][$description]["time_unknown"] = $time_unknown;
        $hlist[$host_name]["service"][$description]["time_warning"] = $time_warning;
        $hlist[$host_name]["service"][$description]["time_critical"] = $time_critical;
        $hlist[$host_name]["service"][$description]["last_notification"] = $last_notification;
        $hlist[$host_name]["service"][$description]["current_notification_number"] = $current_notification_number;
        $hlist[$host_name]["service"][$description]["notifications_enabled"] = $notifications_enabled;
        $hlist[$host_name]["service"][$description]["latency"] = $latency;
        $hlist[$host_name]["service"][$description]["execution_time"] = $execution_time;
        $hlist[$host_name]["service"][$description]["flap_detection_enabled"] = $flap_detection_enabled;
        $hlist[$host_name]["service"][$description]["is_flapping"] = $is_flapping;
        $hlist[$host_name]["service"][$description]["percent_state_change"] = $percent_state_change;
        $hlist[$host_name]["service"][$description]["scheduled_downtime_depth"] = $scheduled_downtime_depth;
        $hlist[$host_name]["service"][$description]["failure_prediction_enabled"] = $failure_prediction_enabled;
        $hlist[$host_name]["service"][$description]["process_performance_data"] = $process_performance_data;
        $hlist[$host_name]["service"][$description]["obsess_over_service"] = $obsess_over_service;
        $hlist[$host_name]["service"][$description]["plugin_output"] = $plugin_output;
        $hlist[$host_name]["service"][$description]["duration"] = $duration;
        $hlist[$host_name]["service"][$description]["duration_in_secs"] = $real_dur;
    }
}
 function LMReliabilityTest($url)
 {
     echo "Testing reliability of Last-Modified timestamp...\n";
     $max = 3;
     // number of consecutive queries to perform
     $unreliable = false;
     for ($i = 1; $i <= $max; $i++) {
         $resp = SimpleHEAD::HEADrequest($url);
         echo "ping {$i}: {$resp['lm']}\t\t";
         $ping[$i] = strtotime($resp['lm']);
         if ($i == 1) {
             echo "starting value\n";
         } else {
             echo "change from last: ";
             if ($ping[$i] - $ping[$i - 1] != 0) {
                 $unreliable = true;
                 echo Duration::toString($ping[$i] - $ping[$i - 1]) . "\n";
             } else {
                 echo "0 seconds\n";
             }
         }
         // wait 5 seconds and check again. if Last-Modified changes, it is unreliable
         if ($i < $max) {
             sleep(3);
         }
     }
     if ($unreliable) {
         return false;
     }
     return true;
 }