Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / dbreplicate.function.php @ b67d8923

Historique | Voir | Annoter | Télécharger (6,53 ko)

1
<?php
2

    
3
/*
4
 * @version $Id: dbreplicate.function.php 7763 2009-01-06 18:44:50Z moyo $
5
 -------------------------------------------------------------------------
6
 GLPI - Gestionnaire Libre de Parc Informatique
7
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
8

9
 http://indepnet.net/   http://glpi-project.org
10
 -------------------------------------------------------------------------
11

12
 LICENSE
13

14
 This file is part of GLPI.
15

16
 GLPI is free software; you can redistribute it and/or modify
17
 it under the terms of the GNU General Public License as published by
18
 the Free Software Foundation; either version 2 of the License, or
19
 (at your option) any later version.
20

21
 GLPI is distributed in the hope that it will be useful,
22
 but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 GNU General Public License for more details.
25

26
 You should have received a copy of the GNU General Public License
27
 along with GLPI; if not, write to the Free Software
28
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
 --------------------------------------------------------------------------
30
 */
31

    
32
/**
33
 * Create slave DB configuration file
34
 * @param host the slave DB host
35
 * @param user the slave DB user
36
 * @param password the slave DB password
37
 * @param DBname the name of the slave DB
38
 */
39
function create_slave_conn_file($host, $user, $password, $DBname) {
40
        global $CFG_GLPI;
41
        $DB_str = "<?php \n class DBSlave extends DBmysql { \n var        \$slave        = true; \n var \$dbhost        = '" . $host . "'; \n var \$dbuser         = '" . $user . "'; \n var \$dbpassword= '" . rawurlencode($password) . "'; \n var \$dbdefault        = '" . $DBname . "'; \n } \n ?>";
42
        $fp = fopen(GLPI_CONFIG_DIR . "/config_db_slave.php", 'wt');
43
        if ($fp) {
44
                $fw = fwrite($fp, $DB_str);
45
                fclose($fp);
46
                return true;
47
        } else
48
                return false;
49
}
50

    
51
/**
52
 * Indicates is the DB replicate is active or not
53
 * @return true if active / false if not active
54
 */
55
function isDBSlaveActive() {
56
        return file_exists(GLPI_CONFIG_DIR . "/config_db_slave.php");
57
}
58

    
59
/**
60
 * Read slave DB configuration file
61
 */
62
function getDBSlaveConf() {
63
        if (isDBSlaveActive()) {
64
                include_once (GLPI_CONFIG_DIR . "/config_db_slave.php");
65
                return new DBSlave;
66
        }
67
}
68

    
69
/**
70
 * Create a default slave DB configuration file
71
 */
72
function createDBSlaveConfig() {
73
        create_slave_conn_file("localhost", "glpi", "glpi", "glpi");
74
}
75

    
76
/**
77
 * Save changes to the slave DB configuration file
78
 */
79
function saveDBSlaveConf($host, $user, $password, $DBname) {
80
        create_slave_conn_file($host, $user, $password, $DBname);
81
}
82

    
83
/**
84
 * Delete slave DB configuration file
85
 */
86
function deleteDBSlaveConfig() {
87
        unlink(GLPI_CONFIG_DIR . "/config_db_slave.php");
88
}
89

    
90
/**
91
 * Switch database connection to slave
92
 */
93
function switchToSlave() {
94
        global $DB;
95
        if (isDBSlaveActive())
96
        {
97
                include_once (GLPI_CONFIG_DIR . "/config_db_slave.php");
98
                $DB = new DBSlave;
99
                return $DB->connected;
100
        }
101
        else
102
                return false;
103
}
104

    
105
/**
106
 * Switch database connection to master
107
 */
108
function switchToMaster() {
109
        global $DB;
110
        $DB = new DB;
111
        return $DB->connected;
112
}
113

    
114
/**
115
 *  Establish a connection to a mysql server (main or replicate)
116
 * @param $use_slave try to connect to slave server first not to main server
117
 * @param $required connection to the specified server is required (if connection failed, do not try to connect to the other server)
118
 * @param $display display error message
119
 */
