Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,196 of 9,196 pages
About the help in other languages
All help pages

View counts in a cluster: every node's access logs, counted on one node

This guide is for administrators who run one Exponential installation on several web servers (nodes) behind a load balancer, or on one server with several web servers in front of it (Apache, nginx, Velocity), and want…

Download as PDF

Address: https://doc.exponential.earth/learninminutes/exphelp/viewer/guides/view-count-in-a-cluster.md

On this page

View counts in a cluster: every node's access logs, counted on one node

This guide is for administrators who run one Exponential installation on several web servers (nodes) behind a load balancer, or on one server with several web servers in front of it (Apache, nginx, Velocity), and want page view counts that include every visit. It explains why the counter of a single server misses visits there, how the cluster mode of the view counter works, every setting with its default, the two commands and their options, how to schedule them (Plesk scheduled tasks, systemd timers, cron), how log rotation is handled, what to check when the numbers look wrong, and how to move to it from the single-log counter or from a site's own scripts.

Everything below was checked against the code: kernel/classes/viewcount/expviewcountcollector.php, expviewcountconfiguration.php, expviewcountlogreader.php, expviewcountstate.php, kernel/private/classes/commands/expviewcountcollect.php, expviewcountupdate.php, kernel/private/classes/cronjobs/viewcountcollect.php, updateviewcount.php, settings/logfile.ini and settings/cronjob.ini. The tests are tests/tests/kernel/classes/viewcount/expViewCountCollectorTest.php and expViewCountConfigurationTest.php (DB-less).

Guides · Before this: View count statistics (one server) · Feature: View counts across nodes · Cluster: Clustering overview · Settings: logfile.ini · Commands: exp:viewcount:collect, exp:viewcount:update

Contents

  1. In short
  2. Why one log per siteaccess misses visits
  3. How the cluster mode works
  4. Settings
  5. Examples
  6. The commands
  7. Scheduling
  8. Log rotation
  9. Troubleshooting
  10. Upgrading and moving over

In short

  • Set [ViewCountSettings] Nodes[] in logfile.ini to the nodes that serve the installation, and PrimaryCommand to a command that tells whether this node is the one that counts.
  • List every access log of a siteaccess: [AccessLogFileSettings] StorageDir/LogFileName as before, and more of them in the new LogFiles[] (one per host, or the log of a web server in front).
  • Run php runcronjobs.php view_count_nodes every 15 minutes on every node. On the counting node it copies every node's logs (exp:viewcount:collect) and counts the copies (exp:viewcount:update); on the others it does nothing.
  • Each copy has a resume state of its own, so no line is counted twice, and a node that takes over goes on where the other one stopped.
  • Without Nodes[] nothing changes: the counter reads the logs of the node it runs on, as it always did.

Why one log per siteaccess misses visits

The counter (updateviewcount.php, exp:viewcount:update) reads an access log from where its last run stopped and adds the page views it finds to ezview_counter. Before 6.0.15 it read one log, of the node it ran on:

  • Several nodes. Behind a load balancer each node's web server writes its own log. A counter on one node never sees the requests the other nodes answered: with a weight of 100 to 10, about one view in eleven was missing, and all of them while the first node was out of rotation.
  • Several hosts. Hosting panels such as Plesk write one log per domain (/var/www/vhosts/system/<domain>/logs/). A siteaccess served under several domains has several logs.
  • Several web servers. A server in front (Velocity, nginx) answers part of the requests itself and writes its own log; the logs of the web server behind it do not have those requests.
  • Running the counter on every node does not help: each node counts its own log, but the counts all go to one database, and the counter has no way to run there only once per line unless the logs are brought together.

How the cluster mode works

 node web1                        node web2 (counting node, PrimaryCommand exit 0)
 /var/log/.../access_log ───────► MirrorDir/web1/var/log/.../access_log ─┐
                                  MirrorDir/web2/var/log/.../access_log ─┼─► exp:viewcount:update ─► ezview_counter
 node web2's own log ───────────►                                       ─┘   one resume state per copy

One run of the cronjob part view_count_nodes (or the two commands) does this:

  1. Who counts. PrimaryCommand is run. Exit 0: this node counts. Exit 1: another node counts; the run ends without doing anything. Any other exit code: no answer; the run fails, so it is noticed (it neither counts everywhere nor nowhere). Empty: this node counts (a single counting node by configuration).
  2. State handover (exp:viewcount:collect). The resume states (StateDir) and the copies (MirrorDir) of every other node are taken over where they are newer (rsync --update). Only one node counts at a time, so the newest copy is the right one. This is what lets a node that takes over after a failover go on where the other one stopped.
  3. Copy (exp:viewcount:collect). Every configured log of every node is copied to MirrorDir/<node>/<the log's whole path>. This node's own logs are copied with PHP: a log that grew is appended to, one that was rotated or replaced (another first line, or shorter than the copy) is copied anew. A remote node's logs are copied with rsync -t --inplace over RemoteShell, which sends only what changed. The rotated copies (RotatedSuffixes[]) are copied beside them. A node or a log that cannot be reached is a warning; the next run catches up.
  4. Count (exp:viewcount:update). Each siteaccess reads the copies of its logs, each copy with a resume state of its own (StateDir, one JSON file per copy). A line is counted once, by the siteaccess it belongs to.

