sla: Stupid Rasmus and his needles and haystacks
[ninja.git] / application / models / scheduling_queue.php
blobe200680501d2749459a97f7da3cd33da592e4503
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * Handle comments for hosts and services
5 */
6 class Scheduling_queue_Model extends Model {
8 private $limit = 1000;
9 private $offset = 0;
11 /**
12 * Sets how many objects show_schduling_queue should return
14 * @param $limit
15 * @param $offset
17 public function set_range( $limit, $offset ) {
18 $this->limit = $limit;
19 $this->offset = $offset;
22 /**
23 * Fetch scheduled events
25 * @param $service_filter string = null
26 * @param $host_filter string = null
27 * @return Database result object or false if none if $count is false or unset, otherwise the number of result rows
29 public function show_scheduling_queue($service_filter = null, $host_filter = null)
31 $result = array();
32 $ls = Livestatus::instance();
34 $max_objects = $this->limit + $this->offset; /* At most object needed to be fetched */
36 $service_options = array(
37 'columns' => array(
38 'host_name',
39 'description',
40 'last_check',
41 'next_check',
42 'check_type', // 0 == active, 1 == passive
43 'active_checks_enabled'
45 'filter' => array( 'should_be_scheduled' => 1 ),
46 'limit' => $max_objects,
47 'order' => array( 'next_check' => 'asc' )
50 if($service_filter) {
51 $service_options['filter']['description'] = array("~~" => ".*$service_filter.*");
53 if($host_filter) {
54 $service_options['filter']['host_name'] = array("~~" => ".*$host_filter.*");
56 $service_checks = $ls->getServices($service_options);
58 $host_options = $service_options;
60 $host_options['columns'] = array(
61 'name',
62 'last_check',
63 'next_check',
64 'check_type', // 0 == active, 1 == passive
65 'active_checks_enabled'
67 $host_options['filter'] = array(
68 'filter' => array( 'should_be_scheduled' => 1 )
71 if($host_filter) {
72 $host_options['filter']['host_name'] = array("~~" => ".*$host_filter.*");
74 $host_checks = $ls->getHosts($host_options);
75 if(!$host_checks && !$service_checks) {
76 return array();
79 /* Do merge */
80 $host_ptr = 0;
81 $service_ptr = 0;
82 $output = array();
83 for( $i=0; $i<$this->limit + $this->offset; $i++ ) {
84 $host = isset($host_checks[$host_ptr]) ? $host_checks[$host_ptr] : false;
85 $service = isset($service_checks[$service_ptr]) ? $service_checks[$service_ptr] : false;
87 /* No more objects */
88 if( $host === false && $service === false ) break;
90 if( $host === false || ($service !== false && $host['next_check'] > $service['next_check'] )) {
91 /* Service */
92 if( $i >= $this->offset ) {
93 $output[] = (object)$service;
95 $service_ptr++;
96 } else {
97 /* Host */
98 if( $i >= $this->offset )
99 $output[] = (object)$host;
100 $host_ptr++;
104 return $output;