1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
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
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 ';
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
46 foreach ($this->files2backup
as $path) {
47 foreach (glob($path) as $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]))
61 $files = $nagcfg[$interesting_file];
62 if (!is_array($files))
63 $files = array($files);
64 foreach ($files as $file) {
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
)
85 $this->template
->content
= $this->add_view('backup/list');
86 $this->template
->title
= _('Configuration » Backup/Restore');
87 $this->template
->content
->suffix
= $this->backup_suffix
;
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
->toolbar
= new Toolbar_Controller( _( "Backup/Restore" ) );
99 $link = '<a id="verify" href="' . url
::base() . 'index.php/backup/verify/">%s %s</a>';
100 $link = sprintf( $link,
101 html
::image( $this->add_path('/icons/16x16/backup.png'), array('alt' => _('Save your current Monitor configuration'), 'title' => _('Save your current Monitor configuration'), 'style' => 'margin-bottom: -3px')),
102 _('Save your current op5 Monitor configuration')
105 $this->template
->toolbar
->info( $link );
107 $this->template
->content
->files
= $backupfiles;
110 public function download($file) {
112 $file_path = $this->backups_location
. "/" . $file;
113 $fp = fopen($file_path, "r");
115 $this->template
->content
= $this->add_view('backup/view');
116 $this->template
->message
= "Couldn't create filehandle.";
119 /* Prevent buffering and rendering */
120 Kohana
::close_buffers(FALSE);
121 $this->auto_render
= false;
122 $hs = headers_sent();
124 header('Content-Description: File Transfer');
125 header("Content-Type: application/octet-stream");
126 header("Content-Disposition: attachment; filename=".$file.".tar.gz");
127 header("Content-Transfer-Encoding:binary");
128 header('Expires: 0');
129 header('Cache-Control: must-revalidate');
130 header('Pragma: public');
131 header('Content-Length: ' . filesize($file_path));
136 public function view($file)
138 if ($this->unauthorized
)
141 $this->template
->content
= $this->add_view('backup/view');
142 $this->template
->title
= _('Configuration » Backup/Restore » View');
143 $this->template
->content
->backup
= $file;
145 $this->template
->toolbar
= new Toolbar_Controller( _( "Backup/Restore" ), $file );
147 $this->template
->toolbar
->info(
148 '<a href="' . url
::base() . 'index.php/backup" title="' . _( "Backup/Restore" ) . '">' . _( "Backup/Restore List" ) . '</a>'
153 exec($this->cmd_view
. $this->backups_location
. '/' . $file, $contents, $status);
156 $this->template
->content
->files
= $contents;
159 public function verify()
161 if ($this->unauthorized
)
164 $this->template
= $this->add_view('backup/verify');
167 exec($this->asmonitor
. $this->cmd_verify
, $output, $status);
170 $this->template
->status
= false;
171 $this->template
->message
= "The current configuration is invalid";
172 $this->debug
= implode("\n", $output);
176 $this->template
->status
= true;
177 $this->template
->message
= "The current configuration is valid. Creating a backup...";
181 public function backup()
183 if ($this->unauthorized
)
186 $this->template
= $this->add_view('backup/backup');
188 $file = strftime('backup-%Y-%m-%d_%H.%M.%S');
189 exec($this->asmonitor
. $this->cmd_backup
. $this->backups_location
. '/' . $file . $this->backup_suffix
190 . ' ' . implode(' ', $this->files2backup
), $output, $status);
193 $this->template
->status
= false;
194 $this->template
->file
= '';
195 $this->template
->message
= "Could not backup the current configuration";
196 $this->debug
= implode("\n", $output);
200 $this->template
->status
= true;
201 $this->template
->file
= $file;
202 $this->template
->message
= "A backup of the current configuration has been created";
206 public function restore($file)
208 if ($this->unauthorized
)
211 $this->template
= $this->add_view('backup/restore');
212 $this->template
->status
= false;
216 exec($this->asmonitor
. $this->cmd_restore
. $this->backups_location
. '/' . $file, $output, $status);
219 $this->template
->message
= "Could not restore the configuration '{$file}'";
220 $this->debug
= implode("\n", $output);
224 exec($this->asmonitor
. $this->cmd_verify
, $output, $status);
227 $this->template
->message
= "The configuration '{$file}' has been restored but seems to be invalid";
228 $this->debug
= implode("\n", $output);
233 $this->cmd_reload
= str_replace('{TIME}', $time , $this->cmd_reload
);
235 exec($this->cmd_reload
, $output, $status);
237 $this->template
->message
= "Could not reload the configuration '{$file}'";
238 $this->debug
= implode("\n", $output);
242 $this->template
->status
= true;
243 $this->template
->message
= "The configuration '{$file}' has been restored";
244 foreach($this->files2backup
as $onefile){
245 $onefile = trim($onefile);
246 if(pathinfo($onefile, PATHINFO_EXTENSION
) === "cfg") {
247 if(file_exists($onefile) && is_writable($onefile)) {
256 public function delete($file)
258 if ($this->unauthorized
)
261 $this->template
= $this->add_view('backup/delete');
263 $this->template
->status
= @unlink
($this->backups_location
. '/' . $file);
264 $this->template
->message
= $this->template
->status ?
"The backup '{$file}' has been deleted"
265 : "Could not delete the backup '{$file}'";