Tags

, , , ,


Introduction

I had a requirement to quickly generate a report on the WordPress version our customers were using and have the report program report each morning from all the servers running WordPress hosting.

in usual fashion, it had to run from CRON, log its activity and be neat and easy to use for the other guys to take care of. So logically I implemented a shell script to generate a nicely formatted report, had it email itself to the support team and log its activity in case the email never made it.

find-wp-version.sh

#!/bin/bash
#
# Scan for word press versions and save them away
#
#
#
# file format:
#
# $wp_version = ‘3.8’;
# ./site.com.au/httpdocs/wp-includes/version.php
#
EMAIL=”me@my-domain.com,you@your-domain.com”
HOST=`hostname`
#
# Common Logging Code – we log important stuff
# to /logs so we can review it later
#
function LogStart

{
echo “====== Log Start =========” >> $LF
echo “Time: `date`” >> $LF
echo ” ” >> $LF
}
function LogEnd
{
echo ” ” >> $LF
echo “Time: `date`” >> $LF
echo “====== Log End =========” >> $LF
}
function LogMsg
{
echo “`date ‘+%Y-%m-%d|%H:%M:%S|’`$$|OK|$1” >> $LF
}
#
UMASK=002
FILE_DATE=`date ‘+%Y-%m-%d’`
LF_DIR=/logs/cron
LF=$LF_DIR/wp-versions-$FILE_DATE.log
mkdir -p $LF_DIR
chmod 777 /logs/cron
touch $LF
chmod 644 $LF
#
WORKDIR=/tmp
WORKFILE=${WORKDIR}/wordpress-ver.$$
#
RESULTDIR=/tmp
RESULTFILE=${RESULTDIR}/wordpress-report.txt
#
LogStart
LogMsg “Scanning /var/www/vhosts”
find /var/www/vhosts -name “version.php” -exec grep wp_version {} \; -print | egrep -v “@global” > ${WORKFILE}
#
LogMsg “Processing File ${WORKFILE}”
echo “subject: WordPress scan on ${HOST}” > $RESULTFILE
echo “from: root”>> $RESULTFILE
echo ” ” >> $RESULTFILE
echo “WordPress scan on ${HOST} run at $FILE_DATE” >> $RESULTFILE
echo ” ” >> $RESULTFILE
cnt=0
while read line
do
VER=`echo $line| cut -d’=’ -f2| tr -d ‘;’`
read urlline
URL=`echo ${urlline}|tr ‘/’ ‘:’|cut -d’:’ -f5`
printf “%-40s %-15s %s\n” $URL $VER $urlline >> ${RESULTFILE}
LogMsg “URL [ ${URL} ] version [ ${VER} ]”
((cnt++))
done < ${WORKFILE}
LogMsg “${cnt} wordpress sites found”
echo “${cnt} wordpress sites found” >> ${RESULTFILE}
rm -f ${WORKFILE}
cat $RESULTFILE | sendmail “${EMAIL}”
rm -f $RESULTFILE
LogEnd

Analysis

The script is pretty basic, some logging functions that I use everywhere, some setup code to create the required directories. Then a “find” to select candidate files with the right keywords in it and save the output to a file.

find /var/www/vhosts -name “version.php” -exec grep wp_version {} \; -print | egrep -v “@global” > ${WORKFILE}

Then a loop to process the lines, we need to get 2 lines, format up the output.

while read line
do
VER=`echo $line| cut -d’=’ -f2| tr -d ‘;’`
read urlline
URL=`echo ${urlline}|tr ‘/’ ‘:’|cut -d’:’ -f5`
printf “%-40s %-15s %s\n” $URL $VER $urlline >> ${RESULTFILE}
LogMsg “URL [ ${URL} ] version [ ${VER} ]”
((cnt++))
done < ${WORKFILE}

Then we email the report file and cleanup.

rm -f ${WORKFILE}
cat $RESULTFILE | sendmail “${EMAIL}”
rm -f $RESULTFILE
LogEnd

Cron Entry

The CRON file is setup to firstly clean up old logs and secondly, run the report each morning.

#
#
#
# Cleanup Log files in /logs - can also do other directories as needed :)
#
59 23 * * * /root/cleanup-directories.sh -r 21 -d /logs/cron
55 23 * * * /root/cleanup-directories.sh -r 28 -d /logs
#
0 8 * * * /root/find-wp-versions.sh > /dev/null 2>&1

Enjoy!

Advertisements