ona  18.1.1
About: OpenNetAdmin provides a database managed inventory of your IP network (with Web and CLI interface).
  Fossies Dox: ona-18.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

mysql_purge_logs.inc.php
Go to the documentation of this file.
1 <?php
2 // DON'T put whitespace at the beginning or end of this file!!!
3 
4 
5 
7 // Function: mysql_purge_logs (string $options='')
8 //
9 // Input Options:
10 // $options = key=value pairs of options for this function.
11 // multiple sets of key=value pairs should be separated
12 // by an "&" symbol.
13 //
14 // Output:
15 // Returns a two part list:
16 // 1. The exit status of the function. 0 on success, non-zero on
17 // error. All errors messages are stored in $self['error'].
18 // 2. A textual message for display on the console or web interface.
19 //
20 // Example: list($status, $result) = mysql_purge_logs('slaves=STRING');
22 function mysql_purge_logs($options) {
23  global $conf, $self, $ona_db;
24  printmsg('DEBUG => mysql_purge_logs('.$options.') called', 3);
25 
26  // Version - UPDATE on every edit!
27  $version = '1.00';
28 
29  // Parse incoming options string to an array
30  $options = parse_options($options);
31 
32  // Return the usage summary if we need to
33  if ($options['help'] or !$options['slaves']) {
34  $self['error'] = 'ERROR => Insufficient parameters';
35  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
36  return(array(1,
37 <<<EOM
38 
40 Connects to a specified list of MySQL slave servers, checks where they are
41 in reading/replicating the master server's binary logs, and deletes logs
42 from the associated master(s) which are no longer needed by any slave system.
43 
44 A list of slave servers is supplied as input, and master servers are detected
45 automatically.
46 
47  Synopsis: mysql_purge_logs [KEY=VALUE]
48 
49  Required:
50  slaves=NAME[,NAME ...] list of slave server(s) to connect to
51 
52  Optional:
53  commit=[yes|no] commit changes to database (default: no)
54  user=NAME mysql username (default: root)
55  password=STRING mysql password (default: blank)
56 
57 \n
58 
59 EOM
60 
61  ));
62  }
63 
64  // Set default user ID, if none was provided.
65  if(!$options['user'])
66  $options['user']='root';
67 
68  // Sanitize "options[commit]" (no is the default)
69  $options['commit'] = sanitize_YN($options['commit'], 'N');
70 
71  // Split out the list of slave servers into an array (comma-delimited).
72  $slaves = preg_split('/,/', $options['slaves']);
73 
74  // Now we begin...
75  $masters = array();
76  foreach ($slaves as $slave_host) {
77  if(!$slave_host or $slave_host == "")
78  continue;
79  printmsg("DEBUG => Connect to slave host mysql://{$options['user']}:{$options['password']}@{$slave_host}", 4);
80  $dbh = db_connect('mysql', $slave_host, $options['user'], $options['password'], 'mysql');
81  if(!$dbh || !$dbh->IsConnected())
82  continue;
83 
84  // Find out this slave's replication status.
85  $q = "show slave status;";
86  $rs = $dbh->Execute($q);
87  $array = $rs->FetchRow();
88 
89  // Check if our master is listed, and if so, make sure the oldest
90  // binary logfile (by name) is stored in the array.
91  $matched = 0;
92  foreach($masters as $host => $binlog) {
93  if($host == $array['Master_Host'] && $binlog > $array['Master_Log_File']) {
94  $masters['$host'] = $array['Master_Log_File'];
95  $matched = 1;
96  break;
97  }
98  }
99 
100  // If our master wasn't listed, then create a new entry.
101  if($matched == 0)
102  $masters[$array['Master_Host']] = $array['Master_Log_File'];
103  }
104 
105 
106  // Now the "output" step...
107  $retval_string = "";
108  $retval_errlvl = 0;
109  foreach($masters as $host => $binlog) {
110  if($options['commit'] == 'Y') {
111  $dbh = db_connect('mysql', $host, $options['user'], $options['password'], 'mysql');
112  if(!$dbh || !$dbh->IsConnected()) {
113  $self['error'] .= "ERROR => Could not connect to host '{$host}' to execute query. Skipping.\n";
114  $retval_errlvl = 2;
115  continue;
116  }
117  }
118 
119  $q = "purge master logs to '{$binlog}'";
120 
121  if($options['commit'] == 'Y') {
122  $rs = $dbh->Execute($q);
123  $error = $dbh->ErrorMsg();
124 
125  // Report any errors
126  if ($rs === false or $error) {
127  $self['error'] .= 'ERROR => SQL query on host {$host} failed: ' . $error . "\n";
128  $retval_errlvl = 2;
129  } else {
130  $retval_string .= "Successfully executed ({$q}) on host '{$host}'.\n";
131  }
132  } else {
133  $retval_string .= "Not commiting changes. Would have executed: ({$q}) on host '{$host}'.\n";
134  }
135  }
136 
137  // Return our results, as success strings and (perhaps) error strings.
138  return(array($retval_errlvl, $retval_string));
139 }
140 
141 
142 
143 
144 
146 // Function: db_connect ($db_type, $host, $user, $pass, $database)
147 //
148 // Input Options:
149 // $db_type = name of database driver to use (e.g. 'mysql')
150 // $host = database hostname
151 // $user = database user id
152 // $pass = password (if any) to go with user id
153 // $database = which database to connect to
154 //
155 // Output:
156 // Returns a single object:
157 // 1. An object representing the filehandle connected to the selected
158 // database.
159 //
160 // Example: <DB_HANDLE> = db_connect('mysql','localhost','root','','somedb');
162 
163 function db_connect($db_type, $host, $user, $pass, $database) {
164  $object = NewADOConnection($db_type);
165  $connected = 0;
166  for ($a = 1; $a <= 3 and $connected == 0; $a++) {
167  $ok1 = $object->Connect($host, $user, $pass, $database);
168  $ok2 = $object->IsConnected();
169  $ok3 = $object->ErrorMsg();
170  // If the connection didn't work, bail.
171  if (!$ok1 or !$ok2 or $ok3) { printmsg("ERROR => {$db_type} DB connection failed: " . $object->ErrorMsg(), 1); }
172  // Otherwise exit the for loop.
173  else { $connected = 1; }
174  }
175 
176  // If it still isn't connected, return an error.
177  if ($connected == 0) {
178  printmsg("ERROR => {$db_type} DB connection failed after 3 tries! Maybe server is down? Error: " . $object->ErrorMsg());
179  }
180 
181  return $object;
182 }
183 
184 ?>
$rs
if(isset($_REQUEST['nrows'])) else $rs
Definition: server.php:94
db_connect
db_connect($db_type, $host, $user, $pass, $database)
Definition: mysql_purge_logs.inc.php:163
$database
$database
Definition: collate-convert.php:18
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
password
</td >< td class="padding">< input id="changebutton" type="button" name="change_pass" value="Change Password" onclick="el('passchange_container').style.display = (el('passchange_container').style.display == 'none') ? '' : 'none'; el('changebutton').style.display = 'none';" ></td ></td ></table ><!-- PASSWORD CHANGE CONTAINER -->< span id="passchange_container" style="display:none;">< form id="passchange_form">< input id="old" name="old" type="hidden" value="">< input id="new1" name="new1" type="hidden" value="">< input id="new2" name="new2" type="hidden" value="">< table style="background-color: {$color['window_content_bg']}; padding-left: 25px; padding-right: 25px;" width="100%" cellspacing="0" border="0" cellpadding="0">< tr >< td align="right" nowrap="true" class="padding" style="font-weight: bold;"> Old password
Definition: app_user_info.inc.php:149
$host
$host
Definition: collate-convert.php:19
$conf
global $conf
Definition: 2-to-3.php:15
$self
global $self
Definition: 2-to-3.php:15
mysql_purge_logs
mysql_purge_logs($options)
Definition: mysql_purge_logs.inc.php:22
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22