#!/bin/bash

###############################################################################
# ERP REQUEST FORENSICS
###############################################################################

LOG="/usr/local/apache/logs/access_log"

START_REGEX="27/Jul/2026:10:[00-10]:"

OUT="erp_forensics.txt"
TMP="/tmp/erp_forensics.$$"

mkdir -p "$TMP"

exec > "$OUT" 2>&1

echo "======================================================"
echo "ERP REQUEST FORENSICS"
echo "======================================================"
echo

###############################################################################
# Extract outage window
###############################################################################

grep "$START_REGEX" "$LOG" > "$TMP/window.log"

echo "Requests in outage window:"
wc -l "$TMP/window.log"
echo

###############################################################################
# HOST ANALYSIS
###############################################################################

echo "======================================================"
echo "TOP HOSTS"
echo "======================================================"

awk -F'"' '
{
    split($0,a," ");
    host=a[1];
    print host;
}' "$TMP/window.log" \
| sort \
| uniq -c \
| sort -nr \
| head -50

echo

###############################################################################
# EVOLUTION ERP REQUESTS
###############################################################################

echo "======================================================"
echo "EVOLUTION ERP REQUESTS"
echo "======================================================"

grep -Ei \
'my\.evolutionerp\.com\.au|portal\.evolutionerp\.com\.au' \
"$TMP/window.log" \
> "$TMP/erp.log"

echo "ERP Requests:"
wc -l "$TMP/erp.log"

echo

###############################################################################
# PAGE= COUNTS
###############################################################################

echo "======================================================"
echo "TOP page= REQUESTS"
echo "======================================================"

grep -oE 'page=[^&" ]+' "$TMP/erp.log" \
| cut -d= -f2 \
| sort \
| uniq -c \
| sort -nr \
| head -100

echo

###############################################################################
# NON PAGE REQUESTS
###############################################################################

echo "======================================================"
echo "NON page= ERP REQUESTS"
echo "======================================================"

grep -v 'page=' "$TMP/erp.log" \
| awk -F'"' '{print $2}' \
| sort \
| uniq -c \
| sort -nr \
| head -100

echo

###############################################################################
# TOP ERP URLS
###############################################################################

echo "======================================================"
echo "TOP ERP URLS"
echo "======================================================"

awk -F'"' '{print $2}' "$TMP/erp.log" \
| sort \
| uniq -c \
| sort -nr \
| head -100

echo

###############################################################################
# TOP ERP IPS
###############################################################################

echo "======================================================"
echo "TOP ERP IPS"
echo "======================================================"

awk '{print $1}' "$TMP/erp.log" \
| sort \
| uniq -c \
| sort -nr \
| head -100

echo

###############################################################################
# ALL OTHER DOMAINS
###############################################################################

echo "======================================================"
echo "OTHER DOMAIN SUMMARY"
echo "======================================================"

grep -Eiv \
'my\.evolutionerp\.com\.au|portal\.evolutionerp\.com\.au' \
"$TMP/window.log" \
> "$TMP/other.log"

echo "Top URLs (all other domains)"

awk -F'"' '{print $2}' "$TMP/other.log" \
| sort \
| uniq -c \
| sort -nr \
| head -100

echo

echo "Top IPs (all other domains)"

awk '{print $1}' "$TMP/other.log" \
| sort \
| uniq -c \
| sort -nr \
| head -100

echo

###############################################################################
# POTENTIAL OOM SUSPECTS
###############################################################################

echo "======================================================"
echo "POTENTIAL ERP OOM SUSPECT PAGES"
echo "======================================================"

grep -oE 'page=[^&" ]+' "$TMP/erp.log" \
| cut -d= -f2 \
| sort \
| uniq -c \
| sort -nr \
| egrep -i \
'quote|dispatch|job|invoice|inventory|report|export|ledger|picking'

echo

rm -rf "$TMP"

echo "Report written to: $OUT"
