4 * Move old monitor_reports data (avail_config, sla_config etc)
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
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";
25 # connects to and selects database. false on error, true on success
28 public $db_opt = false;
30 public $tables_to_convert = array(
32 'avail_config_objects',
34 'scheduled_report_periods',
35 'scheduled_report_types',
37 'scheduled_reports_db_version',
45 public function __construct($db_opt=false)
48 echo "Missing input - exiting\n";
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'],
68 $dbh = mysql_connect($db_opt['host'],
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) {
92 if(empty($query)) return(false);
94 if($this->dbh
=== false) {
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
121 public $DEBUG = 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";
157 * Clean a parsed string, ie trim and remove "'" + ;
159 public function clean_str($str)
164 $str = str_replace("'", "", $str);
165 $str = str_replace(";", "", $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)
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)
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";
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) {
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))."')";
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);
253 $old_reports = new old_reports($db_opt);
258 echo "Done moving data from monitor_reports to merlin\n";