HDDmonitor.au3

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Journey Into Imagination.ico
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Fileversion=1.0.0.35
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=Wolfgang Führer
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;----------------------------------------------
; HDDmonitor.au3
; durchsucht Eventlog und meldet Disk Errors
; Wolfgang Führer
; 10.05.2007  -  08:21:35,93
;----------------------------------------------
 
Opt("MustDeclareVars", 0)       ;0=no, 1=require pre-declare
Opt("TrayIconDebug", 0)         ;0=no info, 1=debug line info
Opt("TrayIconHide", 1)          ;0=show, 1=hide tray icon
 
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$Where_Clause = " WHERE Logfile = 'System' AND SourceName = 'disk' and (Type = 'Warnung' OR Type = 'Fehler' OR Type = 'Error' OR Type = 'Warning')"
Global $filename = ""
Global $progname = "HDD-Monitor"
Global $regpath = "HKEY_CURRENT_USER\Software\HDDmonitor"
Global $logfile = @ScriptDir & "\HDDmonitor.log"
Global $events = ""
Global $eventcountererror = 0
Global $eventcounterwarning = 0
Global $eventtyp = ""
Global $answer = ""
Global $answerflag = 0
Global $loop = 1
Global $iniWarning = 0
Global $iniSharewrite = 0
Global $iniShare = "\\Server\Folder"
global $shareName = ""
 
If Not FileExists(@ScriptDir & "\HDDmonitor.ini") Then
    IniWrite(@ScriptDir & "\HDDmonitor.ini", "Show", "Warnings", "0")
    IniWrite(@ScriptDir & "\HDDmonitor.ini", "Share", "Path", "\\Server\Folder")
    IniWrite(@ScriptDir & "\HDDmonitor.ini", "Share", "Write", "0")
Else
    $iniWarning = IniRead(@ScriptDir & "\HDDmonitor.ini", "Show", "Warnings", "0")
    $iniShare = IniRead(@ScriptDir & "\HDDmonitor.ini", "Share", "Path", "\\Server\Folder")
    $iniSharewrite = IniRead(@ScriptDir & "\HDDmonitor.ini", "Share","Write", "0")
EndIf
 
