Saturday, 16 February 2008

Monitoring all apache virtual hosts on plesk with logwatch

The standard Plesk install has logwatch setup to just monitor the default httpd log, i.e /var/www/httpd/*

This is miss most things as the log file output of all the virtual hosts are not parsed. So I made a little bash script to create a load of new logwatch conf files to parse all my virtual host logfiles too. You will need to run this script each time a new domain is created. A know there is a Plesk way to run custom scripts on events, but I'll leave that for another day.


#!/bin/bash

##
## Script to create conf files and script links on a Plesk
## server that monitor all the apache log files
##
## Visit blog.surgeons.org.uk for updates

## Location of the virtual hosts directories
vhost_root=/var/www/vhosts/

## Various logwatch directories
dir_services="/etc/logwatch/conf/services"
dir_logfiles="/etc/logwatch/conf/logfiles"
dir_scripts="/etc/logwatch/scripts/services"
http_script="/usr/share/logwatch/scripts/services/http"

## Now iter over each directory
for domain in $( ls -Ichroot -Idefault $vhost_root ); do
if [ -d "${vhost_root}${domain}" ]
then
echo "Making services logwatch enteries for ${domain}"
domain_us=`echo $domain | tr . _`
(
cat <<-END_OF_SERVICES_CONF

###########################################################################
# Configuration file for $domain http filter
# See blog.surgeons.org.uk for updates ###########################################################################
Title = "httpd - $domain"

# Which logfile group...
LogFile = http_$domain_us
END_OF_SERVICES_CONF ) > ${dir_services}/http_${domain_us}.conf


echo "Making logfiles logwatch entries for ${domain}"
(
cat <<-END_OF_LOGFILES_CONF
########################################################
# Define log file group for http_$domain_us
# See blog.surgeons.org.uk for updates
#######################################################
LogFile = /var/www/vhosts/$domain/statistics/logs/access_log
LogFile = /var/www/vhosts/$domain/statistics/logs/access_log.processed
LogFile = /var/www/vhosts/$domain/statistics/logs/access_ssl_log
LogFile = /var/www/vhosts/$domain/statistics/logs/access_ssl_log.processed

# If the archives are searched, here is one or more line
# (optionally containing wildcards) that tell where they are...
# If you use a "-" in naming add that as well -mgt

Archive = /var/www/vhosts/$domain/statistics/logs/access_log.processed.?.gz
Archive = /var/www/vhosts/$domain/statistics/logs/access_ssl__log.processed.?.gz

# Expand the repeats (actually just removes them now)
*ExpandRepeats

# Keep only the lines in the proper date range...
*ApplyhttpDate
END_OF_LOGFILES_CONF ) > ${dir_logfiles}/http_${domain_us}.conf


## Make the script links
echo "Creating script link for ${domain}"
ln -s ${http_script} ${dir_scripts}/http_${domain_us}

fi

done

6 comments:

Stefan said...

Hi,
i'm very interessted in your script but i get an error:
line 71: syntax error: unexpected end of file
Do you have any idea whats going wrong? Thanks!

Anonymous said...

Same error here "./plesk.conf: line 71: syntax error: unexpected end of file"

Stas said...

Hello.
Your script is very helpful. However I'm getting the same error: Unexpected end of file.

tplehman said...

hey thanks for the nice script, the bash unexpected end of file issue if fixed by removing some anomalous dashes and periods around the << operators. Also adding another pipe to the tr cmd to strtolower the domain name for the domain_us var value assignment was needed in my case.

Thanks again

rcain said...

Really useful (draft) script, thanks Neville. Just what I was looking for.

To save everyone else the bother, I've posted a corrected, WORKING and tested version here: http://pastebin.com/duabDcSZ

All the best



Neville Dastur said...

Your're welcome. I have to admit that I moved from plesk a while back and so haven't needed to revisit the script.