Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
test_message.php
1 <?php
26 $valid_request = FALSE;
27 // check the legacy use first
28 if (isset($_GET['checkdb']) || isset($_GET['checkreplication'])) {
29  $valid_request = TRUE;
30 }
31 
32 // check new use
33 if (isset($_GET['interrogate']) && $_GET['interrogate'] == 1) {
34  $valid_request = TRUE;
35 }
36 
37 $LOCK_FILE = dirname(__FILE__) . '/.test_message.lock';
38 // not a valid request? exit!
39 if (!$valid_request) {
40  header('HTTP/1.0 200 OK');
41  echo 'the return code was 200';
42  exit;
43 }
44 
45 if (is_file($LOCK_FILE)) {
46  # if the file was last modified < 55 secs ago, then show a 500 error.
47  # we want to limit it to 1 request every minute
48  # (55 secs used so we have a little leeway in case a request comes in slightly early)
49  $one_min_ago = time() - 55;
50  if (filemtime($LOCK_FILE) > $one_min_ago) {
51  header('HTTP/1.0 500 Internal Server Error');
52  exit;
53  }
54 }
55 
56 touch($LOCK_FILE);
57 
61 if (!empty($_GET['checkdb']) || !empty($_GET['checkreplication'])) {
62 
63  define('SQ_SYSTEM_ROOT', '/app/matrix/');
64  define('SQ_LOG_PATH', SQ_SYSTEM_ROOT.'/data/private/logs');
65  $return_code = '200';
66 
67  require_once SQ_SYSTEM_ROOT.'/core/include/init.inc';
68  require SQ_SYSTEM_ROOT . '/data/private/conf/db.inc';
69 
70  if (isset($_GET['checkdb']) && $_GET['checkdb'] == 1) {
71  // we are checking for database availablity
72 
73  $error = FALSE;
74  try {
76  } catch (Exception $e) {
77  $return_code = '500';
78  $error = TRUE;
79  }
80 
81  if (!$error) {
82  $query = MatrixDAL::preparePdoQuery('SELECT assetid FROM sq_ast WHERE assetid = 1');
83  try {
84  $res = MatrixDAL::executePdoOne($query);
85  } catch (Exception $e) {
86  $return_code = '500';
87  }
88  MatrixDAL::dbClose('db');
89  }
90  }
91 
92  if (isset($_GET['checkreplication']) && $_GET['checkreplication'] == 1) {
93  // we are checking that replication is working
94  $file_system_okay = TRUE;
95 
96  error_reporting(15);
97  // find the last line in the rsync.log file and parse it for an error
98  // we need to do a bit of extra work so that we dont read the whole file
99  // into memory as its likely to be big.
100  $fp = fopen('/var/log/rsync.log', 'r');
101 
102  if ($fp) {
103  $info = fstat($fp);
104  // if the modified time of the file is greater than 3 minutes
105  // then rsyncing has not occured
106 
107  if (time() > $info['mtime'] + (3 * 60)) {
108  $file_system_okay = FALSE;
109  } else {
110  // set the file pointer to the end of the file
111  fseek($fp, 0, SEEK_END);
112  $c = null;
113  // find the beginning of the last line
114  while ($c != "\n") {
115  $c = fgetc($fp);
116  fseek($fp, ftell($fp) - 2);
117  }
118  // we are at the last char of the last line so go forward two places
119  fseek($fp, ftell($fp) + 2);
120  $last_line = fgets($fp);
121 
122  list($date, $time, $pid, $msg) = preg_split('/\s+/', $last_line, 4);
123 
124  $error_msg = 'rsync error';
125  if (substr(trim($msg), 0, strlen($error_msg)) == $error_msg) {
126  $file_system_ok = FALSE;
127  }
128  }
129  } else {
130  $file_system_okay = FALSE;
131  }
132 
133  $db_okay = TRUE;
134 
135  // connect to the database as the REPADM user
136  $dsn_info = $db_conf['db'];
137 
138  $dsn_info['username'] = 'repadm';
139  $dsn_info['password'] = 'repadm';
140 
141  try {
142  MatrixDAL::dbConnect($dsn_info, 'dbrepl');
143  $db = MatrixDAL::getDb('dbrepl');
144  $conn_ok = TRUE;
145  } catch (Exception $e) {
146  $db_okay = FALSE;
147  $conn_ok = FALSE;
148  }
149 
150  if ($conn_ok) {
151  // check whether there is a normal status
152  $status = '';
153  $query = MatrixDAL::preparePdoQuery('SELECT status FROM sys.dba_repcat');
154  try {
155  $status = MatrixDAL::executePdoOne($query);
156  } catch (Exception $e) {
157  $db_okay = FALSE;
158  }
159 
160  if (strtoupper($status) !== 'NORMAL') {
161  $db_okay = FALSE;
162  }
163 
164  // verify that there are no errors in the queue
165  $sql = 'SELECT count(*) FROM deferror';
166  $errors = -1;
167  $query = MatrixDAL::preparePdoQuery($sql);
168  try {
169  $errors = MatrixDAL::executePdoOne($query);
170  } catch (Exception $e) {
171  $db_okay = FALSE;
172  }
173 
174  if ($errors != 0) {
175  $db_okay = FALSE;
176  }
177 
178  // verify that there are no transactions that have not been processed in 5 minutes
179  $sql = 'SELECT count(*) FROM deftrandest';
180  $trans = -1;
181 
182  $query = MatrixDAL::preparePdoQuery($sql);
183  try {
184  $trans = MatrixDAL::executePdoOne($query);
185  } catch (Exception $e) {
186  $db_okay = FALSE;
187  }
188 
189  // since the default is -1, we need to check both conditions.
190  if ($trans < 0 || $trans > 200) {
191  $db_okay = FALSE;
192  }
193  }
194 
195  if ($file_system_okay) {
196  if (!$db_okay) {
197  $return_code = '502';
198  }
199  } else {
200  if ($db_okay) {
201  $return_code = '501';
202  } else {
203  $return_code = '503';
204  }
205  }
206  }
207 
208  if ($return_code == '200') {
209  header('HTTP/1.0 200 OK');
210  } else {
211  header('HTTP/1.0 '.$return_code.' Internal Server Error');
212  }
213  echo 'the return code was '.$return_code;
214  exit;
215 }
216 
231 $output = '';
232 
233 define('SQ_SYSTEM_ROOT', realpath(dirname(__FILE__) . '/../..'));
234 $return_code = '200';
235 
236 require_once SQ_SYSTEM_ROOT.'/core/include/init.inc';
237 
238 require SQ_SYSTEM_ROOT . '/data/private/conf/db.inc';
239 
240 $dsn_list = array(
241  'db' => 'SELECT count(msgid) FROM sq_internal_msg', //sq_internal_msg is a replicated target and commonly updated
242  'db2' => 'UPDATE sq_ast SET updated = NOW() WHERE type_code = \'root_user\'', //check write perms for main DSN
243  'db3' => 'UPDATE sq_ast SET updated = NOW() WHERE type_code = \'root_user\'', //check write perms for secondary DSN
244  'dbcache' => 'SELECT count(assetid) FROM sq_cache', //check cache size and accessibility
245  'dbsearch' => 'SELECT count(assetid) FROM sq_sch_idx'
246 );
247 
248 $db_type = $db_conf['db']['type'];
249 
253 $all_db_queries = array();
254 
260 if ($db_type == 'pgsql') {
261  $all_db_queries[] = "SELECT count(*) as count from pg_stat_activity where current_query = '<IDLE>'";
262 }
263 
267 if ($db_type == 'oci8' || $db_type == 'oci') {
268  $dsn_list['db2'] = str_replace('NOW()', 'SYSDATE', $dsn_list['db2']);
269  $dsn_list['db3'] = str_replace('NOW()', 'SYSDATE', $dsn_list['db3']);
270 }
271 
272 $last_dsn = null;
273 foreach ($dsn_list as $dsn_name => $query) {
274  $start_time = time();
275  $output .= $dsn_name.':';
276 
283  switch ($dsn_name) {
284  case 'dbcache':
285  case 'dbsearch':
286  if ($dsn_name == 'dbcache') {
287  $alt_name = 'db2';
288  }
289  if ($dsn_name == 'dbsearch') {
290  $alt_name = 'db';
291  }
292 
293  if (!isset($db_conf[$dsn_name])) {
294  $dsn_name = $alt_name;
295  break;
296  }
297  if ($db_conf[$dsn_name] === null) {
298  $dsn_name = $alt_name;
299  break;
300  }
301  break;
302  }
303 
304  $dsn = $db_conf[$dsn_name]['DSN'];
305 
306  if ($dsn !== $last_dsn) {
307  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection($dsn_name);
308  }
309 
310  $qry = MatrixDAL::preparePdoQuery($query);
311  try {
312  // if it's a select query, use executePdoOne
313  // if it's not, use execPdoQuery - so it will return the number of affected rows.
314 
315  if (strtolower(substr($query, 0, 6)) === 'select') {
316  $res = MatrixDAL::executePdoOne($qry);
317  } else {
318  $res = MatrixDAL::execPdoQuery($qry);
319  }
320  } catch (Exception $e) {
321  $res = '-1';
322  $return_code = '500';
323  }
324 
325  if ($res === NULL && ($dsn_name == 'db2' || $dsn_name == 'db3')) {
326  $res = 1;
327  }
328 
329  $output .= $res.':'.ceil(time() - $start_time);
330 
334  foreach ($all_db_queries as $qry) {
335  $query = MatrixDAL::preparePdoQuery($qry);
336  try {
337  $res = MatrixDAL::executePdoOne($query);
338  } catch (Exception $e) {
339  $return_code = '500';
340  $res = '-1';
341  }
342  $output .= ':' . $res;
343  }
344 
345  $output .="\n";
346 
347  $last_dsn = $dsn;
348 }
349 
350 if ($db_type == 'pgsql') {
351  // fairly broad brush used here, we know that slon uses '<schema_name>_(logtrigger|denyaccess)_[0-9]', so we'll use that to sniff for slon and it's schema name. denyaccess indicates slave.
352  $slon_schema_query = 'SELECT REGEXP_REPLACE(trigger_name, \'(_logtrigger_|_denyaccess_)[0-9]+\', \'\') FROM information_schema.triggers WHERE (trigger_name LIKE \'%_logtrigger_%\' OR trigger_name LIKE \'%_denyaccess_%\') limit 1';
353 
354  // {SLON_SCHEMA} is replaced after the schema name is worked out.
355  $slon_schema_lag_query = 'SELECT cast(extract(epoch from st_lag_time) as int8) FROM {SLON_SCHEMA}.sl_status WHERE st_origin = (SELECT last_value FROM {SLON_SCHEMA}.sl_local_node_id)';
356 
357  $output .= 'slon:';
358  $lag = 0;
359 
360  // Slon test
361  $db_conf['dbslon'] = $db_conf['db2'];
362  MatrixDAL::dbConnect($db_conf['dbslon'], 'dbslon');
363  $db = MatrixDAL::getDb('dbslon');
364 
365  $start_time = time();
366  $query = MatrixDAL::preparePdoQuery($slon_schema_query);
367  try {
368  $slon_schema = MatrixDAL::executePdoOne($query);
369  } catch (Exception $e) {
370  $slon_schema = '';
371  $return_code = '500';
372  }
373 
374  if (!empty($slon_schema)) {
375  $output .= $slon_schema.':';
376  $query = MatrixDAL::preparePdoQuery(str_replace('{SLON_SCHEMA}', $slon_schema, $slon_schema_lag_query));
377  try {
378  $lag = MatrixDAL::executePdoOne($query);
379  } catch (Exception $e) {
380  $lag = '-1';
381  $return_code = '500';
382  }
383  } else {
384  $output .= '0:';
385  }
386  $output .= $lag.':'.ceil(time() - $start_time) ."\n";
387 }
388 
389 if ($return_code == '200') {
390  header('HTTP/1.0 200 OK');
391 } else {
392  header('HTTP/1.0 '.$return_code.' Internal Server Error');
393 }
394 
395 print $output;
396 
397 ?>