#!/usr/bin/env perl
# MySQL Slow Log API call
# Queries the sow_log database
# to get worst queries for a user.
#
# Maintainer: Cruz Cranford
# Support: ccranford@hostgator.com

use strict;
use warnings;
use Carp qw( croak );
use Sys::Hostname;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use Getopt::Long qw(VersionMessage HelpMessage);
use Pod::Usage;
use URI::Escape;

my $VERSION = '1.1';
my $prog = 'slowlog';
my $hostname = hostname;
my $ua = LWP::UserAgent->new;
my $days = 14;
my $username = 'showallusers';
my $output_format = 'cli';
my $full = 0;
my $limit = 30;
my $sort = 'rows';
my $input = GetOptions(
	'username=s' => \$username,
	'days=i' => \$days,
	'of=s' => \$output_format,
	'full' => \$full,
	'limit=i' => \$limit,
	'sort=s' => \$sort,
	'servername=s' => \$hostname,
	'help|?'       => sub {
		pod2usage(-sections => 'NAME', -verbose => 99, -exitval => 'NOEXIT');
		pod2usage(-verbose => 1, -exitval => 1 );
	},
	'man'         => sub { pod2usage(-verbose => 2, -exitval => 1 ); },
	'version'     => sub { printf "%s %s %s\n",  $prog, 'version', $VERSION; exit 1; }
) or pod2usage();

my $req = POST 'http://servermon.hostgator.com/slowlogs/slow_log.php', [ 
	pass => uri_escape('yujmdxyzrluh9qpxcuojtoukEfk3efsZ'),
	username => uri_escape($username),
	servername => uri_escape($hostname),
	days => uri_escape($days),
	of => uri_escape($output_format),
	full => uri_escape($full),
	limit => uri_escape($limit),
	sort => uri_escape($sort),
];
print $ua->request($req)->content;

__END__

=head1 NAME

 slowlog - Parses slow_log data to provide queries that ran longer than 20 seconds.

=head1 DESCRIPTION

 This script will parse data from the remote slow_log server to provide viable data
 regarding any slow queries ran for the last 21 from a custoemr.

=head1 SYNOPSIS

 slowlog --username|--days|--full|--limit|--sort|--server|--of

=head1 ARGUMENTS

 --username	username ( --username cr4zyd )
 --days		days ago ( --days 2 )
 --full		output full query ( --full )
 --limit	limit results ( --limit 10 )
 --sort		sort by ( --sort sent, rows, duration, time )
 --server	servername ( --servername alloftheservers )
 --of		output format ( --of cli, csv, ticket )
 --help		usage
 --version	version

=head1 OPTIONS

 --username specifiy the username of the data you wish to see ( default is to show top 30 users ).
 --days specifiy how many days ago you wish to go back ( default is 14, 21 is cap ).
 --full show the full query ( default is off, this will not truncate the sql_text(query) field ).
 --limit specifiy the maximum amount of results you wish to return ( default is 30 ).
 --sort specifiy the column you wish to sort by, sent, rows, duration, time ( default is total rows ).
 --server specifiy the server you wish to view records for ( default is the local server, if alloftheservers is specified, it will show you top results on the farm ).
 --of output the information in a comma delimited format, csv and ticket will output the full query.
 --help this help message.
 --version the version of the script.

=cut
