Help with perl scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with perl scripting
# 1  
Old 01-27-2010
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.

Code:
#!/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. Smilie
# 2  
Old 01-27-2010
Install sudo and allow the user nagios to run smartctl (with the complete path) without being asked for a password.
# 3  
Old 01-27-2010
Thanks pludi but the existing script uses a variable for smartctl command.
Can you help me modify the existing script using sudo ?
# 4  
Old 01-27-2010
Add this line to the sudoers file (edit with visudo) on each host where smartctl is called:
Code:
nagios ALL = (root) NOPASSWD: smartctl

And in your script, prepend "sudo" to every call to smartctl.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Scripting

I have create this shell script #!/bin/sh if ; then echo "Usage: ./script <filename>" exit 1 elif ; then fname="$1" fi output="output.txt" i=0 ... (11 Replies)
Discussion started by: Evelin90
11 Replies

2. Shell Programming and Scripting

need help in PERL Scripting

I am having file xyz.log Its content is like this int main() { d; #ifdef e; f; #else g; #ifdef h. #else i; (2 Replies)
Discussion started by: naaj_ila
2 Replies

3. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

4. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

5. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

6. UNIX for Dummies Questions & Answers

Need help configuring Active Perl on Windows Vista.: Perl Scripting on Windows

Hi All, Need help configuring Active Perl on Windows Vista. I am trying to install Active Perl on Windows Vista. The version of Active Perl i am trying to install is : ActivePerl 5.10.1 Build 1006 After installing it through cmd, When i try to run perl -v to check the version, i get the... (2 Replies)
Discussion started by: Vabiosis
2 Replies

7. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

8. Shell Programming and Scripting

Need help in perl scripting.

Hi, To run a perl script i am giving command like this in DOS prompt d:> perl D:\<dir_name>\<dir_name>\sample.pl Its throwing the following error while running the above syntax error at <eval 4> line 1, near "use D:" Can anyone help? (3 Replies)
Discussion started by: mvictorvijayan
3 Replies

9. Shell Programming and Scripting

perl scripting

Hi does anyone know how to ouput "I love scripting" to "scripting love I" without using reverse() function in perl? Thanks (2 Replies)
Discussion started by: ccp
2 Replies

10. UNIX for Advanced & Expert Users

Perl scripting

hi Can any one suggest me book for perl scripting on UNIX Platform. Regards (2 Replies)
Discussion started by: rochitsharma
2 Replies
Login or Register to Ask a Question