function parse_log($seekfile, $slogfile, $seeknum, $tablename) { global $Hcity; global $Hregion; $fd = fopen($slogfile, 'r'); if (!$fd) { trans_w_log('/data0/apache/gateway/withdb/logdb3/insertDB_mo.log', "Content file open error!!!!\n"); return; } $i = 0; $nowsec = time(); $conn = mysql_connect("insertdb.gateway.sina.com.cn:3306", "gateway", "4d5SqYXTx1BQYLKN") or die("Could not connect"); mysql_select_db("qxt_smslog"); /*-----fseek到文件传输开始位置-----*/ fseek($fd, $seeknum); while ($data = fgets($fd, 1024)) { $data = chop($data); if (substr($data, 0, 1) !== "[" or substr($data, 9, 1) !== "]") { echo "日志格式错误:{$i行} {$data}\n"; $i++; continue; } //循环控制 //1.获取单条日志时间; //2.当日志时间($logsec)大于程序执行时间($nowsec) 循环结束 $data = addslashes($data); $logarr = explode(',', $data); $timetmp1 = explode('[', $logarr[0]); $timetmp2 = explode(']', $timetmp1[1]); $logtimestr = date("Y-m-d") . " {$timetmp2['0']}"; $logsec = strtotime("2012-02-08 10:00:00"); $gatewaytmp = explode(' ', $logarr[0]); $gateway = $gatewaytmp[1]; //分析日志,日志各个字段分隔符为"," 短信内容中可能会有逗号 $count = count($logarr) . "\n"; $content = ""; if ($count == 9) { $content = $logarr[$count - 2]; $linkid = $logarr[$count - 1]; } else { $linkid = $logarr[$count - 1]; for ($j = 7; $j < $count - 1; $j++) { $content .= $logarr[$j]; } } if (substr($content, 0, 4) == "0500") { $len = strlen($content) - 12; $tmpcontent = pack("H{$len}", substr($content, 12)); $content = substr($content, 0, 12) . $tmpcontent; } //获取tablename echo "tablename {$tablename}\n"; $premobile = substr($logarr[4], 0, 7); $mobile_reg = $Hregion[$premobile]; $mobile_city = $Hcity[$premobile]; echo "{$Hregion}\n"; $sql = "insert into {$tablename} (msgtime,gateway,mobile,region,city,longnum,msgfeecode,msglength,msg,linkid) values ('{$timetmp2['0']}','{$gateway}','{$logarr['4']}','{$mobile_reg}','{$mobile_city}','{$logarr['2']}','{$logarr['3']}','{$logarr['6']}','{$content}','{$linkid}')"; echo $sql . "\n"; $result = mysql_query($sql); if (0 != mysql_errno()) { //mysql 1146错误,表示没这个表,建立之 if (mysql_errno() == 1146) { trans_w_log('/data0/apache/gateway/withdb/logdb3/insertDB_mo.log', "create table {$tablename}\n"); $sql = "CREATE TABLE `{$tablename}` (\n\t\t\t\t\t`id` int(9) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`msgtime` time NOT NULL DEFAULT '00:00:00',\n\t\t\t\t\t`gateway` varchar(6) NOT NULL ,\n\t\t\t\t\t`mobile` varchar(32) NOT NULL,\n\t\t\t\t\t`region` varchar(6) NOT NULL,\n\t\t\t\t\t`city` varchar(6) NOT NULL,\n\t\t\t\t\t`longnum` varchar(32) NOT NULL,\n\t\t\t\t\t`msgfeecode` varchar(21) NOT NULL DEFAULT '',\n\t\t\t\t\t`msglength` varchar(4) DEFAULT NULL,\n\t\t\t\t\t`msg` text,\n\t\t\t\t\t`linkid` varchar(255) DEFAULT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`),\n\t\t\t\t\tKEY `mobile` (`mobile`),\n\t\t\t\t\tKEY `linkid` (`linkid`),\n\t\t\t\t\tKEY `region` (`region`),\n\t\t\t\t\tKEY `city` (`city`),\n\t\t\t\t\tKEY `longnum` (`longnum`),\n\t\t\t\t\tKEY `gateway` (`gateway`)\n\t\t\t\t\t)"; mysql_query($sql); continue; } else { //数据库访问错误,直接退出 trans_w_log('/data0/apache/gateway/withdb/logdb3/insertDB_mo.log', "insert error " . mysql_errno() . ": " . mysql_error() . "\n"); continue; } } $i++; if ($i > 10000) { break; } } //trans_w_log('/data0/apache/gateway/withdb/logdb3/mtlog2buf.log',"seek is ". ftell($fd) . "\n" ); mysql_close($conn); /*-----获取seek-----*/ $seeklast = ftell($fd); fclose($fd); $fdseek = fopen($seekfile, 'w'); fwrite($fdseek, "{$seeklast}"); fclose($fdseek); }
function genSMSbuf($bufpath, $gateway, $msgid, $buf) { if (!is_dir("{$bufpath}{$gateway}")) { mkdirs("{$bufpath}{$gateway}"); } $buffile = $bufpath . $gateway . "/{$msgid}"; $fd = fopen($buffile, "w"); if (flock($fd, LOCK_EX)) { fwrite($fd, $buf); flock($fd, LOCK_UN); // 释放锁定 } else { //锁失败 trans_w_log('/data0/apache/gateway/log/lockerr.log', "<pho>" . $mobile . "</pho><message>" . $a . "</message>\n"); } fclose($fd); }
function parse_log($info_posfile, $pos, $info_log, $my_log) { return; global $log_tag; global $my_tag; global $rd_lines_each_time; global $msg_type_info; global $msg_type_err; global $conn; $fd = fopen($info_log, 'r'); if (!$fd) { $msg = "{$info_log} can not read"; echo date("Ymd H:i:s") . ': ' . $my_tag . ': ' . $msg . "\n"; phplog($my_log, $log_tag, $msg); return; } else { fseek($fd, $pos); } // connect to db by ODBC. // $conn = db_connect(); $rd_cnt = 0; while ($data = fgets($fd, 2048)) { $parse_ok = true; $data = chop(urldecode($data)); if (substr($data, 0, 1) !== "[" or substr($data, 9, 1) !== "]") { echo date("Ymd H:i:s") . ': ' . $my_tag . ": log fmt error: {$data}\n"; $rd_cnt++; continue; } $data = addslashes($data); parse_str($data, $log_arr); if ($log_arr['type'] == $msg_type_err) { send_sms('error: ', '15801564398'); } $timetmp1 = explode('[', $logarr[0]); $timetmp2 = explode(']', $timetmp1[1]); $logtimestr = date("Y-m-d") . " {$timetmp2['0']}"; $logsec = strtotime("2012-02-08 10:00:00"); $gatewaytmp = explode(' ', $logarr[0]); $gateway = $gatewaytmp[1]; //分析日志,日志各个字段分隔符为"," 短信内容中可能会有逗号 $count = count($logarr) . "\n"; $content = ""; if ($count == 9) { $content = $logarr[$count - 2]; $linkid = $logarr[$count - 1]; } else { $linkid = $logarr[$count - 1]; for ($j = 7; $j < $count - 1; $j++) { $content .= $logarr[$j]; } } if (substr($content, 0, 4) == "0500") { $len = strlen($content) - 12; $tmpcontent = pack("H{$len}", substr($content, 12)); $content = substr($content, 0, 12) . $tmpcontent; } //获取tablename echo "tablename {$tablename}\n"; $premobile = substr($logarr[4], 0, 7); $mobile_reg = $Hregion[$premobile]; $mobile_city = $Hcity[$premobile]; echo "{$Hregion}\n"; $sql = "insert into {$tablename} (msgtime,gateway,mobile,region,city,longnum,msgfeecode,msglength,msg,linkid) values ('{$timetmp2['0']}','{$gateway}','{$logarr['4']}','{$mobile_reg}','{$mobile_city}','{$logarr['2']}','{$logarr['3']}','{$logarr['6']}','{$content}','{$linkid}')"; echo $sql . "\n"; $result = mysql_query($sql); if (0 != mysql_errno()) { //mysql 1146错误,表示没这个表,建立之 if (mysql_errno() == 1146) { trans_w_log('/data0/apache/gateway/withdb/logdb3/insertDB_mo.log', "create table {$tablename}\n"); $sql = "CREATE TABLE `{$tablename}` (\n\t\t\t\t\t`id` int(9) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`msgtime` time NOT NULL DEFAULT '00:00:00',\n\t\t\t\t\t`gateway` varchar(6) NOT NULL ,\n\t\t\t\t\t`mobile` varchar(32) NOT NULL,\n\t\t\t\t\t`region` varchar(6) NOT NULL,\n\t\t\t\t\t`city` varchar(6) NOT NULL,\n\t\t\t\t\t`longnum` varchar(32) NOT NULL,\n\t\t\t\t\t`msgfeecode` varchar(21) NOT NULL DEFAULT '',\n\t\t\t\t\t`msglength` varchar(4) DEFAULT NULL,\n\t\t\t\t\t`msg` text,\n\t\t\t\t\t`linkid` varchar(255) DEFAULT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`),\n\t\t\t\t\tKEY `mobile` (`mobile`),\n\t\t\t\t\tKEY `linkid` (`linkid`),\n\t\t\t\t\tKEY `region` (`region`),\n\t\t\t\t\tKEY `city` (`city`),\n\t\t\t\t\tKEY `longnum` (`longnum`),\n\t\t\t\t\tKEY `gateway` (`gateway`)\n\t\t\t\t\t)"; mysql_query($sql); continue; } else { //数据库访问错误,直接退出本函数 $parse_ok = false; break; } } $rd_cnt++; if ($rd_cnt > $rd_lines_each_time) { break; } } //mysql_close($conn); if ($parse_ok) { $parse_to_line = ftell($fd); fclose($fd); $cmd = $echo . " '{$parse_to_line}' > {$info_posfile} 2>/dev/null"; exec($cmd); } else { // do nothing. return to main for re-try. } }