Apache Logging Filter Robots

[problem]

Sick of filtering through loads of logs, or just spotting real hits from the robots! 🙂

Seriously reduce your apache web logs, by filtering out images, style sheets and your own hits.

[/problem]

[solution]

Simple with Apache’s customlog and setenvif statements.

I’ve also included capturing the user-agent in a separate file, as well as the referer, which is brill for seeing which google searches brought traffic to you.

You can even still capture robot and own hits into a separate log, here is how below.

[/solution]

[example]

         SetEnvIf Request_URI ".(png|gif|jpg|js|css)" image-req         SetEnvIf Request_URI "favicon.ico" image-req         SetEnvIf Request_URI "/icons" image-req         SetEnvIf Request_URI "sitemap.xml.gz" image-req         SetEnvIf REMOTE_ADDR "127.0.0.1" image-req         SetEnvIf REMOTE_ADDR "127.0.0.1" home-req         SetEnvIf User-agent "(Googlebot|msnbot|Spider|crawl|slurp|Jeeves| Mediapartners|FeedBurner)" image-req         SetEnvIf User-agent "(Googlebot|msnbot|Spider|crawl|slurp|Jeeves| Mediapartners|FeedBurner)" bot-req      CustomLog logs/access_log.techieblogs "["%{Referer}i"]n %h %l %u %t "%r" %>s %b" env=!image-req     CustomLog logs/access_log.agents.techieblogs "%h ["%{Referer}i"] ["%{User-agent}i"]" env=!image-req     CustomLog logs/access_log.bots.techieblogs "["%{Referer}i"] %h %l %u %t "%r" %>s %b" env=bot-req     CustomLog logs/access_log.home.techieblogs "["%{Referer}i"] n %h %l %u %t "%r" %>s %b" env=home-req 

[/example]

[reference]

[tags]Apache Logging, Unix Coding School[/tags]

[/reference]

Troubleshoot Apache Startup

[problem]

Here are a few tips, if you are having problems starting apache.

1. A common problem is trying to start Apache on a restricted port. This is where you configure apache to listen on port 80, changing the default 8080.

2. Another problem can be defining logging into directories that don’t exist.

3. If you get an error like User not allowed, or Group not recognized, etc – this is the User and Group statements within your apache configs.

[/problem]

[solution]

1. On UNIX this is considered restricted and generally only available to the root user. On windows it is also commonly restricted, so requires an admin account.

2. This will generally output an error along those lines, into apache’s top level error log. Failing that you can try running apache with truss (search my site to find syntax) on Solaris, strace on Linux and trace on AIX.

3. You need to change to match the user running the command – or if root, the delegated user and group user is in.

Another tip is to run /etc/init.d/httpd configtest – which will effectively run httpd -t to syntax check the config. You can also perform a -S which checks and prints your virtual hosts.

If you getting compliants about modules, try running httpd -l to see compile in mods.

[/solution]

[example]

Nothing here – please see Solution 🙂

[/example]

[reference]

[tags]apache, Unix Coding School[/tags]

[/reference]

Job Control

[problem]

You want to background a process, or leave it running whilst shutting the terminal.

[/problem]

[solution]

To start a UNIX process is the background, simply append the command with an ampersand, like this below.

[/solution]

[example]


sleep 30&;

If you have already started a process and want to start in the background, just hit control Z. Then type bg to background (and continue running) the command.

To display jobs in the background, runs command jobs

To bring back into the foreground, run fg

It is always a good idea to redirect output, for backgrounded jobs. Also if you are going to exit the shell, need to prefix the command with nohup. Or if it is already running, background the job and run disown

[/example]

[reference]

[tags]UNIX Batch Mode, UNIX Background Process, Unix Coding School[/tags]

[/reference]

Watch Multiple Files Simultaneously

[problem]

Performing debugging and you need to view the output, of more than one file at once.

[/problem]

[solution]

A simple way to keep an eye on a file is with tail.


tail -f logfile

You can background this, by appending an ampersand:


tail -f logfile&

[/solution]

[example]

But if you want to watch multiple files, how do you know which one received the update.

Sed to the rescue:


tail -f logfilea | sed 's/^/logfilea: /'&
tail -f logfileb | sed 's/^/logfileb: /'&

[/example]

[reference]

[tags], Unix Coding School[/tags]

[/reference]

Vi Map Keys

