====== ソフトウェアRaid監視(Windows) ====== Windowsでの SoftRaid の自動監視の方法 - diskpart コマンドで、RAID の状況を標準出力に出力 - この出力から RAID の異常を発見するためボリューム毎の「正常」の文字を数える - 数えた結果が0だったらメールを送信する。 - これらの結果をバッチファイルにし、at コマンドでスケジューリングして定期的に自動実行する。 バッチファイルの中味はこんな感じ。 diskpart /s c:\diskpart_cmd.txt | findstr /r /c:"Volume .*C"| findstr "正常" | cscript //Nologo c:\found_mail.vbs > .\log.txt 途中で改行が入ってしまっていますが、これを1行で書きます。\\ 事前に、これらのコマンドやテキストファイルが置かれているディレクトリに移動しておきます。 ===== diskpart コマンドで、RAID の状況を標準出力に出力 ===== 「 diskpart /s c:\diskpart_cmd.txt 」 部分 diskpart は、実行時に /s で指定するとその内容を実行してすぐに終了します。\\ diskpart_cmd.txt には list volume と1行だけ書かれています。 ===== ボリューム毎の「正常」の文字を数える ===== 「 findstr /r /c:"Volume .*C"| findstr "正常" 」 部分 ここで、 findstr の /r は、正規表現の指定 /c: は探す文字列の指定\\ 空白のある文字を指定する場合は、このオプションが必要です。\\ "Volume . C" 部分が監視したいディスクの diskpart の出力と一致するように正規表現で指定します。\\ これらによって、監視したいボリュームの正常の文字をが書かれている行を抽出します。 「 cscript //Nologo c:\found_mail.vbs > .\log.txt 」部分 cscript は、WSH コマンドをバッチファイルで扱うための処理です。 ===== メールを送信 ===== found_mail.vbs はこんな感じ const host = "hostname.hoge.com" const mailto = "support@hoge.com" mailfrom = "postmaster@" & host '-------------------------------------- Dim in_no Dim WSHShell ' 入力行を数える in_no=0 Do Until WScript.StdIn.AtEndOfLine strInput = WScript.StdIn.ReadLine WScript.Echo strInput in_no=in_no+1 LOOP 'WScript.Echo in_no IF in_no > 0 then ' "正常" と書かれている行があった WScript.Echo "Good Soft RAID" Else ' "正常" と書かれている行がなかった WScript.Echo "Illegal Soft RAID" 'メールの送信 ' 変数、定数の定義 Const cdoSendUsingMethod = _ "http://schemas.microsoft.com/cdo/configuration/sendusing" Const cdoSendUsingPort = 2 Const cdoSMTPServer = _ "http://schemas.microsoft.com/cdo/configuration/smtpserver" Const cdoSMTPServerPort = _ "http://schemas.microsoft.com/cdo/configuration/smtpserverport" Const cdoSMTPConnectionTimeout = _ "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout" Const cdoSMTPAuthenticate = _ "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" Const cdoBasic = 1 Const cdoSendUserName = _ "http://schemas.microsoft.com/cdo/configuration/sendusername" Const cdoSendPassword = _ "http://schemas.microsoft.com/cdo/configuration/sendpassword" 'boosterには送信に使う SMTP サーバの IP アドレスか FQDN を指定する Const booster = "mail.hogehoge.com" Dim objConfig, Fields, objMessage 'CDOメッセージの作成 Set objConfig = CreateObject("CDO.Configuration") Set Fields = objConfig.Fields With Fields .Item(cdoSendUsingMethod) = cdoSendUsingPort .Item(cdoSMTPServer) = booster .Item(cdoSMTPServerPort) = 25 .Item(cdoSMTPConnectionTimeout) = 10 '.Item(cdoSMTPAuthenticate) = cdoBasic .Item(cdoSMTPAuthenticate) = cdoAnonymous .Item(cdoSendUserName) = "" .Item(cdoSendPassword) = "" .Update End With Set objMessage = CreateObject("CDO.Message") Set objMessage.Configuration = objConfig With objMessage .To = mailto .From = mailfrom .Subject ="[ Error] Soft raid の異常: " & host .TextBody ="Soft RAIDに異常を発見しました。" .Send End With End If '-------------------------------------- 最初の所で、標準入力からの行数を数えます。\\ 「 findstr /r /c:"Volume . C"| findstr "正常" 」部分で抽出した部分の行数が1行以上有れば、正常だから特には何もしません。\\ 1行も無ければ、これは異常ですので、メールを送信します。\\ 「'メールの送信」と書かれている部分で、メールを送信します。 {{tag>raid}}