Why nothing is counted twice when the state moves

A resume state names its log by inode and device, and by a hash of the log's first line. Inode and device belong to one file system: on another node the same copy has other numbers. Before 6.0.15 the reader took such a log for a rotated one and read it again from its first line (on kracker.org this added 5,689 views once, on 26 November 2026, before a script fixed the inodes by hand). The reader now knows the log by its first line too: the same first line and at least as long as the offset is the same log on another disk (the event moved), and the run goes on from the offset. The state of a copy below StateDir is also named by its path there, not by the installation's path, so the same state file fits every node even when the installations are in different folders.

Settings

All in logfile.ini. Put them in settings/override/logfile.ini.append.php (every siteaccess) or in a siteaccess (settings/siteaccess/<name>/logfile.ini.append.php) for its own logs.

[AccessLogFileSettings]

SettingDefaultWhat it does
StorageDir, LogFileNameemptyThe first access log of the siteaccess, as before.
LogFiles[]emptyNew. More access logs of the siteaccess: other hosts, another web server. A relative path is below the installation. Each log has a resume state of its own.
SitePrefix[], PathPrefix[], Host, Port, Hosts[]as beforeUnchanged; they apply to every log of the siteaccess.

[ViewCountSettings]

SettingDefaultWhat it does
Nodes[]emptyNew. The nodes that serve the installation, by the names RemoteShell reaches them with, this node among them. Empty: single-node mode, the logs are read where they are.
NodeNameempty (short host name)New. This node's name in Nodes[].
MirrorDirempty (StateDir/nodes)New. Where the copies are kept: MirrorDir/<node>/<path of the log>. Keep it below StateDir so one handover takes both.
PrimaryCommandempty (this node counts)New. A shell command: exit 0 this node counts, 1 it does not, anything else no answer (the run fails).
RemoteShellssh -o BatchMode=yes -o ConnectTimeout=10New. How the other nodes are reached, by exp:viewcount:collect and by rsync (-e).
UpdateFollowsCollectdisabledNew. enabled: the count runs where exp:viewcount:collect last copied the logs as the counting node (its note MirrorDir/.collected-by), instead of running PrimaryCommand itself. For a copy as root and a count as a site user that may not ask the load balancer.
MarkerMaxAge3600New. How old that note may be, in seconds; an older one means the copy stopped, and nothing is counted.
StateDirempty (var/<site>/viewcount)The resume states, as before; in cluster mode also the copies (MirrorDir).
RotatedSuffixes[].1, .1.gz, .0The names of a log's rotated copy. Copied beside each copy and read to its end after a rotation.
SiteAccessList[], AllSiteAccesses, MatchSiteAccess, LineRegexp, the filtersas beforeUnchanged.

Settings of an older form

Existing settings keep working without a change. A setting of an older form is read for the new one and noted as a deprecation notice (in the debug output and as a deprecated: warning of exp:viewcount:update); new settings only add behavior:

Older formRead asNotice
StorageDir empty, LogFileName=/whole/path/access_log (the cronjob before 6.0.15 joined the two as they were)the log /whole/path/access_logyes: set StorageDir and LogFileName apart, or use LogFiles[]
StorageDir=/var/log/httpd/ (trailing slash)/var/log/httpd/<LogFileName>no
updateview.log of the cronjob before 6.0.15 in the log folderthe resume point of the first run (section 10 of View count statistics)the run says so
no Nodes[]single-node mode, exactly as beforeno

Examples

A Plesk cluster: two nodes, several domains, Velocity in front

