Skip to content

Commit d448eb8

Browse files
committed
feat(infrastructure): add script to copy Apache logs into Matomo daily
Matomo does not automatically import Apache logs. Importing needs to be done manually. Write a systemd service which runs the log importer (using our fork [1] to incrementally import logs [2]), and a systemd timer to run the service daily. [1] https://github.com/strager/matomo-log-analytics/tree/timestamp [2] matomo-org/matomo-log-analytics#232
1 parent 763f0a0 commit d448eb8

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (C) 2020 Matthew Glazar
2+
# See end of file for extended copyright information.
3+
4+
[Unit]
5+
Description = Sync Apache logs to Matomo daily
6+
7+
[Service]
8+
Type = oneshot
9+
10+
ExecStart = /usr/local/bin/quick-lint-js-matomo-log-sync.sh
11+
12+
Restart = no
13+
TimeoutStartSec = 180min
14+
15+
# Don't hinder user HTTP requests.
16+
Nice = 15
17+
CPUSchedulingPolicy = batch
18+
IOSchedulingClass = idle
19+
20+
# quick-lint-js finds bugs in JavaScript programs.
21+
# Copyright (C) 2020 Matthew Glazar
22+
#
23+
# This file is part of quick-lint-js.
24+
#
25+
# quick-lint-js is free software: you can redistribute it and/or modify
26+
# it under the terms of the GNU General Public License as published by
27+
# the Free Software Foundation, either version 3 of the License, or
28+
# (at your option) any later version.
29+
#
30+
# quick-lint-js is distributed in the hope that it will be useful,
31+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
32+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33+
# GNU General Public License for more details.
34+
#
35+
# You should have received a copy of the GNU General Public License
36+
# along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
# Copyright (C) 2020 Matthew Glazar
4+
# See end of file for extended copyright information.
5+
6+
# This script is used by the quick-lint-js-matomo-log-sync.service systemd
7+
# service.
8+
9+
set -e
10+
set -u
11+
12+
exec python3 /root/matomo-log-analytics/import_logs.py \
13+
--config=/var/www/admin.quick-lint-js.com/matomo/config/config.ini.php \
14+
--url=https://admin.quick-lint-js.com/matomo/ \
15+
--timestamp-file=/var/log/apache2/matomo_analytics.timestamp \
16+
/var/log/apache2/matomo_analytics.log* \
17+
--enable-static
18+
19+
# quick-lint-js finds bugs in JavaScript programs.
20+
# Copyright (C) 2020 Matthew Glazar
21+
#
22+
# This file is part of quick-lint-js.
23+
#
24+
# quick-lint-js is free software: you can redistribute it and/or modify
25+
# it under the terms of the GNU General Public License as published by
26+
# the Free Software Foundation, either version 3 of the License, or
27+
# (at your option) any later version.
28+
#
29+
# quick-lint-js is distributed in the hope that it will be useful,
30+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
31+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32+
# GNU General Public License for more details.
33+
#
34+
# You should have received a copy of the GNU General Public License
35+
# along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (C) 2020 Matthew Glazar
2+
# See end of file for extended copyright information.
3+
4+
[Unit]
5+
Description = Sync Apache logs to Matomo daily
6+
7+
[Timer]
8+
AccuracySec = 1us
9+
OnCalendar = daily
10+
RandomizedDelaySec = 30min
11+
12+
Unit = quick-lint-js-matomo-log-sync.service
13+
14+
# quick-lint-js finds bugs in JavaScript programs.
15+
# Copyright (C) 2020 Matthew Glazar
16+
#
17+
# This file is part of quick-lint-js.
18+
#
19+
# quick-lint-js is free software: you can redistribute it and/or modify
20+
# it under the terms of the GNU General Public License as published by
21+
# the Free Software Foundation, either version 3 of the License, or
22+
# (at your option) any later version.
23+
#
24+
# quick-lint-js is distributed in the hope that it will be useful,
25+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
# GNU General Public License for more details.
28+
#
29+
# You should have received a copy of the GNU General Public License
30+
# along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
# Copyright (C) 2020 Matthew Glazar
3+
# See end of file for extended copyright information.
4+
5+
- name: Download Matomo log analytics
6+
ansible.builtin.get_url:
7+
url: https://github.com/strager/matomo-log-analytics/archive/{{ matomo_log_analytics_version }}.tar.gz
8+
dest: /root/matomo-log-analytics-{{ matomo_log_analytics_version }}.tar.gz
9+
owner: root
10+
group: root
11+
checksum: "{{ matomo_log_analytics_checksum }}"
12+
become: true
13+
14+
- name: Create directory for Matomo log analytics
15+
ansible.builtin.file:
16+
path: /root/matomo-log-analytics-{{ matomo_log_analytics_version }}
17+
owner: root
18+
group: root
19+
state: directory
20+
become: true
21+
22+
- name: Extract Matomo log analytics
23+
ansible.builtin.unarchive:
24+
remote_src: yes
25+
src: /root/matomo-log-analytics-{{ matomo_log_analytics_version }}.tar.gz
26+
dest: /root/matomo-log-analytics-{{ matomo_log_analytics_version }}
27+
extra_opts:
28+
- '--strip-components=1'
29+
owner: root
30+
group: root
31+
become: true
32+
33+
# quick-lint-js finds bugs in JavaScript programs.
34+
# Copyright (C) 2020 Matthew Glazar
35+
#
36+
# This file is part of quick-lint-js.
37+
#
38+
# quick-lint-js is free software: you can redistribute it and/or modify
39+
# it under the terms of the GNU General Public License as published by
40+
# the Free Software Foundation, either version 3 of the License, or
41+
# (at your option) any later version.
42+
#
43+
# quick-lint-js is distributed in the hope that it will be useful,
44+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
45+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46+
# GNU General Public License for more details.
47+
#
48+
# You should have received a copy of the GNU General Public License
49+
# along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.

