I am using Piwik Analytics on a site that doesn't run any javascript and I would prefer to have data created from the access logs instead of from the image tracker - results from the logs are actually better because they can include static file downloads.

This is a LEMP server with Ubuntu 12.04 and nginx 1.4.5.

The python script is located at /var/www/example1.com/public/piwik/misc/log-analytics/import-logs.py and would need to run with several options. The access log is located at /var/www/example2.com/logs/access.log.1.

The relevant portion of /etc/logrotate.d/nginx contains:

/var/www/*/logs/*.log {
   rotate 36500
   create 0640 www-data adm
       if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
           run-parts /etc/logrotate.d/httpd-prerotate; \
       fi; \
                [ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`

I don't know how to integrate a python script such that it will run at the end of this (after the rotations have completed).


An additional issue I didn't consider initially is that this script only needs to run for one domain - the rest of the domains on the server do not require the script. The file above I created so that it would automatically rotate logs for all domains added to the server without my having to edit /etc/logrotate.d/nginx.


You can put the command you want to be executed between the postrotate and endscript lines (make sure to keep the line already there):

      [ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
      /usr/bin/python /var/www/example1.com/public/piwik/misc/log-analytics/import-logs.py somearguments
  • That's so... simple! Thanks! Of course, I just reread my question and realized I have one outstanding issue I didn't address initially and I just edited the question to reflect this. Is is possible to only apply the solution such that the script only runs for one domain? – Paul Mar 1 '14 at 23:08
  • That totally depends on how the script works (which I'm not familiar with). The script is executed after all logs are rotated, what exactly it does and with what logs is entirely up to the script. – Teun Vink Mar 1 '14 at 23:12
  • The script just parses data from the access log and migrates it into the Piwik database. If it ran for every site, then the data could not be used for analysis. It could also work to tell logrotate not to run on a specific location and have a separate entry for that location, or I suppose I could just stop being lazy and make a separate entry for each domain. – Paul Mar 1 '14 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.