分类: 系统运维
2012-02-22 10:31:37
/usr/local/nginx/logs/*.log {
daily
rotate 5
notifempty
missingok
sharedscripts
copytruncate
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
fi
/root/tj_script/test.sh
endscript
}
#!/bin/sh
pid_file="/root/tj_script/test.pid";
if [ -f ${pid_file} ]
then
pid=`cat ${pid_file}`
isRunning=`ps -p ${pid} |grep ${pid} |wc -l`
else
isRunning="0"
fi
if [ ${isRunning} = '1' ] ; then
echo "script is running now"
exit 0;
fi
echo $$ > ${pid_file}
logFileName="/usr/local/nginx/logs/1234.com_access.log.1"
if [ -f $logFileName ]; then
echo "job begin"
php /root/tj_script/access.php $logFileName > /root/tj_script/test.log
echo "job end"
fi
echo "Running is done."
if($_SERVER['argc'] < 2){
die(0);
}
define('DEBUG' ,true);
define('NEWLINE',"\n");
define('ROW_LIMIT',50);
define('CUR_DIR',dirname(__FILE__));
require CUR_DIR.'/mysql.class.php';
function wlog($v = '',$stop = false){
if(defined('DEBUG')){
print_r($v);
echo NEWLINE;
}
if(true == $stop){
exit(0);
}
}
/**
* 批量插入
*/
function batch_insert($p){
if(empty($p) || strlen($p[0]) < 1){
wlog($p);
return false;
}
global $db;
$tmp = '';
$insertSQL['head'] = "INSERT INTO `site_access_log` VALUES ";
$insertSQL['tail'] = array();
foreach($p as $k => $v){
if(false == preg_match("/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?\[(.*?)\s+.*?\]\s+\"(\w{3,6})\s+(.*?)\s+.*\s+(\d+)\s+(\d+)\s+\"(.*?)\"\s+\"(.*?)\"/",$v,$match)){
wlog($v);
continue;
}
$now = time();
$ip = explode('.',$match[1]);
//29/Dec/2011:09:22:06
//var_dump(strtotime("02 Dec 2011 14:14:03"));
$req_hm = substr($match[2],strpos($match[2],':') + 1,5);
$match[2] = str_replace('/',' ',$match[2]);
$match[2]{strpos($match[2],':')} = ' ';
$req_time = date("Y-m-d H:i:s",strtotime($match[2]));
$browser = "";
if(strpos($match[8],"MSIE") !== false){
$browser = "MSIE";
}else if(strpos($match[8],"Firefox") !== false){
$browser = "Firefox";
}else if(strpos($match[8],"Safari") !== false){
$browser = "Safari";
}else if(strpos($match[8],"Konqueror") !== false){
$browser = "Konqueror";
}else{
$browser = "Other";
}
$os = '';
if(strpos($match[8],"Windows") !== false){
$os = "windows";
}else if(strpos($match[8],"Linux") !== false){
$os = "Linux";
}else if(strpos($match[8],"Mac") !== false){
$os = "Mac OS";
}else{
$os = "Other";
}
$match[3] = mysql_escape_string($match[3]);
$match[7] = mysql_escape_string($match[7]);
$match[4] = mysql_escape_string($match[4]);
$insertSQL['tail'][] = "(NULL,'{$req_time}','{$req_hm}',{$ip[0]},{$ip[1]},{$ip[2]},{$ip[3]},'{$os}','{$browser}','{$match[3]}','{$match[7]}','{$match[4]}',{$match[6]},{$match[5]},1,{$now})";
}
try {
$db->query($insertSQL['head'].implode(',',$insertSQL['tail']));
if($db->affectedRows() < 1){
wlog("batch insert rows=".count($insertSQL['tail'])."=".$db->affectedRows());
wlog($insertSQL);
}
}catch(Exception $e){
wlog("Query Error");
wlog($insertSQL);
return false;
}
return true;
}
if(defined('DEBUG')){
error_reporting(E_ERROR|E_WARNING|E_PARSE);
ini_set("display_errors",true);
}else{
error_reporting(0);
ini_set("display_errors",false);
}
$fileName = $_SERVER['argv'][1];
if(substr($fileName,0,1) != '/'){
$fileName = "/usr/local/nginx/logs/{$fileName}";
}
wlog($fileName);
$dbConf = array(
'host' => '192.168.202.151',
'username' => 'root',
'password' => '123456',
'dbname' => 'm_log',
);
$db = new Mysql($dbConf);
if(file_exists($fileName) && is_file($fileName)){
$fp = fopen($fileName,"r");
if(!$fp){
wlog("File {$fileName} cant not open",true);
}
$line = array();
try{
$db->connect();
while(!feof($fp)){
$line[] = fgets($fp,2048);
if(count($line) >= ROW_LIMIT){
if(!batch_insert($line)){
wlog($line);
}else{
$line = array();
}
}
}
}catch(Exception $e){
//db error, todo send message to administrator
}
if($fp){
fclose($fp);
}
if(!batch_insert($line)){
wlog($line);
}else{
unset($line);
}
}else{
wlog("{$fileName} not found");
}
wlog('END of '. $_SERVER['argv'][0]);