package Cpanel::LVEInfo;

# cpanel - Cpanel/LVEInfo.pm                  Copyright(c) 2011 CloudLinux, Inc.
#                                                           All Rights Reserved.
# info@cloudlinux.com                                  http://www.cloudlinux.com
# This module is free software; you can redistribute it and/or modify it under
# the same terms as Perl itself. See http://dev.perl.org/licenses/artistic.html

use Cpanel::Math            ();
use Cpanel::SafeRun::Errors ();
use Cpanel::PwCache         ();
use Cpanel::StatsBar        ();

my $mark_phrase_module;

# On some early cPanel versions, 'Locale::Maketext::Utils::MarkPhrase' does not exist
# and 'Cpanel::CPAN::Locale::Maketext::Utils::MarkPhrase' is used instead
BEGIN {
    eval {
        require Locale::Maketext::Utils::MarkPhrase;
        Locale::Maketext::Utils::MarkPhrase->import();
        $mark_phrase_module = 'Locale::Maketext::Utils::MarkPhrase';
        1;
    } or do {
        require Cpanel::CPAN::Locale::Maketext::Utils::MarkPhrase;
        Cpanel::CPAN::Locale::Maketext::Utils::MarkPhrase->import();
        $mark_phrase_module = 'Cpanel::CPAN::Locale::Maketext::Utils::MarkPhrase';
    };
}

my @lve;
my @def;

sub get_lve_info {
    my ($id) = @_;

    if ( open my $fh, '<', '/var/lve/info' ) {
        while ( my $line = readline $fh ) {
            chomp $line;
            my @vals = split( /,/, $line );
            if ( $vals[0] == $id ) {
                close $fh;
                return @vals;
            }
            elsif ( $vals[0] eq '0' ) {
                @def = @vals;
            }
        }
        close $fh;
    }

    return;
}

sub LVEInfo_init {
    Cpanel::StatsBar::_load_stats_ref();
    $Cpanel::StatsBar::rSTATS->{'lvecpu'}{'module'} = 'LVEInfo';
    $Cpanel::StatsBar::rSTATS->{'lvecpu'}{'_count'} = \&Cpanel::LVEInfo::_cpu;
    $Cpanel::StatsBar::rSTATS->{'lvecpu'}{'_max'}   = 100;
    $Cpanel::StatsBar::rSTATS->{'lvecpu'}{'phrase'} = $mark_phrase_module->can('translatable')->('CPU Usage');
    $Cpanel::StatsBar::rSTATS->{'lvecpu'}{'units'}  = '%';
    $Cpanel::StatsBar::rSTATS->{'lvemem'}{'module'} = 'LVEInfo';
    $Cpanel::StatsBar::rSTATS->{'lvemem'}{'_count'} = \&Cpanel::LVEInfo::_mem;
    $Cpanel::StatsBar::rSTATS->{'lvemem'}{'_max'}   = \&Cpanel::LVEInfo::_mem_limit;
    $Cpanel::StatsBar::rSTATS->{'lvemem'}{'phrase'} = $mark_phrase_module->can('translatable')->('Memory Usage');
    $Cpanel::StatsBar::rSTATS->{'lvemem'}{'units'}  = 'KB';
    $Cpanel::StatsBar::rSTATS->{'lvemep'}{'module'} = 'LVEInfo';
    $Cpanel::StatsBar::rSTATS->{'lvemep'}{'_count'} = \&Cpanel::LVEInfo::_mep;
    $Cpanel::StatsBar::rSTATS->{'lvemep'}{'_max'}   = \&Cpanel::LVEInfo::_mep_limit;
    $Cpanel::StatsBar::rSTATS->{'lvemep'}{'phrase'} = $mark_phrase_module->can('translatable')->('Entry Processes');
    my $uid = getuid();
    @lve = get_lve_info($uid);
}

# Format of line in /var/lve/info (formed by lvestats-server):
# (id, v.mep, v.lep, int(math.ceil(v.cpu_usage)), \
#   v.lcpu, int(math.ceil(v.mem_usage)), v.lmem,\
#     v.mem_fault, v.mep_fault)
use constant ID_INDEX   => 0;
use constant MEP_INDEX  => 1;
use constant LEP_INDEX  => 2;
use constant CPU_INDEX  => 3;
use constant LCPU_INDEX => 4;
use constant MEM_INDEX  => 5;
use constant LMEM_INDEX => 6;
use constant MEMF_INDEX => 7;
use constant MEPF_INDEX => 8;

