{"id":706,"date":"2012-09-10T07:01:39","date_gmt":"2012-09-10T07:01:39","guid":{"rendered":"http:\/\/www.omniweb.com\/wordpress\/?p=706"},"modified":"2012-09-10T07:15:01","modified_gmt":"2012-09-10T07:15:01","slug":"automated-megaraid-monitoring-on-esxi-5","status":"publish","type":"post","link":"https:\/\/www.omniweb.com\/wordpress\/?p=706","title":{"rendered":"Automated MegaRaid monitoring on ESXi 5"},"content":{"rendered":"<p>I have a couple of MegaRaid controllers (Intel RS2BL040 &amp; LSI 9260-4i)<br \/>\nI&#8217;m not thrilled with the monitoring software so I wrote this script to email me in the event of a raid problem:<br \/>\nThere are a couple other pieces but this is the meat of it;<br \/>\nRuns from crontab every 5 minutes, named &#8220;process.pl&#8221;<\/p>\n<p><code>#!\/usr\/bin\/perl<\/p>\n<p>use strict;<br \/>\nuse warnings;<\/p>\n<p>use MIME::Lite::TT;<\/p>\n<p># Process the current log file in <\/p>\n<p>my $logs_dir = '\/home\/megaraid\/logs\/';<\/p>\n<p>my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =<br \/>\n                                                localtime(time);<\/p>\n<p>$year += 1900;<br \/>\n$mon = sprintf('%.2d',$mon+1);<br \/>\n$mday = sprintf('%.2d',$mday);<br \/>\n$hour = sprintf('%.2d',$hour);<\/p>\n<p>while ( $min % 5 ) {<br \/>\n  $min--;<br \/>\n}<br \/>\n$min = sprintf('%.2d',$min);<\/p>\n<p>#print \"year: $year Month: $mon Day $mday hour $hour : $min \\n\";<\/p>\n<p>my $filename = $year.$mon.$mday.$hour.$min.'.txt';<\/p>\n<p>print \"Getting filename $filename \\n\";<\/p>\n<p>my $text_file = '\/home\/megaraid\/logs\/'.$filename;<\/p>\n<p>my @cmd = ( 'scp root@192.168.0.175:\/tmp\/megaraid\/'.$filename.' \/home\/megaraid\/logs\/' );<\/p>\n<p>system( @cmd );<\/p>\n<p>unless ( -f '\/home\/megaraid\/logs\/'.$filename ) { die ( $filename . \" could not be retrieved, exiting now.\\n\\n\" ); }<\/p>\n<p>print \"Successfully retrieved, now processing $text_file \\n...\\n\";<\/p>\n<p>my $capture;<br \/>\nmy $counter = 0;<br \/>\nmy %alerts;<br \/>\nmy $line;<br \/>\nmy %params;<br \/>\nmy %options;<br \/>\n$options{INCLUDE_PATH} = '\/home\/megaraid';<br \/>\n$options{anything} = 'nothing';<\/p>\n<p>open ( TXT, $text_file ) || die \"Couldn't open Text file $text_file!\\n\";<\/p>\n<p>while ($line = <TXT>) {<\/p>\n<p>  if ( $line =~ m\/Device Present\/gi ) {  #We want the next 5 lines, start capture<\/p>\n<p>    $counter++;<\/p>\n<p>  }<\/p>\n<p>  if ( $counter > 0 && $counter < 11 ) {\n\n    $capture .= $line;\n    $counter++;\n\n    if ( $line =~ m\/Degraded\\s*:\\s(\\d+)\\s\/ &#038;&#038; $1 > 0 ) {<br \/>\n      print \"Degraded: $1 \\n\";<br \/>\n      $alerts{'degraded'} = $1;<br \/>\n    }<\/p>\n<p>    if ( $line =~ m\/Offline\\s*:\\s(\\d+)\\s\/ && $1 > 0 ) {<br \/>\n      print \"Offline: $1 \\n\";<br \/>\n      $alerts{'offline'} = $1;<br \/>\n    }<\/p>\n<p>    if ( $line =~ m\/Critical Disks\\s*:\\s(\\d+)\\s\/ && $1 > 0 ) {<br \/>\n      print \"Critical: $1 \\n\";<br \/>\n      $alerts{'critical'} = $1;<br \/>\n    }<\/p>\n<p>    if ( $line =~ m\/Failed Disks\\s*:\\s(\\d+)\\s\/ && $1 > 0 ) {<br \/>\n      print \"Failed: $1 \\n\";<br \/>\n      $alerts{'failed'} = $1;<br \/>\n    }<\/p>\n<p>  } # End of 10 rows we need to look at (physical drive status)<\/p>\n<p>}<\/p>\n<p>close( TXT );<\/p>\n<p>print $capture . \"\\n\";<\/p>\n<p>my $alert_file = '\/home\/megaraid\/status.txt';<br \/>\nmy $alert_text = \"\";<\/p>\n<p>if ( (scalar keys %alerts) > 0 ){<\/p>\n<p>  print \"ALERTS! \\n  Check alert file and possibly send email if new alert status.\\n\";<br \/>\n  foreach my $alert ( keys %alerts ) {<\/p>\n<p>    my $val = $alerts{ $alert };<br \/>\n    print \"$alert => $val\\n\";<\/p>\n<p>    $alert_text = \"$alert $val\\n\";<br \/>\n    open ( ALERT, \"$alert_file\" );<\/p>\n<p>    $counter = 0;<\/p>\n<p>    while ($line = <ALERT>) {<br \/>\n      if ( $line eq $alert_text ) {<br \/>\n        print \"found match $line $alert_text \\n\";<br \/>\n        $counter++;<br \/>\n      }<br \/>\n    }<\/p>\n<p>    close ( ALERT );<\/p>\n<p>    if ( $counter == 0 ) {<\/p>\n<p>      print \"THIS IS A NEW ALERT, send email $alert_text \\n\";<br \/>\n      open ( ALERT, \">>$alert_file\" );<br \/>\n      print ALERT $alert_text;<br \/>\n      close ( ALERT );<\/p>\n<p>      $params{'first_name'} = \"Bob\";<br \/>\n      $params{'alert_text'} = $alert_text;<\/p>\n<p>      my $msg = MIME::Lite::TT->new(<br \/>\n            From        =>  'ESXI@omniweb.com',<br \/>\n            To          =>  'ESXI@omniweb.com,my_cell_number',<br \/>\n            Subject     =>  'RAID Alert',<br \/>\n            Template    =>  'email.txt.tt',<br \/>\n            TmplOptions =>  \\%options,<br \/>\n            TmplParams  =>  \\%params,<br \/>\n      );<\/p>\n<p>      $msg->send;<\/p>\n<p>    }<\/p>\n<p>  }<\/p>\n<p>} else {<\/p>\n<p>  # check if there were previous alerts, notify when cleared<br \/>\n  open ( ALERT, \"$alert_file\" );<\/p>\n<p>  $counter = 0;<br \/>\n  while ($line = <ALERT>) {<br \/>\n    if ( $line ne \"Good\" ) {<br \/>\n      print \"Status was $line now Good\\n\";<br \/>\n      $alert_text .= \"Status was $line now Good\\n\";<br \/>\n      $counter++;<br \/>\n    }<br \/>\n  }<br \/>\n  close ( ALERT );<br \/>\n  if ( $counter > 0 ){<br \/>\n    open ( ALERT, \">$alert_file\" );<br \/>\n    print ALERT \"Good\";<br \/>\n    close ( ALERT );<br \/>\n    print \"Email $alert_text \\n\";<\/p>\n<p>    $params{alert_text}    = $alert_text;<br \/>\n    my $msg = MIME::Lite::TT->new(<br \/>\n          From        =>  'ESXI@omniweb.com',<br \/>\n          To          =>  'ESXI@omniweb.com,my_cell_number',<br \/>\n          Subject     =>  'RAID Good',<br \/>\n          Template    =>  'email.txt.tt',<br \/>\n          TmplOptions =>  \\%options,<br \/>\n          TmplParams  =>  \\%params,<br \/>\n    ); <\/p>\n<p>    $msg->send;<\/p>\n<p>  }<br \/>\n}<\/p>\n<p>exit();<\/code><\/p>\n<p>Leave a comment if you want details on how to get the rest of it working.  Or if there is a better way to do it, please let me know!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a couple of MegaRaid controllers (Intel RS2BL040 &amp; LSI 9260-4i) I&#8217;m not thrilled with the monitoring software so I wrote this script to email me in the event of a raid problem: There are a couple other pieces &hellip; <a href=\"https:\/\/www.omniweb.com\/wordpress\/?p=706\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/706"}],"collection":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=706"}],"version-history":[{"count":9,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/706\/revisions"}],"predecessor-version":[{"id":718,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/706\/revisions\/718"}],"wp:attachment":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}