Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  - Adding first version of wa-admin to Git
  • Loading branch information
Marin Atanasov Nikolov committed Feb 5, 2011
0 parents commit 3fdb8ed
Show file tree
Hide file tree
Showing 6 changed files with 1,128 additions and 0 deletions.
83 changes: 83 additions & 0 deletions README
@@ -0,0 +1,83 @@
wa-admin README.
################

This file just contains some basic instructions to get wa-admin running on your box.

I know it's a bit messy without any documentation or man pages, but that's the result of
just a few hours work, trying to get my logs being added to webalizer in a simple way :-)

So, to install wa-admin, just follow these steps:

1. Get the latest wa-admin copy from the Git repository:

$ git clone git://git.unix-heaven.org/public/wa-admin

2. Create the needed directories:

$ sudo mkdir -p /usr/local/www/wa-admin
$ sudo mkdir -p /usr/local/etc/wa-admin

3. Copy the template files and configuration

$ sudo cp wa-admin/wa-admin.css /usr/local/www/wa-admin/
$ sudo cp wa-admin/*.html /usr/local/etc/wa-admin/
$ sudo cp wa-admin/webalizer.conf.tpl /usr/local/etc/wa-admin/

4. Edit /usr/local/etc/wa-admin/webalizer.conf.tpl

This is a just a template file, which is an actual webalizer.conf file.

Just edit it and add/remove what you need or what you don't.

One thing to keep in mind is to leave the following options as they are,
since they are being used by the wa-admin tool to prepare the template for the vhost:

- LogFile
- HostName
- OutputDir
- DNSCache

If you do not want DNS reverse lookups, just comment the corresponding DNS lines from
the template file.

5. Add the hosts with their log files

To add new log files to be analyzed by webalizer, just do the following:

$ sudo touch /usr/local/etc/wa-admin/wa-admin.vhosts

Please, note that the above is needed to be run only once - the first time
you install wa-admin.

Now to add a new log file, just do the following:

$ sudo wa-admin add <hostname> <path-to-log-file>

Just repeat the above command for all hosts and log files, until ready.

6. Modify Apache

You will need to modify Apache configuration, so that it
finds the wa-admin DocumentRoot, which currently defaults to /usr/local/www/wa-admin

7. Run wa-admin from cron

To run wa-admin from cron, simply put the following line to your /etc/crontab

5 * * * * root /path/to/wa-admin cron

This will run wa-admin every hour and 5 minutes.

8. When ready execute wa-admin run

$ sudo wa-admin run

This will go through all added to wa-admin vhosts and create the graphs

It might take some time, until it finishes, if you are running with the DNS
resolver settings for webalizer.

9. Check your graphs! :)

Now open up a browser and go to your wa-admin Apache vhost.

2 changes: 2 additions & 0 deletions footer.html
@@ -0,0 +1,2 @@
</body>
</html>
7 changes: 7 additions & 0 deletions header.html
@@ -0,0 +1,7 @@
<html>
<head>
<title>webalizer-admin - usage statistics for your vhosts</title>
<link rel="stylesheet" href="wa-admin.css">
</head>
<body>
<h2>unix-heaven.org - wa-admin home page</h2>
217 changes: 217 additions & 0 deletions wa-admin
@@ -0,0 +1,217 @@
#!/bin/sh

#
# wa-admin - a tool for managing webalizer log files
#

CAT=/bin/cat
CP=/bin/cp
BASENAME=/usr/bin/basename
DATE=/bin/date
CUT=/usr/bin/cut
GREP=/usr/bin/grep
MKDIR=/bin/mkdir
RM=/bin/rm
SORT=/usr/bin/sort
SED=/usr/bin/sed
TEE=/usr/bin/tee

wa_admin_prefix=/usr/local
wa_admin_etc=${wa_admin_prefix}/etc
wa_admin_vhosts=${wa_admin_etc}/wa-admin/wa-admin.vhosts
wa_admin_version="0.1"
wa_admin=$(${BASENAME} -- $0)

# read configuration file if any
[ -f "${wa_admin_etc}/wa-admin.conf" ] && . "${wa_admin_etc}/wa-admin.conf"

# set default variable values
: ${wa_admin_doc_root="/usr/local/www/wa-admin"}
: ${wa_admin_conf_template="${wa_admin_etc}/wa-admin/webalizer.conf.tpl"}
: ${wa_admin_html_header="${wa_admin_etc}/wa-admin/header.html"}
: ${wa_admin_html_footer="${wa_admin_etc}/wa-admin/footer.html"}
: ${webalizer_bin="/usr/local/bin/webalizer"}

# usage messages
wa_admin_usage="${wa_admin} -- version ${wa_admin_version}\nUsage: ${wa_admin} [list|add|delete|run|cron|create-index]"
wa_admin_usage_list="Usage: ${wa_admin} list"
wa_admin_usage_cron="Usage: ${wa_admin} cron"
wa_admin_usage_create_index="Usage: ${wa_admin} create-index"
wa_admin_usage_add="Usage: ${wa_admin} add <hostname> <log-file>"
wa_admin_usage_delete="Usage: ${wa_admin} delete <hostname> <log-file>"

#
# Function definitions
#

#
# errmsg
# Prints an error message and exists
#

errmsg () { echo -e "$*" && exit 1; }

#
# wa-admin list
# Lists the currently added to webalizer vhosts
#

wa_admin_list () {
[ ! -f ${wa_admin_etc}/wa-admin/wa-admin.vhosts ] && errmsg "${wa_admin}: There is no ${wa_admin_etc}/wa-admin.vhosts file."

printf "%-30s %-30s\n" Hostname LogFile
echo "------------------------------ ------------------------------"
while read vhost_entry; do
vhost_name=$(echo ${vhost_entry} | ${CUT} -d '|' -f 1)
vhost_log=$(echo ${vhost_entry} | ${CUT} -d '|' -f 2)
printf "%-30s %-30s\n" ${vhost_name} ${vhost_log}
done < ${wa_admin_vhosts}
}

#
# wa-admin add
# Adds a new log file to be analyzed by webalizer
#

wa_admin_add () {
vhost_name=$1
vhost_log=$2
log_name=$(${BASENAME} ${vhost_log})

# check if the log file is already added to wa-admin
${GREP} -x "${vhost_name}|${vhost_log}" ${wa_admin_vhosts} 1> /dev/null 2>&1

[ $? -eq 0 ] && errmsg "${wa_admin}: LogFile ${vhost_log} is already added for host ${vhost_name}."

[ ! -f ${vhost_log} ] && errmsg "${wa_admin}: LogFile ${vhost_log} does not exists."

# add the log file to wa-admin
vhost_conf_dir=${wa_admin_doc_root}/${vhost_name}/${log_name}

${MKDIR} -p ${vhost_conf_dir}
${CP} ${wa_admin_conf_template} ${vhost_conf_dir}/webalizer.conf
${SED} -i "" -e "s|@HOSTNAME@|${vhost_name}|" \
-e "s|@LOGFILE@|${vhost_log}|" \
-e "s|@OUTPUTDIR@|${vhost_conf_dir}|" \
-e "s|@DNSCACHE@|${wa_admin_doc_root}/dns_cache.db|" ${vhost_conf_dir}/webalizer.conf
echo "${vhost_name}|${vhost_log}" >> ${wa_admin_vhosts}

echo "LogFile ${vhost_log} added for host ${vhost_name}."
wa_admin_create_index
}

#
# wa-admin delete
# Deletes a log entry from the wa-admin configuration
#

wa_admin_delete () {
vhost_name=$1
vhost_log=$2
log_name=$(${BASENAME} ${vhost_log})

vhost_line=$(${GREP} -nx "${vhost_name}|${vhost_log}" ${wa_admin_vhosts} | ${CUT} -d ':' -f 1)

[ $? -ne 0 -o -z "${vhost_line}" ] && errmsg "${wa_admin}: LogFile ${vhost_log} for host ${vhost_name} is not added."

vhost_conf_dir=${wa_admin_doc_root}/${vhost_name}/${log_name}

${SED} -i "" "${vhost_line}d" ${wa_admin_vhosts}
${RM} -rf ${vhost_conf_dir}

echo "LogFile ${vhost_log} for host ${vhost_name} was removed."
wa_admin_create_index
}

#
# wa-admin create-index
# Creates index.html file for the added to wa-admin vhosts
#

wa_admin_create_index () {

${SORT} -u ${wa_admin_vhosts} | ${TEE} > ${wa_admin_vhosts}
${CAT} ${wa_admin_html_header} > ${wa_admin_doc_root}/index.html

echo "<strong>Index generated on $(${DATE} "+%a %Y-%m-%d %H:%m %Z")</strong>" >> ${wa_admin_doc_root}/index.html

${CAT} >> ${wa_admin_doc_root}/index.html << __EOF__
<hr />
<center>
<table>
<tr>
<td align="center"><strong>Hostname</strong></td>
<td align="center"><strong>LogFile</strong></td>
</tr>
__EOF__

while read vhost_entry; do
vhost_name=$(echo ${vhost_entry} | ${CUT} -d '|' -f 1)
vhost_log=$(echo ${vhost_entry} | ${CUT} -d '|' -f 2)
log_name=$(${BASENAME} ${vhost_log})

echo "<tr><td><strong>${vhost_name}</strong></td>" >> ${wa_admin_doc_root}/index.html
echo "<td><a href="${vhost_name}/${log_name}">${log_name}</a></td></tr>" >> ${wa_admin_doc_root}/index.html
done < ${wa_admin_vhosts}

${CAT} >> ${wa_admin_doc_root}/index.html << __EOF__
</table>
</center>
<hr />
<strong>Index generated by wa-admin</strong>
__EOF__

${CAT} ${wa_admin_html_footer} >> ${wa_admin_doc_root}/index.html

echo "Index file was created in ${wa_admin_doc_root}/index.html"
}

#
# wa-admin cron
# Suitable for executing webalizer(1) from cron(8)
#

wa_admin_cron () {
[ ! -f ${wa_admin_etc}/wa-admin/wa-admin.vhosts ] && errmsg "${wa_admin}: There is no ${wa_admin_etc}/wa-admin.vhosts file."

while read vhost_entry; do
vhost_name=$(echo ${vhost_entry} | ${CUT} -d '|' -f 1)
vhost_log=$(${BASENAME} $(echo ${vhost_entry} | ${CUT} -d '|' -f 2))

${webalizer_bin} -c ${wa_admin_doc_root}/${vhost_name}/${vhost_log}/webalizer.conf
done < ${wa_admin_etc}/wa-admin/wa-admin.vhosts
}

#
# End of function definitions
#

#
# main
#

[ "$#" -eq 0 ] && errmsg ${wa_admin_usage}

case "$1" in
list)
[ $# -eq 1 ] && wa_admin_list || errmsg ${wa_admin_usage_list}
;;
create-index)
[ $# -eq 1 ] && wa_admin_create_index || errmsg ${wa_admin_usage_create_index}
;;
cron|run)
[ $# -eq 1 ] && wa_admin_cron || errmsg ${wa_admin_usage_cron}
;;
add)
[ $# -eq 3 ] && wa_admin_add $2 $3 || errmsg ${wa_admin_usage_add}
;;
delete)
[ $# -eq 3 ] && wa_admin_delete $2 $3 || errmsg ${wa_admin_usage_delete}
;;
*)
errmsg ${wa_admin_usage}
;;
esac

exit 0

40 changes: 40 additions & 0 deletions wa-admin.css
@@ -0,0 +1,40 @@
body {
background : #e8e8e8;
}

table, td {
border : 5px solid #CCC;
border-collapse : collapse;
}

tbody tr, td {
padding : 5px 10px;
color : #666;
}

tbody tr:hover {
background : #AAA;
}

tbody tr:hover td {
color : #454545;
}

caption {
text-align : left;
font-size : 120%;
padding : 10px 0;
color : #666;
}

table a:link {
color : #666;
}

table a:visited {
color : #666;
}
table a:hover {
color : #003366;
}

0 comments on commit 3fdb8ed

Please sign in to comment.