천

[서버운영] 아파치로그] sql로 바로 저장 입니다.

· 2015-11-23 (월) 20:22:05 · 3079

아래글에 이어 아파치와 php-cli 를 연계하여 로그를 바로 sql 에 저장한다.
예제이므로 DB 클래스와 에러제어 부분은 따로 표기하지 않는다.

PostgreSQL 기준.

httpd.conf

CustomLog "| /etc/apache2/shjsqllog.php" shjsqllog

CREATE TABLE shjlog (
seq SERIAL NOT NULL,
ip CIDR NOT NULL,
port SMALLINT NOT NULL,
bytes BIGINT NOT NULL,
inbytes BIGINT NOT NULL,
outbytes BIGINT NOT NULL,
protocol VARCHAR(30) NOT NULL,
method VARCHAR(10) NOT NULL,
host VARCHAR(200) NOT NULL,
file VARCHAR(300) NOT NULL,
uri VARCHAR(500) NOT NULL,
status SMALLINT NOT NULL,
phpsessid CHAR(32) NOT NULL,
useragent VARCHAR(200) NOT NULL,
referer VARCHAR(500) NOT NULL,
responsetime INTEGER NOT NULL,
datetime TIMESTAMPTZ NOT NULL,
PRIMARY KEY (seq)
);

CREATE INDEX ip_dex ON shjlog (ip);
CREATE INDEX host_dex ON shjlog (host);
CREATE INDEX uri_dex ON shjlog (uri);
CREATE INDEX phpsessid_dex ON shjlog (phpsessid);
CREATE INDEX useragent_dex ON shjlog (useragent);
CREATE INDEX referer_dex ON shjlog (referer);


#!/usr/bin/php
<?php

include_once 'SHJ/debug.inc.php';
include_once 'SHJ/DB/PDOExt.inc.php';

$db = new SHJPDOExt('pgsql', '/etc/apache2/shjlog.ini');
$fields->ip = '';
$fields->port = '';
$fields->bytes = '';
$fields->inbytes = '';
$fields->outbytes = '';
$fields->protocol = '';
$fields->method = '';
$fields->host = '';
$fields->file = '';
$fields->uri = '';
$fields->status = '';
$fields->phpsessid = '';
$fields->useragent = '';
$fields->referer = '';
$fields->responsetime = '';
$fields->datetime = '';
$stmt = $db->insert('shjlog', $fields);

$fp = fopen('php://stdin', 'r');

while(true) {
    $line = fgets($fp, 4096);
    if(!preg_match('/^ip:"(.*?)" port:"(.*?)" bytes:"(.*?)" inbytes:"(.*?)" outbytes:"(.*?)" protocol:"(.*?)" method:"(.*?)" host:"(.*?)" file:"(.*?)" uri:"(.*?)" status:"(.*?)" phpsessid:"(.*?)" useragent:"(.*?)" referer:"(.*?)" responsetime:"(.*?)" datetime:"(.*?)"$/s', $line, $matches)) {
        continue;
    }
    $fields->ip = $matches[1];
    $fields->port = $matches[2];
    $fields->bytes = $matches[3];
    $fields->inbytes = $matches[4];
    $fields->outbytes = $matches[5];
    $fields->protocol = $matches[6];
    $fields->method = $matches[7];
    $fields->host = $matches[8];
    $fields->file = $matches[9];
    $fields->uri = $matches[10];
    $fields->status = $matches[11];
    $fields->phpsessid = $matches[12];
    $fields->useragent = $matches[13];
    $fields->referer = $matches[14];
    $fields->responsetime = $matches[15];
    $fields->datetime = $matches[16];
    try {
        $stmt->exec();
    } catch(Exception $e) {
        $fp2 = fopen('/etc/apache2/shjlogerr.txt', 'w');
        fwrite($fp2, $e->getMessage());
        fclose($fp2);
        $db = new SHJPDOExt('pgsql', '/etc/apache2/shjlog.ini');
        $stmt = $db->insert('shjlog', $fields);
        try {
            $stmt->exec();
        } catch(Exception $e) {}
    }
}

?>

댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
16-06-03 조회 3,035
15-07-30 조회 3,422
15-06-08 조회 5,564
15-05-27 조회 4,421
15-01-08 조회 6,722
14-12-30 조회 5,454
14-10-10 조회 5,444
14-08-13 조회 6,360
14-08-11 조회 5,217
14-08-06 조회 6,549
14-03-19 조회 7,443
13-04-26 조회 9,598
12-12-15 조회 5,122
12-12-15 조회 7,358
12-12-11 조회 5,504
12-11-21 조회 6,032
12-10-11 조회 5,107
12-10-03 조회 4,801
12-09-20 조회 6,099
12-09-02 조회 4,276
12-08-29 조회 5,290
12-08-29 조회 4,167
12-08-24 조회 4,272
12-08-18 조회 4,488
12-08-17 조회 4,529
12-08-16 조회 3,788
12-08-06 조회 5,563
12-08-05 조회 4,849
12-08-03 조회 4,565
12-08-03 조회 4,718
12-07-29 조회 3,564
12-07-24 조회 4,155
12-07-22 조회 3,622
12-07-20 조회 4,386
12-07-19 조회 4,728
12-07-19 조회 3,954
12-07-19 조회 3,900
12-07-19 조회 5,566
12-07-19 조회 3,509
12-07-18 조회 5,202
12-07-15 조회 4,179
12-07-14 조회 3,848
12-07-12 조회 4,520
12-07-11 조회 3,794
12-07-11 조회 3,515
12-07-11 조회 3,491
12-07-10 조회 4,679
12-07-09 조회 3,428
12-07-09 조회 4,245
12-07-09 조회 4,663
12-07-08 조회 4,579
12-07-07 조회 5,503
12-07-06 조회 4,755
12-07-06 조회 4,024
12-07-06 조회 3,907
12-07-06 조회 3,849
12-07-02 조회 5,524
12-07-02 조회 4,221
12-06-29 조회 3,751
12-06-28 조회 4,369
12-06-26 조회 3,864
12-06-26 조회 4,986
12-06-21 조회 3,961
12-06-20 조회 4,661
12-06-20 조회 4,299
12-06-15 조회 4,654
12-06-15 조회 4,388
12-06-13 조회 7,405
12-06-05 조회 7,379
12-06-04 조회 3,955
12-06-03 조회 3,806
12-05-31 조회 3,799
12-05-28 조회 6,647
12-05-27 조회 3,878
12-05-25 조회 4,015
12-05-23 조회 4,423
12-05-21 조회 5,135
12-05-17 조회 4,066
12-05-17 조회 3,985
12-05-09 조회 4,061
12-05-07 조회 4,319
12-05-02 조회 4,504
12-05-01 조회 4,682
12-04-30 조회 3,430
12-04-30 조회 4,374
12-04-25 조회 4,464
12-04-25 조회 4,887
12-04-24 조회 3,856
12-04-24 조회 4,624
12-04-23 조회 3,614
12-04-20 조회 3,274
12-04-17 조회 5,457
12-04-16 조회 4,395
12-04-14 조회 3,673
12-04-14 조회 3,709
12-04-14 조회 3,758
12-04-13 조회 3,378
12-04-13 조회 4,565
12-04-13 조회 6,207
12-04-12 조회 3,369