# settings/override/logfile.ini.append.php
[ViewCountSettings]
SiteAccessList[]
SiteAccessList[]=site
SiteAccessList[]=shop
Nodes[]
Nodes[]=web1
Nodes[]=web2
# the node the load balancer sends the site to counts (7x: xsh asks HAProxy)
PrimaryCommand=/usr/local/bin/xsh lb:site:primary www.example.com --is-me -q
# Plesk rotates access_ssl_log to access_ssl_log.processed, later compressed
RotatedSuffixes[]=.processed
RotatedSuffixes[]=.processed.1.gz
# Velocity's lines end with the duration and the host: the host picks the siteaccess
LineRegexp=~^(?P<ip>\S*)\s+\S+\s+\S+\s+\[(?P<time>[^\]]+)\]\s+"(?P<request>(?:[^"\\]|\\.)*)"\s+(?P<status>\d{3}|-)\s+(?:\d+|-)(?:\s+"(?P<referer>(?:[^"\\]|\\.)*)"\s+"(?P<agent>(?:[^"\\]|\\.)*)")?(?:\s+[0-9.]+ms)?(?:\s+"(?P<host>[^"\s]*)")?\s*$~

# settings/siteaccess/site/logfile.ini.append.php
[AccessLogFileSettings]
StorageDir=/var/www/vhosts/system/www.example.com/logs
LogFileName=access_ssl_log
LogFiles[]=/var/log/vcc/example-php85/site-access.log

# settings/siteaccess/shop/logfile.ini.append.php
[AccessLogFileSettings]
StorageDir=/var/www/vhosts/system/shop.example.com/logs
LogFileName=access_ssl_log

A log that only some nodes have (Velocity runs on web2 only) is simply missing on the others; that is no error.

nginx or Apache on two plain servers

[AccessLogFileSettings]
StorageDir=/var/log/nginx
LogFileName=example.access.log

[ViewCountSettings]
Nodes[]
Nodes[]=app1.internal
Nodes[]=app2.internal
NodeName=app1.internal
# a fixed counting node: only app1 counts, app2's PrimaryCommand answers 1
PrimaryCommand=test "$(hostname -s)" = app1
RotatedSuffixes[]=.1
RotatedSuffixes[]=.1.gz

One server, several logs (no cluster)

LogFiles[] alone, without Nodes[]: the siteaccess reads every log where it is.

[AccessLogFileSettings]
StorageDir=/var/log/httpd
LogFileName=example.com-access_log
LogFiles[]=/var/log/httpd/www.example.com-access_log
LogFiles[]=/var/log/nginx/example.access.log

Siteaccesses that share one log of several hosts

As before (MatchSiteAccess=auto): when several counted siteaccesses read one log, each line goes to the siteaccess the kernel's siteaccess matching finds for its host. A log format that carries the host (vhost_combined, or Velocity's with the LineRegexp above) lets one log serve every siteaccess.

The commands

exp:viewcount:collect

./console exp:viewcount:collect                     # copy every node's logs (on the counting node only)
./console exp:viewcount:collect --dry-run           # what it would copy, nothing written
./console exp:viewcount:collect --all-siteaccesses  # the logs of every siteaccess but the administrations
./console exp:viewcount:collect --siteaccess-list=site,shop
./console exp:viewcount:collect --force             # here, whatever PrimaryCommand says
./console exp:viewcount:collect --format=json       # the report as JSON (exp-viewcount-collect/1)
php bin/php/expviewcountcollect.php --allow-root-user   # as root, for logs only root may read

Exit code 0: fine, also on a node that does not count. 1: no log could be copied, or PrimaryCommand gave no answer. 2: a usage error. Run as root, the states and copies are given to the owner of the var folder, so the counting part can run as the site user.

exp:viewcount:update

The counter, with every option it had (exp:viewcount:update). In cluster mode it reads the copies and runs only where PrimaryCommand says this node counts; --force counts anyway, and --log-file (a trial on one file) is never held back.

Scheduling

Run the same schedule on every node; only the counting node does the work. Every 15 minutes is a good interval: the lines a rotation takes away before they were copied are lost (section 8), so a shorter interval loses less.

The cronjob part

# settings/cronjob.ini (shipped)
[CronjobPart-view_count_nodes]
Scripts[]=viewcountcollect.php
Scripts[]=updateviewcount.php

[CronjobPart-view_count_collect]
Scripts[]=viewcountcollect.php

[CronjobPart-view_count_copies]
Scripts[]=updateviewcount.php
php runcronjobs.php -q -s site view_count_nodes

Do not also schedule updateviewcount.php in the global part when you use view_count_nodes: two runs would wait for each other's lock, but nothing is counted twice either way.

Plesk scheduled tasks

A subscription's tasks run as its system user. With a chrooted shell an "Run a command" task cannot reach PHP or the installation; use "Run a PHP script" (-type php) or a server-wide task. When the logs can only be read by root (Plesk's /var/www/vhosts/system/<domain>/logs, unless the subscription's own logs folder is used), split the work:

# root, every 15 minutes, on every node: copy the logs
plesk bin scheduler --create -user root -type exec -schedule "1,16,31,46 * * * *" \
  -command "cd /var/www/vhosts/example.com/httpdocs && /opt/plesk/php/8.5/bin/php runcronjobs.php -q --allow-root-user -s site view_count_collect" \
  -description "page views 1/2: copy every node's access logs" -notify errors
# the site user, a few minutes later: count the copies
plesk bin scheduler --create -subscription example.com -type php -php 8.5.13 \
  -command /var/www/vhosts/example.com/httpdocs/runcronjobs.php -arguments "-q -s site view_count_copies" \
  -schedule "6,21,36,51 * * * *" -description "page views 2/2: count the copies" -notify errors

