Your IP : 216.73.216.209


Current Path : /scripts/
Upload File :
Current File : //scripts/smartcheck

#!/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_version {
    my ( $line, $quit_saferun_loop_sr ) = @_;

    my $vrs;
    my $smart = get_smartctl();

    # handle if $line is undef
    if ($line) {
        if ( $line =~ m/^$smart->{'version_rgx'}\s*(\d+\.\d+)/ ) {
            $vrs = $1;
            ${$quit_saferun_loop_sr} = 1;
        }
    }

    # if the version can't be determined or is not found in the smartctl hash, use 'default'
    $vrs = 'default' if ( !$vrs || !exists $smart->{$vrs} );

    return $vrs;
}

sub get_smart_version {
    my $smart = shift;
    my ( $vrs, $output );
    Cpanel::SafeRun::Dynamic::livesaferun(
        'prog'      => [ $bin, split( /\s+/, $smart->{'version_cmd'} ) ],
        'formatter' => sub {
            my ($line) = @_;
            $output .= $line;
            return '';
        },
    );
    $vrs = _extract_version($output);

    return $vrs;
}

sub run {

    my %smart = %{ get_smartctl() };

    my $custom_d = Cpanel::DataStore::fetch_ref($cust_d_file);

    my $param = Getopt::Param->new(
        {
            'help_coderef' => sub {
                print <<"SMARTCHECK_HELP";
$0 - check the status of your harddrive(s) with smartctl.

   --help    this screen
   --quiet   bare minimum output
   --noemail output to screen only, do not send email

TROUBLESHOOTING

If you have a hard drive that keeps getting this output via $0:

  "Checking /dev/sda....S.M.A.R.T does not appear to be enabled for this device."

You may confirm directly if your hard drive supports SMART using the following command:

  # smartctl -i /dev/YOURHDD

After this, if you are certain it has SMART support enabled, then you can tell $0
to specify a given drive type to smartctl using a simple YAML file:

  $cust_d_file (described in detail in the following section)

If after verifying using smartctl directly that your drive is supported and you have added
a custom configuration, it is possible that $0 is simply not properly detecting
that your drive is supported. In this case, please file a bug report.

CONFIGURATION FILE

You may add custom configurations, per drive, to $cust_d_file"

This file may contain simple key/value values where the key is the drive and the value is the value of
that drive's type.

For example to tell smartctl that /dev/sda and /dev/sdb is an "ata" we create this YAML file:

  ---
  /dev/sda: ata
  /dev/sdb: ata

Then the next time it's run we'll see this output instead:

  Checking /dev/sda....
      Adding custom '-d ata' flag for /dev/sda from $cust_d_file
  Ok

For more information on -d options available for supporting drives behind RAID controllers, see:

  http://www.smartmontools.org/wiki/Supported_RAID-Controllers

While YAML files are plain text, its best to initialize the configuration file are shown below.

To write the initial entry into $cust_d_file:

  perl -MYAML::Syck=DumpFile -e 'DumpFile("$cust_d_file", {"/dev/sda"=>"ata"});'

To dump the current has to the screen:

  perl -MYAML::Syck=LoadFile -MData::Dumper -e 'print Dumper(LoadFile("$cust_d_file"));'

Note: Subsequent entries should be handed added since the above two commands overrite any existing file.

$cust_d_file may also be used to add additional drives that do not have entrieds
directly in $fstab_file.
SMARTCHECK_HELP
                exit;
            },
        }
    );

    require Digest::SHA;

    my %drive_lookup;
    my $msg      = '';
    my $didhowdy = 0;
    my $quiet    = $param->get_param('quiet')  ? 1 : 0;
    my $nomail   = $param->get_param('nomail') ? 1 : 0;
    my $debug    = 0;

  FINDSMARTCTL:
    if ( !-l $bin ) {
        for my $smartctl (qw(/usr/sbin/smartctl /usr/local/sbin/smartctl)) {
            if ( -e $smartctl ) {

                #if an existing exists and we still have the old, replace it with a link
                if ( -e $bin ) {
                    unlink $bin or die "Could not remove old cPanel smartctl: $!";
                }
                system 'ln', '-s', $smartctl, $bin;
                last;
            }
        }
    }

    my $link = readlink($bin) || 'smartctl';

  DONTRUNIF:
    {
        if ( !-e $bin ) {    # or -l or -x instead ???
            exit;
        }

        if ( -e $disable_file ) {
            exit;
        }
    }

    my $vrs = get_smart_version( \%smart );
    $smart{'BINVER'} = ( $vrs ne 'default' ) ? $vrs : 'unknown';

    # unsupported SCSI was here

    my @mounts;
  FINDDISKS:
    {
        Cpanel::SafeRun::Dynamic::livesaferun(
            'prog'      => ['mount'],
            'formatter' => sub {
                my ($line) = @_;
                push @mounts, $line;
                return '';
            },
        );

        open my $fstab,, '<', $fstab_file or die "Could not open $fstab_file: $!";
        my @fstab_lines = <$fstab>;
        close $fstab;

        for my $mount_lines ( @mounts, @fstab_lines ) {
            next if $mount_lines =~ m{ (?:cdrom | floppy | noauto) }xms;
            my ($drive) = $mount_lines =~ /^(\/\S+)/;
            if ($drive) {
                $drive =~ s{\d+$}{}g;
                $drive_lookup{$drive} = 1;
            }
        }

        # add additional possible entries from custom config
        for my $dsk ( keys %$custom_d ) {
            $drive_lookup{$dsk} = 1;
        }
    }

  CHECKDISKS:
    {
        for my $dsk ( sort keys %drive_lookup ) {
            next if !grep { $dsk =~ m/\Q$_\E/ } @{ $smart{$vrs}->{'ok_drive_rgx'} };
            if ( !$didhowdy ) {
                print qq{Using smartcheck version profile "$vrs" for smartctl ($smart{'BINVER'})\n}
                  if !$quiet;
                $didhowdy++;
            }

            print "Checking $dsk...." unless $quiet;
            if ( exists $custom_d->{$dsk} && $custom_d->{$dsk} =~ m{^\S+$} ) {
                print "\n\tAdding custom '-d $custom_d->{$dsk}' flag for $dsk from $cust_d_file\n" if !$quiet;
                $smart{$vrs}->{'smart_enabled_cmd'} = "-d $custom_d->{$dsk} $smart{$vrs}->{'smart_enabled_cmd'}";
            }

            if ( is_smart_enabled( \%smart, $vrs, $dsk ) ) {
                my $error = '';
                Cpanel::SafeRun::Dynamic::livesaferun(
                    'prog'      => [ $bin, split( /\s+/, $smart{$vrs}->{'errors_only'} ), $dsk ],
                    'formatter' => sub {
                        my ($line) = @_;
                        $error .= $line;
                        return '';
                    },
                );
                $error = Cpanel::StringFunc::Trim::ws_trim($error);

                if ($error) {
                    $msg .= <<"MSG_END";
                S.M.A.R.T Errors on $dsk
                From Command: $link $smart{$vrs}->{'errors_only'} $dsk
                $error
                ----END $dsk--

MSG_END
                    print "\nErrors:\n $error\n\n" if !$quiet;
                }
                else {
                    print "Ok\n" if !$quiet;
                }
            }
            else {
                print "S.M.A.R.T does not appear to be enabled for this device.\n"
                  if !$quiet;
            }
        }
    }

    if ( $msg && -e $last_sent ) {
        $msg = '' if Digest::SHA->new(512)->addfile($last_sent)->hexdigest eq Digest::SHA::sha512_hex($msg);
    }

  ALERT_FAILINGDISKS:
    {
        if ( $msg ne '' ) {
            if ( open my $last_fh, '>', $last_sent ) {
                print {$last_fh} $msg;
                close $last_fh;
            }
            else {
                warn "Could not open $last_sent for writing: $!";
            }

            my $subject = '[cPanel smartcheck] Possible Hard Drive Failure Soon';
            my $smsg    = <<"SMART_END";
IMPORTANT: Do not ignore this email! !!!! READ IT THOUROUGHLY !!

Your system's smartctl utility has detected some errors.
(see the "---- smartctl report --" section below for details).
You should investigate those errors according to `man smartctl`
and documentation at $smart{'url'}
including their mailing list. You can disable this check by doing:
 `touch /var/cpanel/disablesmartcheck`
and reenable it at anytime by removing that file.
If you do find issues you should consider using smartd to
monitor your drives instead of/along with this script.

This email was generated by the cPanel smartcheck script and is
reporting errors from your S.M.A.R.T enabled drives with the
given commands below. It is up to you to investigate further
and take appropriate steps:

  !!!! Do not submit tickets or bug reports if you get this email.
    Use the avenues outlined in this email to deal with your
    system's S.M.A.R.T setup. !!

To begin your troubleshooting you may want to run
 `/usr/local/cpanel/scripts/smartcheck --nomail` as root via SSH.

---- smartctl report --
$msg
SMART_END

            if ( !$nomail ) {
                require Cpanel::Notify;
                Cpanel::Notify::notification_class(
                    'class'            => 'Check::Smart',
                    'application'      => 'Check::Smart',
                    'constructor_args' => [
                        'origin'       => 'smartcheck',
                        'attach_files' => [ { name => 'SMART-log.txt', content => \$msg } ],
                    ]
                );
            }

            if ($debug) {
                print "This was emailed unless you used --nomail:\n" . "Subject: $subject\n\n$smsg";
            }
        }
    }
    return;
}

