用 Matomo/Piwik 定时导入日志实现统计网站访问情况

安装并配置piwik以后,就需要导入access.log中的访问数据了,

首先需要一个导入日志的脚本,这个脚本运行时,需要把当前access.log中的日志导入pwiki,同时再把导入过的备份到别的地方。脚本如下:

#!/bin/sh

LOG_DIR=/var/log/nginx

LOG_ACCESS=$LOG_DIR/access.log
LOG_IMPORTING=$LOG_DIR/access.log.importing
LOG_IMPORTED=$LOG_DIR/imported.log

# move current log file to a tmp file, and restart log server
mv $LOG_ACCESS $LOG_IMPORTING 2>/dev/null
kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null

# import temp log file
/www/piwik/misc/log-analytics/import_logs.py \
    --idsite=1 \
    --url=piwik.example.org \
    --log-format-name=ncsa_extended \
    $LOG_IMPORTING

# join importing log to imported
cat $LOG_IMPORTING >>$LOG_IMPORTED

# remove temp file
rm $LOG_IMPORTING 2>/dev/null

最后还需要配置cron每个小时运行一次导入脚本,再配置logrotate不要处理access.log。

文章作者: 若海; 原文链接: https://www.rehiy.com/post/202/; 转载需声明来自技术写真 - 若海

添加新评论