bigjocker's den

One-liner to view Apache hits per second in real time

Usually I would use apachetop. But some times you don’t have it installed in your server, and installing a big web log analyzer is not always an option. In those cases I use this little one-liner that shows the hits per second in real time:

tail -f access_log | awk -F: '{print $2$3$4}' |
        awk 'BEGIN {a = ""; c = 0;} {if (a != "") {if ($1 == a) c++; else {print a, c; a=$1; c = 1;}} else {a = $1; c = 1;}}'

The first part is pretty straightforward. The formatted AWK script looks like this:

BEGIN {
        a = "";
        c = 0;
}
{
       if (a != "") {
              if ($1 == a) {
                     c++;
              } else {
                     print a, c;
                     a=$1;
                     c = 1;
              }
       } else {
              a = $1; c = 1;
       }
}

It’s very simple: a and c store the timestamp and the number of times that timestamp has appeared on the log. We will print the timestamp and the counter when a different timestamp is found, and we will also replace the timestamp with the new one and reset the counter. It’s not pretty, but gives a real time, quick and accurate read of the hits we are getting in that server.

Leave a Reply

Your email address will not be published. Required fields are marked *