Passed
Push — development ( 2c0f7a...866d79 )
by Thomas
01:59
created

ReplicationMonitor::checkSlave()   C

Complexity

Conditions 9
Paths 24

Size

Total Lines 72
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 43
nc 24
nop 1
dl 0
loc 72
rs 6.0413
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/***************************************************************************
3
 *  For license information see doc/license.txt
4
 *
5
 *
6
 *  - check all slaves and manage table sys_repl_slaves
7
 *  - check all slaves and purge master logs
8
 *  - send warning when too many logs stay on master (file size)
9
 ***************************************************************************/
10
11
checkJob(new ReplicationMonitor());
12
13
class ReplicationMonitor
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
{
15
    public $name = 'replication_monitor';
16
    public $interval = 0;
17
18
    //var $interval = 60;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
19
20
    public function run()
21
    {
22
        global $opt;
23
        $known_ids = [];
24
25
        foreach ($opt['db']['slaves'] as $k => $v) {
26
            $this->checkSlave($k);
27
            $known_ids[] = "'" . sql_escape($k) . "'";
28
        }
29
30
        if (count($known_ids) > 0) {
31
            sql('DELETE FROM `sys_repl_slaves` WHERE `id` NOT IN (' . implode(',', $known_ids) . ')');
32
        } else {
33
            sql('DELETE FROM `sys_repl_slaves`');
34
        }
35
36
        // now, clean up sys_repl_exclude
37
        sql(
38
            "DELETE FROM `sys_repl_exclude` WHERE `datExclude`<DATE_SUB(NOW(), INTERVAL '&1' SECOND)",
39
            $opt['db']['slave']['max_behind']
40
        );
41
    }
42
43
    public function checkSlave($id)
44
    {
45
        global $opt;
46
        $nActive = 0;
47
        $nOnline = 0;
48
        $sLogName = '';
49
        $sLogPos = '';
50
        $nTimeDiff = - 1;
51
52
        $slave = $opt['db']['slaves'][$id];
53
        if ($slave['active'] == true) {
54
            $nActive = 1;
55
56
            // connect
57
            $dblink = @mysql_connect($slave['server'], $slave['username'], $slave['password']);
58
            if ($dblink !== false) {
59
                if (mysql_select_db($opt['db']['placeholder']['db'], $dblink)) {
60
                    // read slave time
61
                    $rs = mysql_query('SELECT `data` FROM `sys_repl_timestamp`', $dblink);
62
                    if ($rs !== false) {
63
                        $rTime = mysql_fetch_assoc($rs);
64
                        mysql_free_result($rs);
65
66
                        // read current master db time
67
                        $nMasterTime = sql_value("SELECT NOW()", null);
68
69
                        $nTimeDiff = strtotime($nMasterTime) - strtotime($rTime['data']);
70
                        if ($nTimeDiff < $opt['db']['slave']['max_behind']) {
71
                            $nOnline = 1;
72
                        }
73
                    }
74
75
                    // update logpos
76
                    $rs = mysql_query("SHOW SLAVE STATUS");
77
                    $r = mysql_fetch_assoc($rs);
78
                    mysql_free_result($rs);
79
                    $sLogName = $r['Master_Log_File'];
80
                    $sLogPos = $r['Read_Master_Log_Pos'];
81
                }
82
                mysql_close($dblink);
83
            }
84
        }
85
86
        // only-flag changed?
87
        if ($nOnline != sql_value("SELECT `online` FROM `sys_repl_slaves` WHERE `id`='&1'", 0, $id)) {
88
            mail(
89
                $opt['db']['error']['mail'],
90
                "MySQL Slave Server Id " . $id . " (" . $slave['server'] . ") is now " . (($nOnline != 0) ? 'Online' : 'Offline'),
91
                ''
92
            );
93
        }
94
95
        sql(
96
            "INSERT INTO `sys_repl_slaves` 
97
             (`id`, `server`, `active`, `weight`, `online`, `last_check`, `current_log_name`, `current_log_pos`)
98
             VALUES ('&1', '&2', '&3', '&4', '&5', NOW(), '&6', '&7')
99
             ON DUPLICATE KEY UPDATE `server`='&2', `active`='&3', `weight`='&4', `online`='&5', `last_check`=NOW(),
100
                                     `current_log_name`='&6', `current_log_pos`='&7'",
101
            $id,
102
            $slave['server'],
103
            $nActive,
104
            $slave['weight'],
105
            $nOnline,
106
            $sLogName,
107
            $sLogPos
108
        );
109
110
        // update time_diff?
111
        if ($nTimeDiff != - 1) {
112
            sql("UPDATE `sys_repl_slaves` SET `time_diff`='&1' WHERE `id`='&2'", $nTimeDiff, $id);
113
        }
114
    }
115
}
116