auto
  #   ˆl–Ãa¾”êeJq@ŠãO\ªbÑ¿Á„m>À¿ <ƒ    #!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/smartcheck                      Copyright 2022 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package scripts::smartcheck;

use strict;
use warnings;
use Cpanel::DataStore        ();
use Getopt::Param            ();
use Cpanel::StringFunc::Trim ();
use Cpanel::SafeRun::Dynamic ();

our $bin                       = '/usr/local/cpanel/3rdparty/bin/smartctl';
our $last_sent                 = '/var/cpanel/last_smartcheck_msg_alert';
our $fstab_file                = '/etc/fstab';
our $cust_d_file               = '/var/cpanel/smartcheck_custom_dash_d.yaml';
our $disable_file              = '/var/cpanel/disablesmartcheck';
our $smart_enabled_rgx_21      = 'Drive supports S.M.A.R.T. and is enabled';
our $smart_enabled_rgx_5x      = '.*SMART.*Enabled|Available.*has.*SMART';
our $smart_enabled_rgx_6x      = '.*SMART.*Enabled|Available.*has.*SMART';
our $smart_enabled_rgx_default = '.*SMART.*Enabled|Available.*has.*SMART';

sub get_smartctl {
    my %smart = (
        'url'         => 'http://smartmontools.sourceforge.net/',
        'version_cmd' => '-V',
        'version_rgx' => '^smartctl\s*(?:version)?\s*',

        # 2.1 is retained for historical reasons and may be removed without notice
        '2.1' => {
            'smart_enabled_cmd' => '-i',
            'smart_enabled_rgx' => $smart_enabled_rgx_21,
            'errors_only'       => '-l',
            'ok_drive_rgx'      => [qw(/sd /hd)],
        },

        # 5.37 is the last version of smartmontools directly distributed with cP/WHM
        '5.37' => {
            'smart_enabled_cmd' => '-i',
            'smart_enabled_rgx' => $smart_enabled_rgx_5x,
            'errors_only'       => '-q errorsonly -H -l selftest -l error',
            'ok_drive_rgx'      => [qw(/sd /hd)],
        },

        # 5.42 is the latest version of smartmontools provided via yum for CentOS 5
        '5.42' => {
            'smart_enabled_cmd' => '-i',
            'smart_enabled_rgx' => $smart_enabled_rgx_5x,
            'errors_only'       => '-q errorsonly -H -l selftest -l error',
            'ok_drive_rgx'      => [qw(/sd /hd)],
        },

        # 5.43 is the latest version of smartmontools provided via yum for CentOS 6
        '5.43' => {
            'smart_enabled_cmd' => '-i',
            'smart_enabled_rgx' => $smart_enabled_rgx_5x,
            'errors_only'       => '-q errorsonly -H -l selftest -l error',
            'ok_drive_rgx'      => [qw(/sd /hd)],
        },

        # 6.2 is the latest version of smartmontools provided via yum for CentOS 7
        '6.2' => {
            'smart_enabled_cmd' => '-i',
            'smart_enabled_rgx' => $smart_enabled_rgx_6x,
            'errors_only'       => '-q errorsonly -H -l selftest -l error',
            'ok_drive_rgx'      => [qw(/sd /hd)],
        },

        # default is suitable for all known versions of 5.x and 6.x
        'default' => {
            'smart_enabled_cmd' => '-i',
            'smart_enabled_rgx' => $smart_enabled_rgx_default,
            'errors_only'       => '-q errorsonly -H -l selftest -l error',
            'ok_drive_rgx'      => [qw(/sd /hd)],
        },
    );
    return \%smart;
}

sub _get_enabled_string {
    my ( $smart, $vrs, $dsk ) = @_;
    my $disk_enabled = '';
    Cpanel::SafeRun::Dynamic::livesaferun(
        'prog'      => [ $bin, split( /\s+/, $smart->{$vrs}->{'smart_enabled_cmd'} ), $dsk ],
        'formatter' => sub {
            my ($line) = @_;
            $disk_enabled .= $line;
            return '';
        },
    );
    return $disk_enabled;
}

sub is_smart_enabled {
    my ( $smart, $vrs, $dsk ) = @_;
    my $disk_enabled = _get_enabled_string( $smart, $vrs, $dsk );
    return ( $disk_enabled =~ m/$smart->{$vrs}->{'smart_enabled_rgx'}/i ) ? 1 : undef;
}

sub _extract_ver