Sign in to follow this  
Followers 0
nfswitch

FlexLM Service Status Checker

1 post in this topic

#1 ·  Posted (edited)

I have started work creating a bunch of scripts I call NetworkSC which can be used to check the status of services on your network. I recently discovered a need for a status checker for a network licence manager we are using called FlexLM running on Windows. It is used a lot for CAD software.

It comes with a program (lmstat) that will give you the status of the server, but it does not create alerts when the server fails, so that is what my software does. It checks the status and will send you an email if the server is down then try to restart it for you. Just set it as a scheduled task to run every 25 mins or so.

Originally I was checking the exit code of lmstat, but this is not foolproof so I modified it to take the STDOUT of lmstat and if it does not say the server is up, the check fails.

There are two files you will need in this post - the NetworkSC itself (you'll need to compile), and an ini file with your email settings. You will also need the lmstat binary you can find with your FlexLM installation and put all 3 into the same folder to run. You will need to modify some hardcoded names in the script (marked as **EDIT**) or you can add them into the $cmdline array which Im not personally concerned about. You will need to place your mail settings into nscmail.ini.

I hope you find it useful!

; NETWORK SERVICE CHECKER


#include <Inet.au3>
#include <Constants.au3>

; MAIN ===============================================================
If Not $CmdLineRaw Then
MsgBox(64, "NetworkSC for FlexLM v1.1", "This program requires a port and IP address of a FlexLM server as a command line argument." & @CRLF & @CRLF & "You must supply it in the form of: program.exe IP port." & @CRLF & "EG - NetworkSC.exe 10.10.10.10 27000" & @CRLF & @CRLF & "lmstat.exe from the FlexLM server package must also be supplied in the same directory as this program.",15)
Exit
EndIf

; DECLARATIONS
Global $flexip = $cmdLine[1]
Global $flexpo = $cmdLine[2]
Global $logtext, $lmres, $testres = 0
Local $logf = @ScriptDir & "\runlog.log", $lmcmd = @ComSpec & " /c """ & @ScriptDir & "\lmstat.exe"" -c " & $flexpo & "@" & $flexip

Call("TestFlex")

If $testres = 0 Then
Local $pingres = Ping($flexip)
If $pingres == 0 Then
; SERVER IS OFFLINE, LOG IT AND EXIT
Call("addlog", "Finished check", "FlexLM SERVER PING FAILED")
Exit
Else
; SEND THE NOTIFICATION, TRY TO RESTART THE SERVICE, WAIT 30 SECS, THEN TEST AGAIN
Call("Servalert", "FlexLM on "&$flexip&" - FAILED", $lmres, $flexip)
Call("addlog", "FAILURE", "FlexLM Service - Sending STOP")
RunWait("sc.exe \\" & $flexip & " stop ""**EDIT** ADD WINDOWS SERVICE NAME""", "", @SW_HIDE)
Call("addlog", "FAILURE", "FlexLM Service - Sending START")
RunWait("sc.exe \\" & $flexip & " start ""**EDIT** ADD WINDOWS SERVICE NAME""", "", @SW_HIDE)
Sleep(30000)
Call("TestFlex")
; LOOP - IF THE SERVICE IS STILL FAILED, THE LOOP WILL ENTER AND KEEP RETESTING
While $testres = 0
Sleep(600000)
Call("addlog", "Starting check from failure loop", "")
; CHECK AGAIN, SUCCESS WILL EXIT LOOP OTHERWISE IT WILL WAIT 10 MINUTES
Call("TestFlex")
WEnd
Call("Servalert", "FlexLM on "&$flexip&" - RECOVERED", $lmres, $flexip)
Call("addlog", "RECOVERED", "Exiting failure loop due to recovery")
EndIf
EndIf

; SERVICE IS RUNNING, WRITE LOG AND EXIT
Call("addlog", "PASSED", "")
; END MAIN ============================================================================================

; FUNCTIONS ===========================================================================================
Func TestFlex()
$lmres = ""
Local $lmex = Run($lmcmd, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
While 1
$lmres &= StdoutRead($lmex)
If @error Then ExitLoop
WEnd
$testres = StringInStr($lmres, "**EDIT** ADD SERVER UP TEXT FROM LMSTAT (HINT: RUN WITH THE MSGBOX LINE UNCOMMENTED TO SEE THE LMSTAT TEXT)")

; UNCOMMENT THIS LINE FOR DEBUG
;MsgBox(0, "", "Received: " & @CRLF & $testres & @CRLF & $lmres)
EndFunc ;==>TestFlex

Func addlog($status, $text)
$logtext = $flexip & " " & @MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & ": " & $status & @CRLF
If $text <> "" Then
$logtext &= $text & @CRLF
EndIf
$logtext &= "----------------------------" & @CRLF
FileWrite($logf, $logtext)
EndFunc ;==>addlog

Func Servalert($fsname, $errdes, $testedip)
Local $mserdet = IniReadSection(@ScriptDir & "\nscmail.ini", "ServerDetails")
Local $fmdet = IniReadSection(@ScriptDir & "\nscmail.ini", "FailMail")
Local $s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body[4]
For $i = 1 To $mserdet[0][0]
Switch $mserdet[$i][0]
Case "EmServer"
$s_SmtpServer = $mserdet[$i][1]
Case "FromName"
$s_FromName = $mserdet[$i][1]
Case "FromAddress"
$s_FromAddress = $mserdet[$i][1]
Case "ToAddress"
$s_ToAddress = $mserdet[$i][1]
EndSwitch
Next
For $i = 1 To $fmdet[0][0]
Switch $fmdet[$i][0]
Case "sSubject"
$s_Subject = $fmdet[$i][1] & " - " & $fsname
Case "asBody"
$as_Body[0] = $fmdet[$i][1]
EndSwitch
Next
$as_Body[1] = $fsname
$as_Body[2] = "LMstat Result:" & @CRLF & $errdes
$as_Body[3] = "IP tested was " & $testedip
$Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body)
EndFunc ;==>Servalert

nscmail.ini

[NetworkSC Mail Settings]

[ServerDetails]
EmServer=10.10.10.10
FromName=NetworkSC
FromAddress=NetworkSC@yourdomain.com.au
ToAddress=youraddress@yourdomain.com.au

[FailMail]
sSubject=Service
asBody=Service details:
Edited by nfswitch

Share this post


Link to post
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
Sign in to follow this  
Followers 0