Posts Tagged ‘perl’

Rate me, bitches!

You know at the bottom of those emails from your tech support people, where it asks you to take a survey and you might win something? Yeah, they use those ratings for something. I wrote a little script so I could cut and paste my most recent ratings and see my average.  We’re expected to stay above a 9.0. I’m just on the edge. I got rep bombed with some 1′s. :(

 $ ./rate.pl -n
Sean's Hack-O-Matic Rating Hack! HUZZAH!
#       Rating  Total   Avg
========================================
1:      10      10      10.00
2:      10      20      10.00
3:      10      30      10.00
4:      10      40      10.00
5:      9       49      9.80
6:      1       50      8.33
7:      10      60      8.57
8:      10      70      8.75
9:      9       79      8.78
10:     1       80      8.00
11:     9       89      8.09
12:     10      99      8.25
13:     10      109     8.38
14:     8       117     8.36
15:     10      127     8.47
16:     10      137     8.56
17:     10      147     8.65
18:     9       156     8.67
19:     9       165     8.68
20:     10      175     8.75
21:     10      185     8.81
22:     10      195     8.86
23:     5       200     8.70
24:     10      210     8.75
25:     8       218     8.72
26:     10      228     8.77
27:     10      238     8.81
28:     10      248     8.86
29:     10      258     8.90
30:     9       267     8.90
31:     8       275     8.87
32:     10      285     8.91
33:     10      295     8.94
34:     10      305     8.97
35:     10      315     9.00
36:     10      325     9.03
37:     10      335     9.05
38:     10      345     9.08
39:     10      355     9.10
40:     10      365     9.12
41:     4       369     9.00
42:     10      379     9.02
43:     10      389     9.05
44:     10      399     9.07
45:     5       404     8.98
46:     10      414     9.00
47:     10      424     9.02
48:     10      434     9.04
49:     6       440     8.98
50:     8       448     8.96
51:     10      458     8.98
Rating Average: 8.98

Here’s the code, just for those who are curious:

#!/usr/bin/perl

use Getopt::Std;
getopts('hn');

if ($opt_n) { $nolog = 1; }
if ($opt_h) { print "USAGE: $0 [-n]\n\t-n = no logging\n"; exit; }

# Eat it, Yoshi!
@ratings = `cat ./rate.txt`;
$history = "history.txt";
# Setup variables
$total = 0;
$rate_total = 0;

print "Sean's Hack-O-Matic Rating Hack! HUZZAH!\n";
print "#\tRating\tTotal\tAvg\n";
print "========================================\n";

foreach $line (@ratings)
{
        chomp $line;

        # If a blank line or a comment (#) skip the line
        if ($line eq "" || substr($line,0,1) eq "#") { $currentline++; next; }

        # DURTY DURTY
        ($spam, $ham) = split/Rating: /,$line;
        ($rating, $spam) = split/\]/,$ham;

        # NO NUMBER? NO CARE!
        if (!$rating) { next; }

        # If ticket had multiple ratings, let's not cheat ourselves. Conquer and divide... (and average!)
        if ($rating =~ ",") {

                @multirating = split/\,/,$rating;
                foreach $mr (@multirating) {
                        # For each rating, you need to increment your count to keep the average correct.
                        $total++;
                        # Add the new to the existing
                        $rate_total = $rate_total + $mr;
                        $current_average = sprintf("%.2f",$rate_total / $total);
                        print "$total:\t$mr\t$rate_total\t$current_average\n";
                }

        } else {
                # Incremement for the average.
                $total++;
                # Add the new to the existing
                $rate_total = $rate_total + $rating;
                $current_average = sprintf("%.2f",$rate_total / $total);
                print "$total:\t$rating\t$rate_total\t$current_average\n";
        }
}

# Average the totals, round to 2 decimal places
$avg = sprintf("%.2f",$rate_total / $total);

print "Rating Average: $avg\n";

# Log it for historical tracking.
if ($nolog) {
        print "Logging disabled\n";
} else {
        &HistoryLog("$avg");
}

sub HistoryLog
{
    my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

    open (LOGFILE, ">>$history");

    printf LOGFILE "%02d/%02d/%04d %02d:%02d:%02d => %s\n",
    $mon+1, $mday, $year+1900, $hour, $min, $sec, $_[0];

    close(LOGFILE);
}

OMG WALL HAX

So, I was trying to figure out who filled up /home today. Believe it or not, running `du -h –max-depth=1` on a 1TB drive under heavy use takes a while. Luckily, on a cPanel server, there is a .ftpquota file in each user’s home directory. This is not updated in real time, but it’s generally close enough.  So I cheated and wrote this little hack.

#!/usr/bin/perl
@files = `ls /var/cpanel/users`;
foreach $user (@files) {
        chomp $user;
        $quotafile = "/home/$user/.ftpquota";
        if (-e $quotafile) {
                $quota = `cat $quotafile`;
                chomp $quota;
                ($spam, $ham) = split/ /,$quota;
                $gb = sprintf("%.2f",$ham / 1024 / 1024 / 1024);
                if ($gb < 1) { next; }
                print "$user: $gb\n";
        }
}

Let me know if you’ve got a better way to do this.  I’m interested to learn new dirty hacks.

Oh, and by the way, one user was consuming 863GB.