[problem]

Are there a combinations of key strokes you constantly enter?

[/problem]

[solution]

Maybe you are a Java programming! 🙂 Or PHP/Perl – either way if you are using vi a lot, these tips could be worth your pc’s weight in gold. 😉

[/solution]

[example]

To create a one time map (of any key), just run this within vi:


:map #1 :!echo hey mapped f1^M

You need to hold down control and hit m to get the return, so this will run itself (no enter required).

To see current maps, just type :map

To make it permanent, just add same syntax to $HOME/.exrc

I think another good tip, is to map f1 to cat the exrc – like this:


:map #1 :!cat $HOME/.exrc^M

Then map #2, 3, 4 …

[/example]

[reference]

[tags]vi, macros, Unix Coding School[/tags]

[/reference]

Vi Swap Columns

[problem]

You need to substitute columns, whilst editing a file with vi.

[/problem]

[solution]

Okay, bit more complex with this one.

To substitute columns, we bracket off the pattern – like this (pattern)

Then we simply use an escaped number to substitute.

[/solution]

[example]

Here is a demo, whilst editing a file with vi:


:%s/(.*) (.*)/2 1/

This says match all lines (%), substitute (s). Match anything up to a space, store in column one. Match anything else, store in column two. Now swop column two with column one.

Here is a run through – unchanged:


a z
b y
c x
d w
e v
f u
g t
h s
i r
j q
k p
l o
m n

The substitute:


:%s/(.*) (.*)/2 1/

The result:


z a
y b
x c
w d
v e
u f
t g
s h
r i
q j
p k
o l
n m

[/example]

[reference]

[tags]UNIX, vi, regular expression, Unix Coding School[/tags]

[/reference]

Vi Exit

[problem]

Exiting a file in vi.

[/problem]

[solution]

There are quite a few ways to exit a file in vi. Here are some of my favorites.

[/solution]

[example]

The standard: :wq

Force, in the case of read-only: :wq!

Abort updates: :q!

Reset the file without writing: :e!

Quick save and exit: 😡

Lazy one, without using a colon: ZZ

Going to another file: n filename

Go back to the first file, when you’v edited with wildcard or list of files: :rew

[/example]

[reference]

[tags]vi, Unix Coding School[/tags]

[/reference]

Date Function Calcualate Last Sunday

[problem]

Showing the date last Sunday, using Perl.

[/problem]

[solution]

Perl has some powerful functions, to perform date manipulation. Such as strftime, localtime and mktime.

[/solution]

[example]


#!/usr/local/bin/perl

use POSIX qw(strftime);

$epoch=$^T; # you get a control T by holding down ctrl key and pressing v key, then T key.

$day=strftime "%a", localtime($epoch); # this captures the day of the week for today (Wed).
$myDate=strftime "%Y-%m-%d", localtime($epoch); # this captures the year, month and day for today

print("myDate: $myDatenday: $dayn"); # this just displays date today and day of week today, first 2 lines of output below

until($day =~ /Sun/) { # We then spin around until the day equals Sun

$epoch-=(60*60*24); # take 24 hours off our epoch
$day=strftime "%a", localtime($epoch); # calculate the day of the week for the new epoch
}

$myDate=strftime "%Y-%m-%d", localtime($epoch); # recalculate date for that epoch
print("day: $daynday: $daynmyDate: $myDaten"); # display, second 2 lines of output below

[/example]

[reference]

[tags]POSIX, strftime, localtime, Perl Coding School[/tags]

[/reference]

Converting String Scalar Number

[problem]

Converting a string scalar into a number.

[/problem]

[solution]

To convert a string scalar to a number, just add zero.

Sometimes Perl calculations get a bit screwed, when Perl decides it is dealing with a string – not a number.

[/solution]

[example]

Here is the code to fix it up:


print $scalar + 0;
$str+=0;

[/example]

[reference]

[tags]Scalars, Perl Coding School[/tags]

[/reference]

Convert String Array

[problem]

Converting a string into an array

[/problem]

[solution]

To convert a string to an array, use split.

[/solution]

[example]

Here we have a string, comprising of fields separated by commas.

So we just split on the comma, but this could also be spaces, etc.


$str="one,two,three";
@rr=split ',', $str;
print $#rr."n"; # outputs 2

[/example]

[reference]

[tags], Perl Coding School[/tags]

[/reference]