sub _mem {
    return $lve[MEM_INDEX] << 2 if (@lve);
    return 0;
}

sub round_mem {
    return RoundMemK( $lve[MEM_INDEX] << 2 ) if (@lve);
    return 0;
}

sub LVEInfo_mem {
    print round_mem();
}

sub _mem_limit {
    return $lve[LMEM_INDEX] << 2 if (@lve);
    return $def[LMEM_INDEX] << 2;
}

sub round_mem_limit {
    return RoundMemK( $lve[LMEM_INDEX] << 2 ) if (@lve);
    return RoundMemK( $def[LMEM_INDEX] << 2 );
}

sub LVEInfo_mem_limit {
    print round_mem_limit();
}

sub _cpu {
    if   ( @lve && $lve[LCPU_INDEX] != 0 ) { return int( $lve[CPU_INDEX] / $lve[LCPU_INDEX] * 100 ); }
    else                                   { return 0; }
}

sub LVEInfo_cpu {
    print _cpu();
}

sub getuid() {
    my $user = $Cpanel::user;
    return ( Cpanel::PwCache::getpwnam($user) )[2];
}

sub LVEInfo_start {
}

sub _mep {
    if   (@lve) { return $lve[MEP_INDEX]; }
    else        { return 0; }
}

sub LVEInfo_mep {
    print _mep();
}

sub _mep_limit {
    if   (@lve) { return $lve[LEP_INDEX]; }
    else        { return $def[LEP_INDEX]; }
}

sub LVEInfo_mep_limit {
    print _mep_limit();
}

sub isError {
    my $x = shift;
    return $x ? 'errors' : 'lveok';
}

sub api2_getUsage {
    my %CFG = @_;
    my @RSD;

    my $lveid   = getuid();
    my $period  = !exists $CFG{'period'} || $CFG{'period'} eq '' ? '1d' : $CFG{'period'};
    my $INPUT   = Cpanel::SafeRun::Errors::saferunallerrors( '/usr/sbin/lveinfo', '--csv', "--period=$period", "--id=$lveid" );
    my @results = split( /\n+/, $INPUT );
    shift @results;

    foreach my $i (@results) {

        # From,To,aCPU,mCPU,lCPU,aEP,mEP,lEP,aMem,mMem,lMem,MemF,MepF
        my ( $from, $to, $acpu, $mcpu, $lcpu, $aep, $mep, $lep, $aMem, $mMem, $lMem, $memf, $mepf ) = split( /,/, $i );
        my ( $apcpu, $mpcpu ) = ( 0, 0 );
        $lcpu  = 100 if ( $lcpu == 0 );
        $apcpu = Cpanel::Math::ceil( $acpu * 100 / $lcpu );
        $mpcpu = Cpanel::Math::ceil( $mcpu * 100 / $lcpu );

        unshift(
            @RSD,
            {
                'from'    => $from,
                'to'      => $to,
                'acpu'    => $acpu,
                'mcpu'    => $mcpu,
                'lcpu'    => $lcpu,
                'aep'     => $aep,
                'mep'     => $mep,
                'lep'     => $lep,
                'aMem'    => $aMem,
                'mMem'    => $mMem,
                'lMem'    => $lMem,
                'memf'    => $memf,
                'mepf'    => $mepf,
                'apcpu'   => $apcpu,
                'mpcpu'   => $mpcpu,
                'aMemStr' => RoundMemB($aMem),
                'mMemStr' => RoundMemB($mMem),
                'lMemStr' => RoundMemB($lMem),
                'eapcpu'  => isError( $apcpu > 75 ),
                'empcpu'  => isError( $mpcpu > 90 ),
                'eaep'    => isError( ( $lep > 0 ) && ( $aep * 2 > $lep ) ),
                'emep'    => isError( ( $lep > 0 ) && ( $mep > $lep * .9 ) ),
                'eaMem'   => isError( ( $lMem > 0 ) && ( $aMem * 2 > $lMem ) ),
                'emMem'   => isError( ( $lMem > 0 ) && ( $mMem > $lMem * .9 ) ),
                'ememf'   => isError( $memf > 0 ),
                'emepf'   => isError( $mepf > 0 )
            }
        );
    }
    return \@RSD;
}

sub api2 {
    my ($func) = @_;
    my %API = (
        'getUsage' => {
            'func'   => 'api2_getUsage',
            'engine' => 'hasharray',
        },
    );
    return $API{$func};
}

sub get_usage_info {
    return api2_getUsage( 'period' => '1d' );
}

