Skip to content

Instantly share code, notes, and snippets.

@fedek6
Last active February 25, 2020 21:10
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 fedek6/f56638ae0b16fa1eb373f921de5f75c2 to your computer and use it in GitHub Desktop.
Save fedek6/f56638ae0b16fa1eb373f921de5f75c2 to your computer and use it in GitHub Desktop.
Parse logs for Matomo (Piwik) on Windows [using Powershell]
#Based on https://matomo.org/docs/log-analytics-tool-how-to/
#------------------------------------------------------------------------
# Config
#:: Python URL (must be 2.x)
$PY_BIN="C:\Program Files\Python\Python27\python.exe"
#:: Matomo import_logs executable
$IMPORT_LOGS="[path_to_piwik]\piwik\misc\log-analytics\import_logs.py"
#:: IIS log dir
$LOGS_DIR="C:\inetpub\logs\LogFiles"
#:: Access token from Piwik config (check admin panel)
$TOKEN="*****************"
#:: URL to your Piwik installation
$URL="http://website.com/piwik/"
# CPU threads
$THREADS=2
#matomo.json extraction
$sites=(Get-Content -Raw -Path .\sites.json | ConvertFrom-Json)
#.json file example:
#[
# {"DIR":"Directory1","ID":"1"},
# {"DIR":"Directory2","ID":"2"},
# {"DIR":"Directory3","ID":"3"}
#]
# Run command
foreach ($site in $sites)
{
# Automatically generated vars
$LOG_FILE="u_ex" + (Get-Date -UFormat "%y%m%d") + ".log"
$LOG_PATH="$LOGS_DIR\" + ($site).DIR + "\$LOG_FILE"
$PY_ARGs=" $IMPORT_LOGS --url=$URL --idsite=" + ($site).ID + " --token-auth=$TOKEN --recorders=$THREADS --enable-http-errors --enable-http-redirects --enable-static --enable-bots $LOG_PATH"
Start-Process $PY_BIN $PY_ARGs -wait
# Uncomment for debug:
# $PY_ARGs
Write-Host "Parsed log file: $LOG_PATH"
}
# how many days to go back
$PastLogs = 30
foreach($i in 1..$PastLogs)
{
# Run command
foreach ($site in $sites)
{
# Automatically generated vars
$LOG_FILE=”u_ex” + (Get-Date (Get-Date).AddDays(-$i) -UFormat “%y%m%d”) + “.log”
$LOG_PATH=”$LOGS_DIR\” + ($site).DIR + “\$LOG_FILE”
$PY_ARGs=” $IMPORT_LOGS –url=$URL –idsite=” + ($site).ID + ” –token-auth=$TOKEN –recorders=$THREADS –enable-http-errors –enable-http-redirects –enable-static –enable-bots $LOG_PATH”
Start-Process $PY_BIN $PY_ARGs -wait
# Uncomment for debug:
# $PY_ARGs
Write-Host “Parsed log file: $LOG_PATH”
}
}
[
{"id":3, "dir":"W3SVC3", "name":"website.com"}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment