Example #1
0
function get_report_data($from, $to, $report_type, $report_checks = array())
{
    global $tool, $form;
    // Generating reports can take a while
    // Increase time out
    set_time_limit(1500);
    // We also need to increase the memory limit see below
    // Different report types
    // * one_down_all_down ie worst case
    // * one_up_all_up     ie best case
    // * summary
    if ($report_type != 'one_down_all_down' && $report_type != 'one_up_all_up') {
        $report_type = 'summary';
    }
    // Check From date
    if (is_numeric($from) && date($from)) {
        $start_stamp = date($from);
    } elseif ($from != '' && strtotime($from)) {
        $start_stamp = strtotime($from);
    } else {
        $form->warning("Invalid start date {$from}");
        return false;
    }
    // Check To date
    if (is_numeric($to) && date($to)) {
        $end_stamp = date($to);
    } elseif ($to != '' && strtotime($to)) {
        $end_stamp = strtotime($to);
    } else {
        $form->warning("Invalid End date {$to}");
        return false;
    }
    if ($start_stamp > $end_stamp) {
        $form->warning("Error: End date is before Start date {$start_stamp} -- {$end_stamp}");
        return false;
    }
    // print "Generate report From date ". date("Y md",$start_stamp) ." To date ". date("Y md",$end_stamp)." Total secs = $total_secs<br>";
    // Get all events
    // Build time line
    $timers = array();
    $timers[ok] = 0;
    $timers[warning] = 0;
    $timers[critical] = 0;
    $timers[unknown] = 0;
    $timers[other] = 0;
    $aggr_timers = array();
    $aggr_timers[0] = 0;
    $aggr_timers[1] = 0;
    $aggr_timers[2] = 0;
    $aggr_timers[3] = 0;
    $aggr_timers[4] = 0;
    $work_time = $start_stamp;
    // Create a loop per 24 hours, otherwise too much memory usage
    $step = 24 * 60 * 60;
    $run_counter = $start_stamp;
    $run = 0;
    //print "will loop from $start_stamp to $end_stamp with increments of $step <br>";
    while ($run_counter < $end_stamp) {
        $run++;
        $run_start = $run_counter;
        $run_end = $run_start + $step - 1;
        if ($run_end > $end_stamp) {
            //print "rewrote run end from $run_end to end time $end_stamp<br>";
            $run_end = $end_stamp;
        }
        $run_counter = $run_counter + $step;
        $all_checks = array();
        //print "<br>working on run $run counter is $run_counter  $run_start - $run_end<br>" ;
        // 1 create a hash of all time stamps and their status values for each check
        // this is stored in a 2 dimensional array called all_checks;
        foreach ($report_checks as $my_check_id) {
            unset($events);
            //$events = Event::get_events_for_checks($my_check_id,$start_stamp,$end_stamp);
            $events = Event::get_events_for_checks($my_check_id, $run_start, $run_end);
            foreach ($events as $event_id => $status) {
                $event = new Event($event_id);
                // Check if event start is before our check time, if so set start to,
                // begin of check period. Otherwise we do unnecesary loops.. waste of resource
                $start_sec = strtotime($event->get_insert_date());
                if ($start_sec < $run_start) {
                    $start_sec = $run_start;
                }
                // Same for end
                $end_sec = strtotime($event->get_last_updated());
                if ($end_sec > $run_end) {
                    //print "set end from $end_sec to $end_stamp<br>";
                    $end_sec = $run_end;
                }
                for ($counter = $start_sec; $counter <= $end_sec; $counter += 1) {
                    $all_checks[$my_check_id][$counter] = $event->get_status();
                }
                unset($counter);
            }
        }
        // Now that we have all status values for all seconds between start and end
        // we'll generate the report result;
        // comparison is done based on the report type
        // Loop though all secs and compare status values
        for ($counter = $run_start; $counter <= $run_end; $counter += 1) {
            // If no value is found we set status to 4, ie no data.
            if ($report_type == 'one_down_all_down') {
                $status = 4;
                foreach ($report_checks as $my_check_id) {
                    if (!isset($all_checks[$my_check_id][$counter])) {
                        if (!($status >= 0 && $status < 4)) {
                            $status = 4;
                        }
                    } elseif ($all_checks[$my_check_id][$counter] == 2) {
                        $status = 2;
                    } elseif ($all_checks[$my_check_id][$counter] == 1) {
                        // Wanrning over rules all but error (2)
                        if ($status != 2) {
                            $status = 1;
                        }
                    } elseif ($all_checks[$my_check_id][$counter] == 0) {
                        // OK only over rules unknown and no data
                        if ($status != 2 && $status != 1) {
                            $status = 0;
                        }
                    } elseif ($all_checks[$my_check_id][$counter] == 3) {
                        // Only set to unknown (3) if all of them are unknown.
                        if ($status != 0 && $status != 1 && $status != 2) {
                            $status = 3;
                        }
                    } elseif (!($status >= 0 && $status < 4)) {
                        // In case there's no data
                        $status = 4;
                    }
                }
                $aggr_timers[$status] += 1;
            } elseif ($report_type == 'one_up_all_up') {
                $status = 4;
                foreach ($report_checks as $my_check_id) {
                    if (!isset($all_checks[$my_check_id][$counter])) {
                        if (!($status >= 0 && $status < 4)) {
                            $status = 4;
                        }
                    } elseif ($all_checks[$my_check_id][$counter] == 0) {
                        $status = 0;
                    } elseif ($all_checks[$my_check_id][$counter] == 1) {
                        // Warning only overrules Critical and unknown
                        if ($status != 0) {
                            $status = 1;
                        }
                    } elseif ($all_checks[$my_check_id][$counter] == 2) {
                        // critical only overrules unknown and no data
                        if ($status != 0 && $status != 1) {
                            $status = 2;
                        }
                    } elseif ($all_checks[$my_check_id][$counter] == 3) {
                        // Only set to unknown (3) if all of them are unknown.
                        if ($status != 0 && $status != 1 && $status != 2) {
                            $status = 3;
                        }
                    } elseif (!($status >= 0 && $status < 4)) {
                        $status = 4;
                    }
                }
                $aggr_timers[$status] += 1;
            } elseif ($report_type == 'summary') {
                // Summary uses all samples.
                $status = 4;
                foreach ($report_checks as $my_check_id) {
                    if (!isset($all_checks[$my_check_id][$counter])) {
                        if (!($status >= 0 && $status < 4)) {
                            $status = 4;
                        }
                    } elseif ($all_checks[$my_check_id][$counter] >= 0 && $all_checks[$my_check_id][$counter] <= 3) {
                        $status = $all_checks[$my_check_id][$counter];
                    } else {
                        $status = 4;
                    }
                    // Note that this is in the foreach loop
                    // As we want to add all samples;
                    $aggr_timers[$status] += 1;
                }
            } else {
                print "Invalid report type {$report_type}<br>";
                break;
            }
        }
        /** 
        		 if ($report_type == 'summary') {
        			// If it's a summary we'll need to divide the results by the number of checks
        			// that are in	this report
        			$num_checks = count($report_checks);
        			foreach ($aggr_timers as $status => $secs) {
        				// print "Number of Checks: $num_checks<br>";
        				// print "Number of Seconds for Status $status: $secs<br>";
        				$aggr_timers[$status] = $secs/$num_checks;
        				// print "aggr_timers[status]: $aggr_timers[$status] <br>";
        			}
        		} */
    }
    if ($report_type == 'summary') {
        // If it's a summary we'll need to divide the results by the number of checks
        // that are in	this report
        $num_checks = count($report_checks);
        foreach ($aggr_timers as $status => $secs) {
            // print "Number of Checks: $num_checks<br>";
            // print "Number of Seconds for Status $status: $secs<br>";
            $aggr_timers[$status] = $secs / $num_checks;
            // print "aggr_timers[status]: $aggr_timers[$status] <br>";
        }
    }
    $timers[ok] += $aggr_timers[0];
    $timers[warning] += $aggr_timers[1];
    $timers[critical] += $aggr_timers[2];
    $timers[unknown] += $aggr_timers[3];
    $timers[no_data] += $aggr_timers[4];
    return $timers;
}