sub LVEInfo_print_usage_overview {
    my $usage = get_usage_info();

    unless (@$usage) {
        print '<h3>There has been no activity on your site within the past 24 hours</h3><br/>';
        return;
    }

    my $cpu_limited_flag = 0;
    my $memf_total       = 0;
    my $mepf_total       = 0;
    my $cpu_max          = 0;
    my $mem_max          = 0;
    my $lmem_max         = 0;
    my $ep_max           = 0;
    my $lep_max          = 0;

    foreach my $hash (@$usage) {
        $cpu_limited_flag = 1 if ( $hash->{'mcpu'} >= $hash->{'lcpu'} );
        $memf_total += $hash->{'memf'};
        $mepf_total += $hash->{'mepf'};
        $cpu_max  = $hash->{'mpcpu'} if ( $hash->{'mpcpu'} > $cpu_max );
        $mem_max  = $hash->{'mMem'}  if ( $hash->{'mMem'} > $mem_max );
        $lmem_max = $hash->{'lMem'}  if ( $hash->{'lMem'} > $lmem_max );
        $ep_max   = $hash->{'mep'}   if ( $hash->{'mep'} > $ep_max );
        $lep_max  = $hash->{'lep'}   if ( $hash->{'lep'} > $lep_max );
    }

    if ( ($cpu_limited_flag) || ( $memf_total > 0 ) || ( $mepf_total > 0 ) ) {
        print '<h3>Your site has been limited within the past 24 hours</h3>';

        if ($cpu_limited_flag) {
            print '<p>CPU resources were limited for your site';
        }

        if ( $memf_total > 0 ) {
            print '<p>Memory resources were limited for your site';
        }

        if ( $mepf_total > 0 ) {
            print '<p>You have reached entry processes (number of simultaneously running php and cgi scripts, ', 'as well as cron jobs and shell sessions) limit <b>', $mepf_total, '</b> times';
        }
    }
    else {
        print '<h3>Your site had no issues in the past 24 hours</h3>';
    }

    if (   ( ( !$cpu_limited_flag ) && ( $cpu_max > 90 ) )
        || ( ( $memf_total == 0 ) && ( $lmem_max > 0 ) && ( $mem_max > $lmem_max * 0.9 ) )
        || ( ( $mepf_total == 0 ) && ( $lep_max > 0 )  && ( $ep_max > $lep_max * 0.9 ) ) ) {
        print '<p><h3>Your site might hit resource limits soon</h3>';

        if ( ( !$cpu_limited_flag ) && ( $cpu_max > 90 ) ) {
            print '<p>Your CPU usage was at ', $cpu_max, '% out of 100%';
        }

        if ( ( $memf_total == 0 ) && ( $lmem_max > 0 ) && ( $mem_max > $lmem_max * 0.9 ) ) {
            print '<p>Your Memory usage was at ', RoundMemB($mem_max), ' out of ', RoundMemB($lmem_max);
        }

        if ( ( $mepf_total == 0 ) && ( $lep_max > 0 ) && ( $ep_max > $lep_max * 0.9 ) ) {
            print '<p>You had ', $ep_max, ' entry processes (number of simultaneously running php and cgi scripts, ', 'as well as cron jobs and shell sessions) out of ', $lep_max, ' max entry processes allowed';
        }
    }
}

# Function converts bytes to kilobytes, megabytes or gigabytes (if needed)
# and appends appropriate suffix (K, M, G)
# Parameter $_[1] = memory in bytes
sub RoundMemB {
    my $mem = shift;

    if ( $mem >= ( 1024 * 1024 * 1024 ) ) {
        return sprintf( "%.1fG", $mem / ( 1024 * 1024 * 1024 ) );
    }

    if ( $mem >= 1024 * 1024 ) {
        return sprintf( "%.1fM", $mem / ( 1024 * 1024 ) );
    }

    if ( $mem >= 1024 ) {
        return sprintf( "%.0fK", $mem / 1024 );
    }

    return "$mem";
}

# Function converts kilobytes to megabytes or gigabytes (if needed)
# and appends appropriate suffix (K, M, G)
# Parameter $_[1] = memory in kilobytes
sub RoundMemK {
    my $mem = shift;

    if ( $mem >= ( 1024 * 1024 ) ) {
        return sprintf( "%.1fG", $mem / ( 1024 * 1024 ) );
    }

    if ( $mem >= 1024 ) {
        return sprintf( "%.1fM", $mem / 1024 );
    }

    return "$mem" . 'K';
}

1;