infrastructure/quick-lint-js-web-2/roles/matomo/tasks/main.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,54 @@
3838
state: link
3939
become: true
4040

41+
- name: Check if Matomo log analytics is installed
42+
ansible.builtin.stat:
43+
path: /root/matomo-log-analytics-{{ matomo_log_analytics_version }}/index.php
44+
register: matomo_log_analytics_installed_check
45+
become: true
46+
47+
- name: Install Matomo log analytics
48+
ansible.builtin.include_tasks:
49+
file: install_matomo_log_analytics.yml
50+
when: not matomo_log_analytics_installed_check.stat.exists
51+
52+
- name: Create symlink point to the desired version of Matomo log analytics
53+
ansible.builtin.file:
54+
src: /root/matomo-log-analytics-{{ matomo_log_analytics_version }}
55+
dest: /root/matomo-log-analytics
56+
owner: root
57+
group: root
58+
state: link
59+
become: true
60+
61+
- name: Install Matomo service scripts
62+
ansible.builtin.copy:
63+
src: quick-lint-js-matomo-log-sync.sh
64+
dest: /usr/local/bin/quick-lint-js-matomo-log-sync.sh
65+
owner: root
66+
group: root
67+
mode: "0755"
68+
become: true
69+
70+
- name: Install Matomo services
71+
ansible.builtin.copy:
72+
src: "{{ item }}"
73+
dest: /etc/systemd/system
74+
owner: root
75+
group: root
76+
mode: "0644"
77+
become: true
78+
loop:
79+
- quick-lint-js-matomo-log-sync.service
80+
- quick-lint-js-matomo-log-sync.timer
81+
82+
- name: Start Matomo services
83+
ansible.builtin.service:
84+
name: quick-lint-js-matomo-log-sync.timer
85+
state: started
86+
enabled: yes
87+
become: true
88+
4189
# quick-lint-js finds bugs in JavaScript programs.
4290
# Copyright (C) 2020 Matthew Glazar
4391
#

infrastructure/quick-lint-js-web-2/vars.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
matomo_version: 4.9.1
66
matomo_checksum: sha256:2ff734151ce43d050fc2ecf0dcbb845859430c5106b9755c5be3244b8d9d5a54
77

8+
matomo_log_analytics_version: 1e85636b72d76ad31f87c42693a14cc8cf51f6b0
9+
matomo_log_analytics_checksum: sha256:106bf146e0b8531efd1b9927a28c714e1ebf874bbffba49b54c83cd55372da11
10+
811
# quick-lint-js finds bugs in JavaScript programs.
912
# Copyright (C) 2020 Matthew Glazar
1013
#

0 commit comments

Comments
 (0)