Data_Warehouse/getDBLog.php

70 lines
2.7 KiB
PHP
Raw Normal View History

2021-03-19 17:26:42 +00:00
<?php
ini_set("memory_limit","8024M");
header('Access-Control-Allow-Origin: *');
date_default_timezone_set('UTC');
2021-05-07 15:54:46 +00:00
// if started from commandline, wrap parameters to $_POST and $_GET
if (!isset($_SERVER["HTTP_HOST"]) && isset($argv[1])) {
parse_str($argv[1], $_REQUEST);
}
2021-03-19 17:26:42 +00:00
$api_key = "cC7d2DHDnEwJTwiHLakhyjZGKLyWR";
if( isset($_REQUEST['apiKey']) && $_REQUEST['apiKey'] == $api_key && isset($_REQUEST['timestamp']) && is_numeric($_REQUEST['timestamp']) ) {
$connect = mysqli_connect ('example-cluster-1.cluster-xxxxxxxxxx.ca-central-1.rds.amazonaws.com', 'dbuser', 'password');
mysqli_select_db ( $connect,'mysql');
$output = '{ "message": "", "data": { "transactions": [';
2021-05-07 15:54:46 +00:00
$hour_of_day = date("G");
$chunk_size = 1000;
$starting = 0;
$still_have_rows = true;
$temp_path = "/mnt/cache/tmp/output.json";
$myfile = fopen($temp_path, "w") or die("Unable to open file!");
fwrite($myfile, $output);
2021-03-19 17:26:42 +00:00
2021-05-07 15:54:46 +00:00
// Get the data from the current log
$result = mysqli_query ( $connect, 'DROP TEMPORARY TABLE IF EXISTS logtemp.backupGeneralTable'.$hour_of_day);
$result = mysqli_query ( $connect, 'CREATE TEMPORARY TABLE logtemp.backupGeneralTable'.$hour_of_day.' AS (select UNIX_TIMESTAMP(event_time) as event_time,argument from mysql.general_log where (upper(argument) like "%INSERT %" or upper(argument) like "%UPDATE %" or upper(argument) like "%CREATE %") and argument NOT LIKE "%mysql.rds_heartbeat2%" AND argument NOT LIKE "%INFORMATION_SCHEMA.GLOBAL_STATUS%" AND argument NOT LIKE "%CREATE TEMPORARY TABLE%")');
2021-03-19 17:26:42 +00:00
// Rotate the general log table
$query = 'CALL mysql.rds_rotate_general_log';
$result = mysqli_query ( $connect, $query);
2021-05-07 15:54:46 +00:00
$first_time = true;
while($still_have_rows){
$query = 'SELECT event_time,argument FROM logtemp.backupGeneralTable'.$hour_of_day.' LIMIT '.$starting.', '.$chunk_size;
$result = mysqli_query ( $connect, $query);
if(mysqli_num_rows($result)>0)
{
while ($thedata = mysqli_fetch_assoc($result)) {
if($first_time){
fwrite($myfile, '{"event_time" : "' . str_replace('"', '\"', $thedata["event_time"]) . '","argument" : "' . str_replace('"', '\"', str_replace('\"', '\\"', str_replace('\\', '\\\\', str_replace("\'", "'", str_replace("\t", " ", $thedata["argument"]))))) . '"}');
$first_time = false;
}else{
fwrite($myfile, ',{"event_time" : "' . str_replace('"', '\"', $thedata["event_time"]) . '","argument" : "' . str_replace('"', '\"', str_replace('\"', '\\"', str_replace('\\', '\\\\', str_replace("\'", "'", str_replace("\t", " ", $thedata["argument"]))))) . '"}');
}
}
$starting = $starting + $chunk_size;
}else{
$still_have_rows = false;
}
}
fwrite($myfile, ']}}');
fclose($myfile);
readfile($temp_path);
unlink($temp_path);
2021-03-19 17:26:42 +00:00
}else{
echo '{ "message": "Invalid post variables", "data": {}}';
}
?>