1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
2 require_once('op5/auth/Auth.php');
3 require_once('op5/auth/User_AlwaysAuth.php');
5 * CLI controller for command line access to Ninja
7 * op5, and the op5 logo are trademarks, servicemarks, registered servicemarks
8 * or registered trademarks of op5 AB.
9 * All other trademarks, servicemarks, registered trademarks, and registered
10 * servicemarks mentioned herein may be the property of their respective owner(s).
11 * The information contained herein is provided AS IS with NO WARRANTY OF ANY
12 * KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY, AND FITNESS FOR A
15 class Cli_Controller
extends Controller
{
17 public function __construct()
19 # Only grant permission for cli access or if
20 # user has been given the ADMIN role
21 if (PHP_SAPI
!== "cli" &&
22 !Auth
::instance()->logged_in(Ninja_Controller
::ADMIN
)) {
23 return url
::redirect('default/index');
25 parent
::__construct();
26 $this->auto_render
= false;
27 $op5_auth = Op5Auth
::factory(array('session_key' => false));
28 $op5_auth->force_user(new Op5User_AlwaysAuth());
32 * Parse input data from commandline and stores in an array
33 * An equivalent to getopt() but easier for us in this environment
35 private function _parse_parameters($noopt = array())
38 $params = $GLOBALS['argv'];
39 // could use getopt() here (since PHP 5.3.0), but it doesn't work relyingly
41 while (list($tmp, $p) = each($params)) {
43 $pname = substr($p, 1);
45 if ($pname{0} == '-') {
46 // long-opt (--<param>)
47 $pname = substr($pname, 1);
48 if (strpos($p, '=') !== false) {
49 // value specified inline (--<param>=<value>)
50 list($pname, $value) = explode('=', substr($p, 2), 2);
53 // check if next parameter is a descriptor or a value
54 $nextparm = current($params);
55 if (!in_array($pname, $noopt) && $value === true && $nextparm !== false && $nextparm{0} != '-')
56 list($tmp, $value) = each($params);
57 $result[$pname] = $value;
59 // param doesn't belong to any option
66 private function _handle_nacoma_trigger($type, $old_name, $new_name = null) {
67 if (PHP_SAPI
!== "cli") {
68 die("illegal call\n");
70 $this->auto_render
=false;
71 $cli_access = Kohana
::config('config.cli_access');
73 if (empty($cli_access)) {
74 echo "no cli access, it's turned off in config/config.php\n";
78 # figure out path from argv
79 $path = $GLOBALS['argv'][0];
81 $report_types = array('avail', 'sla', 'summary');
82 foreach ($report_types as $report_type) {
83 $obj = Report_options
::setup_options_obj($report_type);
84 $reports = $obj->get_all_saved();
85 foreach ($reports as $report) {
86 $report_data = Report_options
::setup_options_obj($report_type, $report->id
);
87 if ($report_data['report_type'] === 'services' && $type === 'host') {
89 foreach ($report_data['objects'] as $idx => $name) {
90 foreach ($report_data['objects'] as $idx => $svc) {
91 $parts = explode(';', $svc);
92 if ($parts[0] === $old_name) {
95 $report_data['objects'][$idx] = $new_name.';'.$parts[1];
98 unset($report_data['objects'][$idx]);
105 $report_data->save();
108 if ($report_data['report_type'] != $type . 's') {
111 $key = array_search($old_name, $report_data['objects']);
112 if ($key === false) {
117 $report_data['objects'][$key] = $new_name;
120 unset($report_data['objects'][$key]);
122 $report_data->save();
128 * When an object is renamed, things like scheduled reports and rrdtool data must be renamed as well
130 * @param $type string
131 * @param $old_name string
132 * @param $new_name string
134 public function handle_rename($type, $old_name, $new_name)
136 return $this->_handle_nacoma_trigger($type, $old_name, $new_name);
140 * Perform post-deletion cleanup
142 * @param $type string
143 * @param $old_name string
145 public function handle_deletion($type, $old_name)
147 return $this->_handle_nacoma_trigger($type, $old_name);
150 public function save_widget()
152 if (PHP_SAPI
!== "cli") {
153 die("illegal call\n");
156 $this->auto_render
=false;
157 $cli_access = Kohana
::config('config.cli_access');
159 if (empty($cli_access)) {
160 # CLI access is turned off in config/config.php
161 echo "no cli access\n";
165 $params = $this->_parse_parameters();
166 if (!isset($params['page']) ||
!isset($params['name']) ||
!isset($params['friendly_name']))
167 die("Usage: {$params[0]} {$params[1]} --page <page> --name <name> --friendly_name <friendly_name>\n");
169 Ninja_widget_Model
::install($params['page'], $params['name'], $params['friendly_name']);
172 public function rename_widget()
174 if (PHP_SAPI
!== "cli") {
175 die("illegal call\n");
177 $this->auto_render
=false;
178 $cli_access = Kohana
::config('config.cli_access');
180 if (empty($cli_access)) {
181 # CLI access is turned off in config/config.php
182 echo "no cli access\n";
186 $params = $this->_parse_parameters();
187 if (!isset($params['from']) ||
!isset($params['to']))
188 die("Usage: {$params[0]} {$params[1]} --from <old_name> --to <new_name>\n");
190 Ninja_widget_Model
::rename_widget($params['from'], $params['to']);
193 public function rename_friendly_widget()
195 if (PHP_SAPI
!== "cli") {
196 die("illegal call\n");
198 $this->auto_render
=false;
199 $cli_access = Kohana
::config('config.cli_access');
201 if (empty($cli_access)) {
202 # CLI access is turned off in config/config.php
203 echo "no cli access\n";
207 $params = $this->_parse_parameters();
208 if (!isset($params['from']) ||
!isset($params['to']))
209 die("Usage: {$params[0]} {$params[1]} --from <old_name> --to <new_name>\n");
211 Ninja_widget_Model
::rename_friendly_widget($params['from'], $params['to']);
215 * Migrate avail < 10 = ninja < 2.1 = monitor < 6.0 where the meaning
216 * of host/service filter status is inverted
218 public function upgrade_excluded()
221 if (PHP_SAPI
!== 'cli') {
222 die("illegal call\n");
224 $this->auto_render
=false;
225 $cli_access = Kohana
::config('config.cli_access');
227 if (empty($cli_access)) {
228 # CLI access is turned off in config/config.php
229 echo "no cli access\n";
233 $db = Database
::instance();
234 $res = $db->query('SELECT version FROM avail_db_version');
235 if ($res->current()->version
>= 10)
236 return; // already upgraded
238 $reports = $db->query('SELECT id, host_filter_status, service_filter_status FROM avail_config');
239 foreach ($reports as $report) {
240 $host_filter_status = @unserialize
($report->host_filter_status
);
241 $service_filter_status = @unserialize
($report->service_filter_status
);
243 if (!is_array($host_filter_status)) {
244 $host_filter_status = array();
246 $new_filter_status = array();
247 foreach (Reports_Model
::$host_states as $id => $name) {
248 if ($name == 'pending')
249 $name = 'undetermined';
250 # we need to replace the name with the id, and invert which ones are set
251 if ($id == Reports_Model
::HOST_EXCLUDED ||
isset($host_filter_status[$name]))
253 $new_filter_status[$id] = 0;
255 $host_filter_status = $new_filter_status;
258 if (!is_array($service_filter_status)) {
259 $service_filter_status = array();
261 $new_filter_status = array();
262 foreach (Reports_Model
::$service_states as $id => $name) {
263 # we need to replace the name with the id, and invert which ones are set
264 if ($id == Reports_Model
::SERVICE_EXCLUDED ||
isset($service_filter_status[$name]))
266 $new_filter_status[$id] = 0;
268 $service_filter_status = $new_filter_status;
270 $reports = $db->query('UPDATE avail_config SET host_filter_status = '.$db->escape(serialize($host_filter_status)).', service_filter_status = '.$db->escape(serialize($service_filter_status)).' WHERE id = '.$db->escape($report->id
));
275 * Migrate auth for ninja < 2.1 = monitor < 6.0 to op5lib backed auth
277 public function upgrade_auth()
279 $cfg = Op5Config
::instance();
280 $users = $cfg->getConfig('auth_users');
281 $groups = $cfg->getConfig('auth_groups');
282 $db = Database
::instance();
283 $res = $db->query('SELECT username, realname, email, password, password_algo, system_information, configuration_information,
284 system_commands, all_services, all_hosts, all_service_commands, all_host_commands
285 FROM users LEFT JOIN ninja_user_authorization ON users.id = ninja_user_authorization.user_id');
286 foreach ($res->result(false) as $row) {
287 $username = $row['username'];
288 if (isset($users[$username]))
289 $user = $users[$username];
293 foreach (array('username', 'realname', 'email', 'password', 'password_algo') as $param) {
294 if (!isset($user[$param]))
295 $user[$param] = $row[$param];
298 $levels = array_filter(array_keys($row), function($arg) use ($row) {return (bool)$row[$arg];});
299 if (empty($levels)) {
300 // no levels, no group, no action
301 } else if (count($levels) === count($row)) {
302 // all levels, superuser
303 $user['groups'] = array('admins');
305 if (isset($groups['user_'.$username]))
306 $group = $groups['user_'.$username];
309 $group = array_merge($group, Op5Authorization
::nagios_rights_to_op5auth($levels));
310 $groups['user_'.$username] = $group;
311 if (!isset($user['groups']))
312 $user['groups'] = array();
313 $user['groups'][] = 'user_'.$username;
314 $user['groups'] = array_unique($user['groups']);
317 $users[$username] = $user;
320 @exec
('cp -p /etc/op5/auth_users.yml /etc/op5/auth_users.yml.' . $now . ' 2> /dev/null');
321 @exec
('cp -p /etc/op5/auth_groups.yml /etc/op5/auth_groups.yml.' . $now . ' 2> /dev/null');
323 $cfg->setConfig('auth_users', $users);
324 $cfg->setConfig('auth_groups', $groups);
327 public function upgrade_recurring_downtime()
329 $db = Database
::instance();
330 $res = $db->query('SELECT * FROM recurring_downtime');
332 'hosts' => 'host_name',
333 'services' => 'service_description',
334 'hostgroups' => 'hostgroup',
335 'servicegroups' => 'servicegroup'
337 foreach ($res->result(false) as $row) {
338 if ($row['start_time'])
339 continue; // already migrated
340 $data = i18n
::unserialize($row['data']);
341 $data['start_time'] = $data['time'];
342 $data['end_time'] = ScheduleDate_Model
::time_to_seconds($data['time']) + ScheduleDate_Model
::time_to_seconds($data['duration']);
343 $data['end_time'] = ($data['end_time'] / 3600 %
24) . ':' . ($data['end_time'] / 60 %
60) . ':' +
($data['end_time'] %
60);
344 $data['weekdays'] = $data['recurring_day'];
345 $data['months'] = $data['recurring_month'];
346 $data['downtime_type'] = $data['report_type'];
347 $data['objects'] = $data[$report[$data['report_type']]];
348 $data['author'] = $row['author'];
349 $data['comment'] = $row['comment'];
350 $sd = new ScheduleDate_Model();
351 $sd->edit_schedule($data, $row['id']);
355 public function license_start() {
356 $row = Database
::instance()->query("SELECT MIN(timestamp) from report_data");
361 $value = $row->result(false)->current();
362 echo $value['timestamp'];