When the site user may not run PrimaryCommand (7x: xsh lb:site:primary reads root's cache), set UpdateFollowsCollect=enabled: the root task asks the load balancer and notes the counting node, and the count follows that note.

A "Run a PHP script" task starts in the subscription's home folder; runcronjobs.php (and the two bin/php scripts) change to the installation by themselves when started elsewhere.

systemd timers

# /etc/systemd/system/example-viewcount.service
[Service]
Type=oneshot
User=example
WorkingDirectory=/var/www/example
Nice=10
IOSchedulingClass=idle
ExecStart=/usr/bin/php runcronjobs.php -q -s site view_count_nodes

# /etc/systemd/system/example-viewcount.timer
[Timer]
OnCalendar=*:1/15
[Install]
WantedBy=timers.target

The guard can also be systemd's own: ExecCondition=/usr/local/bin/xsh lb:site:primary www.example.com --is-me -q with PrimaryCommand left empty.

Log rotation

  • Moved and created anew (logrotate's create, Plesk's .processed): the copy of the rotated file (one of RotatedSuffixes[]) is read from where the last run stopped, then the new log from its start.

  • Compressed (.1.gz, .processed.1.gz): read the same way, found by its first line.

  • Copied and emptied (copytruncate): the same inode, another first line; the rotated copy is found by its first line.

  • Plesk's append and truncate (some Plesk versions append the log to <log>.webstat for the statistics and empty it): the lines written since the last run are at the end of .webstat, which starts with older lines, so they cannot be found by the first line; they are lost (the report says "truncated"). Keep the interval short, or point Plesk's statistics elsewhere.

  • Older rotations (.processed.2.gz and before) are not read by the scheduled run. Count them once by hand with a state of their own, so they never mix with the live logs:

    ./console exp:viewcount:update --siteaccess-list=site --log-file=/var/log/httpd/access_log.2.gz --state-dir=var/viewcount-history

Troubleshooting

What you seeWhyWhat to do
A burst of views once, after a failoverThe state was taken over but the reader did not know the log (a kernel before 6.0.15)Update; the reader now resumes by the first line (event moved).
Views of one node missingThe node is not in Nodes[], or RemoteShell cannot log in (keys, BatchMode), or the user cannot read its logs./console exp:viewcount:collect --dry-run --format=json lists each node and log with missing or failed; try ssh <node> test -r <log> as the user the run is for.
skipped: <node> is not the node that counts everywherePrimaryCommand answers 1 on every nodeRun it by hand on each node; exactly one must exit 0.
PrimaryCommand gave no answerIt exits with another code (the load balancer could not be asked)The run fails on purpose; fix the command.
this node (x) is not in Nodes[]The short host name differs from the name in Nodes[]Set NodeName.
Permission denied on the copiesThe copy ran as root, the count as the site userThe copy gives its files to the owner of var; check that folder's owner.
deprecated: warningsA setting of an older formSee Settings of an older form.
no copy of its access logs in ... yetThe count ran before the first copyRun exp:viewcount:collect first; view_count_nodes does both in order.

Upgrading and moving over

From the single-log counter

Nothing has to change: without Nodes[] the counter works as before. To count every node, add Nodes[], PrimaryCommand and, for more logs, LogFiles[]; schedule view_count_nodes on every node and drop updateviewcount.php from the other parts. The first run copies the logs and counts each copy from its first line: when the old counter had already counted the current log of the counting node, start that copy from its end first with ./console exp:viewcount:update --skip-to-end right after the first exp:viewcount:collect.

From a site's own scripts (kracker.org's)

kracker.org counted its nodes with three scripts of its extension. They map onto the kernel like this:

kracker.orgKernel
kracker_viewcount_pull_state.sh (rsync of var/site/viewcount and the mirrors, inode rewrite)the handover of exp:viewcount:collect; the inode rewrite is no longer needed (event moved)
kracker_viewcount_collect_node_logs.sh (KRACKER_VIEWCOUNT_NODES, _HOSTS, _LOCAL)Nodes[], LogFiles[] per siteaccess, NodeName; the copies go to MirrorDir
kracker_viewcount_update_all_nodes.sh (KRACKER_VIEWCOUNT_SITES)SiteAccessList[] and exp:viewcount:update
ExecCondition=xsh lb:site:primary kracker.org --is-me -qPrimaryCommand (or keep the ExecCondition)
logs/viewcount-nodes/<node>/<host>/access_ssl_logMirrorDir/<node>/var/www/vhosts/system/<host>/logs/access_ssl_log

The copies have other paths than the script's mirrors, so their states are new: run exp:viewcount:collect, then exp:viewcount:update --skip-to-end once, then switch the timer over, and the counting goes on without a gap or a double count.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x