run() if !caller();

1;

__END__

=head1 NAME

/scripts/smartcheck

=head1 USAGE

  /scripts/smartcheck [--help] | [--quiet --noemail]

=head1 REQUIRED ARGUMENTS

None

=head1 OPTIONS

=over 4

=item -- help

List the help section, then exit immediately.

=item --quiet

Do not output the normal dialog to STDOUT.

=item --noemail

Do not send an email alert if a disk is found to be failing.

=back

=head1 DESCRIPTION

This utility is a wrapper around smartmontool's smartctl tool. It is run regularly
as part of C</scripts/maintenance>. It may be run independently from the commandline
or as part of a more frequently occurring crontab entry.

If hard drives that are SMART enabled show signs of impending failure, this utility
will generate the necessary warnings necessary to alert the admin of this server.

=head1 DIAGNOSTICS

None

=head1 CONFIGURATION

C</var/cpanel/smartcheck_custom_dash_d.yaml> may be used for two purposes.

First, it may be used to define the type (passed to smartctl's -d flag) for a specific
drive if smartctl is unable to determine this without help.

Secondly, the configuration file may be used to define SMART enabled hard drives that
are not directly listed in the system's C</etc/fstab> file.

For more information on -d options available for supporting drives behind RAID
controllers, please visit:

L<http://www.smartmontools.org/wiki/Supported_RAID-Controllers>

=head1 EXIT STATUS

Exit status is 0 (success) unless an unexpected error occurs, in which case it will
die (255).

=head1 DEPENDENCIES

This utility requires that a modern version of smartmontools be installed. It has been
tested to work with versions 5.x and 6.x, although there is legacy support for version
2.1.

=head1 INCOMPATIBILITIES

None

=head1 SEE ALSO

The help section displayed using the C<--help> option is very handy.

=head1 BUGS AND LIMITATIONS

SMART checks are only made on SMART enabled hard drives that have device names
beginning with "hd" and "sd".

=head1 LICENSE AND COPYRIGHT

   Copyright 2022 cPanel, L.L.C.

Rosenblum TV: Video training, virtual workshops, classes, tutorials
logologologologo
  • About
  • What We Do
  • Our Clients
  • Case Studies
    • The BBC
    • CBS News
    • New York Times Television
    • Spectrum News
    • The Newark Star-Ledger
    • The United Nations
    • McGraw/Hill
    • Oyster Yachts
    • Scottish Environmental Protection Agency
  • The Power of Storytelling
  • Why iPhones?
  • Michael on Media
  • Books
  • Contact
  • About
  • What We Do
  • Our Clients
  • Case Studies
    • The BBC
    • CBS News
    • New York Times Television
    • Spectrum News
    • The Newark Star-Ledger
    • The United Nations
    • McGraw/Hill
    • Oyster Yachts
    • Scottish Environmental Protection Agency
  • The Power of Storytelling
  • Why iPhones?
  • Michael on Media
  • Books
  • Contact

RE-INVENTING THE TELEVISION NEWS BUSINESS*

A revolution in video storytelling

Creating entirely new & cost-effective production methods

From the world leaders in video production training and the creators of Character Driven Storytelling™

*and every other business that uses video

WHAT WE DO

Over the past 35 years, we have designed, built or restructured some of the most powerful news and journalism companies in the world.

We replace the traditional TV news ‘crew’ with one highly trained journalist, working alone with nothing but an iPhone.

No more TV news ‘crews’, no editors and no field producers.

This is television news done the way newspaper journalism is done – one reporter with their electronic pad and pencil.

In doing this, we can cut the cost of production by as much as 75% while increasing ratings and audience engagement.

In the place of conventional TV news ‘packages’ – ie, reporter stand up, interview, b-roll, man on the street, we marry great journalism with Netflix and Hollywood storytelling.

It’s a combination that works.

We have taken most of our clients to #1 in their respective markets.

And it’s not just for news. Any company, any profit, any NGO and anyone else who is online needs to tell their story in compelling yet cost-effective video. We can teach you to do that. Either in person or virtually.

EXAMPLES OF WHAT WE CAN TEACH YOUR STAFF TO PRODUCE

ITAY HOD

Itay Hod, MMJ with KPIX/CBS in San Francisco, took the 5-Day Intensive Video Storytelling Bootcamp in 2018.

Because he works alone, with only an iPhone, he was able to embed himself with a homeless family.

Here’s the story he produced in a one-day turn.

KIET DO

Kiet Do, an MMJ with KPIX/CBS in San Francisco, took the 5-Day Intensive Video Storytelling Bootcamp in 2021.

Here is a story he produced, all on his own, with only an iPhone and in a one-day turn.

TAYLOR SCHAUB

Taylor Schaub, an MMJ with Spectrum News 1 in LA, took the 5-Day Intensive Video Storytelling Bootcamp in 2023.

Here is a story he turned in only one day, using only an iPhone. It was the first video story he ever did and it was nominated for an Emmy.

THE BOOTCAMP

How do we convert stations and whole networks to working in this way?

Since 1988, we have run intensive 5-Day Video Storytelling Bootcamps

We have done these all over the world.

These are hands-on bootcamps, and participants learn an entirely new way of creating TV news stories.

-We shoot at a 3:1 ratio or lower, so turnaround times are fast.

-We go directly from camera to timelilne and edit – no written scripts.  We work in the medium of pictures and sound.

-We are entirely character-driven.

-We are driven by pictures and real events.

-We are focused almost entirely on ’the viewer experience’.

Since 1988, more than 70,000 journalists around the world have taken our bootcamps, either in person on virtualy.

Case Studies

CBS Case Study Logos
CBS News

We have started to work with CBS News, bringing our ideas of character-driven storytelling to one of the most successful and biggest networks in the United States. Since beginning to work with them ratings have climbed and more importantly, audience engagement is through the ceiling.

Learn More
NYT Case Study Logos
New York Times Television

We started New York Times Television in 1990 and it was the first paper to be brought into the world of TV. It quickly became one of the most successful non-fiction production companies in the United States. The series and documentaries we produced won many awards including multiple Emmys.

Learn More
BBC Case Study Logos
The BBC

We have been working with the BBC since the year 2000 helping to convert their national news network to our visual storytelling technique. Most recently we have trained teams from their sports, documentaries, and comedy divisions to make character-driven stories using only smartphones.

Learn More
Spectrum Case Study Logos
Spectrum News

For the past five years we have worked with Spectrum News to introduce and train their journalists on visual, character driven storytelling using smartphones helping to create a different kind of local news for their network of 24-Hour News Stations across the United States.

Learn More
UN Case Study Logos
The United Nations

In 2006, we were approached by the United Nations. Rather than rely on news outlets, it would be much easier to train the field operatives to produce their own stories. We spent two years working with the UN, training more than 100 of their staff in bootcamps in Geneva and Nairobi.

Learn More
Star Ledger Case Study Logos
The Newark Star-Ledger

We trained 50 print reporters at the paper to shoot and tell their own stories, in conjunction with their print work. We built a TV newsroom in their existing print newsroom – you could not ask for a better set and they began to live stream their stories in conjunction with their print work.

Learn More
Mcgraw Hill Case Study Logos
Mcgraw Hill

We spent two years with McGraw Hill, training more than 150 of their staffers, making them completely video literate. McGraw/Hill media properties we transit included Business Week, Aviation Week, (what was the name of the architecture magazine), and JD Power and Associates.

Learn More
VOA Case Study Logos
Voice of America

In 1990, we were approached by The Voice of America, the official broadcasting agency for the United States Government. When we met with VOA, they were only a short wave radio broadcaster, but working with them, we took them into television, launching VOA-TV.

Learn More
Oyster Case Study Logos
Oyster Yachts

British based Oyster Yachts makes some of the finest yachts in the world. Like every other company, they had to find a way to feed the never-ending video demands of social media – sites like Instagram and TikTok. We trained the Oyster staff to tell their own stories, using only iPhones.

Learn More
SEPA Case Study Logos
Scottish Environmental Protection Agency

We were approached by SEPA, the Scottish Environmental Protection Agency because they had to continually find a way to ‘feed the media beast’. The result was that SEPA was able to tell their own stories, whenever they wanted, and at almost no additional cost.

Learn More
Image 11-11-22 at 6.25 PM

Michael on Media

Michael Rosenblum has been writing about the media since 1988. His work and ideas have appeared in The Guardian, The Huffington Post, Ilkeston Life and many other publications.

He has been blogging regularly for the past 35 years on this subject. Having taught media studies at Columbia University, NYU and now the University of Oxford, he is considered an expert on this subject.

Continue reading this post or look back at previous posts.

Read More

Do You Have Questions About Learning Video Skills?

If you would like to know more about our courses contact us and one of our training advisors will be happy to call you.

Contact Us

Copyright 2024. All rights reserved.