histogram: Make histograms crash less
[ninja.git] / application / controllers / backup.php
blob55d493cd644e400487fd5c779d5ca4e1bce09cf6
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Backup controller
5 * op5, and the op5 logo are trademarks, servicemarks, registered servicemarks
6 * or registered trademarks of op5 AB.
7 * All other trademarks, servicemarks, registered trademarks, and registered
8 * servicemarks mentioned herein may be the property of their respective owner(s).
9 * The information contained herein is provided AS IS with NO WARRANTY OF ANY
10 * KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY, AND FITNESS FOR A
11 * PARTICULAR PURPOSE.
13 class Backup_Controller extends Authenticated_Controller {
14 public $debug = false;
15 public $model = false;
17 private $files2backup;
18 private $asmonitor = '/usr/bin/asmonitor -q ';
19 private $cmd_backup = '/opt/monitor/op5/backup/backup ';
20 private $cmd_restore = '/opt/monitor/op5/backup/restore ';
21 private $cmd_view = 'tar tfz ';
22 private $cmd_verify;
24 private $backup_suffix = '.tar.gz';
25 private $backups_location = '/var/www/html/backup';
27 private $unauthorized = false;
29 public function __construct()
31 parent::__construct();
32 $nagioscfg = System_Model::get_nagios_etc_path()."nagios.cfg";
33 $this->cmd_verify = '/opt/monitor/bin/nagios -v '.$nagioscfg;
34 $this->files2backup = array(
35 System_Model::get_nagios_etc_path().'nagios.cfg',
36 System_Model::get_nagios_etc_path().'cgi.cfg',
37 System_Model::get_nagios_base_path().'/var/*.log',
38 System_Model::get_nagios_base_path().'/var/status.sav',
39 System_Model::get_nagios_base_path().'/var/archives', # Isn't this a config backup?
40 System_Model::get_nagios_base_path().'/var/errors', # Then why would we want these?
41 System_Model::get_nagios_base_path().'/var/traffic',
42 '/etc/op5/*.yml' # :TODO Read value from op5config
45 $backup = array();
46 foreach ($this->files2backup as $path) {
47 foreach (glob($path) as $file) {
48 $backup[] = $file;
51 $this->files2backup = $backup;
53 $this->template->disable_refresh = true;
54 $this->auto_render = true;
55 $this->cmd_reload = 'echo "[{TIME}] RESTART_PROGRAM" >> ' . System_Model::get_pipe();
57 $nagcfg = System_Model::parse_config_file($nagioscfg);
58 foreach (array('cfg_file', 'resource_file', 'cfg_dir') as $interesting_file) {
59 if (!isset($nagcfg[$interesting_file]))
60 continue;
61 $files = $nagcfg[$interesting_file];
62 if (!is_array($files))
63 $files = array($files);
64 foreach ($files as $file) {
65 if ($file[0] !== '/')
66 $file = System_Model::get_nagios_etc_path().$file;
67 $this->files2backup[] = $file;
71 $user = Auth::instance()->get_user();
72 if (!$user->authorized_for('configuration_information') || !$user->authorized_for('system_commands')) {
74 $this->template->content = $this->add_view('unauthorized');
75 $this->template->content->error_message = _("It appears as though you aren't authorized to access the backup interface.");
76 $this->template->content->error_description = _('Read the section of the documentation that deals with authentication and authorization for more information.');
77 $this->unauthorized = true;
81 public function index()
83 if ($this->unauthorized)
84 return;
85 $this->template->content = $this->add_view('backup/list');
86 $this->template->title = _('Configuration » Backup/Restore');
87 $this->template->content->suffix = $this->backup_suffix;
89 $backupfiles = false;
90 foreach (glob($this->backups_location.'/*'.$this->backup_suffix) as $filename) {
91 $backupfiles[] = basename($filename);
94 if ($backupfiles === false)
95 throw new Exception('Cannot get directory contents: ' . $this->backups_location);
97 $this->template->content->files = $backupfiles;
100 public function download($file) {
102 $file_path = $this->backups_location . "/" . $file;
103 $fp = fopen($file_path, "r");
104 if ($fp === false) {
105 $this->template->content = $this->add_view('backup/view');
106 $this->template->message = "Couldn't create filehandle.";
107 return;
109 /* Prevent buffering and rendering */
110 Kohana::close_buffers(FALSE);
111 $this->auto_render = false;
112 $hs = headers_sent();
114 header('Content-Description: File Transfer');
115 header("Content-Type: application/octet-stream");
116 header("Content-Disposition: attachment; filename=".$file.".tar.gz");
117 header("Content-Transfer-Encoding:binary");
118 header('Expires: 0');
119 header('Cache-Control: must-revalidate');
120 header('Pragma: public');
121 header('Content-Length: ' . filesize($file_path));
122 fpassthru($fp);
123 fclose($fp);
126 public function view($file)
128 if ($this->unauthorized)
129 return;
131 $this->template->content = $this->add_view('backup/view');
132 $this->template->title = _('Configuration » Backup/Restore » View');
133 $this->template->content->backup = $file;
135 $contents = array();
136 $status = 0;
137 exec($this->cmd_view . $this->backups_location . '/' . $file, $contents, $status);
138 sort($contents);
140 $this->template->content->files = $contents;
143 public function verify()
145 if ($this->unauthorized)
146 return;
148 $this->template = $this->add_view('backup/verify');
150 $output = array();
151 exec($this->asmonitor . $this->cmd_verify, $output, $status);
152 if ($status != 0)
154 $this->template->status = false;
155 $this->template->message = "The current configuration is invalid";
156 $this->debug = implode("\n", $output);
158 else
160 $this->template->status = true;
161 $this->template->message = "The current configuration is valid. Creating a backup...";
165 public function backup()
167 if ($this->unauthorized)
168 return;
170 $this->template = $this->add_view('backup/backup');
172 $file = strftime('backup-%Y-%m-%d_%H.%M.%S');
173 exec($this->asmonitor . $this->cmd_backup . $this->backups_location . '/' . $file . $this->backup_suffix
174 . ' ' . implode(' ', $this->files2backup), $output, $status);
175 if ($status != 0)
177 $this->template->status = false;
178 $this->template->file = '';
179 $this->template->message = "Could not backup the current configuration";
180 $this->debug = implode("\n", $output);
182 else
184 $this->template->status = true;
185 $this->template->file = $file;
186 $this->template->message = "A backup of the current configuration has been created";
190 public function restore($file)
192 if ($this->unauthorized)
193 return;
195 $this->template = $this->add_view('backup/restore');
196 $this->template->status = false;
198 $status = 0;
199 $output = array();
200 exec($this->asmonitor . $this->cmd_restore . $this->backups_location . '/' . $file, $output, $status);
201 if ($status != 0)
203 $this->template->message = "Could not restore the configuration '{$file}'";
204 $this->debug = implode("\n", $output);
205 return;
208 exec($this->asmonitor . $this->cmd_verify, $output, $status);
209 if ($status != 0)
211 $this->template->message = "The configuration '{$file}' has been restored but seems to be invalid";
212 $this->debug = implode("\n", $output);
213 return;
216 $time = time();
217 $this->cmd_reload = str_replace('{TIME}', $time , $this->cmd_reload);
219 exec($this->cmd_reload, $output, $status);
220 if ($status != 0) {
221 $this->template->message = "Could not reload the configuration '{$file}'";
222 $this->debug = implode("\n", $output);
224 else
226 $this->template->status = true;
227 $this->template->message = "The configuration '{$file}' has been restored";
228 foreach($this->files2backup as $onefile){
229 $onefile = trim($onefile);
230 if(pathinfo($onefile, PATHINFO_EXTENSION) === "cfg") {
231 if(file_exists($onefile) && is_writable($onefile)) {
232 touch($onefile);
237 return;
240 public function delete($file)
242 if ($this->unauthorized)
243 return;
245 $this->template = $this->add_view('backup/delete');
247 $this->template->status = @unlink($this->backups_location . '/' . $file);
248 $this->template->message = $this->template->status ? "The backup '{$file}' has been deleted"
249 : "Could not delete the backup '{$file}'";