root/trunk/controllers/services.php @ 112

Revision 112, 9.1 KB (checked in by divagater, 5 years ago)

Various updates

Line 
1<?php
2/**
3 * Services controller class
4 *
5 * This is the access point to the npc_services table.
6 *
7 * @filesource
8 * @author              Billy Gunn <billy@gunn.org>
9 * @copyright           Copyright (c) 2007
10 * @link                http://trac2.assembla.com/npc
11 * @package             npc
12 * @subpackage          npc.controllers
13 * @since               NPC 2.0
14 * @version             $Id: $
15 */
16
17require_once($config["base_path"]."/plugins/npc/controllers/comments.php");
18
19/**
20 * Services controller class
21 *
22 * Services controller provides functionality, such as building the
23 * Doctrine querys and formatting output.
24 *
25 * @package     npc
26 * @subpackage  npc.controllers
27 */
28class NpcServicesController extends Controller {
29
30    /**
31     * getServices
32     *
33     * Gets and formats services for output.
34     *
35     * @return string   json output
36     */
37    function getServices() {
38
39        $results = $this->services();
40
41        $comments = new NpcCommentsController;
42
43        $services = $this->flattenArray($results);
44
45        for ($i = 0; $i < count($services); $i++) {
46            if ($services[$i]['problem_has_been_acknowledged']) {
47                $services[$i]['acknowledgement'] = $comments->getAck($services[$i]['service_object_id']);
48            }
49            // Add the last comment to the array
50            $services[$i]['comment'] = $comments->getLastComment($services[$i]['service_object_id']);
51        }   
52
53        return($this->jsonOutput($services));
54    }
55
56    /**
57     * getStateInfo
58     *
59     * Gets and formats service state information
60     *
61     * @return string   json output
62     */
63    function getStateInfo() {
64
65        $fields = array(
66            'current_state',
67            'output',
68            'perfdata',
69            'last_state_change',
70            'check_command',
71            'command_line',
72            'current_check_attempt',
73            'last_check',
74            'next_check',
75            'event_handler',
76            'latency',
77            'execution_time',
78            'is_flapping',
79            'scheduled_downtime_depth',
80            'process_performance_data',
81            'active_checks_enabled',
82            'passive_checks_enabled',
83            'event_handler_enabled',
84            'flap_detection_enabled',
85            'notifications_enabled',
86            'obsess_over_service'
87        );
88
89        $service = $this->services();
90
91        $results = $this->flattenArray($service);
92
93        $x = 0;
94        foreach ($fields as $key) {
95            $output[$x] = array('name' => $this->columnAlias[$key], 'value' => $this->formatStateInfo($key, $results[0]));
96            $x++;
97        }
98
99        return($this->jsonOutput($output));
100    }
101
102    /**
103     * summary
104     *
105     * Returns a summary of the state of all services.
106     *
107     * @return string   json output
108     */
109    function summary() {
110
111        $status = array('critical' => 0,
112                        'warning'  => 0,
113                        'unknown'  => 0,
114                        'ok'       => 0,
115                        'pending'  => 0);
116
117        $q = new Doctrine_Query();
118        $services = $q->from('NpcServices s')->where('s.config_type = 1')->execute();
119
120        foreach($services as $service) {
121            $status[$this->serviceState[$service->Status->current_state]]++;
122        }
123
124        return($this->jsonOutput($status));
125    }
126
127    /**
128     * getServiceStatesByHost
129     *
130     * A utility method to simply return the state of every service belonging
131     * to the specified host.
132     *
133     * @return array  list of all services with status
134     */
135    function getServiceStatesByHost($host_object_id) {
136
137        $q = new Doctrine_Query();
138        $q->select('ss.current_state')
139          ->from('NpcServicestatus ss, NpcServices s')
140          ->where('ss.service_object_id = s.service_object_id AND s.host_object_id = ?', $host_object_id);
141
142        $results = $q->execute(array(), Doctrine::FETCH_ARRAY);
143
144        return($results);
145    }
146
147    /**
148     * services
149     *
150     * Retrieves all services along with status information
151     *
152     * @return array  list of all services with status
153     */
154    function services($id=null, $where='') {
155
156        // Maps searchable fields passed in from the client
157        $fieldMap = array('service_description' => 'o.name2',
158                          'host_name' => 'o.name1',
159                          'output' => 'ss.output');
160
161
162        // Build the where clause
163        if ($where != '') {
164            $where .= ' AND ';
165        }
166
167        $where .= ' s.config_type = 1 ';
168
169        if ($this->id || $id) {
170            $where .= sprintf(" AND s.service_object_id = %d", is_null($id) ? $this->id : $id);;
171        }
172
173        $where .= " AND ss.current_state in (" . $this->stringToState[$this->state] . ") ";
174
175        if ($this->searchString) {
176            $where = $this->searchClause($where, $fieldMap);   
177        }
178
179        $q = new Doctrine_Pager(
180            Doctrine_Query::create()
181                ->select('i.instance_name,'
182                        .'s.host_object_id,'
183                        .'o.name1 AS host_name,'
184                        .'o.name2 AS service_description,'
185                        .'ss.*')
186                ->from('NpcServicestatus ss')
187                ->leftJoin('ss.Object o')
188                ->leftJoin('ss.Service s')
189                ->leftJoin('ss.Instance i')
190                ->where("$where")
191                ->orderby( 'i.instance_name ASC, host_name ASC, service_description ASC' ),
192            $this->currentPage,
193            $this->limit
194        );
195               
196        $services = $q->execute(array(), Doctrine::FETCH_ARRAY);
197
198        // Set the total number of records
199        $this->numRecords = $q->getNumResults();
200
201        return($services);
202    }
203
204    function getPerfData($id=null) {
205
206        $id = $this->id ? $this->id : $id;
207
208        $q = new Doctrine_Pager(
209            Doctrine_Query::create()
210                ->select('n.*')
211                ->from('NpcServicechecks n, NpcServices n2')
212                  ->where('n.service_object_id = ? AND n2.service_object_id = n.service_object_id AND n.start_time'
213                       .' > now() - INTERVAL n2.check_interval * 2 MINUTE', $id)
214                  ->orderby('n.start_time DESC'), 0, 1);
215
216        return($q->execute(array(), Doctrine::FETCH_ARRAY));
217    }
218
219    /**
220     * listServivcesCli
221     *
222     * Returns all services and associated object ID's
223     *
224     * @return array   Array of services/id's
225     */
226    function listServicesCli() {
227
228        $q = new Doctrine_Query();
229        $q->select('display_name as name, service_object_id as id')->from('NpcServices')->orderBy('display_name ASC');
230
231        return($q->execute(array(), Doctrine::FETCH_ARRAY));
232    }
233
234    /**
235     * formatStateInfo
236     *
237     * Formats the service state info results for display.
238     * This is a workaround for some of the limitations of
239     * EXT property grid.
240     *
241     * @return string   The formatted results
242     */
243    function formatStateInfo($key, $results) {
244
245        // Set the default return value
246        $return = $results[$key];
247
248        $cs = array(
249            '0'  => '<img ext:qtip="OK" src="images/icons/greendot.gif">',
250            '1'  => '<img ext:qtip="WARNING" src="images/icons/yellowdot.gif">',
251            '2'  => '<img ext:qtip="CRITICAL" src="images/icons/reddot.gif">',
252            '3'  => '<img ext:qtip="UNKNOWN" src="images/icons/orangedot.gif">',
253            '-1' => '<img ext:qtip="PENDING" src="images/icons/bluedot.gif">'
254        );
255
256        if ($key == 'current_state') {
257            $return = $cs[$results[$key]];
258            if ($results['problem_has_been_acknowledged']) {
259                $comments = new NpcCommentsController;
260                $string = $comments->getAck($results['service_object_id']);
261                $ack = preg_split("/\*\|\*/", $string);
262                $return = '<pre>' . $return . '   (Acknowledged by ' . $ack[0] . ')</pre>';
263            }
264        }
265
266        if ($key == 'current_check_attempt') {
267            $return = $results[$key] . '/' . $results['max_check_attempts'];
268        }
269
270        if (preg_match("/_enabled/", $key) || $key == 'obsess_over_service') {
271            if($results[$key]) {
272                $return = '<img src="images/icons/tick.png">';
273            } else {
274                $return = '<img src="images/icons/cross.png">';
275            }
276        }
277
278        if ($key == 'last_state_change' || $key == 'last_check' || $key == 'next_check') {
279            $format = read_config_option('npc_date_format') . ' ' . read_config_option('npc_time_format');
280            $return = date($format, strtotime($results[$key]));
281        }
282
283        if ($key == 'scheduled_downtime_depth' || $key == 'is_flapping' || $key == 'process_performance_data') {
284            if ($results[$key]) {
285                $return = 'Yes';
286            } else {
287                $return = 'No';
288            }
289        }
290
291        // Add the full command as a tooltip
292        if ($key == 'command_line') {
293            $perf = $this->getPerfData($results['service_object_id']);
294            $return = $perf[0]['command_line'];
295        }
296
297        if ($return == '' || !$return) {
298            $return = 'NA';
299        }
300
301        return($return);
302    }
303}
304
305
306
Note: See TracBrowser for help on using the browser.