Jump to content

CRL checking script - I need a better way to do this


jussie
 Share

Recommended Posts

I've created this script to be able to download certificate revocation lists and check when they expire for multiple CRL's for work. The tool I have created seems to work but I'm worried It will have a memory leak or keep using more and more handles until the machine it is running on locks up. The tool uses a csv file that has the name of the CRL, the download location, a reference name for it and I'm now working on a notification bit that will email either the person responsible of an on-call email if the CRL expires. One of the problems I have is that I would normally create the labels and then update them within the while statement but I can't seem to be able to figure out how to referenece back to the array at the location I want the data to be updated. I tried creating the labels and progress bars but how do I reference each variable??

Here is the code minus a few bits:

#include <Constants.au3>
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <INet.au3>

Dim $aRecords, $Results, $Effective_date, $Expiry_Details, $Expiry_DetailsData, $CertUtilLogs, $FILE
If Not _FileReadToArray("CRL_Check.csv", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array   error:" & @error)
    Exit
EndIf

$mainWindow = GUICreate("CRL Endpoint Check v3.0 (UPDATES EVERY 10 MINUTES)", 700, (($aRecords[0] * 20) + 100))
Opt("TrayIconHide", 1)
Opt("GUIOnEventMode", 0)

; Email Settings
$s_SmtpServer = "*** REMOVED ***"
$s_helo = "EHLO CRL_Endpoint_Check_v3.0"
$s_first = -1
$s_FromName = "CRL Endpoint Check v3.0"
$s_FromAddress = "*** REMOVED ***"

Dim $as_Body[2]

GUISetState()

GUICtrlCreateGroup("Environment ---> EndPoint", 20, 10, 250, (($aRecords[0] * 20) + 40))
GUICtrlCreateGroup("Expiry Times Percentage", 280, 10, 280, (($aRecords[0] * 20) + 40))
GUICtrlCreateGroup("Mins before Expiry", 570, 10, 110, (($aRecords[0] * 20) + 40))
$LastUpdate = GUICtrlCreateLabel("Last Updated...", 30, (($aRecords[0] * 20) + 55), 200, 17)
GUICtrlCreateLabel("Service Started: " & _NowCalc(), 30, (($aRecords[0] * 20) + 75), 200, 17)
$RefreshProgress = GUICtrlCreateProgress(290, (($aRecords[0] * 20) + 70), 250, 17)

$RefreshButton = GUICtrlCreateButton("Manual Refresh", 575, (($aRecords[0] * 20) + 65), 100, 25)
GUICtrlSetData($RefreshButton, "Updating")
GUICtrlSetState($RefreshButton, $GUI_DISABLE)

$line = 40
While 1
    For $x = 1 to $aRecords[0]
        $Endpoints = StringSplit($aRecords[$x], ",")
        $s_ToAddress = $Endpoints[4]
        If Not FileExists($Endpoints[1]) Then
            DirCreate($Endpoints[1])
        EndIf
        
        $EndPointsLabel = GUICtrlCreateLabel($Endpoints[1] & " ---> " & $Endpoints[2], 30, $line, 220, 17)
        $MinsLeft = GUICtrlCreateLabel("** Updating **", 580, $line, 95, 17)
        $ProgressCreate = GUICtrlCreateProgress(290, $line, 250, 17)
        
        #Alternate
        Local $hDownload = InetGet($Endpoints[3], $Endpoints[1] & "\" & $Endpoints[2] & ".crl", 3, 1)
        Do
            Sleep(500)
        Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
        Local $aData = InetGetInfo($hDownload) ; Get all information.
        InetClose($hDownload) ; Close the handle to release resourcs.
        If $aData[4] <> 0 then
            GUICtrlSetData($MinsLeft, "Download ERR")
            GUICtrlSetColor($MinsLeft, 0xff0000) ; Red
            GUICtrlSetData($ProgressCreate, "0")
            Sleep(500)
            $line = $line + 20
        Else
            Sleep(500)
            $line = $line + 20
            
            ;******************************************************************************
            ;Interrogating the CRL with Certutil.exe and dumping to file
            $CertUtilLogs = FileOpen($Endpoints[1] & "\" & $Endpoints[2] & ".txt", 2)
            If $CertUtilLogs = -1 Then
                MsgBox(0, "Error", "Unable to create CRL Dump file. May be a file permission problem.")
                Exit
            EndIf
            
            local $Run = Run(@ComSpec & " /c " & "certutil.exe " & $Endpoints[1] & "\" & $Endpoints[2] & ".crl", "", @SW_HIDE, $STDOUT_CHILD)
            local $Certutilline
            Sleep(50)
            While 1
                $Certutilline = StdoutRead($Run)
                If @error Then ExitLoop
                Sleep(200)
                FileWriteLine($CertUtilLogs, $Certutilline)
            Wend
            FileClose($CertUtilLogs)
            Sleep(50)

            ;******************************************************************************
            ;******************************************************************************
            ;Opening dumped file and processing to look for effective and expity date to calculate CRL duration
            $FILE = FileOpen($Endpoints[1] & "\" & $Endpoints[2] & ".txt", 0)
            If $FILE = -1 Then
                MsgBox(0, "Error", "Unable to open file so service probably isn't responding to commands.")
                Exit
            EndIf
            
            While 1
                $READLINE = FileReadLine($FILE)
                If @error = -1 Then ExitLoop
                
                
                $Effective_date = StringRegExp($READLINE, ' ThisUpdate: (.*)', 2)
                For $I = 0 To UBound($Effective_date) - 1
                    $Effective_dateData = $Effective_date[1]
                Next
                
                $Expiry_Details = StringRegExp($READLINE, ' NextUpdate: (.*)', 2)
                For $I = 0 To UBound($Expiry_Details) - 1
                    $Expiry_DetailsData = $Expiry_Details[1]
                Next
                
            WEnd
            FileClose($FILE)

            ;******************************************************************************
            ;******************************************************************************

            ;Reformatting expiry details to suite date/time convention
            $date = StringSplit($Expiry_DetailsData, " ")

            ; Format the time
            If $date[3] = "PM" Then
                $hour = StringSplit($date[2], ":")
                If $hour[1] = 12 Then
                    $hoursplit = 12
                Else
                    $hoursplit = $hour[1] + 12
                    If $hoursplit = 24 Then
                        $hoursplit = 0
                    Else
                        $hoursplit = $hour[1] + 12
                    EndIf
                EndIf
            EndIf
            
            If $date[3] = "AM" Then
                $hour = StringSplit($date[2], ":")
                $hoursplit = $hour[1]
                If $hoursplit = 12 Then
                    $hoursplit = 0
                Else
                    $hoursplit = $hour[1]
                EndIf
            EndIf

            ;format the date
            $datesplit = StringSplit($date[1], "/")
            $FormattedDate = $datesplit[3] & "/" & $datesplit[2] & "/" & $datesplit[1] & " " & $hoursplit & ":" & $hour[2] & ":00"

            ;Date difference to _NowCalc() for minutes to expiry
            $iDateCalc = _DateDiff('n', _NowCalc(), $FormattedDate)

            ;******************************************************************************
            ;******************************************************************************

            ;Reformatting effective details to suite date/time convention
            $date1 = StringSplit($Effective_dateData, " ")

            ; Format the time
            If $date1[3] = "PM" Then
                $hour1 = StringSplit($date1[2], ":")
                If $hour1[1] = 12 Then
                    $hoursplit1 = 12
                Else
                    $hoursplit1 = $hour1[1] + 12
                    If $hoursplit1 = 24 Then
                        $hoursplit1 = 0
                    Else
                        $hoursplit1 = $hour1[1] + 12
                    EndIf
                EndIf
            EndIf
            
            If $date1[3] = "AM" Then
                $hour1 = StringSplit($date1[2], ":")
                $hoursplit1 = $hour1[1]
                If $hoursplit1 = 12 Then
                    $hoursplit1 = 0
                Else
                    $hoursplit1 = $hour1[1]
                EndIf
            EndIf

            ;format the date
            $datesplit1 = StringSplit($date1[1], "/")
            $FormattedDate1 = $datesplit1[3] & "/" & $datesplit1[2] & "/" & $datesplit1[1] & " " & $hoursplit1 & ":" & $hour1[2] & ":00"

            ;Date difference to _NowCalc()
            $iDateCalc1 = _DateDiff('n', $FormattedDate1, _NowCalc())

            $iDateCalc2 = _DateDiff('n', $FormattedDate1, $FormattedDate)

            $ResultPercent = (($iDateCalc1 / $iDateCalc2) * 100)
            
            ;******************************************************************************
            GUICtrlSetData($ProgressCreate, $ResultPercent)
            
            If $iDateCalc < "55" Then
                GUICtrlSetData($MinsLeft, $iDateCalc & " (Expires Soon)")
                GUICtrlSetColor($MinsLeft, 0xff0000) ; Red
            Else
                GUICtrlSetColor($MinsLeft, 0x000000) ; Black
            EndIf
            
            If $iDateCalc < 1 Then
                GUICtrlSetData($MinsLeft, "Expired")
                GUICtrlSetData($ProgressCreate, 100)
                $s_Subject = "CRL Endpoint Check v3.0 - CRL (" & $Endpoints[2] & ") has expired"
                $as_Body[0] = "CRL Endpoint Check v3.0 from " & @ComputerName & @CRLF
                $as_Body[1] = "Please contact the CRL privider and let them know their CRL has expired." & @CRLF & "CRL (" & $Endpoints[2] & ".crl) expired at: " & $FormattedDate & @CRLF & @CRLF & "Endpoint URL: " & $Endpoints[3]
                $Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_helo, $s_first)
            Else
                GUICtrlSetData($MinsLeft, $iDateCalc)
            EndIf
            
            FileDelete($Endpoints[1] & "\" & $Endpoints[2] & ".txt")
            FileDelete($Endpoints[1] & "\" & $Endpoints[2] & ".crl")
        EndIf
    Next
    GUICtrlSetData($LastUpdate, "Last Updated:    " & _NowCalc())
    $line = 40
    ;******************************************************************************

    GUICtrlSetData($RefreshButton, "Manual Refresh")
    GUICtrlSetState($RefreshButton, $GUI_ENABLE)
    GUICtrlSetData($RefreshProgress, 0)

    $Timer = TimerInit()
    $ProgressStep = ""
    $TimerValue = 600000; 10 minute approx
    
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $RefreshButton
                GUICtrlSetData($RefreshButton, "Updating")
                GUICtrlSetState($RefreshButton, $GUI_DISABLE)
                GUICtrlSetData($RefreshProgress, 0)
                $Timer = TimerInit()
                ExitLoop
            Case $msg = $GUI_EVENT_CLOSE; Or $msg = $BUTTON2
                GUIDelete()
                FileClose($FILE)
                FileClose($CertUtilLogs)
                Exit
        EndSelect

        GUICtrlSetData($RefreshProgress, (TimerDiff($Timer) / $TimerValue * 100))
        
        If TimerDiff($Timer) > $TimerValue Then
            GUICtrlSetData($RefreshProgress, 100)
            $Timer = TimerInit()
            GUICtrlSetData($RefreshButton, "Updating")
            GUICtrlSetState($RefreshButton, $GUI_DISABLE)
            ExitLoop
        EndIf
    WEnd
WEnd

The input file looks like this

Production,testcrl1,http://testsite1/latest.crl,testcrl1_latest.crl,test1@website.com

Production,testcrl2,http://testsite2/latest.crl,testcrl2_latest.crl,test2@website.com

The first tab is the environment. the second is a lable for the CRL, the third is the CRL download address and the last is the email address to email to.

Attached is a screenshot of what it looks like after the initial CRL processing then waiting.

If someone has a better way (as I'm sure there is) of doing this then please let me know. I have commented some of it but not all so sorry if it becomes confusing.

Cheers

Justin

post-10196-0-34572000-1315191835_thumb.j

Link to comment
Share on other sites

I'm not sure your concerns about exhausting system resources are much to worry about, but I looked over the routine for a few and noticed a few things I'd change.

1. You have some GuiCtrlCreate() statements in your main (outer) loop.

2. You have some one-time initialization code being executed each loop when not necessary (since you only read your csv file once).

3. I think your use of the Endpoint[] array limits correcting #1 (above) and makes the code harder to read or update in the future. Splitting that array into separate arrays solved #1 and makes the variable names much more self-documenting.

It was easier to just do it, so, maybe this addresses your concern about eventually creating too many handles?

#include <Constants.au3>
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <INet.au3>
Dim $aRecords, $Results, $Effective_date, $Expiry_Details, $Expiry_DetailsData, $CertUtilLogs, $FILE
If Not _FileReadToArray("CRL_Check.csv", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array   error:" & @error)
    Exit
EndIf
Global $s_Environment[$aRecords[0] + 1], $s_Endpoint[$aRecords[0] + 1], $s_URL[$aRecords[0] + 1], $s_ToAddress[$aRecords[0] + 1]
For $x = 1 to $aRecords[0]
    $atemp = StringSplit($aRecords[$x], ",")
    $s_Environment[$x] = $atemp[1]
    $s_Endpoint[$x] = $atemp[2]
    If Not FileExists($s_Endpoint[$x]) Then DirCreate($s_Environment[$x])
    $s_URL[$x] = $atemp[3]
    $s_ToAddress[$x] = $atemp[4]
Next
Global $Label_Environment[$aRecords[0] + 1], $Label_Endpoint[$aRecords[0] + 1],$Label_MinsLeft[$aRecords[0] + 1], $Progress[$aRecords[0] + 1]
$mainWindow = GUICreate("CRL Endpoint Check v3.0 (UPDATES EVERY 10 MINUTES)", 700, (($aRecords[0] * 20) + 100))
Opt("TrayIconHide", 1)
Opt("GUIOnEventMode", 0)
; Email Settings
$s_SmtpServer = "*** REMOVED ***"
$s_helo = "EHLO CRL_Endpoint_Check_v3.0"
$s_first = -1
$s_FromName = "CRL Endpoint Check v3.0"
$s_FromAddress = "*** REMOVED ***"
Dim $as_Body[2]
GUISetState()
GUICtrlCreateGroup("Environment ---> EndPoint", 20, 10, 250, (($aRecords[0] * 20) + 40))
GUICtrlCreateGroup("Expiry Times Percentage", 280, 10, 280, (($aRecords[0] * 20) + 40))
GUICtrlCreateGroup("Mins before Expiry", 570, 10, 110, (($aRecords[0] * 20) + 40))
For $x = 1 to $aRecords[0]
    $Label_Environment[$x] = GUICtrlCreateLabel($s_Environment[$x],  30, 20 + $x * 20, 60, 17)
GUICtrlCreateLabel("--->", 100, 20 + $x * 20, 30, 17)
    $Label_Endpoint[$x] = GUICtrlCreateLabel($s_Endpoint[$x], 140, 20 + $x * 20, 120, 17)
    $Progress[$x] = GUICtrlCreateProgress(290, 20 + $x * 20, 250, 17)
    $Label_MinsLeft[$x] = GUICtrlCreateLabel("", 580, 20 + $x * 20, 95, 17)
Next
$LastUpdate = GUICtrlCreateLabel("Last Updated...", 30, (($aRecords[0] * 20) + 55), 200, 17)
GUICtrlCreateLabel("Service Started: " & _NowCalc(), 30, (($aRecords[0] * 20) + 75), 200, 17)
$RefreshProgress = GUICtrlCreateProgress(290, (($aRecords[0] * 20) + 70), 250, 17)
$RefreshButton = GUICtrlCreateButton("Manual Refresh", 575, (($aRecords[0] * 20) + 65), 100, 25)
GUICtrlSetData($RefreshButton, "Updating")
GUICtrlSetState($RefreshButton, $GUI_DISABLE)
$line = 40
While 1
    For $x = 1 to $aRecords[0]
        $Endpoints = StringSplit($aRecords[$x], ",")
        $s_ToAddress = $Endpoints[4]
        If Not FileExists($s_Environment[$x]) Then
            DirCreate($s_Environment[$x])
        EndIf
        GUICtrlSetData($Label_MinsLeft[$x], "** Updating **")
        #Alternate
        Local $hDownload = InetGet($s_URL[$x], $s_Environment[$x] & "\" & $s_Endpoint[$x] & ".crl", 3, 1)
        Do
            Sleep(500)
        Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
        Local $aData = InetGetInfo($hDownload) ; Get all information.
        InetClose($hDownload) ; Close the handle to release resourcs.
        If $aData[4] <> 0 then
            GUICtrlSetData($Label_MinsLeft[$x], "Download ERR")
            GUICtrlSetColor($Label_MinsLeft[$x], 0xff0000) ; Red
            GUICtrlSetData($Progress[$x], "0")
            Sleep(500)
            $line = $line + 20
        Else
            Sleep(500)
            $line = $line + 20
            ;******************************************************************************
            ;Interrogating the CRL with Certutil.exe and dumping to file
            $CertUtilLogs = FileOpen($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".txt", 2)
            If $CertUtilLogs = -1 Then
                MsgBox(0, "Error", "Unable to create CRL Dump file. May be a file permission problem.")
                Exit
            EndIf
            local $Run = Run(@ComSpec & " /c " & "certutil.exe " & $s_Environment[$x] & "\" & $s_Endpoint[$x] & ".crl", "", @SW_HIDE, $STDOUT_CHILD)
            local $Certutilline
            Sleep(50)
            While 1
                $Certutilline = StdoutRead($Run)
                If @error Then ExitLoop
                Sleep(200)
                FileWriteLine($CertUtilLogs, $Certutilline)
            Wend
            FileClose($CertUtilLogs)
            Sleep(50)
            ;******************************************************************************
            ;******************************************************************************
            ;Opening dumped file and processing to look for effective and expity date to calculate CRL duration
            $FILE = FileOpen($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".txt", 0)
            If $FILE = -1 Then
                MsgBox(0, "Error", "Unable to open file so service probably isn't responding to commands.")
                Exit
            EndIf
            While 1
                $READLINE = FileReadLine($FILE)
                If @error = -1 Then ExitLoop
 
                $Effective_date = StringRegExp($READLINE, ' ThisUpdate: (.*)', 2)
                For $I = 0 To UBound($Effective_date) - 1
                    $Effective_dateData = $Effective_date[1]
                Next
                $Expiry_Details = StringRegExp($READLINE, ' NextUpdate: (.*)', 2)
                For $I = 0 To UBound($Expiry_Details) - 1
                    $Expiry_DetailsData = $Expiry_Details[1]
                Next
            WEnd
            FileClose($FILE)
            ;******************************************************************************
            ;******************************************************************************
            ;Reformatting expiry details to suite date/time convention
            $date = StringSplit($Expiry_DetailsData, " ")
            ; Format the time
            If $date[3] = "PM" Then
                $hour = StringSplit($date[2], ":")
                If $hour[1] = 12 Then
                    $hoursplit = 12
                Else
                    $hoursplit = $hour[1] + 12
                    If $hoursplit = 24 Then
                        $hoursplit = 0
                    Else
                        $hoursplit = $hour[1] + 12
                    EndIf
                EndIf
            EndIf
            If $date[3] = "AM" Then
                $hour = StringSplit($date[2], ":")
                $hoursplit = $hour[1]
                If $hoursplit = 12 Then
                    $hoursplit = 0
                Else
                    $hoursplit = $hour[1]
                EndIf
            EndIf
            ;format the date
            $datesplit = StringSplit($date[1], "/")
            $FormattedDate = $datesplit[3] & "/" & $datesplit[2] & "/" & $datesplit[1] & " " & $hoursplit & ":" & $hour[2] & ":00"
            ;Date difference to _NowCalc() for minutes to expiry
            $iDateCalc = _DateDiff('n', _NowCalc(), $FormattedDate)
            ;******************************************************************************
            ;******************************************************************************
            ;Reformatting effective details to suite date/time convention
            $date1 = StringSplit($Effective_dateData, " ")
            ; Format the time
            If $date1[3] = "PM" Then
                $hour1 = StringSplit($date1[2], ":")
                If $hour1[1] = 12 Then
                    $hoursplit1 = 12
                Else
                    $hoursplit1 = $hour1[1] + 12
                    If $hoursplit1 = 24 Then
                        $hoursplit1 = 0
                    Else
                        $hoursplit1 = $hour1[1] + 12
                    EndIf
                EndIf
            EndIf
            If $date1[3] = "AM" Then
                $hour1 = StringSplit($date1[2], ":")
                $hoursplit1 = $hour1[1]
                If $hoursplit1 = 12 Then
                    $hoursplit1 = 0
                Else
                    $hoursplit1 = $hour1[1]
                EndIf
            EndIf
            ;format the date
            $datesplit1 = StringSplit($date1[1], "/")
            $FormattedDate1 = $datesplit1[3] & "/" & $datesplit1[2] & "/" & $datesplit1[1] & " " & $hoursplit1 & ":" & $hour1[2] & ":00"
            ;Date difference to _NowCalc()
            $iDateCalc1 = _DateDiff('n', $FormattedDate1, _NowCalc())
            $iDateCalc2 = _DateDiff('n', $FormattedDate1, $FormattedDate)
            $ResultPercent = (($iDateCalc1 / $iDateCalc2) * 100)
            ;******************************************************************************
            GUICtrlSetData($Progress[$x], $ResultPercent)
            If $iDateCalc < "55" Then
                GUICtrlSetData($Label_MinsLeft[$x], $iDateCalc & " (Expires Soon)")
                GUICtrlSetColor($Label_MinsLeft[$x], 0xff0000) ; Red
            Else
                GUICtrlSetColor($Label_MinsLeft[$x], 0x000000) ; Black
            EndIf
            If $iDateCalc < 1 Then
                GUICtrlSetData($Label_MinsLeft[$x], "Expired")
                GUICtrlSetData($Progress[$x], 100)
                $s_Subject = "CRL Endpoint Check v3.0 - CRL (" & $s_Endpoint[$x] & ") has expired"
                $as_Body[0] = "CRL Endpoint Check v3.0 from " & @ComputerName & @CRLF
                $as_Body[1] = "Please contact the CRL privider and let them know their CRL has expired." & @CRLF & "CRL (" & $s_Endpoint[$x] & ".crl) expired at: " & $FormattedDate & @CRLF & @CRLF & "Endpoint URL: " & $s_URL[$x]
                $Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_helo, $s_first)
            Else
                GUICtrlSetData($Label_MinsLeft[$x], $iDateCalc)
            EndIf
            FileDelete($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".txt")
            FileDelete($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".crl")
        EndIf
    Next
    GUICtrlSetData($LastUpdate, "Last Updated:  " & _NowCalc())
    $line = 40
    ;******************************************************************************
    GUICtrlSetData($RefreshButton, "Manual Refresh")
    GUICtrlSetState($RefreshButton, $GUI_ENABLE)
    GUICtrlSetData($RefreshProgress, 0)
    $Timer = TimerInit()
    $ProgressStep = ""
    $TimerValue = 600000; 10 minute approx
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $RefreshButton
                GUICtrlSetData($RefreshButton, "Updating")
                GUICtrlSetState($RefreshButton, $GUI_DISABLE)
                GUICtrlSetData($RefreshProgress, 0)
                $Timer = TimerInit()
                ExitLoop
            Case $msg = $GUI_EVENT_CLOSE; Or $msg = $BUTTON2
                GUIDelete()
                FileClose($FILE)
                FileClose($CertUtilLogs)
                Exit
        EndSelect
        GUICtrlSetData($RefreshProgress, (TimerDiff($Timer) / $TimerValue * 100))
        If TimerDiff($Timer) > $TimerValue Then
            GUICtrlSetData($RefreshProgress, 100)
            $Timer = TimerInit()
            GUICtrlSetData($RefreshButton, "Updating")
            GUICtrlSetState($RefreshButton, $GUI_DISABLE)
            ExitLoop
        EndIf
    WEnd
WEnd

Pardon the missing blank lines and screwed up indentation, forum pastes seem to be doing that to me lately.

Edit: Removed the no longer referenced $line variable.

Edited by Spiff59
Link to comment
Share on other sites

Hi spiff59,

I did have an issue about exhausting resources with another tool I created a long time ago so was just a bit worried. Running the code that you have created looks good so will test it and see how it goes.

I knew that I needed to create the labels outside the loop and just update the data within the loop but I just couldn't figure out how to reference each variable in the array, looks like you have cracked it.

Your help is very much appreciated. The setting up arrays thing is relatively new to me with the simple tools I have created in the past so I shall learn from your code.

Cheers

Link to comment
Share on other sites

Rearanged main loop and created function Update() which is called correctly from main GUI loop,

also fixed setting progress value only when it changes (antiflicker fix) and few more fixes.

#include <Constants.au3>
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <INet.au3>
 
Opt("TrayIconHide", 1)
 
Dim $aRecords, $Results, $Effective_date, $Expiry_Details, $Expiry_DetailsData, $CertUtilLogs, $FILE
Global $Timer
Global $TimerValue = 600000; 10 minute approx
Global $progress_current
Global $as_Body[2]
 
If Not _FileReadToArray("CRL_Check.csv", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array   error:" & @error)
    Exit
EndIf
 
; Email Settings
$s_SmtpServer = "*** REMOVED ***"
$s_helo = "EHLO CRL_Endpoint_Check_v3.0"
$s_first = -1
$s_FromName = "CRL Endpoint Check v3.0"
$s_FromAddress = "*** REMOVED ***"
 
Global $s_Environment[$aRecords[0] + 1], $s_Endpoint[$aRecords[0] + 1], $s_URL[$aRecords[0] + 1], $s_ToAddress[$aRecords[0] + 1]
 
For $x = 1 to $aRecords[0]
    $atemp = StringSplit($aRecords[$x], ",")
    $s_Environment[$x] = $atemp[1]
    $s_Endpoint[$x] = $atemp[2]
    If Not FileExists($s_Endpoint[$x]) Then DirCreate($s_Environment[$x])
    $s_URL[$x] = $atemp[3]
    $s_ToAddress[$x] = $atemp[4]
Next
 
Global $Label_Environment[$aRecords[0] + 1], $Label_Endpoint[$aRecords[0] + 1],$Label_MinsLeft[$aRecords[0] + 1], $Progress[$aRecords[0] + 1]
 
$mainWindow = GUICreate("CRL Endpoint Check v3.0 (UPDATES EVERY 10 MINUTES)", 700, (($aRecords[0] * 20) + 100))
GUICtrlCreateGroup("Environment ---> EndPoint", 20, 10, 250, (($aRecords[0] * 20) + 40))
GUICtrlCreateGroup("Expiry Times Percentage", 280, 10, 280, (($aRecords[0] * 20) + 40))
GUICtrlCreateGroup("Mins before Expiry", 570, 10, 110, (($aRecords[0] * 20) + 40))
 
For $x = 1 to $aRecords[0]
    $Label_Environment[$x] = GUICtrlCreateLabel($s_Environment[$x],  30, 20 + $x * 20, 60, 17)
    GUICtrlCreateLabel("--->", 100, 20 + $x * 20, 30, 17)
    $Label_Endpoint[$x] = GUICtrlCreateLabel($s_Endpoint[$x], 140, 20 + $x * 20, 120, 17)
    $Progress[$x] = GUICtrlCreateProgress(290, 20 + $x * 20, 250, 17)
    $Label_MinsLeft[$x] = GUICtrlCreateLabel("", 580, 20 + $x * 20, 95, 17)
Next
 
$LastUpdate = GUICtrlCreateLabel("Last Updated...", 30, (($aRecords[0] * 20) + 55), 200, 17)
GUICtrlCreateLabel("Service Started: " & _NowCalc(), 30, (($aRecords[0] * 20) + 75), 200, 17)
$RefreshProgress = GUICtrlCreateProgress(290, (($aRecords[0] * 20) + 70), 250, 17)
$RefreshButton = GUICtrlCreateButton("Manual Refresh", 575, (($aRecords[0] * 20) + 65), 100, 25)
GUISetState()
 
Update()
 
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $RefreshButton
            Update()
        Case $msg = $GUI_EVENT_CLOSE; Or $msg = $BUTTON2
            GUIDelete()
            FileClose($FILE)
            FileClose($CertUtilLogs)
            Exit
    EndSelect
    
    If TimerDiff($Timer) / $TimerValue * 100 <> $progress_current Then
        $progress_current = TimerDiff($Timer) / $TimerValue * 100
        GUICtrlSetData($RefreshProgress, $progress_current)
    EndIf
    
    If TimerDiff($Timer) > $TimerValue Then Update()
WEnd
 
Func Update()
    GUICtrlSetData($RefreshButton, "Updating")
    GUICtrlSetState($RefreshButton, $GUI_DISABLE)
    GUICtrlSetData($RefreshProgress, 0)
    
    For $x = 1 to $aRecords[0]
        $Endpoints = StringSplit($aRecords[$x], ",")
        $s_ToAddress = $Endpoints[4]
        If Not FileExists($s_Environment[$x]) Then DirCreate($s_Environment[$x])
        GUICtrlSetData($Label_MinsLeft[$x], "** Updating **")
        #Alternate
        Local $hDownload = InetGet($s_URL[$x], $s_Environment[$x] & "\" & $s_Endpoint[$x] & ".crl", 3, 1)
        Do
            Sleep(500)
        Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
        Local $aData = InetGetInfo($hDownload) ; Get all information.
        InetClose($hDownload) ; Close the handle to release resourcs.
        If $aData[4] <> 0 then
            GUICtrlSetData($Label_MinsLeft[$x], "Download ERR")
            GUICtrlSetColor($Label_MinsLeft[$x], 0xff0000) ; Red
            GUICtrlSetData($Progress[$x], "0")
            Sleep(500)
        Else
            Sleep(500)
            ;******************************************************************************
            ;Interrogating the CRL with Certutil.exe and dumping to file
            $CertUtilLogs = FileOpen($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".txt", 2)
            If $CertUtilLogs = -1 Then
                MsgBox(0, "Error", "Unable to create CRL Dump file. May be a file permission problem.")
                Exit
            EndIf
            local $Run = Run(@ComSpec & " /c " & "certutil.exe " & $s_Environment[$x] & "\" & $s_Endpoint[$x] & ".crl", "", @SW_HIDE, $STDOUT_CHILD)
            local $Certutilline
            Sleep(50)
            While 1
                $Certutilline = StdoutRead($Run)
                If @error Then ExitLoop
                Sleep(200)
                FileWriteLine($CertUtilLogs, $Certutilline)
            Wend
            FileClose($CertUtilLogs)
            Sleep(50)
            ;******************************************************************************
            ;******************************************************************************
            ;Opening dumped file and processing to look for effective and expity date to calculate CRL duration
            $FILE = FileOpen($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".txt", 0)
            If $FILE = -1 Then
                MsgBox(0, "Error", "Unable to open file so service probably isn't responding to commands.")
                Exit
            EndIf
            While 1
                $READLINE = FileReadLine($FILE)
                If @error = -1 Then ExitLoop
 
                $Effective_date = StringRegExp($READLINE, ' ThisUpdate: (.*)', 2)
                For $I = 0 To UBound($Effective_date) - 1
                    $Effective_dateData = $Effective_date[1]
                Next
                $Expiry_Details = StringRegExp($READLINE, ' NextUpdate: (.*)', 2)
                For $I = 0 To UBound($Expiry_Details) - 1
                    $Expiry_DetailsData = $Expiry_Details[1]
                Next
            WEnd
            FileClose($FILE)
            ;******************************************************************************
            ;******************************************************************************
            ;Reformatting expiry details to suite date/time convention
            $date = StringSplit($Expiry_DetailsData, " ")
            ; Format the time
            If $date[3] = "PM" Then
                $hour = StringSplit($date[2], ":")
                If $hour[1] = 12 Then
                    $hoursplit = 12
                Else
                    $hoursplit = $hour[1] + 12
                    If $hoursplit = 24 Then
                        $hoursplit = 0
                    Else
                        $hoursplit = $hour[1] + 12
                    EndIf
                EndIf
            EndIf
            If $date[3] = "AM" Then
                $hour = StringSplit($date[2], ":")
                $hoursplit = $hour[1]
                If $hoursplit = 12 Then
                    $hoursplit = 0
                Else
                    $hoursplit = $hour[1]
                EndIf
            EndIf
            ;format the date
            $datesplit = StringSplit($date[1], "/")
            $FormattedDate = $datesplit[3] & "/" & $datesplit[2] & "/" & $datesplit[1] & " " & $hoursplit & ":" & $hour[2] & ":00"
            ;Date difference to _NowCalc() for minutes to expiry
            $iDateCalc = _DateDiff('n', _NowCalc(), $FormattedDate)
            ;******************************************************************************
            ;******************************************************************************
            ;Reformatting effective details to suite date/time convention
            $date1 = StringSplit($Effective_dateData, " ")
            ; Format the time
            If $date1[3] = "PM" Then
                $hour1 = StringSplit($date1[2], ":")
                If $hour1[1] = 12 Then
                    $hoursplit1 = 12
                Else
                    $hoursplit1 = $hour1[1] + 12
                    If $hoursplit1 = 24 Then
                        $hoursplit1 = 0
                    Else
                        $hoursplit1 = $hour1[1] + 12
                    EndIf
                EndIf
            EndIf
            If $date1[3] = "AM" Then
                $hour1 = StringSplit($date1[2], ":")
                $hoursplit1 = $hour1[1]
                If $hoursplit1 = 12 Then
                    $hoursplit1 = 0
                Else
                    $hoursplit1 = $hour1[1]
                EndIf
            EndIf
            ;format the date
            $datesplit1 = StringSplit($date1[1], "/")
            $FormattedDate1 = $datesplit1[3] & "/" & $datesplit1[2] & "/" & $datesplit1[1] & " " & $hoursplit1 & ":" & $hour1[2] & ":00"
            ;Date difference to _NowCalc()
            $iDateCalc1 = _DateDiff('n', $FormattedDate1, _NowCalc())
            $iDateCalc2 = _DateDiff('n', $FormattedDate1, $FormattedDate)
            $ResultPercent = (($iDateCalc1 / $iDateCalc2) * 100)
            ;******************************************************************************
            GUICtrlSetData($Progress[$x], $ResultPercent)
            If $iDateCalc < "55" Then
                GUICtrlSetData($Label_MinsLeft[$x], $iDateCalc & " (Expires Soon)")
                GUICtrlSetColor($Label_MinsLeft[$x], 0xff0000) ; Red
            Else
                GUICtrlSetColor($Label_MinsLeft[$x], 0x000000) ; Black
            EndIf
            If $iDateCalc < 1 Then
                GUICtrlSetData($Label_MinsLeft[$x], "Expired")
                GUICtrlSetData($Progress[$x], 100)
                $s_Subject = "CRL Endpoint Check v3.0 - CRL (" & $s_Endpoint[$x] & ") has expired"
                $as_Body[0] = "CRL Endpoint Check v3.0 from " & @ComputerName & @CRLF
                $as_Body[1] = "Please contact the CRL privider and let them know their CRL has expired." & @CRLF & "CRL (" & $s_Endpoint[$x] & ".crl) expired at: " & $FormattedDate & @CRLF & @CRLF & "Endpoint URL: " & $s_URL[$x]
                $Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_helo, $s_first)
            Else
                GUICtrlSetData($Label_MinsLeft[$x], $iDateCalc)
            EndIf
            FileDelete($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".txt")
            FileDelete($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".crl")
        EndIf
    Next
    
    GUICtrlSetData($LastUpdate, "Last Updated:  " & _NowCalc())
    GUICtrlSetData($RefreshButton, "Manual Refresh")
    GUICtrlSetState($RefreshButton, $GUI_ENABLE)
    GUICtrlSetData($RefreshProgress, 0)
    $Timer = TimerInit()
EndFunc
Edited by Zedna
Link to comment
Share on other sites

Looks like you're getting the "full treatment", jussie!

I'd only addressed your initial question, Zedna has now brought it a big step closer to clean, organized code.

I guess we like hearing encouraging things like "Your help is very much appreciated" and " I shall learn from your code" :mellow:

I've always found studying a working example to be the easiest way for me to learn.

Link to comment
Share on other sites

Wow thanks guys for the info. I've since added more features like multiple address emailing, changing the progress colours according to thresholds, reading some of the info from a config file and some local logging.

Sorry Zedna but I hadn't seen this post until now so have coded with Spiff59's suggestions but will now take a look at what you suggest and make the changes.

I'll include what I have so far before your suggestions and also some sample required files for it to work. The Gui looks relatively the same.

The attached screenshot was triggered to show the progress colours and status info.

#include <Constants.au3>
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <INet.au3>
Dim $aRecords, $Results, $Effective_date, $Expiry_Details, $Expiry_DetailsData, $CertUtilLogs, $FILE, $LOG
Global $XS_n
$Version = "CRL Endpoint Check v3.2.2"
If NOT FileExists("Config.ini") then
    MsgBox(16, "Config File Read Error", "The configuration file Config.ini is missing or is unable to be opened." & @CRLF & @CRLF & "Make sure a file called 'config.ini' is in the same directory as this .exe file and the application has read permissions." & @CRLF & @CRLF & "Press OK to exit this application.")
    Exit
EndIf
; Email Settings from config.ini
$s_SmtpServer = IniRead("Config.ini", "MailSettings", "SMTPServer", "")
$s_FromName = IniRead("Config.ini", "MailSettings", "FromName", "")
$s_FromAddress = IniRead("Config.ini", "MailSettings", "FromAddress", "")
$s_helo = "EHLO VANguard_CRL_Endpoint_Check"
$s_first = -1
Dim $as_Body[2]
; General Settings from config.ini
$CSVInput = IniRead("Config.ini", "Settings", "InputCSV", "")
$LogFile = IniRead("Config.ini", "Settings", "LogFileName", "")
$TimerValue = IniRead("Config.ini", "Settings", "RefreshTimeMins", "")
If $TimerValue < 2 Then $TimerValue = 2
$TimerValue = $TimerValue * 60000
$WarningThreshold = IniRead("Config.ini", "Settings", "WarningThreshold", "")
If $WarningThreshold < 20 then $WarningThreshold = 20
$ResolvedThreshold = $WarningThreshold
If Not _FileReadToArray($CSVInput, $aRecords) Then
    MsgBox(16, "CSV File Read Error", "Error reading log to Array." & @CRLF & @CRLF & "Make sure a file called " & $CSVInput & " is in the same directory as this .exe file." & @CRLF & @CRLF & "Press OK to exit this application.")
    Exit
EndIf
$LOG = FileOpen($LogFile, 1)
; Check if file opened for writing OK
If $LOG = -1 Then
    MsgBox(16, "Error", "Unable to open log file. Check permissions for read/write access to the current directory." & @CRLF & @CRLF & "Press OK to exit this application.")
    Exit
EndIf
FileWriteLine($LOG, @CRLF & _NowCalc() & " ---> " & $Version & " Application :- Started" & @CRLF)
Global $s_Environment[$aRecords[0] + 1], $s_Endpoint[$aRecords[0] + 1], $s_URL[$aRecords[0] + 1], $s_ToAddress[$aRecords[0] + 1]
For $x = 1 to $aRecords[0]
    $atemp = StringSplit($aRecords[$x], ",")
    $s_Environment[$x] = $atemp[1]
    $s_Endpoint[$x] = $atemp[2]
    If Not FileExists($s_Endpoint[$x]) Then DirCreate($s_Environment[$x])
    $s_URL[$x] = $atemp[3]
Next
Global $Label_Environment[$aRecords[0] + 1], $Label_Endpoint[$aRecords[0] + 1], $Label_MinsLeft[$aRecords[0] + 1], $Progress[$aRecords[0] + 1], $Status[$aRecords[0] + 1], $StatusCount[$aRecords[0] + 1]
$mainWindow = GUICreate($Version & " (Updates every " & ($TimerValue / 60000) & " Minute/s)", 700, (($aRecords[0] * 20) + 100))
Opt("TrayIconHide", 1)
Opt("GUIOnEventMode", 0)
XPStyleToggle(1)
GUISetState()
GUICtrlCreateGroup("Environment ---> EndPoint", 20, 10, 250, (($aRecords[0] * 20) + 40))
GUICtrlCreateGroup("Expiry Times Percentage", 280, 10, 280, (($aRecords[0] * 20) + 40))
GUICtrlCreateGroup("Mins before Expiry", 570, 10, 110, (($aRecords[0] * 20) + 40))
;Create array for labels
For $x = 1 to $aRecords[0]
    $Label_Environment[$x] = GUICtrlCreateLabel($s_Environment[$x], 30, 20 + $x * 20, 60, 17)
    GUICtrlCreateLabel("--->", 100, 20 + $x * 20, 30, 17)
    $Label_Endpoint[$x] = GUICtrlCreateLabel($s_Endpoint[$x], 120, 20 + $x * 20, 120, 17)
    $Progress[$x] = GUICtrlCreateProgress(290, 20 + $x * 20, 260, 17, $PBS_SMOOTH)
    $Label_MinsLeft[$x] = GUICtrlCreateLabel("", 580, 20 + $x * 20, 95, 17)
    $Status[$x] = ""
    $StatusCount[$x] = ""
Next
$LastUpdate = GUICtrlCreateLabel("Last Updated...", 30, (($aRecords[0] * 20) + 55), 200, 17)
GUICtrlCreateLabel("Service Started: " & _NowCalc(), 30, (($aRecords[0] * 20) + 75), 200, 17)
$RefreshProgress = GUICtrlCreateProgress(290, (($aRecords[0] * 20) + 57), 260, 17, $PBS_SMOOTH)
$RefreshButton = GUICtrlCreateButton("Manual Refresh", 570, (($aRecords[0] * 20) + 57), 110, 20)
GUICtrlCreateLabel("Email triggered at '" & $WarningThreshold & "'", 575, (($aRecords[0] * 20) + 80), 200, 17)
GUICtrlSetData($RefreshButton, "Updating")
GUICtrlSetState($RefreshButton, $GUI_DISABLE)
$SMTPLabel = GUICtrlCreateLabel("Last Email Sent about: (None triggered)", 290, (($aRecords[0] * 20) + 80), 260, 17)
$line = 40
While 1
    For $x = 1 to $aRecords[0]
        $Endpoints = StringSplit($aRecords[$x], ",")
        $s_ToAddress = $Endpoints[4]
        If Not FileExists($s_Environment[$x]) Then
            DirCreate($s_Environment[$x])
        EndIf
        GUICtrlSetColor($Label_MinsLeft[$x], 0x000000) ; Black
        GUICtrlSetData($Label_MinsLeft[$x], "*** UPDATING ***")
        #Alternate
        Local $hDownload = InetGet($s_URL[$x], $s_Environment[$x] & "\" & $s_Endpoint[$x] & ".crl", 3, 1)
        Do
            Sleep(20)
        Until InetGetInfo($hDownload, 2) ; Check if the download is complete.
        Local $aData = InetGetInfo($hDownload) ; Get all information.
        InetClose($hDownload) ; Close the handle to release resourcs.
        If $aData[4] <> 0 then
            GUICtrlSetData($Label_MinsLeft[$x], "Download ERR")
            GUICtrlSetColor($Label_MinsLeft[$x], 0xff0000) ; Red
            GUICtrlSetData($Progress[$x], "0")
            Sleep(20)
            $line = $line + 20
            FileWriteLine($LOG, _NowCalc() & " ---> CRL (" & $s_Endpoint[$x] & ") Download Error. Will try again on next poll" & @CRLF)
        Else
            Sleep(20)
            $line = $line + 20
            ;******************************************************************************
            ;Interrogating the CRL with Certutil.exe and dumping to file
            $CertUtilLogs = FileOpen($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".txt", 2)
            If $CertUtilLogs = -1 Then
                MsgBox(16, "Error", "Unable to create CRL Dump file. May be a file permission problem." & @CRLF & @CRLF & "Press OK to exit this application.")
                Exit
            EndIf
            local $Run = Run(@ComSpec & " /c " & "certutil.exe " & $s_Environment[$x] & "\" & $s_Endpoint[$x] & ".crl", "", @SW_HIDE, $STDOUT_CHILD)
            local $Certutilline
            Sleep(20)
            While 1
                $Certutilline = StdoutRead($Run)
                If @error Then ExitLoop
                Sleep(20)
                FileWriteLine($CertUtilLogs, $Certutilline)
            Wend
            FileClose($CertUtilLogs)
            Sleep(20)
            ;******************************************************************************
            ;******************************************************************************
            ;Opening dumped file and processing to look for effective and expity date to calculate CRL duration
            $FILE = FileOpen($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".txt", 0)
            If $FILE = -1 Then
                MsgBox(16, "Error", "Unable to open Certutil file for some reason. File may be in use when trying to open it." & @CRLF & @CRLF & "Press OK to exit this application.")
                Exit
            EndIf
            While 1
                $READLINE = FileReadLine($FILE)
                If @error = -1 Then ExitLoop
 
                $Effective_date = StringRegExp($READLINE, ' ThisUpdate: (.*)', 2)
                For $I = 0 To UBound($Effective_date) - 1
                    $Effective_dateData = $Effective_date[1]
                Next
                $Expiry_Details = StringRegExp($READLINE, ' NextUpdate: (.*)', 2)
                For $I = 0 To UBound($Expiry_Details) - 1
                    $Expiry_DetailsData = $Expiry_Details[1]
                Next
            WEnd
            FileClose($FILE)
            ;******************************************************************************
            ;******************************************************************************
            ;Reformatting expiry details to suite date/time convention
            $date = StringSplit($Expiry_DetailsData, " ")
            ; Format the time
            If $date[3] = "PM" Then
                $hour = StringSplit($date[2], ":")
                If $hour[1] = 12 Then
                    $hoursplit = 12
                Else
                    $hoursplit = $hour[1] + 12
                    If $hoursplit = 24 Then
                        $hoursplit = 0
                    Else
                        $hoursplit = $hour[1] + 12
                    EndIf
                EndIf
            EndIf
            If $date[3] = "AM" Then
                $hour = StringSplit($date[2], ":")
                $hoursplit = $hour[1]
                If $hoursplit = 12 Then
                    $hoursplit = 0
                Else
                    $hoursplit = $hour[1]
                EndIf
            EndIf
            ;format the date
            $datesplit = StringSplit($date[1], "/")
            $FormattedDate = $datesplit[3] & "/" & $datesplit[2] & "/" & $datesplit[1] & " " & $hoursplit & ":" & $hour[2] & ":00"
            ;Date difference to _NowCalc() for minutes to expiry
            $iDateCalc = _DateDiff('n', _NowCalc(), $FormattedDate)
            ;******************************************************************************
            ;******************************************************************************
            ;Reformatting effective details to suite date/time convention
            $date1 = StringSplit($Effective_dateData, " ")
            ; Format the time
            If $date1[3] = "PM" Then
                $hour1 = StringSplit($date1[2], ":")
                If $hour1[1] = 12 Then
                    $hoursplit1 = 12
                Else
                    $hoursplit1 = $hour1[1] + 12
                    If $hoursplit1 = 24 Then
                        $hoursplit1 = 0
                    Else
                        $hoursplit1 = $hour1[1] + 12
                    EndIf
                EndIf
            EndIf
            If $date1[3] = "AM" Then
                $hour1 = StringSplit($date1[2], ":")
                $hoursplit1 = $hour1[1]
                If $hoursplit1 = 12 Then
                    $hoursplit1 = 0
                Else
                    $hoursplit1 = $hour1[1]
                EndIf
            EndIf
            ;format the date
            $datesplit1 = StringSplit($date1[1], "/")
            $FormattedDate1 = $datesplit1[3] & "/" & $datesplit1[2] & "/" & $datesplit1[1] & " " & $hoursplit1 & ":" & $hour1[2] & ":00"
            ;Date difference to _NowCalc()
            $iDateCalc1 = _DateDiff('n', $FormattedDate1, _NowCalc())
            $iDateCalc2 = _DateDiff('n', $FormattedDate1, $FormattedDate)
            $ResultPercent = (($iDateCalc1 / $iDateCalc2) * 100)
            ;******************************************************************************
            ;******************************************************************************
            GUICtrlSetData($Progress[$x], $ResultPercent)
            If $iDateCalc >= $WarningThreshold Then
                GUICtrlSetData($Label_MinsLeft[$x], $iDateCalc)
                GUICtrlSetColor($Label_MinsLeft[$x], 0x000000) ; Black
                GUICtrlSetColor($Progress[$x], 0x33FF00) ; Green
                If $Status[$x] = "EXPIRED" OR $Status[$x] = "SOON" Then
                    $s_Subject = $s_FromName & " - CRL (" & $s_Endpoint[$x] & ") *** RESOLVED ***"
                    $as_Body[0] = "Message @ " & _NowCalc() & @CRLF
                    $as_Body[1] = "CRL (" & $s_Endpoint[$x] & ") new expiry time is now: " & $FormattedDate & @CRLF & @CRLF & "CRL had been expired for (" & $StatusCount[$x] & ") Counts." & @CRLF & "If Counts = () then the CRL has updated from 'Expiring Soon' status or CRL resolved within one poll." & @CRLF & @CRLF & "Endpoint URL: " & $s_URL[$x]
                    $ToAddressArray = StringSplit($s_ToAddress, ";")
                    For $y = 1 to $ToAddressArray[0]
                        $Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $ToAddressArray[$y], $s_Subject, $as_Body, $s_helo, $s_first)
                        $ERR = @error
                        If $Response = 1 Then
                            FileWriteLine($LOG, _NowCalc() & " ---> CRL (" & $s_Endpoint[$x] & ") new expiry time is now: " & $FormattedDate & "CRL had been expired for (" & $StatusCount[$x] & ") Counts." & @CRLF & "If Counts = () then the CRL has updated from 'Expiring Soon' status or CRL resolved within one poll. Email sent to: " & $ToAddressArray[$y] & ". Email Status: OK" & @CRLF)
                            GUICtrlSetData($SMTPLabel, "Last Email Sent about: (" & $s_Endpoint[$x] & "): OK")
                        Else
                            FileWriteLine($LOG, _NowCalc() & " ---> CRL (" & $s_Endpoint[$x] & ") new expiry time is now: " & $FormattedDate & "CRL had been expired for (" & $StatusCount[$x] & ") Counts." & @CRLF & "If Counts = () then the CRL has updated from 'Expiring Soon' status or CRL resolved within one poll. Email sent to: " & $ToAddressArray[$y] & ". Email Status: NOT SENT, Err: " & $ERR & @CRLF)
                            GUICtrlSetData($SMTPLabel, "Last Email Sent about: (" & $s_Endpoint[$x] & "): ERROR" & $ERR)
                        EndIf
                        Sleep(20)
                    Next
                EndIf
                $Status[$x] = ""
                $StatusCount[$x] = ""             
            EndIf
            If $iDateCalc < $WarningThreshold And $iDateCalc >= 1 And Not $iDateCalc < 1 Then
                GUICtrlSetData($Label_MinsLeft[$x], $iDateCalc & " (Expires Soon)")
                GUICtrlSetColor($Label_MinsLeft[$x], 0x6600CC) ; Purple
                GUICtrlSetColor($Progress[$x], 0x6600CC) ; Purple
                $s_Subject = $s_FromName & " - CRL (" & $s_Endpoint[$x] & ") Expires in " & $iDateCalc & " Minutes"
                $as_Body[0] = "Message @ " & _NowCalc() & @CRLF
                $as_Body[1] = "CRL (" & $s_Endpoint[$x] & ") expiry time is: " & $FormattedDate & @CRLF & @CRLF & "Endpoint URL: " & $s_URL[$x]
                $ToAddressArray = StringSplit($s_ToAddress, ";")
                For $y = 1 to $ToAddressArray[0]
                    $Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $ToAddressArray[$y], $s_Subject, $as_Body, $s_helo, $s_first)
                    $ERR = @error
                    If $Response = 1 Then
                        FileWriteLine($LOG, _NowCalc() & " ---> CRL (" & $s_Endpoint[$x] & ") expires in " & $iDateCalc & " Minutes.  Expiry time: " & $FormattedDate & ". Email sent to: " & $ToAddressArray[$y] & ". Email Status: OK" & @CRLF)
                        GUICtrlSetData($SMTPLabel, "Last Email Sent about: (" & $s_Endpoint[$x] & "): OK")
                    Else
                        FileWriteLine($LOG, _NowCalc() & " ---> CRL (" & $s_Endpoint[$x] & ") expires in " & $iDateCalc & " Minutes.  Expiry time: " & $FormattedDate & ". Email sent to: " & $ToAddressArray[$y] & ". Email Status: NOT SENT, Err: " & $ERR & @CRLF)
                        GUICtrlSetData($SMTPLabel, "Last Email Sent about: (" & $s_Endpoint[$x] & "): OK")
                    EndIf
                    Sleep(20)
                Next
                $Status[$x] = "SOON"
                $StatusCount[$x] = ""
            EndIf         
            If $iDateCalc < 1 Then
                GUICtrlSetData($Label_MinsLeft[$x], "EXPIRED")
                GUICtrlSetColor($Label_MinsLeft[$x], 0xff0000) ; Red
                GUICtrlSetData($Progress[$x], 100)
                GUICtrlSetColor($Progress[$x],0xff0000) ; Red
                If $Status[$x] = "EXPIRED" Then
                    $StatusCount[$x] = $StatusCount[$x] + 1
                    GUICtrlSetData($Label_MinsLeft[$x], "EXPIRED" & " (" & $StatusCount[$x] & ")")
                Else
                    $s_Subject = $s_FromName & " - CRL (" & $s_Endpoint[$x] & ") has *** EXPIRED ***"
                    $as_Body[0] = "Message @ " & _NowCalc() & @CRLF
                    $as_Body[1] = "CRL (" & $s_Endpoint[$x] & ") expired at: " & $FormattedDate & ". " & @CRLF & "Please contact the CRL privider and let them know their CRL has expired." & @CRLF & @CRLF & "Endpoint URL: " & $s_URL[$x]
                    $ToAddressArray = StringSplit($s_ToAddress, ";")
                    For $y = 1 to $ToAddressArray[0]
                        $Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $ToAddressArray[$y], $s_Subject, $as_Body, $s_helo, $s_first)
                        $ERR = @error
                        If $Response = 1 Then
                            FileWriteLine($LOG, _NowCalc() & " ---> CRL (" & $s_Endpoint[$x] & ") EXPIRED at: " & $FormattedDate & ". Email sent to: " & $ToAddressArray[$y] & ". Email Status: OK" & @CRLF)
                            GUICtrlSetData($SMTPLabel, "Last Email Sent about: (" & $s_Endpoint[$x] & "): OK")
                        Else
                            FileWriteLine($LOG, _NowCalc() & " ---> CRL (" & $s_Endpoint[$x] & ") EXPIRED at: " & $FormattedDate & ". Email sent to: " & $ToAddressArray[$y] & ". Email Status: NOT SENT, Err: " & $ERR & @CRLF)
                            GUICtrlSetData($SMTPLabel, "Last Email Sent about: (" & $s_Endpoint[$x] & "): ERROR" & $ERR)
                        EndIf
                        Sleep(20)
                    Next  
                EndIf
                $Status[$x] = "EXPIRED"
            EndIf
            FileDelete($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".txt")
            FileDelete($s_Environment[$x] & "\" & $s_Endpoint[$x] & ".crl")
        EndIf
    Next
    GUICtrlSetData($LastUpdate, "Last Updated:  " & _NowCalc())
    $line = 40
    ;******************************************************************************
    ;******************************************************************************
    GUICtrlSetData($RefreshButton, "Manual Refresh")
    GUICtrlSetState($RefreshButton, $GUI_ENABLE)
    GUICtrlSetData($RefreshProgress, 0)
    $Timer = TimerInit()
    $ProgressStep = ""
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $RefreshButton
                GUICtrlSetData($RefreshButton, "Updating")
                GUICtrlSetState($RefreshButton, $GUI_DISABLE)
                GUICtrlSetData($RefreshProgress, 0)
                $Timer = TimerInit()
                ExitLoop
            Case $msg = $GUI_EVENT_CLOSE
                GUIDelete()
                FileClose($FILE)
                FileClose($CertUtilLogs)
                FileClose($LOG)
                Exit
        EndSelect
        GUICtrlSetData($RefreshProgress, (TimerDiff($Timer) / $TimerValue * 100))
        If TimerDiff($Timer) > $TimerValue Then
            GUICtrlSetData($RefreshProgress, 0)
            $Timer = TimerInit()
            GUICtrlSetData($RefreshButton, "Updating")
            GUICtrlSetState($RefreshButton, $GUI_DISABLE)
            ExitLoop
        EndIf
    WEnd
WEnd
 
Func XPStyleToggle($OnOff = 1)
    If Not StringInStr(@OSTYPE, "WIN32_NT") Then Return 0
    If $OnOff Then
        $XS_n = DllCall("uxtheme.dll", "int", "GetThemeAppProperties")
        DllCall("uxtheme.dll", "none", "SetThemeAppProperties", "int", 0)
        Return 1
    ElseIf IsArray($XS_n) Then
        DllCall("uxtheme.dll", "none", "SetThemeAppProperties", "int", $XS_n[0])
        $XS_n = ""
        Return 1
    EndIf
    Return 0
EndFunc

config.ini file

;----------------------------------------------------------------------------------------------------------------
; This is the config file for the CRL Endpoint Check. Some settings have minimum values
;
; RefreshTimeMins has a minimum value of 2 and if set below will default to 2
; WarningThreshold has a minimum value of 20 and if set below will default to 20
;----------------------------------------------------------------------------------------------------------------
 
[MailSettings]
SMTPServer=***SMTP Server****
FromName=CRL Endpoint Check v3.2.2
FromAddress=***Email address to send from***
[Settings]
InputCSV=CRL_Check.csv
RefreshTimeMins=10
WarningThreshold=45
LogFileName=Log.txt

Sample CRL_Check.csv file

Environment,CRLAliasname,http://someaddress/crl.crl,someemail@somedomain.com;anotheremaill@someotherdomain.com

post-10196-0-15847300-1315977041_thumb.j

Edited by jussie
Link to comment
Share on other sites

It's looking very good.

One area that I would try to clean up is having a couple hundred lines of code hanging under the "else" of the "If $aData[4] <> 0 then" comparison. Something like that destroys readability and hampers maintainability. Seeing that has me looking for good candidates to make into separate logical functions to be moved down to the bottom of the program. I do see you have a couple chunks of code in there performing file I/O, those could possibly become Read_CRL_File() and Write_CRL_File(). I'd definately yank that date/time formatting code out of there as it is duplicated line-for-line. You could pull 50 lines out of the bloated "else" clause and create a single 25-line routine that would return your formatted date after being called with something like "FormattedDate = Format_Date_Time($Expiry_DetailsData)" and "FormattedDate1 = Format_Date_Time($Effective_dateData)"

Edited by Spiff59
Link to comment
Share on other sites

Thanks Spiff59,

I've not been good with tidy or efficient code but I do get my code working so I'll just have to keep trying to find better ways of doing it like your suggestions. When I code I look at it from top to bottom and loop if necessary but I have trouble when it comes to calling functions and going back and forward in the code. I understand it is the eficient way to do it but it confuses me a bit as to where I am at. I don't use it much now but I was quite good at batch files calling different parts in the code but I guess the complexity of all the available options in AutoIt coding comfuses me.

I'll try to find my way through creating functions like you suggest.

Thank you both for your insight into making my code work better. My coleagues are impressed with the tool as it can make us proactive instead if reactive with CRL issues.

As you say:

I've always found studying a working example to be the easiest way for me to learn.

Cheers

Jussie

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...