Skip to content

Commit 53ecc46

Browse files
committed
Release 1.0.2
fix(wpst-goaccess): Fixed detection of gzip files improvement(cron-shim): Updated cron-shim.sh to use cron-shim.conf fix(cron-shim): Removed skipping plugins and themes as this will cause tasks not to run.
1 parent 658e2a0 commit 53ecc46

File tree

3 files changed

+84
-46
lines changed

3 files changed

+84
-46
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.1
1+
1.0.2

bin/wpst-goaccess.sh

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Version: $WPST_VERSION
4848
# -- check_goaccess
4949
check_goaccess () {
5050
# -- Check if goaccess is installed
51-
_debug "Checking if goaccess is installed"
51+
_debug "===== Checking if goaccess is installed"
5252
_cexists goaccess
5353
_debug "\$CMD_EXISTS: $CMD_EXISTS"
5454
if [[ $CMD_EXISTS == "1" ]]; then
@@ -62,7 +62,7 @@ check_goaccess () {
6262
# -- check_goaccess_version
6363
check_goaccess_version () {
6464
# -- Get goaccess version
65-
_debug "Getting goaccess version"
65+
_debug "===== Getting goaccess version"
6666
GOACCESS_VERSION=$(goaccess -V | awk '{print $3}')
6767
_debug "\$GOACCESS_VERSION: $GOACCESS_VERSION"
6868

@@ -77,7 +77,7 @@ check_goaccess_version () {
7777

7878
# -- compile_goaccess
7979
function compile_goaccess () {
80-
_debug "Running compile_goaccess"
80+
_debug "===== Running compile_goaccess"
8181

8282
# Check if goaccess is already installed
8383
_debug "Checking if goaccess is installed"
@@ -110,7 +110,13 @@ function compile_goaccess () {
110110

111111
# -- set_format
112112
set_format () {
113-
_debug "Running set_format on $FORMAT"
113+
_debug "===== Running set_format on $FORMAT"
114+
# Checking if format is overridden
115+
_debug "Checking if format is overridden"
116+
if [[ -n $SET_FORMAT ]]; then
117+
_debug "Format is overridden to $SET_FORMAT"
118+
FORMAT="$SET_FORMAT"
119+
fi
114120
if [[ $FORMAT == "COMBINED" ]]; then
115121
_debug "Format is COMBINED"
116122
LOG_FORMAT="COMBINED"
@@ -160,6 +166,7 @@ set_format () {
160166
# -- detect_logs
161167
function detect_logs () {
162168
echo "Detecting log files"
169+
_debug "===== Running detect_logs"
163170

164171
# -- Check if $FORMAT is set
165172
if [[ -z $FORMAT ]]; then
@@ -179,9 +186,11 @@ function detect_logs () {
179186
elif [[ -d /etc/nginx ]] && [[ -d /var/log/nginx ]]; then
180187
echo "Detected NGINX logs"
181188
FORMAT="NGINX"
189+
[[ $ACTION == "ALL" ]] && { LOG_FILE_LOCATION="/var/log/nginx"; LOG_FILTER="*access*log*"; }
190+
[[ $ACTION == "DOMAIN" ]] && { LOG_FILE_LOCATION="/var/log/nginx"; LOG_FILTER="${DOMAIN}*access*log*"; }
182191
elif [[ -d /usr/local/lsws ]]; then
183192
echo "Detected OLS logs"
184-
FORMAT="NGINX"
193+
FORMAT="OLS"
185194
else
186195
echo "No logs detected"
187196
exit
@@ -196,35 +205,54 @@ function detect_logs () {
196205

197206
# -- collect_logs
198207
collect_logs () {
208+
_debug "===== Collecting logs"
199209
local CATCMD="cat"
210+
local LOGCMD
211+
212+
# Collect logs
200213
_debug "Format: $FORMAT Log File Location:$LOG_FILE_LOCATION Log Filter: $LOG_FILTER"
201214
LOG_COLLECT_DATA=$(mktemp)
215+
216+
# -- Check if $LOG_FILE_LOCATION and $LOG_FILTER are set
217+
[[ -z $LOG_FILE_LOCATION ]] && { echo "No log file location set, exiting"; exit; }
218+
[[ -z $LOG_FILTER ]] && { echo "No log filter set, exiting"; exit; }
219+
220+
# -- Domain Action
202221
if [[ $ACTION == "DOMAIN" ]]; then
203-
if [[ $DRY_RUN == "1" ]]; then
204-
echo "cat ${LOG_FILE_LOCATION}/${LOG_FILTER} > $LOG_COLLECT_DATA"
205-
else
206-
cat ${LOG_FILE_LOCATION}/${LOG_FILTER} > $LOG_COLLECT_DATA
207-
fi
222+
# Get all logs into an array
223+
PROCESS_LOGS=($(find $LOG_FILE_LOCATION -type f -name "$LOG_FILTER"))
224+
225+
# Go through each log and process it
226+
for LOG in "${PROCESS_LOGS[@]}"; do
227+
_debug "Processing $LOG"
228+
[[ $LOG == *gz ]] && CATCMD="zcat" || CATCMD="cat"
229+
LOGCMD="$CATCMD $LOG >> $LOG_COLLECT_DATA"
230+
if [[ $DRY_RUN == "1" ]]; then
231+
echo $LOGCMD
232+
else
233+
eval $LOGCMD
234+
echo $LOGCMD
235+
fi
236+
done
237+
238+
# -- All Action
208239
elif [[ $ACTION == "ALL" ]]; then
209-
if [[ $DRY_RUN == "1" ]]; then
210-
echo "cat ${LOG_FILE_LOCATION}/*.access.log > $LOG_COLLECT_DATA; zcat ${LOG_FILE_LOCATION}/${LOG_FILTER} >> $LOG_COLLECT_DATA"
240+
[[ -z $LOG_FILE_LOCATION ]] && { echo "No log file location set, exiting"; exit; }
241+
LOGCMD="cat ${LOG_FILE_LOCATION}/*.access.log > $LOG_COLLECT_DATA; zcat ${LOG_FILE_LOCATION}/${LOG_FILTER} >> $LOG_COLLECT_DATA"
242+
if [[ $DRY_RUN == "1" ]]; then
243+
echo $LOGCMD
211244
else
212-
cat ${LOG_FILE_LOCATION}/*.access.log > $LOG_COLLECT_DATA; zcat ${LOG_FILE_LOCATION}/${LOG_FILTER} >> $LOG_COLLECT_DATA
245+
eval $LOGCMD
246+
echo $LOGCMD
213247
fi
214-
elif [[ $ACTION == "FILE" ]]; then
215-
if [[ $DRY_RUN == "1" ]]; then
216-
echo "$CATCMD ${LOG_FILE_LOCATION} > $LOG_COLLECT_DATA"
217-
else
218-
[[ $LOG_FILE_LOCATION == "*.gz" ]] && CATCMD="zcat"
219-
$CATCMD ${LOG_FILE_LOCATION} > $LOG_COLLECT_DATA
220-
fi
221-
fi
248+
fi
222249
LOG_DATA_FILE=$LOG_COLLECT_DATA
223250
}
224251

225252
# -- do_goaccess
226253
function do_goaccess () {
227-
_debug "Running do_goaccess"
254+
_debug "===== Running do_goaccess"
255+
[[ $LOG_DATA_FILE == *.gz ]] && { CATCMD="zcat"; _debug "Detected gzip file, setting \$CAT_CMD to zcat"; } || { CATCMD="cat"; _debug "Detected non-gzip file, setting \$CAT_CMD to cat"; }
228256
local $CMD
229257
local $GOACCESS_EXTRA
230258

@@ -261,7 +289,6 @@ function do_goaccess () {
261289

262290
# -- Run goaccess
263291
_debug "Proceeding with do_goaccess \$ACTION: $ACTION \$FILE: $FILE \$FORMAT: $FORMAT \$DRY_RUN: $DRY_RUN \$UNKNOWN_UA: $UNKNOWN_UA \$TIME_SPEC: $TIME_SPEC \$BROWSER_LIST: $BROWSER_LIST"
264-
[[ $LOG_FILE_LOCATION == "*.gz" ]] && CATCMD="zcat" || CATCMD="cat"
265292
CMD="$CATCMD $LOG_DATA_FILE | goaccess ${GOACCESS_EXTRA}"
266293
_debug "CMD: $CMD"
267294
[[ $DRY_RUN == "1" ]] && { echo $CMD; } || { eval "$CMD"; echo "CMD: $CMD"; }
@@ -270,7 +297,7 @@ function do_goaccess () {
270297

271298
# -- sed_logs
272299
function sed_logs() {
273-
_debug "Processing logs using custom time - $CUSTOM_TIME"
300+
_debug "===== Processing logs using custom time - $CUSTOM_TIME"
274301
SED_LOG=$(mktemp)
275302
if ! [[ $CUSTOM_TIME =~ ^[0-9]{2}/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{2}/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then
276303
echo "Error: Please provide dates in the format dd/Mon/yyyy:hh:mm:ss"
@@ -310,7 +337,7 @@ case $key in
310337
shift # past value
311338
;;
312339
-f|--format)
313-
FORMAT="${2^^}"
340+
SET_FORMAT="${2^^}"
314341
shift # past argument
315342
shift # past value
316343
;;
@@ -377,7 +404,7 @@ _debug "\$ALLARGS: $ALLARGS"
377404
_debug "============="
378405
_debug "\$ACTION: $ACTION"
379406
_debug "\$PLATFORM: $PLATFORM"
380-
_debug "\$FORMAT: $FORMAT"
407+
_debug "\$SET_FORMAT: $SET_FORMAT"
381408
_debug "\$DOMAIN: $DOMAIN"
382409
_debug "\$FILE: $FILE"
383410
_debug "\$TIME_SPEC: $TIME_SPEC"
@@ -414,33 +441,36 @@ elif [[ $ACTION == "DOMAIN" ]]; then
414441
collect_logs
415442
[[ ! -z $CUSTOM_TIME ]] && sed_logs $CUSTOM_TIME
416443
do_goaccess $ACTION $LOG_DATA_FILE
444+
# ------------
445+
# -- FILE
446+
# ------------
417447
elif [[ $ACTION == "FILE" ]]; then
418448
[[ -z $FILE ]] && usage && echo "Error: specify a file" && exit
419449
_debug "Running for file $FILE"
420-
421450
# -- Process logs on file
422451
check_goaccess
423452
detect_logs
424-
LOG_DATA_FILE=$FILE
425-
set_format
453+
LOG_DATA_FILE=$FILE
454+
set_format
426455
[[ ! -z $CUSTOM_TIME ]] && sed_logs $CUSTOM_TIME
427456
do_goaccess $ACTION $LOG_DATA_FILE
457+
# ------------
458+
# -- ALL
459+
# ------------
428460
elif [[ $ACTION == "ALL" ]]; then
429461
_debug "Running for all domains"
430-
431462
# -- Process logs on all domains
432463
check_goaccess
433464
detect_logs
434465
set_format
435466
collect_logs
467+
[[ ! -z $CUSTOM_TIME ]] && sed_logs $CUSTOM_TIME
436468
do_goaccess $ACTION $LOG_DATA_FILE
437469
elif [[ $ACTION == "TEST" ]]; then
438470
_debug "Running for test"
439-
440471
# -- Process logs on all domains
441472
check_goaccess
442473
LOG_DATA_FILE=$TEST_LOG
443-
[[ -z $FORMAT ]] && FORMAT="GPNGINX"
444474
set_format
445475
do_goaccess $ACTION $LOG_DATA_FILE
446476
elif [[ $ACTION == "COMPILE" ]]; then

scripts/cron-shim.sh

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# -- Created by Jordan - [email protected] - https://managingwp.io
3-
# -- Version 1.1.0 -- Last Updated: 2023-06-07
3+
# -- Version 1.0.2 -- Last Updated: 2023-08-23
44
#
55
# Purpose: Run WordPress crons via wp-cli and log the output to stdout, syslog, or a file.
66
# Usage: Add the following to your crontab (replacing /path/to/wordpress with the path to your WordPress install):
@@ -13,19 +13,27 @@
1313
# TODO - Provide an example of passing an evnironment variable
1414
# Example: */5 * * * * /home/systemuser/cron-shim.sh
1515

16-
# Set up the necessary variables
16+
# -- Where are we?
1717
SCRIPT_DIR=$(dirname "$(realpath "$0")") # - Directory of this script
18-
WP_CLI="/usr/local/bin/wp" # - Location of wp-cli
19-
WP_ROOT="" # - Path to WordPress, blank will try common directories.
20-
CRON_CMD="$WP_CLI --skip-plugins --skip-themes cron event run --due-now" # - Command to run
21-
HEARTBEAT_URL="" # - Heartbeat monitoring URL, example https://uptime.betterstack.com/api/v1/heartbeat/23v123v123c12312 leave blank to disable or pass in via environment variable
22-
POST_CRON_CMD="" # - Command to run after cron completes
23-
24-
# Logging
25-
LOG_TO_STDOUT="1" # - Log to stdout? 0 = no, 1 = yes
26-
LOG_TO_SYSLOG="1" # - Log to syslog? 0 = no, 1 = yes
27-
LOG_TO_FILE="0" # - Log to file? 0 = no, 1 = yes
28-
LOG_FILE="" # Location for WordPress cron log file if LOG_TO_FILE="1", if left blank then ${WP_ROOT}/../wordpress-crons.log"
18+
19+
# -- Check if cron-shim.conf exists and source it
20+
if [[ -f $SCRIPT_DIR/cron-shim.conf ]]; then
21+
echo "Found and sourcing $SCRIPT_DIR/cron-shim.conf"
22+
source $SCRIPT_DIR/cron-shim.conf
23+
fi
24+
25+
# -- Default Settings
26+
[[ -z $WP_CLI ]] && WP_CLI="/usr/local/bin/wp" # - Location of wp-cli
27+
[[ -z $WP_ROOT ]] && WP_ROOT="" # - Path to WordPress, blank will try common directories.
28+
[[ -z $CRON_CMD ]] && CRON_CMD="$WP_CLI cron event run --due-now" # - Command to run
29+
[[ -z $HEARTBEAT_URL ]] && HEARTBEAT_URL="" # - Heartbeat monitoring URL, example https://uptime.betterstack.com/api/v1/heartbeat/23v123v123c12312 leave blank to disable or pass in via environment variable
30+
[[ -z $POST_CRON_CMD ]] && POST_CRON_CMD="" # - Command to run after cron completes
31+
32+
# -- Logging Settings
33+
[[ -z $LOG_TO_STDOUT ]] && LOG_TO_STDOUT="1" # - Log to stdout? 0 = no, 1 = yes
34+
[[ -z $LOG_TO_SYSLOG ]] && LOG_TO_SYSLOG="1" # - Log to syslog? 0 = no, 1 = yes
35+
[[ -z $LOG_TO_FILE ]] && LOG_TO_FILELOG_TO_FILE="0" # - Log to file? 0 = no, 1 = yes
36+
[[ -z $LOG_FILE ]] && LOG_FILE="" # Location for WordPress cron log file if LOG_TO_FILE="1", if left blank then ${WP_ROOT}/../wordpress-crons.log"
2937
LOG="" # Clearing variable
3038

3139
# Check if running as root

0 commit comments

Comments
 (0)