$shareName = StringMid($inishare,3,StringInStr($inishare,"\",1,3)-3)
 
$Output = ""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_NTLogEvent" & $Where_Clause, "WQL", _
        $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
 
Sleep(180000) ;wait 3 minutes after reboot or login
 
$lastshown = RegRead($regpath, "LastShownEvent")
 
If IsObj($colItems) Then
    For $objItem In $colItems
        $loop = $loop + 1
        $Output = $Output & "Zeit: " & @TAB & @TAB & _WMIDateStringToDate($objItem.TimeGenerated) & @CRLF
        $eventdate = _WMIDateStringToSerialdate($objItem.TimeGenerated)
        $eventtyp = $objItem.Type
        $Output = $Output & "Type: " & @TAB & @TAB & $eventtyp & @CRLF
        If ($eventtyp == "Fehler") Or ($eventtyp == "Error") Then
            $eventcountererror = $eventcountererror + 1
        Else
            $eventcounterwarning = $eventcounterwarning + 1
        EndIf
        $Output = $Output & "Message: " & @CRLF & $objItem.Message & @CRLF
        $strInsertionStrings = $objItem.InsertionStrings (0)
        $Output = $Output & "Zeichenkette: " & @TAB & $strInsertionStrings & @CRLF
        $Output = $Output & "EventCode: " & @TAB & $objItem.EventCode & @CRLF
        $Output = $Output & "RecordNumber: " & @TAB & $objItem.RecordNumber & @CRLF
        ;$Output = $Output & "EventIdentifier: " & @TAB & $objItem.EventIdentifier & @CRLF
        ;$Output = $Output & "Category: " & $objItem.Category & @CRLF
        ;$Output = $Output & "CategoryString: " & $objItem.CategoryString & @CRLF
        ;$Output = $Output & "ComputerName: " & $objItem.ComputerName & @CRLF
        ;$strData = $objItem.Data(0)
        ;$Output = $Output & "Data: " & $strData & @CRLF
        ;$Output = $Output & "EventIdentifier: " & $objItem.EventIdentifier & @CRLF
        ;$Output = $Output & "EventType: " & $objItem.EventType & @CRLF
        ;$Output = $Output & "Logfile: " & $objItem.Logfile & @CRLF
        ;$Output = $Output & "RecordNumber: " & $objItem.RecordNumber & @CRLF
        ;$Output = $Output & "SourceName: " & $objItem.SourceName & @CRLF
        ;$Output = $Output & "TimeWritten: " & WMIDateStringToDate($objItem.TimeWritten) & @CRLF
        ;$Output = $Output & "User: " & $objItem.User & @CRLF
        If ($eventtyp == "Fehler") Or ($eventtyp == "Error") Then
            $icon = 48 + 4096 + 262144 + 1
        Else
            $icon = 64 + 4096 + 262144 + 1
        EndIf
 
        If $eventtyp == "Fehler" Or $eventtyp == "Error" Or ($iniWarning == 1 And ($eventtyp == "Warnung" Or $eventtyp == "Warning")) Then
            If $lastshown < $eventdate Then ;if not shown before
                If $answerflag == 0 Then
                    $answer = MsgBox($icon, $progname, $Output)
                    If RegRead($regpath, "LastShownEvent") < $eventdate Then
                        _WriteToReg("LastShownEvent", $eventdate)
                    EndIf
                EndIf
                If $answer == 2 Then $answerflag = 1 ;no more messages
            EndIf
        EndIf
        $events = $events & @CRLF & $loop / 2 & ". ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CRLF & $Output
        $Output = ""
        $loop = $loop + 1
    Next
Else
    MsgBox(0, $progname, "Keine Meldungen gefunden.", 2)
EndIf
 
$events = "Errors:   " & $eventcountererror & @CRLF & _
        "Warnings: " & $eventcounterwarning & @CRLF & @CRLF & $events
 
_WriteToLog($events)
_WriteToReg("ErrorEvents", $eventcountererror)
_WriteToReg("WarningEvents", $eventcounterwarning)
 
If $eventcountererror > 10 Then
    MsgBox(48 + 4096 + 262144, $progname, "Achtung!" & @CRLF & @CRLF & "Ihre Festplatte hat einen Defekt." & _
            @CRLF & "Es wurden bereits " & $eventcountererror & " kritische Fehler vom Betriebssystem gemeldet.   " & _
            @CRLF & @CRLF & "Bitte wenden Sie sich an das Helpdesk." & @CRLF)
EndIf
 
If $eventcountererror > 0 And $iniSharewrite == "1" Then
    _WriteLog($iniShare, $events)
EndIf
 
Exit(0)
 
;=========================================================================================
;Functions
;=========================================================================================
 
Func _WMIDateStringToDate($dtmDate)
    Return (StringMid($dtmDate, 7, 2) & "." & _
            StringMid($dtmDate, 5, 2) & "." & StringLeft($dtmDate, 4) _
             & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate, 13, 2))
EndFunc   ;==>_WMIDateStringToDate
 
Func _WMIDateStringToSerialdate($dtmDate)
    Return StringLeft($dtmDate, 4) & StringMid($dtmDate, 5, 2) & StringMid($dtmDate, 7, 2) & _
            StringMid($dtmDate, 9, 2) & StringMid($dtmDate, 11, 2) & StringMid($dtmDate, 13, 2)
EndFunc   ;==>_WMIDateStringToSerialdate
 
Func _WriteLog($path, $string)
    $ping = Ping($shareName)
    If $ping > 0 Then
        $handle = FileOpen($path & "\HDDmonitor_" & @ComputerName & ".log", 2)
        If $handle <> -1 Then
            FileWrite($handle, $string)
            FileClose($handle)
        EndIf
    EndIf
EndFunc   ;==>_WriteLog
 
Func _WriteToReg($value, $string)
    $errorval = RegWrite($regpath, $value, "REG_SZ", $string)
    If $errorval = 0 Then
        MsgBox(0, $progname, "Wert '" & $value & "' konnte nicht in die Registry geschrieben werden.")
        Exit
    EndIf
 
EndFunc   ;==>_WriteToReg
 
Func _WriteToLog($string)
    Local $handle
    Local $counter
    Local $exitstring = ""
    Do
        $handle = FileOpen($logfile, 2) ;try to open logfile
        If $handle = -1 Then Sleep(250) ;wait 0,2 second if failed
        $counter = $counter + 1;
        If $counter > 10 Then
            MsgBox(48 + 4096, $progname, "Schreibfehler!" & @CRLF & @CRLF & "Logfile konnte nicht geschrieben werden")
            Exit;if failed over 10 times then exit program
        EndIf
    Until $handle <> -1;retry open logfile
    FileWrite($handle, @CRLF & $string) ;write to log
    FileClose($handle)
EndFunc   ;==>_WriteToLog