3.22.61.246 |    

Navigation

Google Advertisement

Dispay a list of predefined column of http logfiles.

httplogs.sh
  1. #!/bin/bash
  2. # *****************************************************************************
  3. # file: httplogs.sh
  4. # date: 2007-12-17 15:50
  5. # author: Marko Schulz - <info@tuxnet24.de>
  6. # description: Dispay a list of predefined column of http logfiles.
  7. # *****************************************************************************
  8. # This function display the program usaage.
  9.  
  10. function f_alert () {
  11.  
  12. local error=$1
  13. local header=$( head -n7 $0 | grep -v '/bin/bash' )
  14.  
  15. echo -e "$header\n"
  16. [ -n "$error" ] && echo -e "\aERROR: ${error}\n"
  17. echo -e "Usage: $( basename $0 ) -f /path/to/htttp/logfile [ -c \"<column1> <column2> <columnX>\" ] [ -h ]\n"
  18. echo -e "\t-f http logfile (e.g. /var/log/apache/access.log)"
  19. echo -e "\t-c column list, 0 is the first column (e.g. \"1 2 5\")"
  20. echo -e "\t-h display this screen\n\n"
  21. echo -e "  meaning of columns\n"
  22. echo -e "\t 0 => Date"
  23. echo -e "\t 1 => Server Alias"
  24. echo -e "\t 2 => Host"
  25. echo -e "\t 3 => User"
  26. echo -e "\t 4 => Method"
  27. echo -e "\t 5 => Request URL"
  28. echo -e "\t 6 => Protocol"
  29. echo -e "\t 7 => MimeType"
  30. echo -e "\t 8 => ResponseCode"
  31. echo -e "\t 9 => Size (in bytes)"
  32. echo -e "\t10 => ProcessTime"
  33. echo -e "\t11 => Useragent"
  34. echo -e "\t12 => Referer"
  35. echo -e "\t13 => Cookie\n"
  36. exit 1
  37.  
  38. }
  39.  
  40. # *****************************************************************************
  41. # This function read a text or compressed text file.
  42.  
  43. function f_cat () {
  44.  
  45. case "$1" in
  46. 	*gz|*Z) zcat $1 ;;
  47. 	*) cat $1 ;;
  48. esac
  49.  
  50. }
  51.  
  52. # *****************************************************************************
  53. # get the commad line arguments...
  54.  
  55. while getopts f:c:h Optionen 2>/dev/null; do
  56.         case $Optionen in
  57.                 f) pFile=$OPTARG ;;
  58.                 c) pLine=$OPTARG ;;
  59.                 h) f_alert ;;
  60.                 *) f_alert "invalid argument" ;;
  61.         esac
  62. done
  63.  
  64. # check if logfile is defined...
  65. [ ! -f "$pFile" ] && f_alert "No file defined"
  66.  
  67. # replace spaces with dashes...
  68. pLine=$( echo $pLine | sed 's/[[:space:]]/-/g' )
  69.  
  70. # read & dispay http logfile -> Perl rules ;-)
  71. f_cat $pFile | perl -ne '
  72. BEGIN {
  73. $string="'$pLine'";
  74. }
  75.  
  76. # split line in array elements... 
  77. my @line=split( /"\s+"/, $_ );
  78.  
  79. # remove new line and " character...
  80. map { $_ =~ s/("|\n)//g } @line;
  81.  
  82. # split request in array elements...
  83. my @req=split( /\s+/, $line[4] );
  84.  
  85. # extract the request entry ( like: GET /index.php HTTP/1.1 )
  86. # and replace them with the single entrys from array "@req".
  87. splice( @line, 4, 1, $req[0] );
  88. splice( @line, 5, 0, $req[1] );
  89. splice( @line, 6, 0, $req[2] );
  90.  
  91. if ( $string ne "" ) {
  92.  
  93. 	my @args=split( /-/, $string );
  94. 	foreach my $i ( @args ) { print $line[$i]." "; }
  95. 	print "\n";
  96.  
  97. } else {
  98.  
  99. 	foreach my $i ( @line ) { print $i." "; }
  100. 	print "\n";
  101.  
  102. }
  103.  
  104. '
  105.  
  106. exit 0
  107.  
  108. # *****************************************************************************
  109. # end of this script...
Parsed in 0.005 seconds at 625.65 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 6 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)