Help with perl scripting

Hi All,

I need to run smartctl using nagios account but I'm not familiar with perl scripting.
The existing script runs smartctl using root account. I need it to run using nagios to eliminate security risks.

#!/usr/bin/perl -w
# checks for possible disk errors
use strict;

my $smartctl=preCheck();
my $name=`hostname`;
my $remoteHost=$ARGV[0];
my $remoteDisk=$ARGV[1];
    CHECK: my $dskCheck1=`ssh -o StrictHostKeyChecking=no nagios\@$remoteHost $smartctl -i $remoteDisk`;
    my $retry=1;
        if (grep /Enabled/ig, $dskCheck1) {
            #print "smart support for $remoteDisk in $remoteHost is enabled. proceeding with check...\n";
            report(dskCheck2($remoteDisk), $remoteDisk);
        } elsif (grep /Unavailable/ig, $dskCheck1) {
            print "smart support for $remoteDisk in $remoteHost is unavailable.\n";
            exit 3;
        } else {
            ##try to enable
            unless ($retry==2) {
                #print "smart support for $remoteDisk in $remoteHost is currently disabled. trying to enable...\n";    
                $retry++;
                `ssh -o StrictHostKeyChecking=no nagios\@$remoteHost $smartctl -s on $remoteDisk`;
                goto CHECK;
            } else {
                my $err="Retry $retry: Failed enabling smart support for $remoteDisk in $remoteHost. [".scalar(localtime)."]\n
";
                logThis($err);
                print $err;
                exit 3;
            }
        }

## subs
sub preCheck {
    #check if privileged
=comment
    my $me=`whoami`;
    unless ($me eq "root\n") {
        print "You aint root!\n";
        exit 3;
    }
=cut
    #check for arguments passed
    unless (@ARGV==2) {
            print "Usage: $0 remoteHost /path/to/dev.\n";
            exit 3;
    }
    #look for smartctl in the usual places
    my $remoteHost=$ARGV[0];
    my $searchRes=`ssh -o StrictHostKeyChecking=no nagios\@$remoteHost which smartctl`;
    my @smartctls=split(/ /, $searchRes);
    foreach my $smartctl (@smartctls) {
        if ($smartctl=~/bin.+smartctl/g) {
        chomp $smartctl;
            #print "found $smartctl\n";
            return $smartctl;
        }
    }
    print "Unable to find smartctl. It\'s in the smartmontools package.\n";
    exit 3;
}

sub dskCheck2 {
    my $disk=$_[0];
    my $dskCheck2=`ssh -o StrictHostKeyChecking=no nagios\@$remoteHost $smartctl -t short $disk`;
    sleep 120;
    my $dskCheck2Res=`ssh -o StrictHostKeyChecking=no nagios\@$remoteHost $smartctl -l error $disk`;
    return $dskCheck2Res;
}

I really appreciate your reply guys on this forum. :b:

Install sudo and allow the user nagios to run smartctl (with the complete path) without being asked for a password.

Thanks pludi but the existing script uses a variable for smartctl command.
Can you help me modify the existing script using sudo ?

Add this line to the sudoers file (edit with visudo) on each host where smartctl is called:

nagios ALL = (root) NOPASSWD: smartctl

And in your script, prepend "sudo" to every call to smartctl.