Filter log file entries based on date range

前端 未结 3 483
刺人心
刺人心 2020-11-22 07:32

My server is having unusually high CPU usage, and I can see Apache is using way too much memory. I have a feeling, I\'m being DOS\'d by a single IP - maybe you can help me f

相关标签:
3条回答
  • 2020-11-22 07:35

    If someone encounters with the awk: invalid -v option, here's a script to get the most active IPs in a predefined time range:

    cat <FILE_NAME> | awk '$4 >= "[04/Jul/2017:07:00:00" && $4 < "[04/Jul/2017:08:00:00"' | awk '{print $1}' | sort -n | uniq -c | sort -nr | head -20
    
    0 讨论(0)
  • 2020-11-22 07:41

    As this is a common perl task

    And because this is not exactly same than extract last 10 minutes from logfile where it's about a bunch of time upto the end of logfile.

    And because I've needed them, I (quickly) wrote this:

    #!/usr/bin/perl -ws
    # This script parse logfiles for a specific period of time
    
    sub usage {
        printf "Usage: %s -s=<start time> [-e=<end time>] <logfile>\n";
        die $_[0] if $_[0];
        exit 0;
    }
    
    use Date::Parse;
    
    usage "No start time submited" unless $s;
    my $startim=str2time($s) or die;
    
    my $endtim=str2time($e) if $e;
    $endtim=time() unless $e;
    
    usage "Logfile not submited" unless $ARGV[0];
    open my $in, "<" . $ARGV[0] or usage "Can't open '$ARGV[0]' for reading";
    $_=<$in>;
    exit unless $_; # empty file
    # Determining regular expression, depending on log format
    my $logre=qr{^(\S{3}\s+\d{1,2}\s+(\d{2}:){2}\d+)};
    $logre=qr{^[^\[]*\[(\d+/\S+/(\d+:){3}\d+\s\+\d+)\]} unless /$logre/;
    
    while (<$in>) {
        /$logre/ && do {
            my $ltim=str2time($1);
            print if $endtim >= $ltim && $ltim >= $startim;
        };
    };
    

    This could be used like:

    ./timelapsinlog.pl -s=09:18 -e=09:24 /path/to/logfile
    

    for printing logs between 09h18 and 09h24.

    ./timelapsinlog.pl -s='2017/01/23 09:18:12' /path/to/logfile
    

    for printing from january 23th, 9h18'12" upto now.

    In order to reduce perl code, I've used -s switch to permit auto-assignement of variables from commandline: -s=09:18 will populate a variable $s wich will contain 09:18. Care to not miss the equal sign = and no spaces!

    Nota: This hold two diffent kind of regex for two different log standard. If you require different date/time format parsing, either post your own regex or post a sample of formatted date from your logfile

    ^(\S{3}\s+\d{1,2}\s+(\d{2}:){2}\d+)         # ^Jan  1 01:23:45
    ^[^\[]*\[(\d+/\S+/(\d+:){3}\d+\s\+\d+)\]    # ^... [01/Jan/2017:01:23:45 +0000]
    
    0 讨论(0)
  • 2020-11-22 07:44

    yes, there are multiple ways to do this. Here is how I would go about this. For starters, no need to pipe the output of cat, just open the log file with awk.

    awk -vDate=`date -d'now-2 hours' +[%d/%b/%Y:%H:%M:%S` '$4 > Date {print Date, $0}' access_log
    

    assuming your log looks like mine (they're configurable) than the date is stored in field 4. and is bracketed. What I am doing above is finding everything within the last 2 hours. Note the -d'now-2 hours' or translated literally now minus 2 hours which for me looks something like this: [10/Oct/2011:08:55:23

    So what I am doing is storing the formatted value of two hours ago and comparing against field four. The conditional expression should be straight forward.I am then printing the Date, followed by the Output Field Separator (OFS -- or space in this case) followed by the whole line $0. You could use your previous expression and just print $1 (the ip addresses)

    awk -vDate=`date -d'now-2 hours' +[%d/%b/%Y:%H:%M:%S` '$4 > Date {print $1}' | sort  |uniq -c |sort -n | tail
    

    If you wanted to use a range specify two date variables and construct your expression appropriately.

    so if you wanted do find something between 2-4hrs ago your expression might looks something like this

    awk -vDate=`date -d'now-4 hours' +[%d/%b/%Y:%H:%M:%S` -vDate2=`date -d'now-2 hours' +[%d/%b/%Y:%H:%M:%S` '$4 > Date && $4 < Date2 {print Date, Date2, $4} access_log'
    

    Here is a question I answered regarding dates in bash you might find helpful. Print date for the monday of the current week (in bash)

    0 讨论(0)
提交回复
热议问题