Skip to content

Instantly share code, notes, and snippets.

@sebthemonster
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebthemonster/5f5872585abd29331969 to your computer and use it in GitHub Desktop.
Save sebthemonster/5f5872585abd29331969 to your computer and use it in GitHub Desktop.
A little script to parse http server logs with Piwik (piwik.org) script. Log files are in a given directory with sub-directories for each website present in Piwik. Each website has an idsite in Piwik and this idsite is passed to parser script via a piwik.conf file present in website directory

Script parsing logs to Piwik analytics

Create web site

  1. Create website in Piwik and get its idsite
  2. Create a directory for this website in /home/analytics/website_logs/ and create in it a directory parsed

For example.org, create /home/analytics/website_logs/example.org dir and /home/analytics/website_logs/example.org/parsed

  1. Write a file piwik.conf in this directory with almost this line: idsite={{idsite}}

Parse logs

  1. Puts log files in directory of required website and have fun

A cron launch parse_analytic_logs script. This script parse logs file through the script provided by Piwik with given parameter idsite. source for script provided by Piwik : http://piwik.org/docs/log-analytics-tool-how-to

WTFPL

#! /bin/bash
# =======================================================================
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright © 2014 Sebastien Ballarin <sebastien.ballarin@gmail.com>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
# =======================================================================
for site in /home/analytics/website_logs/*
do
if [ -d $site ]; then
echo "$site analytic logs"
if [ -f $site/piwik.conf ]; then
piwik_id=$(cat $site/piwik.conf | grep idsite | sed 's/idsite=//')
for file in $site/*.log
do
if [ -f $file ]; then
filename=$(basename $file .log)
echo "parsing file : $file for site with piwik ID $piwik_id ..."
python /var/www/piwik/misc/log-analytics/import_logs.py --url=http://analytics.sebthemonster.com --idsite=$piwik_id --enable-http-errors --enable-http-redirects $file > /home/analytics/logs/$filename.parsing.log && mv $file $site/parsed
fi
done
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment