Ever want to dip your toes into the code pool but were just too intimidated? Here's your chance. The code below is fairly simple. All it really does, when placed within the body of a Windows Script Monitor, is tell you the number of days until your SSL Certificate will expire. Read through the code. Everything after the apostrophe (') is a user comment that explains that section of the code. Hopefully, this will give you a little more confidence when editing scripts.

BTW: SAM has an SSL Expiration Date Monitor which can be found here. The following example demonstrates a typical use case scenario using a Windows Script Monitor utilizing VB script.

Warning: The code below is user-submitted. Use at your own risk. SolarWinds is not responsible for the impact this code may have on your system. Exercise caution when executing any code with which you are not familiar.

Note: This is merely a demonstration of how you can use VB script with the Windows Script Monitor.


In order to calculate the number of days until an SSL certificate expires, a vbscript can be implemented with the Windows Script Monitor. This script relies on OpenSSL being installed on your SolarWinds server to check for the expiration date. The script below accepts one argument in the form of a URL, with the socket number, and returns the statistic in the number of days remaining until expiration. Before using this script you need to install OpenSSL, which is freely available on the internet. Ensure the path given below has the OpenSSL plug-in installed or change the path as per your OpenSSL plug-in. For each instance of the monitor, change the text file that the monitor writes to, (e.g.: cer1.txt, cer2.txt), otherwise the monitors may return a value of, "Unknown."

 

Installation: You will need to pass the argument site name plus the port number.

Example: www.google.co.uk:443

 

Simply copy and paste the code below into your Windows Script Monitor.


Code:

Option Explicit

Dim oShell
Dim ArgObj
Dim Fh
Dim FSO
Dim Line
Dim Expiry
Dim DaysLeft
Dim ExpDate
Dim oExec
Dim cert
Dim tstream
Dim i
Dim str
Dim PluginPath
Dim CertFilePath
Dim char
Dim XString

Const ForReading = 1

CertFilePath = "C:\TMP\cert99.txt"
PluginPath = "C:\Program Files\Solarwinds\Common\OpenSSL\bin\"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set tstream = FSO.createtextfile(CertFilePath, True)

ArgObj = wscript.Arguments(0)

Set oShell = wscript.CreateObject("WScript.Shell")

'Calling funtion to retieve the host
Set oExec = oShell.exec(PluginPath & "openssl.exe s_client -connect " & ArgObj)

'Write the certificate into a text file
Do Until oExec.StdOut.AtEndOfStream
    char = oExec.StdOut.Read(1)
    tstream.Write (char)

    If Len(XString) < 20 Then
XString = XString & char
    ElseIf XString = "END CERTIFICATE-----" Then

Exit Do

    Else
XString = Mid(XString, 2, 19)
XString = XString & char
    End If

Loop

tstream.Close
oExec.Terminate

'Read SSL certificate for start date and end date
Set oExec = oShell.exec(PluginPath & "openssl.exe x509 -noout -in " & CertFilePath & " -dates")

'Return start date and end date of the certificate
Line = oExec.StdOut.Readall

'Get the expiration date of the certificate
Expiry = (Mid(Line, Len(Line) - 24, 24))
ExpDate = ConvertDate(Expiry)

'Calculating number of days
DaysLeft = DateDiff("d", Now(), ExpDate)

'Display the number of days remaining until expiration
wscript.echo "Statistic: " & DaysLeft
wscript.echo "Message: Number of days remaning of expiry for SSL certificate are " & DaysLeft

'Object Closing code
oExec.Terminate
Set oShell = Nothing
Set oExec = Nothing

'This function converts the date into the required format
Function ConvertDate(DateStr)

Dim Components
Dim Month
Dim Day
Dim Year

Components = Split(DateStr)

Select Case Components(0)
    Case "Jan", "January"
    Month = 1

    Case "Feb", "February"
    Month = 2

    Case "Mar", "March"
    Month = 3

    Case "Apr", "April"
    Month = 4

    Case "May"
    Month = 5

    Case "Jun", "June"
    Month = 6

    Case "Jul", "July"
    Month = 7

    Case "Aug", "August"
    Month = 8

    Case "Sep", "Sept", "September"
    Month = 9

    Case "Oct", "October"
    Month = 10

    Case "Nov", "November"
    Month = 11

    Case "Dec", "December"
    Month = 12

Case Else

    Month = 1

End Select

    Day = Components(1)
    Year = Components(3)

If UBound(Components) = 5 Then
    Day = Components(2)
    Year = Components(4)
Else
    Day = Components(1)
    Year = Components(3)
End If

ConvertDate = CDate(Day & "/" & Month & "/" & Year)

End Function