Skip to content

Commit eed6b3c

Browse files
author
vogelor
committed
the new rescue-module is now able to rescue (restart) mysql
1 parent 4ebc7d1 commit eed6b3c

File tree

4 files changed

+283
-102
lines changed

4 files changed

+283
-102
lines changed

server/lib/classes/db_mysql.inc.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ function db()
6161
function updateError($location)
6262
{
6363
global $app;
64-
$this->errorNumber = mysql_errno($this->linkId);
65-
$this->errorMessage = mysql_error($this->linkId);
64+
$this->errorNumber = @mysql_errno($this->linkId);
65+
$this->errorMessage = @mysql_error($this->linkId);
6666
$this->errorLocation = $location;
6767
if($this->errorNumber && $this->show_error_messages && method_exists($app,'log'))
6868
{
@@ -76,7 +76,7 @@ function connect()
7676
{
7777
if($this->linkId == 0)
7878
{
79-
$this->linkId = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
79+
$this->linkId = @mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
8080
if(!$this->linkId)
8181
{
8282
$this->updateError('DB::connect()-> mysql_connect');
@@ -463,7 +463,7 @@ function dropTable($table_name) {
463463
return $this->query($sql);
464464
}
465465

466-
// gibt Array mit Tabellennamen zurück
466+
// gibt Array mit Tabellennamen zur�ck
467467
function getTables($database_name = '') {
468468

469469
if($database_name == '') $database_name = $this->dbName;
@@ -474,7 +474,7 @@ function getTables($database_name = '') {
474474
return $tb_names;
475475
}
476476

477-
// gibt Feldinformationen zur Tabelle zurück
477+
// gibt Feldinformationen zur Tabelle zur�ck
478478
/*
479479
$columns = array(action => add | alter | drop
480480
name => Spaltenname

server/lib/classes/monitor_tools.inc.php

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,16 @@ public function monitorServices() {
518518
/** the id of the server as int */
519519
$server_id = intval($conf['server_id']);
520520

521-
/** get the "active" Services of the server from the DB */
521+
/** get the "active" Services of the server from the DB */
522522
$services = $app->dbmaster->queryOneRecord('SELECT * FROM server WHERE server_id = ' . $server_id);
523+
/*
524+
* If the DB is down, we have to set the db to "yes".
525+
* If we don't do this, then the monitor will NOT monitor, that the db is down and so the
526+
* rescue-module can not try to rescue the db
527+
*/
528+
if ($services == null) {
529+
$services['db_server'] = 1;
530+
}
523531

524532
/* The type of the Monitor-data */
525533
$type = 'services';
@@ -1539,16 +1547,16 @@ private function _checkTcp($host, $port) {
15391547
* We got a connection, but maybe apache is not able to send data over this
15401548
* connection?
15411549
*/
1542-
fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
1550+
fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
15431551
stream_set_timeout($fp, 2);
15441552
$res = fread($fp, 10);
1545-
$info = stream_get_meta_data($fp);
1553+
$info = stream_get_meta_data($fp);
15461554
fclose($fp);
1547-
if ($info['timed_out']) {
1555+
if ($info['timed_out']) {
15481556
return false; // Apache was not able to send data over this connection
1549-
} else {
1557+
} else {
15501558
return true; // Apache was able to send data over this connection
1551-
}
1559+
}
15521560
} else {
15531561
return false; // Apache was not able to establish a connection
15541562
}
@@ -1577,6 +1585,67 @@ private function _checkFtp($host, $port) {
15771585
return false;
15781586
}
15791587
}
1588+
1589+
/*
1590+
* Set the state to the given level (or higher, but not lesser).
1591+
* * If the actual state is critical and you call the method with ok,
1592+
* then the state is critical.
1593+
*
1594+
* * If the actual state is critical and you call the method with error,
1595+
* then the state is error.
1596+
*/
1597+
private function _setState($oldState, $newState)
1598+
{
1599+
/*
1600+
* Calculate the weight of the old state
1601+
*/
1602+
switch ($oldState) {
1603+
case 'no_state': $oldInt = 0;
1604+
break;
1605+
case 'ok': $oldInt = 1;
1606+
break;
1607+
case 'unknown': $oldInt = 2;
1608+
break;
1609+
case 'info': $oldInt = 3;
1610+
break;
1611+
case 'warning': $oldInt = 4;
1612+
break;
1613+
case 'critical': $oldInt = 5;
1614+
break;
1615+
case 'error': $oldInt = 6;
1616+
break;
1617+
}
1618+
/*
1619+
* Calculate the weight of the new state
1620+
*/
1621+
switch ($newState) {
1622+
case 'no_state': $newInt = 0 ;
1623+
break;
1624+
case 'ok': $newInt = 1 ;
1625+
break;
1626+
case 'unknown': $newInt = 2 ;
1627+
break;
1628+
case 'info': $newInt = 3 ;
1629+
break;
1630+
case 'warning': $newInt = 4 ;
1631+
break;
1632+
case 'critical': $newInt = 5 ;
1633+
break;
1634+
case 'error': $newInt = 6 ;
1635+
break;
1636+
}
1637+
1638+
/*
1639+
* Set to the higher level
1640+
*/
1641+
if ($newInt > $oldInt){
1642+
return $newState;
1643+
}
1644+
else
1645+
{
1646+
return $oldState;
1647+
}
1648+
}
15801649

15811650
private function _getIntArray($line) {
15821651
/** The array of float found */

server/mods-available/rescue_core_module.inc.php

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ private function _doRescue() {
9393
*/
9494
$this->_rescueApache();
9595

96+
/*
97+
* rescue mysql if needed
98+
*/
99+
$this->_rescueMySql();
100+
96101
/*
97102
* The last step is to save the rescue-data
98103
*/
@@ -263,16 +268,95 @@ private function _rescueApache(){
263268
$app->log('Apache is down! Try rescue apache (try:' . $tryCount . ')...', LOGLEVEL_WARN);
264269
// echo 'Apache is down! Try rescue apache (try:' . $tryCount . ')...';
265270

266-
/*
267-
* First we stop the running service "normally"
268-
*/
269-
$daemon = '';
270271
if(is_file($conf['init_scripts'] . '/' . 'httpd')) {
271272
$daemon = 'httpd';
272273
} else {
273274
$daemon = 'apache2';
274275
}
275276

277+
$this->_rescueDaemon($daemon);
278+
}
279+
280+
/**
281+
* restarts mysql, if needed
282+
*/
283+
private function _rescueMySql(){
284+
global $app, $conf;
285+
286+
/*
287+
* do nothing, if it is not allowed to rescue mysql
288+
*/
289+
if ((isset($conf['serverconfig']['rescue']['do_not_try_rescue_mysql']) && ($conf['serverconfig']['rescue']['do_not_try_rescue_mysql']) == 'y')){
290+
return;
291+
}
292+
293+
/*
294+
* if the service is up and running, or the service is not installed there is nothing to do...
295+
*/
296+
if ($this->_monitoringData[0][0]['data']['mysqlserver'] != 0){
297+
/* Clear the try counter, because we do not have to try to rescue the service */
298+
$this->_rescueData['mysqlserver']['try_counter'] = 0;
299+
return;
300+
}
301+
302+
/*
303+
* OK, the service is installed and down.
304+
* Maybe this is because of a restart of the service by the admin.
305+
* This means, we check the data 1 minute ago
306+
*/
307+
if ((!isset($this->_monitoringData[1][0]['data']['mysqlserver'])) ||
308+
((isset($this->_monitoringData[1][0]['data']['mysqlserver'])) && ($this->_monitoringData[1][0]['data']['mysqlserver'] != 0))){
309+
/*
310+
* We do NOT have this data or we have this data, but the webserver was not down 1 minute ago.
311+
* This means, it could be, that the admin is restarting the server.
312+
* We wait one more minute...
313+
*/
314+
return;
315+
}
316+
317+
/*#####
318+
* The service is down and it was down 1 minute ago.
319+
* We try to rescue it
320+
*#####*/
321+
322+
/* Get the try counter */
323+
$tryCount = (!isset($this->_rescueData['mysqlserver']['try_counter']))? 1 : $this->_rescueData['mysqlserver']['try_counter'] + 1;
324+
325+
/* Set the new try counter */
326+
$this->_rescueData['mysqlserver']['try_counter'] = $tryCount;
327+
328+
/* if 5 times will not work, we have to give up... */
329+
if ($tryCount > 5){
330+
$app->log('MySQL is down! Rescue will not help!', LOGLEVEL_ERROR);
331+
return;
332+
}
333+
334+
335+
$app->log('MySQL is down! Try rescue mysql (try:' . $tryCount . ')...', LOGLEVEL_WARN);
336+
// echo 'MySQL is down! Try rescue mysql (try:' . $tryCount . ')...';
337+
338+
if(is_file($conf['init_scripts'] . '/' . 'mysqld')) {
339+
$daemon = 'mysqld';
340+
} else {
341+
$daemon = 'mysql';
342+
}
343+
344+
$this->_rescueDaemon($daemon);
345+
}
346+
347+
/**
348+
* Tries to stop and then restart the daemon
349+
*
350+
* @param type $daemon the name of the daemon
351+
*/
352+
private function _rescueDaemon($daemon){
353+
global $conf;
354+
355+
// if you need to find all restarts search for "['init_scripts']"
356+
/*
357+
* First we stop the running service "normally"
358+
*/
359+
276360
/*
277361
* ATTENTION!
278362
* The service hangs. this means it could be, that "stop" will hang also.
@@ -292,7 +376,5 @@ private function _rescueApache(){
292376
*/
293377
exec($conf['init_scripts'] . '/' . $daemon . ' start');
294378
}
295-
296-
// if you need to find all restarts search for "['init_scripts']"
297379
}
298380
?>

0 commit comments

Comments
 (0)