Igor Gariev (gariev) wrote in changelog,
Igor Gariev
gariev
changelog

[ljcom] r10513: LJSUP-8793: log incoming to Apache reque...

Committer: gariev
LJSUP-8793: log incoming to Apache request's start and stop to file
U   trunk/cgi-bin/LJ/Hooks/SUP.pm
Modified: trunk/cgi-bin/LJ/Hooks/SUP.pm
===================================================================
--- trunk/cgi-bin/LJ/Hooks/SUP.pm	2011-05-17 05:14:10 UTC (rev 10512)
+++ trunk/cgi-bin/LJ/Hooks/SUP.pm	2011-05-17 06:43:30 UTC (rev 10513)
@@ -7,6 +7,7 @@
 use LJ::HomepageItems;
 use LJ::Request;
 use Encode();
+use GTop;
 
 my $udp_log;
 
@@ -1015,35 +1016,58 @@
         push @$req_list, $client_ip;
     }
 });
+
 {
- LJ::register_hook("post_read_request" => sub {
-    return unless LJ::is_enabled("log_requests_start_stop");
+    my $gtop;
 
-    my $rec = LJ::Request->header_in("Host") . LJ::Request->uri . "?" . LJ::Request->args;
-    my (undef, $swap, $it) = split /\s+/ => `cat /proc/meminfo|grep SwapFree`;
+    ## return hashref with /proc/meminfo data
+    sub _meminfo {
+        local *FILE;
+        my %data;
+        open FILE, '/proc/meminfo' or return;
+        while (<FILE>) {
+            chomp;
+            my ($k, $v) = split /:\s*/, $_, 2;
+            $data{$k} = $v;
+        }
+        close FILE;
+        
+        $gtop ||= GTop->new;
+        my $pm = $gtop->proc_mem($$);
+        my $size = $pm->size;
+        $data{'_proc_size'} = $size;    ## virtual size
 
-    ## write from all process to the same file may cause data corruption,
-    ## but it is not critical here.
-    local *FILE;
-    open FILE, ">>", $LJ::REQUESTS_LOG_FILE or warn "Can't open $LJ::REQUESTS_LOG_FILE: $!";
-    print FILE "S $$ " . time() . " " . $rec . " $swap $it\n";
-    close FILE;
+        return \%data;
+    }
 
- });
- LJ::register_hook("clenaup_end_request" => sub {
-    return unless LJ::is_enabled("log_requests_start_stop");
+    sub _print_log_info {
+        my $tag = shift;
 
-    my $rec = LJ::Request->header_in("Host") . LJ::Request->uri . "?" . LJ::Request->args;
-    my (undef, $swap, $it) = split /\s+/ => `cat /proc/meminfo|grep SwapFree`;
+        return unless LJ::is_enabled("log_requests_start_stop");
 
-    ## write from all process to the same file may cause data corruption,
-    ## but it is not critical here.
-    local *FILE;
-    open FILE, ">>", $LJ::REQUESTS_LOG_FILE or warn "Can't open $LJ::REQUESTS_LOG_FILE: $!";
-    print FILE "E $$ " . time() . " " . $rec . " $swap $it\n";
-    close FILE;
+        my $rec = LJ::Request->header_in("Host") . LJ::Request->uri . "?" . LJ::Request->args;
+        my $meminfo = _meminfo();
+        my $swap = $meminfo->{'Active'};
+        my $it = $meminfo->{'SwapFree'};
+        my $proc_size = $meminfo->{'_proc_size'};
+        my $time = time();
+        ## write from all process to the same file may cause data corruption,
+        ## but it is not critical here.
+        local *FILE;
+        open FILE, ">>", $LJ::REQUESTS_LOG_FILE or warn "Can't open $LJ::REQUESTS_LOG_FILE: $!";
+        print FILE "$tag $$ $time $swap $it $proc_size $rec\n";
+        close FILE;
+       
+    }
+}
 
+{
+ LJ::register_hook("post_read_request" => sub {
+    _print_log_info("S"); 
  });
+ LJ::register_hook("clenaup_end_request" => sub {
+    _print_log_info("E"); 
+ });
 }
 
 sub _is_trusted_ip {

Tags: gariev, ljcom, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments