reports: Order selected objects when loading
[ninja.git] / op5-upgradescripts / move_reports_tables.php
blob5ea6ad2fbcb0a65c082613ce56a007439492b916
1 <?php
3 /**
4 * Move old monitor_reports data (avail_config, sla_config etc)
5 * into merlin.
6 */
7 $argv = isset($argv) ? $argv : $GLOBALS['argv'];
8 $db_opt['type'] = 'mysql'; # mysql is the only one supported for now.
9 $db_opt['host'] = 'localhost';
10 $db_opt['user'] = isset($argv[2]) ? $argv[2] : false;
11 $db_opt['passwd'] = isset($argv[3]) ? $argv[3] : false;
12 $db_opt['old_database'] = 'monitor_reports';
13 $db_opt['new_database'] = 'merlin';
14 $db_opt['persistent'] = true; # set to false if you're using php-cgi
16 $DEBUG = false;
18 $prefix = isset($argv[1]) ? $argv[1] : false;
20 if (empty($db_opt['user'])) {
21 echo "No database user for monitor_reports supplied - exiting\n";
22 exit(1);
25 # connects to and selects database. false on error, true on success
26 class old_reports
28 public $db_opt = false;
29 public $dbh = false;
30 public $tables_to_convert = array(
31 'avail_config',
32 'avail_config_objects',
33 'avail_db_version',
34 'scheduled_report_periods',
35 'scheduled_report_types',
36 'scheduled_reports',
37 'scheduled_reports_db_version',
38 'sla_config',
39 'sla_config_objects',
40 'sla_db_version',
41 'sla_periods'
45 public function __construct($db_opt=false)
47 if (empty($db_opt)) {
48 echo "Missing input - exiting\n";
49 exit(1);
51 $this->db_opt = $db_opt;
52 $this->dbh = $this->db_connect();
55 public function db_connect() {
56 $db_opt = $this->db_opt;
58 if($db_opt['type'] !== 'mysql') {
59 die("Only mysql is supported as of yet.<br />\n");
62 if(!empty($db_opt['persistent'])) {
63 # use persistent connections
64 $dbh = mysql_pconnect($db_opt['host'],
65 $db_opt['user'],
66 $db_opt['passwd']);
67 } else {
68 $dbh = mysql_connect($db_opt['host'],
69 $db_opt['user'],
70 $db_opt['passwd']);
73 if($dbh === false) return(false);
75 return(mysql_select_db($db_opt['old_database']));
79 # fetch a single row to associative array
80 public function sql_fetch_array($resource) {
81 return(mysql_fetch_array($resource, MYSQL_ASSOC));
84 public function sql_escape_string($string)
86 return mysql_real_escape_string($string);
89 # execute an SQL query with error handling
90 public function sql_exec_query($query) {
91 global $DEBUG;
92 if(empty($query)) return(false);
94 if($this->dbh === false) {
95 $this->db_connect();
98 $result = mysql_query($query);
99 if($result === false) {
100 echo "SQL query failed with the following error message;\n" .
101 mysql_error() . "\n";
102 if($DEBUG) echo "Query was:\n".$query."\n";
105 return $result && mysql_num_rows($result) ? $result : false;
109 class ninja_report_import
111 private $db_type = false;
112 private $db_host = false;
113 private $db_user = false;
114 private $db_pass = false;
115 private $db_database = false;
116 public $prefix = false;
117 private $merlin_conf_file = false;
118 private $merlin_path = '/opt/monitor/op5/merlin'; # where to find merlin files
119 private $nagios_cfg_path = '/opt/monitor/etc'; # path to nagios cfg files
120 # no trailing slash
121 public $DEBUG = false;
122 public $db = false;
125 * Initialize object with database settings from merlin
127 public function __construct()
129 $this->merlin_conf_file = $this->merlin_path.'/import.php';
131 # find db config settings from merlin
132 exec("/bin/grep -m1 'imp->db_type' ".$this->merlin_conf_file."|/bin/awk -F = {'print $2'}", $db_type, $retval);
133 exec("/bin/grep -m1 'imp->db_host' ".$this->merlin_conf_file."|/bin/awk -F = {'print $2'}", $db_host, $retval);
134 exec("/bin/grep -m1 'imp->db_user' ".$this->merlin_conf_file."|/bin/awk -F = {'print $2'}", $db_user, $retval);
135 exec("/bin/grep -m1 'imp->db_pass' ".$this->merlin_conf_file."|/bin/awk -F = {'print $2'}", $db_pass, $retval);
136 exec("/bin/grep -m1 'imp->db_database' ".$this->merlin_conf_file."|/bin/awk -F = {'print $2'}", $db_database, $retval);
138 $this->db_type = !empty($db_type) ? $this->clean_str($db_type[0]) : false;
139 $this->db_host = !empty($db_host) ? $this->clean_str($db_host[0]) : false;
140 $this->db_user = !empty($db_user) ? $this->clean_str($db_user[0]) : false;
141 $this->db_pass = !empty($db_pass) ? $this->clean_str($db_pass[0]) : false;
142 $this->db_database = !empty($db_database) ? $this->clean_str($db_database[0]) : false;
144 # verify that we have all database info
145 # assuming pass might be empty
146 if (empty($this->db_type) || empty($this->db_host) ||
147 empty($this->db_user) || empty($this->db_database))
149 echo "ERROR: Unable to connect to database - some information is missing\n";
150 if($this->DEBUG) echo "db_type: ".print_r($db_type)."\ndb_host: $db_host\ndb_user: $db_user\ndb_database: $db_database\n";
151 exit(1);
153 $this->db_connect();
157 * Clean a parsed string, ie trim and remove "'" + ;
159 public function clean_str($str)
161 if (empty($str))
162 return false;
163 $str = trim($str);
164 $str = str_replace("'", "", $str);
165 $str = str_replace(";", "", $str);
166 return $str;
170 * Connect to database
172 public function db_connect()
174 if($this->db_type !== 'mysql') {
175 die("Only mysql is supported as of yet.\n");
178 $this->db = mysql_connect
179 ($this->db_host, $this->db_user, $this->db_pass);
181 if ($this->db === false)
182 return(false);
184 if ($this->DEBUG) echo " Successfully connected to database\n";
185 return mysql_select_db($this->db_database);
189 * Fetch a single row as an object
191 public function sql_fetch_object($resource=false)
193 return mysql_fetch_object($resource);
197 * Return nr of rows returned from a query
199 public function sql_num_rows($resource=false)
201 return mysql_num_rows($resource);
204 public function sql_escape_string($string)
206 return mysql_real_escape_string($string);
209 # execute an SQL query with error handling
210 public function sql_exec_query($query)
212 if(empty($query))
213 return(false);
215 # workaround for now
216 if($this->db === false) {
217 $this->gui_db_connect();
220 $result = mysql_query($query);
221 if($result === false) {
222 echo "SQL query failed with the following error message;<br />\n" .
223 mysql_error() . "<br />\n";
224 if($this->DEBUG) echo "Query was;<br />\n<b>$query</b><br />\n";
227 return($result);
231 $old_reports = new old_reports($db_opt);
232 echo "Moving data from monitor_reports to merlin\n";
233 foreach ($old_reports->tables_to_convert as $table) {
234 $sql = "SELECT * FROM ".$db_opt['old_database'].".".$table;
235 $old_res = $old_reports->sql_exec_query($sql);
237 if ($old_res !== false) {
238 $sql = false;
239 while ($row = $old_reports->sql_fetch_array($old_res)) {
240 $sql[] = "INSERT INTO ".$db_opt['new_database'].".".$table." (".implode(',', array_keys($row)).") VALUES ('".implode("', '", array_values($row))."')";
242 unset($old_reports);
243 if (!empty($sql)) {
244 $merlin = new ninja_report_import();
245 $merlin->prefix = $prefix;
246 echo "Moving data for $table\n";
247 $merlin->sql_exec_query("TRUNCATE TABLE $table");
248 foreach ($sql as $query) {
249 $merlin->sql_exec_query($query);
251 unset($merlin);
253 $old_reports = new old_reports($db_opt);
257 unset($old_reports);
258 echo "Done moving data from monitor_reports to merlin\n";