程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php小代碼---從慢日志文件分離出日志記錄並存放於excel文件中

php小代碼---從慢日志文件分離出日志記錄並存放於excel文件中

編輯:關於PHP編程

php小代碼---從慢日志文件分離出日志記錄並存放於excel文件中


 
<?php
 
header("Content-type:text/html; charset=UTF-8");
ini_set('max_execution_time', '10000');
ini_set('memory_limit','1024M');
 
function dealslowContent($content) {
    $data = array();
    preg_match('/#\sUser@Host:(.+)\n#\sThread_id/', $content, $match);
    $data['User@Host'] = $match[1];
    preg_match('/#\sQuery_time:\s(.+)\sLock_time/', $content, $match);
    $data['Query_time'] = $match[1];
    preg_match('/\sLock_time:\s(.+)\sRows_sent/', $content, $match);
    $data['Lock_time'] = $match[1];
 
    preg_match('/\sRows_examined:\s(.+)\sRows_affected/', $content, $match);
    $data['Rows_examined'] = $match[1];
    preg_match('/\sRows_read:\s(.+)\n#\sBytes_sent/', $content, $match);
    $data['Rows_read'] = $match[1];
 
    preg_match('/timestamp=(\d+);\s/', $content, $match);
    $data['timestamp'] = $match[1];
 
    preg_match('/#\sBytes_sent:\s(.+)\sSET/', $content, $match);
    $data['Bytes_sent'] = $match[1];
 
    $sqlPos = strpos($content, ';');
    $data['sql'] = substr($content, $sqlPos + 1, strlen($content));
    return $data;
}
 
$slowData = array();
 
$handle = fopen('mysql-slow.log.2', 'r');
$content = '';
$middlerCha = '';
$identifier = '# User@Host';
$identifierLength = strlen($identifier);
while (!feof($handle)) {
    $oneTakeCha = fread($handle, 1);
    if ($oneTakeCha === '#') {
        $middlerCha = '#' . fread($handle, $identifierLength - 1);
        if ($middlerCha === $identifier) {
            if ($content != '') {
                $slowData[] = dealslowContent($content);
                $content = $middlerCha;
            } else {
                $content = $middlerCha;
            }
        } else {
            $content.=$middlerCha;
        }
    } else {
        $content.= $oneTakeCha;
    }
}
$slowData[] = dealslowContent($content);
fclose($handle);
 
include 'libraries/PHPExcel.php';
include 'libraries/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
 
$titlearray = array(
    "主機信息" => 'User@Host', "查詢時間" => 'Query_time', "鎖表時間" => 'Lock_time'
    , "查詢時檢查行數" => 'Rows_examined', "讀取行數" => 'Rows_read', "時間" => 'timestamp',
    "發送字節數" => 'Bytes_sent', "執行的sql" => 'sql'
);
 
$col = 0;
foreach ($titlearray as $key => $title) {
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $key);
    $col++;
}
 
$j = 2;
foreach ($slowData as $key => $sd) {
    $col = 0;
    foreach ($titlearray as $key2 => $title) {
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $j, $sd[$title]);
        $col++;
    }
    $j++;
}
 
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
// Sending headers to force the user to download the file
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="slowsql.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved