Elitist Jerks
Register
Blogs
Forums


Go Back   Elitist Jerks » Blogs » Fiesty and Short

Once upon a time, a casual tank formed a raid coalition for BC. Stuff happened.
Rate this Entry

Automatic Stasiscl Log Posting

Posted 08/14/08 at 1:58 PM by Abbi
Hey, it's been a while since I posted bad code. OK! Here we go!

This is a pair of scripts which I use to automatically handle log uploads. My goal was to be able to allow people to upload logs into an FTP directory, and run a script out of cron that would periodically check that directory and do intelligent things. I'm anal, so I wanted the logs to be renamed, organized, and so forth.

It assumes four directories -- one for incoming logs, one for processed logs, one for raids, and one for saved files (which is a backup, so if something screws up you can go get the originals).

If you look at this and have no idea what to do with it, it's probably not the tool for you. UNIX shell scripting competence is important. Sorry!

There are two scripts; the main one, and a little perl script that detects which instance you've been running. You could cut the second one out if you're not as obsessive as I am.

This doesn't perform perfectly if someone dumps a log with multiple instances on it, but it doesn't break either.

Code:
#!/bin/sh

TMP=/tmp

#### Directories & Stasis Location
####
#### Edit these to suit your needs

# RAID = where you want your raid files
RAID=~/htdocs/wir/logs/raids
LOGS=~/wow/wir-logs-in
OLD=~/wow/wir-logs-old
REPO=~/htdocs/wir/logs

# Directory in which stasis lives
STASIS=~/wow-scripts/stasiscl

mkdir -p $RAID $LOGS $OLD $REPO

# Lockfile

if [ -f /tmp/log.processing ]; then
    exit;
fi
touch /tmp/log.processing

# The commented out line is what I use, because I process these on a different host than people ftp them to. I left it in for example only.

cd $LOGS
# /usr/local/bin/wget --quiet --delete-remote --ftp-user=wildly_logs --ftp-password=XXXX ftp://www.XXXX.org/*

if [ `ls $LOGS | wc -l` -eq 0 ]; then
    rm /tmp/log.processing
    exit
fi

ls $LOGS

if [ `ls *.zip 2> /dev/null | wc -l` -gt 0 ]; then
    for ZIP in *.zip ; do
        /usr/local/bin/unzip -qq "$ZIP"
        mv "$ZIP" $OLD
    done
fi

if [ `ls *.ZIP 2> /dev/null | wc -l` -gt 0 ]; then
    for ZIP in *.ZIP ; do
        /usr/local/bin/unzip -qq "$ZIP"
        mv "$ZIP" $OLD
    done
fi

if [ `ls *.gz 2> /dev/null | wc -l` -gt 0 ]; then
    for GZ in *gz; do
        /usr/bin/gunzip -q -c "$GZ" > "$GZ.txt"
        mv "$GZ" $OLD
    done
fi

# Mac cleanup
rm -rf $LOGS/__MACOSX

for LOG in *; do
    if [ -f "$LOG" ]; then
        echo "Processing $LOG..."
        cp "$LOG" $TMP
        mv "$LOG" $OLD

        # Date stuff

        ODATE=`head -10 "$TMP/$LOG" | tail -1 | awk '{ print $1 }'`
        DAY=${ODATE##*/}
        MON=${ODATE%%/*}
        YEAR=`date +%y`
        if [ $MON -eq "12" ]; then
            if [ `date +%m` -eq "01" ]; then
                YEAR=`date -v-1y +%y`
            fi
        fi
        if [ ${#DAY} -eq 1 ]; then
            DAY=0$DAY;
        fi
        if [ ${#MON} -eq 1 ]; then
            MON=0$MON;
        fi
        DATE=${MON}-${DAY}-${YEAR}

        if [ -f "$REPO/$LOG" ]; then
            rm /tmp/log.processing
            exit
        fi

        grep "^${ODATE}" "$TMP/$LOG" > "$TMP/$LOG.out"
        rm "$TMP/$LOG"

        # What raid?

        INSTANCE=`~/bin/getraid.pl "$TMP/$LOG.out"`

        # Other logs?

        LAST=1
        if [ -f $REPO/${DATE}-${INSTANCE}.1.txt.gz ]; then
            LAST=`ls $REPO | tail -1`
            LAST=${LAST#*.}
            LAST=${LAST%.txt.gz}
            LAST=$(( $LAST + 1 ))
        fi

        # Move into place

        cp "$TMP/$LOG.out" $REPO/${DATE}-${INSTANCE}.${LAST}.txt
        rm "$TMP/$LOG.out"

        # Prep directory

        mkdir -p $RAID/$MON-$YEAR

        # Stasis

        START=`date +%s`

        cd $STASIS
        ./stasis add -dir $RAID/$MON-$YEAR -file $REPO/${DATE}-${INSTANCE}.${LAST}.txt -attempt -trash -overall -collapse

        echo -n "Run time: "
        echo -n $(( `date +%s` - $START ))
        echo " seconds."

        # Tidy up

        echo "Added $REPO/${DATE}-${INSTANCE}.${LAST}.txt"
        gzip $REPO/${DATE}-${INSTANCE}.${LAST}.txt
        cd $LOGS
    fi
done

rm -f /tmp/log.processing
And getraid.pl. Does not include Sunwell. Should be fairly obvious...

Code:
#!/usr/bin/perl

%mobs =    (
                "Kurinnaxx"                 => "aq20",
                "General Rajaxx"            => "aq20",
                "Buru the Gorger"           => "aq20",
                "Moam"                      => "aq20",
                "Ossirian the Unscarred"    => "aq20",
                "Ayamiss"                   => "aq20",

                "Bloodlord Mandokir"        => "zg",
                "Hakkar"                    => "zg",
                "High Priest Thekal"        => "zg",
                "High Priest Venoxxis"      => "zg",
                "High Priestess Jek'lik"    => "zg",
                "High Priestess Mar'li"     => "zg",
                "Jin'do"                    => "zg",

                "Onyxia"                    => "ony",

                "Lucifron"                  => "mc",
                "Golemagg"                  => "mc",
                "Majordomo Executus"        => "mc",
                "Ragnaros"                  => "mc",
                "Baron Geddon"              => "mc",

                "The Prophet Skeram"        => "aq40",
                "Lord Kri"                  => "aq40",
                "Princess Yauj"             => "aq40",
                "Vem"                       => "aq40",

                "Razorgore"                 => "bwl",
                "Firemaw"                   => "bwl",
                "Nefarian"                  => "bwl",

                "Anub'Rekhan"               => "naxx",
                "Crypt Guard"               => "naxx",
                "Stoneskin Gargoyle"        => "naxx",
                "Sludge Belcher"            => "naxx",
                "Mad Scientist"             => "naxx",
                "Surgical Assistant"        => "naxx",
                "Living Monstrosity"        => "naxx",

                "Attumen the Huntsman"      => "kzan",
                "Midnight"                  => "kzan",
                "Moroes"                    => "kzan",
                "Aran"                      => "kzan",
                "Illhoof"                   => "kzan",
                "Nightbane"                 => "kzan",
                "Netherspite"               => "kzan",
                "Maiden of Virtue"          => "kzan",

                "High King Maulgar"         => "gruul",
                "Gruul the Dragonkiller"    => "gruul",
                "Lair Brute"                => "gruul",

                "Magtheridon"               => "mags",
                "Hellfire Warder"           => "mags",

                "The Lurker Below"          => "ssc",
                "Greyheart Technician"      => "ssc",
                "Fathom-Lord Karathress"    => "ssc",
                "Morogrim Tidewalker"       => "ssc",
                "Leotheras the Blind"       => "ssc",
                "Lady Vashj"                => "ssc",
                "Hydross the Unstable"      => "ssc",

                "Al'ar"                     => "tk",
                "Void Reaver"               => "tk",
                "High Astromancer Solarian" => "tk",
                "Thaladred the Darkener"    => "tk",

                "Doom Lord Kazzak"          => "kazzak",

                "Nalorakk"                  => "za",
                "Akil'zon"                  => "za",
                "Jan'alai"                  => "za",
                "Halazzi"                   => "za",
                "Hex Lord Malacrass"        => "za",
                "Zul'jin"                   => "za",

                "High Warlord Naj'entus"    => "bt",
                "Supremus"                  => "bt",
                "Shade of Akama"            => "bt",
                "Teron Gorefiend"           => "bt",
                "Gurtogg Bloodboil"         => "bt",
                "Mother Shahraz"            => "bt",
                "Lady Malande"              => "bt",
                "Illidan Stormrage"         => "bt",

                "Rage Winterchill"          => "hyj",
                "Anetheron"                 => "hyj",
                "Kaz'rogal"                 => "hyj",
                "Azgalor"                   => "hyj",
                "Archimonde"                => "hyj",
            );

$file = shift;
open $fh, $file or die "could not open file: $file";

while (<$fh>) {
    s/"//g;
    @data = split(/,/, $_);
    if ($data[0] =~ /SWING_DAMAGE$/ && exists $mobs{$data[5]}) {
        $raids{$mobs{$data[5]}}++;
    }
}

close(INPUT);

$raidname = (sort { $raids{$b} <=> $raids{$a} } keys %raids)[0];
print $raidname, "\n";
Total Comments 0

Comments