http://www.perlmonks.org?node_id=1016320

Hi,

Given below is a perl script that logs into EMC NAS Celerra Arrays and runs a command to check the DM status and emails the output.

All thanks to the suggestions put forth by topher, keszler, Athanasius and two anonymous monks who helped me here (Seeking guidance for more idiomatic way of (re)writing this script.) that I was able to write a much better script and in the process learnt some new stuff. Thank you for all your suggestions. I have tried to incorporate them in here. If anyone else has further suggestions to make this better, please let me know.

DWIM Perl Version 5.14.2 was used for this. Would also like to thank the DWIM Perl Creators. This has a lot of modules pre installed.

Here's how the script works. There are 3 files required.Its better to have the files in the same directory. You'll need to put the script in Windows Scheduler so that it runs at regular intervals as you define.

1) The file with the IPs of the NAS Arrays (given below).

############################################################### # #Lines beginning with "#" will be ignored by the script. # #This file contains the NAS IPs. # #Please use tab or space to seperate the name and IP of #the NAS Array +s. # ########################################################### NASBOX1 127.0.0.1 NASBOX2 127.0.0.2 NASBOX3 127.0.0.3 NASBOX4 127.0.0.4

2) The file that the Config::Tiny module will read from. Its named as "nasconfig" (Given below). This file will have the various parameters like the name of the smtp mail server, the location of the nas ip text file, e-mail addresses etc etc. You'll need to populate the fields accordingly except the "nas_command" parameter.

[params] smtp_server_name = mail.server.com nas_array_list ="location/of/nas_array_ip_list.txt" mail_from = your.name@email.com mail_to = some.name@email.com username = usrname password = passwd nas_command =/nas/sbin/getreason mail_text = "location/of/dmcheck.txt"

3) The script file itself. And given below is the script

#!/usr/bin/perl use warnings; use strict; use Net::SSH2; use MIME::Lite; use Config::Tiny; my $nasconfig = Config::Tiny->new(); $nasconfig = Config::Tiny->read ('nasconfig'); my $mailserver = $nasconfig->{params}->{smtp_server_name}; my $nasarraylist = $nasconfig->{params}->{nas_array_list}; my $mailfrom = $nasconfig->{params}->{mail_from}; my $mailto = $nasconfig->{params}->{mail_to}; my $username = $nasconfig->{params}->{username}; my $password = $nasconfig->{params}->{password}; my $nascmd = $nasconfig->{params}->{nas_command}; my $mailtext = $nasconfig->{params}->{mail_text}; my $msg; my $type = ""; my $ipfilefh;#File handle for NAS IP File. my $mailfh; #File handle for E-mail Alert. my $nowtime = localtime; MIME::Lite->send ("smtp", "$mailserver"); sub email_alert() { open $mailfh, ">>", "$mailtext" || die "Cannot open $mailtext in s +ub email_alert. $!. $^E"; $type = shift @_; if ( $type eq "ip_list_error" ) { $msg = MIME::Lite->new( From => $mailfrom, To => $mailto, Data => "Error:nas_array_ip_list.txt:$!.$^E\n", Subject => "IP List File Not Found For $0 Script on $ENV{COMPUTERNAME}\n", ); $msg->send; } elsif ( $type eq "all_ok" ) { print $mailfh "DM is OK:$_\n"; } elsif ( $type eq "fault" ) { print $mailfh "Possible DM Failure: $_:Possible DM Failure\n"; } elsif ( $type eq "unsure" ) { print $mailfh "Unknown Status: $_\n"; } else { die "email_alert - bad type: $type\n"; } } if (!open $ipfilefh , "<" , "$nasarraylist") { &email_alert("ip_list_error"); die "Cannot open $nasarraylist:$!.$^E"; exit (0); } open $mailfh, ">", "$mailtext" || die "Cannot open $mailtext. $!. $^E" +; print $mailfh '##################################################################### +#############################'; print $mailfh "\nScript $0 on $ENV{COMPUTERNAME} auto run at $nowtime +[$ENV{COMPUTERNAME} Time]\n"; print $mailfh '##################################################################### +###############################'; while (<$ipfilefh>) { next if /^#/; my ($ipname, $ipaddr) = split /\s+/; my $ssh2 = Net::SSH2->new(); print $mailfh "\n--------------------------------------------- +----------------"; print $mailfh "\nData Mover Check For $ipname ($ipaddr)\n"; print $mailfh "----------------------------------------------- +--------------\n"; $ssh2->connect("$ipaddr") || die "PROBELM - $!"; $ssh2->auth_password("$username","$password") || die "Username +/Password not right"; my $chan = $ssh2->channel(); $chan->blocking(0); $chan->exec('/nas/sbin/getreason'); sleep 10; while (<$chan>) { chomp; next if (/10 - slot_0 primary control station/); if ($_=~ /contacted$/ig) { &email_alert("all_ok"); } elsif ($_=~ /fault/ig) { &email_alert("fault"); } else { &email_alert("unsure"); } } $chan->close(); } close $mailfh; my $nasmsg = MIME::Lite->new( From => $mailfrom, To => $mailto, Subject => "Automated Check For NAS DataMover Health.", Type => 'Multipart/mixed', ); $nasmsg->attach( Type => 'TEXT', Path => "$mailtext", Filename => "$mailtext", Disposition => 'inline', ); $nasmsg->send; unlink $mailtext || die "Cannot delete $mailtext.$!.$^E";

And here is the output (Copy pasted from the mail)

###################################################################### +##################################################################### +############### Script nasmon.pl on mgmthost auto run at Thu Jan 31 05:53:19 2013 [mgm +thost Time] ###################################################################### +##################################################################### +############### ------------------------------------------------------------- Data Mover Check For NASBOX1 (127.0.0.1) ------------------------------------------------------------- DM is OK: 5 - slot_2 contacted DM is OK: 5 - slot_3 contacted DM is OK: 5 - slot_4 contacted DM is OK: 5 - slot_5 contacted ------------------------------------------------------------- Data Mover Check For NASBOX2 (127.0.0.2) ------------------------------------------------------------- DM is OK: 5 - slot_2 contacted DM is OK: 5 - slot_3 contacted DM is OK: 5 - slot_4 contacted DM is OK: 5 - slot_5 contacted ------------------------------------------------------------- Data Mover Check For NASBOX3 (127.0.0.3) ------------------------------------------------------------- DM is OK: 5 - slot_2 contacted DM is OK: 5 - slot_3 contacted DM is OK: 5 - slot_4 contacted DM is OK: 5 - slot_5 contacted ------------------------------------------------------------- Data Mover Check For NASBOX4 (127.0.0.4) ------------------------------------------------------------- DM is OK: 5 - slot_2 contacted DM is OK: 5 - slot_3 contacted DM is OK: 5 - slot_4 contacted DM is OK: 5 - slot_5 contacted

I haven't got access to a test NAS Array to test how the message looks if there is a fault on the data mover, but I should be getting access to a test box pretty soon and will give the updates here.

Perlpetually Indebted To PerlMonks

use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";