#!/usr/bin/perl

###########
# montool - Kevin Schottie (kschottie@hostgator.com)
# 
# This is the monitor tool suite for hostgator.com
#
# GIT: http://git.toolbox.hostgator.com/montool
# WIKI: Not yet created.
#
# Please submit all bug reports at bugs.hostgator.com
#
# (C) 2011 - HostGator.com, LLC
###########

package main;

use lib '/opt/hgmods/monitoring/montool/lib';

use strict;
use warnings;
use core;

my $montool = new montool();
my $monitor = $montool->monitor();

# commands with no arguments
our %noargcommands = (   "help" => "Help screen",
                        "version" => "Report montool module version.",
                        "bsyncwatch" => "Watch the current progress of a bsync.",
                        "topper" => "top 20 cpu users according to ps.",
                        "reniceservices" => "renice any services that are set at nice 0",
                        "notnice" => "List processes running at nice 0",
                        "clearsemaphores" => "Clear apache semaphores",
                        "clearblackholes" => "Clear blackholed IP addresses",
                        "postreboot" => "Post reboot hardware check.",
                        "curdom" => "Process Apache domain access log(s) for the given user, log file, stdin reads.",
                        "resps" => "List current running processes and their associated resellers",
                        "cpucount" => "Display the number of CPU cores in this server.",
                        "worklogbegin" => "Output the information that you need to start your worklog.",
                        "countprocesses" => "Display a summary count of running processes.",
                        "rebuildcache" => "Limit the number of running php processes to allow disk cache to rebuild.",
                        "forcekillsql" => "Forcefully kill mySQL when 'service mysql stop' does not work.",
                        "runningservices" => "List all ports that have a service listening on them with the PID and executable name.",
                        "checkssl" => "Check the primary SSL cert on the server and whether the server is listening on port 443.",
                        "sysinfo" => "Display a system information overview report (incomplete but functional)",
                        "checkfs" => "Check filesystem states for the server.",
                        "tmaint" => "Place the server in maintenance mode.",
                        "remaint" => "Take the server out of maintenance mode."
                     );

# Commands that handle the sanitization of their input adequately.
our %commands = ( "htaccessblock" => "Enable htaccess IP restriction",
                 "htaccessunblock" => "Remove htaccess IP restriction",
                 "ht503ipblock" => "Enable 503 block",
                 "ht503ipunblock" => "Disables 503 block",
                 "nullroutedip" => "Migrate accounts off of a null route IP to existing shared IP addresses.",
                 "user" => "Get current information on a user's usage",
                 "suspendreseller" => "Suspend the public_html of a reseller and all sub accounts",
                 "unsuspendreseller" => "Un-Suspend the public_html of a reseller and all sub accounts",
                 "suspend" => "Suspend a script or directory.",
                 "unsuspend" => "Un-Suspend a script or directory.",
                 "sqlsuspend" => "Suspend mySQL usage for a user.",
                 "sqlunsuspend" => "Un-Suspend mySQL usage for a user.",
                 "sak" => "Swiss Army Knife 0.1.2 - jsouth\@hostgator.com",
                 "bam" => "Block Anything Menacing",
                 "defragtablesforuser" => "OPTIMIZE all fragmented mySQL tables owned by a user. (reports only at this time.)",
                 "vzsuspend" => "Suspend a script or directory within a container.",
                 "vzsunuspend" => "Un-Suspend a script or directory within a container.",
                 "movedatabase" => "Move a user's mySQL database from /var to /home",
                 "servermon" => "Display historical data from servermon."

               );

unless ( @ARGV ) {
        print "No argument provided.\n";
        $montool->help();
}

chomp(my $thiscommand = "@ARGV");
$montool->logthis($monitor, $thiscommand);
undef $thiscommand;
undef $monitor;
my $montoolcommand = shift @ARGV;


        # Special circumstance commands
        if ($montoolcommand =~ /^help$/){
                $montool->$montoolcommand(@ARGV);
        }elsif ($montoolcommand =~ /^titleload$/){
                undef %commands;
                undef %noargcommands;
                $montool->$montoolcommand(getppid());

        # For simple commands like version that do not require arguments        
        }elsif (defined $noargcommands{$montoolcommand}) {
                undef %commands;
                undef %noargcommands;
                # $SIG{INT} = sub { return };
                if(@ARGV){
                      $montool->$montoolcommand(@ARGV);  
                }else{
                        $montool->$montoolcommand();
                }

        # All other commands (routines must be self sanitizing).
        }elsif (defined $commands{$montoolcommand}) {
                undef %commands;
                undef %noargcommands;
                if (@ARGV){
                        $montool->$montoolcommand(@ARGV);
                }else{
                        print "[$montoolcommand] requires additional arguments. Try 'montool help $montoolcommand'\n";
                }
}else{
        print "\n[!] Invalid command! [$montoolcommand]. Try 'help'\n\n";
        exit;
}