120
function establishDBConnection($use_slave, $required, $display=true) {
121
        global $DB;
122
        $DB = null;
123

    
124
        $res=false;
125
        // First standard config : no use slave : try to connect to master
126
        if (!$use_slave){
127
                $res = switchToMaster();
128
        } 
129
        
130
        // If not already connected to master due to config or error
131
        if (!$res){
132
                // No DB slave : first connection to master give error
133
                if (!isDBSlaveActive()){ 
134

    
135
                        // Slave wanted but not defined -> use master
136
                        // Ignore $required when no slave configured
137
                        if ($use_slave){
138
                                $res = switchToMaster();
139
                        }                
140
                // SLave DB configured
141
                } else { 
142
                        // Try to connect to slave if wanted
143
                        if ($use_slave){
144
                                $res = switchToSlave();
145
                        }
146

    
147
                        // No connection to 'mandatory' server
148
                        if (!$res && !$required){
149
                                //Try to establish the connection to the other mysql server
150
                                if ($use_slave){
151
                                        $res = switchToMaster();
152
                                } else {
153
                                        $res = switchToSlave();
154
                                }
155
        
156
                                if ($res) {
157
                                        $DB->first_connection=false;        
158
                                }
159
                        }
160
                }
161
        }
162
        // Display error if needed                
163
        if (!$res && $display){
164
                displayMySQLError();
165
        }
166
        return $res;
167
}
168

    
169
/**
170
 *  Get delay between slave and master
171
 */
172
function getReplicateDelay() {
173
        include_once (GLPI_CONFIG_DIR . "/config_db_slave.php");
174

    
175
        return (int) (getHistoryMaxDate(new DB) - getHistoryMaxDate(new DBSlave));
176
}
177

    
178
/**
179
 *  Get history max date of a GLPI DB
180
 * @param $DBconnection DB conneciton used
181
 */
182
function getHistoryMaxDate($DBconnection) {
183
        $result = $DBconnection->query("SELECT UNIX_TIMESTAMP(MAX(date_mod)) as max_date FROM glpi_history");
184
        if ($DBconnection->numrows($result) > 0)
185
                return $DBconnection->result($result, 0, "max_date");
186
        else
187
                return "";
188
}
189
/**
190
 *  Display a common mysql connection error
191
 */
192
function displayMySQLError() {
193
        nullHeader("Mysql Error", $_SERVER['PHP_SELF']);
194

    
195
        if (!isCommandLine()) {
196
                echo "<div class='center'><p><strong>A link to the Mysql server could not be established. Please Check your configuration.</strong></p><p><strong>Le serveur Mysql est inaccessible. V&eacute;rifiez votre configuration</strong></p></div>";
197
        } else {
198
                echo "A link to the Mysql server could not be established. Please Check your configuration.\n";
199
                echo "Le serveur Mysql est inaccessible. Vérifiez votre configuration\n";
200
        }
201

    
202
        nullFooter("Mysql Error", $_SERVER['PHP_SELF']);
203
        die();
204
}
205

    
206
/**
207
 *  Cron process to check DB replicate state
208
 */
209
function cron_dbreplicate() {
210
        global $DB, $CFG_GLPI, $LANG;
211

    
212
        //Lauch cron only is : 
213
        // 1 the master database is avalaible
214
        // 2 the slave database is configurated
215
        if (!$DB->isSlave() && isDBSlaveActive())
216
        {
217
                $diff = getReplicateDelay();
218
                
219
                //If admin must be notified when slave is not synchronized with master
220
                if ($CFG_GLPI["dbreplicate_notify_desynchronization"] && $diff > $CFG_GLPI["dbreplicate_maxdelay"]) {
221
                        $msg = $LANG["setup"][807] . " " . timestampToString($diff);
222
                        $mmail = new glpi_phpmailer();
223
                        $mmail->From = $CFG_GLPI["admin_email"];
224
                        $mmail->AddReplyTo($CFG_GLPI["admin_email"], '');
225
                        $mmail->FromName = $CFG_GLPI["dbreplicate_email"];
226
                        $mmail->AddAddress($CFG_GLPI["dbreplicate_email"], "");
227
                        $mmail->Subject = $LANG["setup"][808];
228
                        $mmail->Body = $msg;
229
                        $mmail->isHTML(false);
230
                        if (!$mmail->Send())
231
                                return 1;
232
                        else
233
                                return 0;
234
                }
235
        }
236
        return 0;
237
}
238
?>
Redmine Appliance - Powered by TurnKey Linux