:: FlexNetTroubleshoot.bat
:: Should be on the Floating troubleshooting page on FMEPedia
:: Created by Ryan Cragg & Liyang Zhang
:: Edited by De Wet van Niekerk
:: Last edited on 2015-01-11

:: We turn echo off so that the screen doesn't show each command as it is run
	@ECHO OFF
	
:: This batch file must be run from the directory it is placed in
:: but if it is run in Admin mode, it will sometimes run from \windows\system32
:: We can set it to run from the current folder using pushd %~dp0

	pushd %~dp0

::We use REPORT_FILE to make it easier to rename the output at a later date.
	set REPORT_NAME=FlexNetReport.html
	set REPORT_FILE="%CD%\%REPORT_NAME%"
	del %REPORT_FILE%

:: cls clears the screen, just to keep things tidy.  
	cls
:: I set the color just to make it grab the users attention.
	color c0

:: Let's inform the customer about what is about to happen
	ECHO ***Important! ***
	echo.
	echo This batch file will edit your firewall settings.
	Echo.
	ECHO This batch file should be run using "Run as Administrator"
	echo.
	ECHO If you just double-clicked it, run it again by right-clicking on it
	ECHO and selecting "Run as Administrator"
	echo.
	ECHO Press CTRL-C to Quit, or
	pause

:: cls clears the screen, just to keep things tidy.  Change away from the warning colour
	cls
	color 0f

:: Let's reassure the customer that something is actuall happening
	echo Gathering Information and saving it into %REPORT_FILE%
	echo This may take several minutes...

	
:: Start the document with the html header
	call:htmlHeader

:: It might be handy to know what drive the customer ran the bath file from
	echo FlexNetTroubleshoot.bat was run from %CD% >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	
:: Now, let's get the system information
call:htmlSectionHeader sysinfo "System Information"

echo   Take note of:  >> %REPORT_FILE%
echo ^<ul^> >> %REPORT_FILE%
echo ^<li^>Volume ID (If changing over time, the Registration Key will change)  ^</li^> >> %REPORT_FILE%
echo ^<li^>OS Name  ^</li^> >> %REPORT_FILE%
echo ^<li^>OS Version  ^</li^> >> %REPORT_FILE%
echo ^<li^>System Model  ^</li^> >> %REPORT_FILE%
echo ^<li^>System Type  ^</li^> >> %REPORT_FILE%
echo ^<li^>Available Physical Memory^</li^>  >> %REPORT_FILE%
echo ^</ul^> >> %REPORT_FILE%
echo.  >> %REPORT_FILE%
	
	echo ^<pre^> >> %REPORT_FILE%

:: This command grabs the system information
:: It might not work on XP
	vol >> %REPORT_FILE%
	systeminfo  >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	
	echo ^</pre^> >> %REPORT_FILE%

call:htmlSectionFooter
	
call:htmlSectionHeader processes "Running Processes"
echo.  >> %REPORT_FILE%
echo Here is an edited list of what is running ^<br /^> >> %REPORT_FILE%
echo   Take note of:^<br /^>  >> %REPORT_FILE%
echo		^<ul^>^<li^>Anything with FME in the name^</li^>  >> %REPORT_FILE%
echo		^<li^>safe.exe^</li^>  >> %REPORT_FILE%
echo		^<li^>lmgrd.exe^</li^> >> %REPORT_FILE%
echo		^<li^>Executables with other company names.  Might be vendor deamons.  ^</li^>^</ul^> >> %REPORT_FILE%

	echo ^<pre^>  >> %REPORT_FILE%

	
:: Tell the customer what we are doing
	echo Scanning running tasks...
:: This grabs the shortened task list
	tasklist /FI "IMAGENAME ne System Idle Process" /FI "IMAGENAME ne System" /FI "IMAGENAME ne smss.exe" /FI "IMAGENAME ne csrss.exe" /FI "IMAGENAME ne wininit.exe" /FI "IMAGENAME ne winlogon.exe" /FI "IMAGENAME ne services.exe" /FI "IMAGENAME ne lsass.exe" /FI "IMAGENAME ne lsm.exe" /FI "IMAGENAME ne svchost.exe" /FI "IMAGENAME ne LogonUI.exe" /FI "IMAGENAME ne SLsvc.exe" /FI "IMAGENAME ne spoolsv.exe" /FI "IMAGENAME ne taskeng.exe" /FI "IMAGENAME ne logon.scr" /FI "IMAGENAME ne winlogon.exe" /FI "IMAGENAME ne explorer.exe" /FI "IMAGENAME ne tasklist.exe" >> %REPORT_FILE%
	echo ^</pre^> >> %REPORT_FILE%
	
	call:htmlSectionFooter

	call:htmlSectionHeader envvar "Environment Variables"

:: Next, let's check the path, and all other environment variables:

:: Tell the customer what we are doing
	echo Saving Environment Variables...

:: Check to see if LM_LICENSE_FILE or SAFE_LICENSE_FILE exist.
:: They shouldn't.  Note the !'s used in the IF statement.
	echo ^<pre^> >> %REPORT_FILE%
:: Tell the customer what we are doing
	echo Scanning Environment Variables...
:: This command grabs all of the environment variables
	set >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	echo ^</pre^> >> %REPORT_FILE%
	
	call:htmlSectionFooter
	

	call:htmlSectionHeader registry Registry
echo Here are the contents of the Registry. ^<br /^>  >> %REPORT_FILE%
echo Take Note of: ^<br /^>  >> %REPORT_FILE%
echo ^<ul^>^<li^>This is where the service is^</li^> >> %REPORT_FILE%
echo ^<li^>This is where it is installed^</li^>  >> %REPORT_FILE%
echo ^<li^>If this is blank, the license service is not installed^</li^>^</ul^>  >> %REPORT_FILE%
echo.  >> %REPORT_FILE%
:: Tell the customer what we are doing
	echo Scanning the Registry...

	echo ^<pre^> >> %REPORT_FILE%
	
:: These commands grab the Registry information
	reg query "HKLM\SYSTEM\CurrentControlSet\Services\FME License Server" >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	reg query "hklm\software\wow6432node\flexlm license manager\FME License Server" >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	reg query "hklm\software\flexlm license manager\FME License Server" >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	
:: OK, this part is tricky.  We are going to find the location of LMGRD.EXE used by the FME License Server Service.
:: First, we grab the entire Registry Key that has the location
:: The pipe has to be escaped with a ^ because it in the the FOR loop.


::find lmgrd.exe
FOR /f "tokens=*" %%A IN ('reg query "hklm\software\flexlm license manager\FME License Server" ^| findstr lmgrd.exe') do set FME_LMGRD_PATH=%%A
FOR /f "tokens=*" %%A IN ('reg query "hklm\software\wow6432node\flexlm license manager\FME License Server" ^| findstr lmgrd.exe') do set FME_LMGRD_PATH=%FME_LMGRD_PATH%%%A

:: We have to remove ImagePath, REG_EXPAND_SZ, lmgrd.exe and the " from FME_LMGRD_PATH
set FME_LMGRD_PATH=%FME_LMGRD_PATH:lmgrd=%
set FME_LMGRD_PATH=%FME_LMGRD_PATH:REG_SZ=%
set FME_LMGRD_PATH=%FME_LMGRD_PATH:.exe=%
set FME_LMGRD_FOLDER=%FME_LMGRD_PATH:~8%
set FME_LMUTIL_PATH=%FME_LMGRD_PATH:~8%lmutil.exe
set FME_SAFE_PATH=%FME_LMGRD_PATH:~8%safe.exe
set FME_OPT_PATH=%FME_LMGRD_PATH:~8%safe.opt
set FME_LMGRD_PATH=%FME_LMGRD_PATH:~8%lmgrd.exe

::find safe.lic

FOR /f "tokens=*" %%A IN ('reg query "hklm\software\flexlm license manager\FME License Server" ^| findstr safe.lic') do set SAFE_LIC_PATH=%%A
FOR /f "tokens=*" %%A IN ('reg query "hklm\software\wow6432node\flexlm license manager\FME License Server" ^| findstr safe.lic') do set SAFE_LIC_PATH=%SAFE_LIC_PATH%%%A

set SAFE_LIC_PATH=%SAFE_LIC_PATH:license=%
set SAFE_LIC_PATH=%SAFE_LIC_PATH:REG_SZ=%
set SAFE_LIC_PATH=%SAFE_LIC_PATH:~8%

FOR /f "tokens=*" %%A IN ('reg query "hklm\software\flexlm license manager\FME License Server" ^| findstr safe.log') do set SAFE_LOG_PATH=%%A
FOR /f "tokens=*" %%A IN ('reg query "hklm\software\wow6432node\flexlm license manager\FME License Server" ^| findstr safe.log') do set SAFE_LOG_PATH=%SAFE_LOG_PATH%%%A


set SAFE_LOG_PATH=%SAFE_LOG_PATH:LMGRD_LOG_FILE=%
set SAFE_LOG_PATH=%SAFE_LOG_PATH:REG_SZ=%
set SAFE_LOG_PATH=%SAFE_LOG_PATH:~8%

:: Let's print LMGRD_PATH to the report, to make sure we captured it correctly
	echo The location of the FME License Server installation is:  >> %REPORT_FILE%
	echo  %FME_LMGRD_PATH%  >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%

:: When referring to the working folder, use %CD% because %FME_LMGRD_PATH% has leading spaces
:: This doesn't cause PUSHD any problems, but can break other commands.
	
	echo ^</pre^> >> %REPORT_FILE%
	
	call:htmlSectionFooter
	
	call:htmlSectionHeader firewall "Firewall Exceptions"


echo Here are the Firewall Exceptions. ^<br /^>  >> %REPORT_FILE%
echo What to Know: ^<br /^> >> %REPORT_FILE%
echo		^<ul^>^<li^>Safe.exe and lmgrd.exe should be listed.  >> %REPORT_FILE%
echo 		^<li^>Keep in mind that if they are running this on an Amazon EC2 >> %REPORT_FILE%
echo 		instance, they also have to open ports in the security groups.^</li^>^</ul^> >> %REPORT_FILE%

:: Tell the customer what we are doing
	echo Querying the Firewall...
	echo ^<pre^> >> %REPORT_FILE%
:: This command lists the allowed program exceptions for the firewall
	netsh firewall show allowedprogram >> %REPORT_FILE%
	
:: This part adds safe.exe and lmgrd.exe. Disabled because of PR60611	
::	echo. >> %REPORT_FILE%
::	echo Adding %FME_SAFE_PATH% to Firewall >> %REPORT_FILE%
::	netsh firewall set allowedprogram "%FME_SAFE_PATH%" "safe.exe" >>%REPORT_FILE%
::
::	echo Adding %FME_LMGRD_PATH% to Firewall >> %REPORT_FILE%
::	netsh firewall set allowedprogram "%FME_LMGRD_PATH%" "lmgrd.exe" >>%REPORT_FILE%
	echo.  >> %REPORT_FILE%

	echo ^</pre^> >> %REPORT_FILE%

	call:htmlSectionFooter
	call:htmlSectionHeader fsfold "FlexServer folder"
echo Here are the contents of the FlexServer folder  >> %REPORT_FILE%
echo 	What to know:  >> %REPORT_FILE%
echo 		^<ul^>^<li^>SAFE.EXE is the Vendor Daemon.  >> %REPORT_FILE%
echo 		It should be in the same folder as LMGRD.EXE and SAFE.LIC.^</li^>  >> %REPORT_FILE%
echo 		^<li^>SAFE.EXE and LMGRD.EXE have been added to the Firewall.^</li^>^</ul^> >> %REPORT_FILE%


:: Tell the customer what we are doing
	echo Scanning FlexNet configuration...
:: This command will save the file names to the report

	echo ^<pre^> >> %REPORT_FILE%
	dir "%FME_LMGRD_FOLDER%" /b >>  %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	
echo ***************************************************************************  	>> %REPORT_FILE%
echo Now we check the Versions of Safe.exe and lmgrd.exe							>> %REPORT_FILE%
echo The versions must be 11.8 or higher											>> %REPORT_FILE%
echo ***************************************************************************  	>> %REPORT_FILE%
:: Let's LMUTIL everything to see what is available or running
	echo. >> %REPORT_FILE%
	echo safe.exe >> %REPORT_FILE%
	"%FME_LMUTIL_PATH%" lmver "%FME_SAFE_PATH%" >> %REPORT_FILE%
	echo lmgrd.exe >> %REPORT_FILE%
	"%FME_LMUTIL_PATH%" lmver "%FME_LMGRD_PATH%" >> %REPORT_FILE%
	ECHO. >> %REPORT_FILE%
	
	echo ^</pre^> >> %REPORT_FILE%

	call:htmlSectionFooter
	call:htmlSectionHeader host "safe.lic Comparison"

echo Here is the HOSTNAME and HOSTID.												>> %REPORT_FILE%
echo These should be in the safe.lic file.											>> %REPORT_FILE%
echo The license file must contain the hostname or "localhost", 					>> %REPORT_FILE%
echo and just one of the hostids													>> %REPORT_FILE%

	
	echo ^<pre^> >> %REPORT_FILE%
	echo The hostname is: 															>> %REPORT_FILE%
	"%FME_LMUTIL_PATH%" lmhostid -hostname >> %REPORT_FILE%
	echo. 																			>> %REPORT_FILE%
	echo The hostid(s) is: 															>> %REPORT_FILE%
	"%FME_LMUTIL_PATH%" lmhostid >> %REPORT_FILE%
	echo. 																			>> %REPORT_FILE%


:: Add the contents of safe.lic to the report
	echo.  >> %REPORT_FILE%
	echo The contents of >> %REPORT_FILE%
	echo safe.lic >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	type "%SAFE_LIC_PATH%" >> %REPORT_FILE% 
	echo.  >> %REPORT_FILE%

:: Add the contents of the safe.opt file to the report	
	echo. >> %REPORT_FILE%
	echo ***OPTIONS FILE*** >> %REPORT_FILE%
	echo The safe.opt file is optional. >> %REPORT_FILE%
	echo It might not exist >> %REPORT_FILE%
	echo Watch for groupings, exclusions, and keep in mind that it is case sensitive. >> %REPORT_FILE%
	echo. >> %REPORT_FILE%
	echo.
	echo The optional options file may be below: >> %REPORT_FILE%
	type "%FME_OPT_PATH%" >> %REPORT_FILE%
	echo ^</pre^> >> %REPORT_FILE%
	
	call:htmlSectionFooter
	call:htmlSectionHeader log "Log File, pre-restart"
	
echo Here is the existing log file ^<br /^>											>> %REPORT_FILE%
echo 	What to watch for: ^<br /^>													>> %REPORT_FILE%
echo 		^<ul^>^<li^>Any error messages^</li^>									>> %REPORT_FILE%
echo 		^<li^>Any warnings^</li^>^</ul^>										>> %REPORT_FILE%

	echo ^<pre^> >> %REPORT_FILE%
:: Add the initial contents of safe.log to the report
	echo.  >> %REPORT_FILE%
	echo The initial contents of >> %REPORT_FILE%
	echo safe.log >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	type "%SAFE_LOG_PATH%" >> %REPORT_FILE% 
	echo. >> %REPORT_FILE%
	echo ^</pre^> >> %REPORT_FILE%

	call:htmlSectionFooter
	call:htmlSectionHeader restart "License Server Restart"
	cls
	
echo Now we Stop/Restart the license server service.							>> %REPORT_FILE%
echo If the service is not starting, it might be named or installed incorrectly. >> %REPORT_FILE%

	echo ^<pre^> >> %REPORT_FILE%
:: Now we will Stop/Restart the FME License Server Service	
:: Tell the customer what we are doing
	echo Stopping and Restarting the FME License Server Service...
	ECHO. >> %REPORT_FILE%
	ECHO Attempting to stop FME License Server... >> %REPORT_FILE%
	net stop "FME License Server" >> %REPORT_FILE%
	ECHO Attempting to start FME License Server... >> %REPORT_FILE%
	net start "FME License Server" >> %REPORT_FILE%
	echo.  >> %REPORT_FILE%
	echo.   >> %REPORT_FILE%
	
	echo ^</pre^> >> %REPORT_FILE%
	
	call:htmlSectionFooter
	call:htmlSectionHeader lognew "New Log File"
	

echo Here is the new log file.^<br /^>											>> %REPORT_FILE%
echo 	What to watch for:^<br /^>												>> %REPORT_FILE%
echo 		^<ul^>^<li^>Any error messages^</li^>								>> %REPORT_FILE%
echo 		^<li^>Any warnings^</li^>											>> %REPORT_FILE%
echo 		^<li^>Any hostnames^</li^>											>> %REPORT_FILE%
echo 		^<li^>Any hostids^</li^>											>> %REPORT_FILE%
echo 		^<li^>Any port numbers^</li^>^</ul^>								>> %REPORT_FILE%
	echo ^<pre^> >> %REPORT_FILE%
	echo. >> %REPORT_FILE%
	type "%SAFE_LOG_PATH%" >> %REPORT_FILE% 
	echo ^</pre^> >> %REPORT_FILE%

	:: We should indicate that the report has ended
	echo. >> %REPORT_FILE%
	echo *** End of Report File ***  >> %REPORT_FILE%
	
:: Let's copy the report to the Desktop, to make the customer's life a little easier
	copy %REPORT_FILE% "%USERPROFILE%\Desktop\"

:: Set the color of the screen to something nice
	cls
	color 0a

:: Some final instructions for the customer
	Echo Please email %REPORT_FILE% to support@safe.com
	echo.
	echo A copy of %REPORT_NAME% is on your Desktop
	echo.
	echo Thank You!
	pause
	
	color 0f
	
	goto:eof
	
::::::: Functions here :::::::	
	
:htmlHeader
echo ^<^!doctype html^> > %REPORT_FILE%
echo ^<html^>^<head^> >> %REPORT_FILE%
echo ^<title^>FlexNet Troubleshooting Report^</title^> >> %REPORT_FILE%
echo ^</head^> >> %REPORT_FILE%
echo ^<body^> >> %REPORT_FILE%
echo ^<h1^>FlexNet Troubleshooting Report^</h1^> >> %REPORT_FILE%
echo ^<h2^>Contents^</h2^> >> %REPORT_FILE%
echo ^<ul^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#sysinfo"^>System Information^</a^>^</li^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#processes"^>Running Processes^</a^>^</li^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#envvar"^>Environment Variables^</a^>^</li^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#registry"^>Registry^</a^>^</li^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#firewall"^>Firewall Exceptions^</a^>^</li^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#fsfold"^>FlexServer Folder^</a^>^</li^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#host"^>safe.lic comparison^</a^>^</li^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#log"^>Log file, pre-restart^</a^>^</li^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#restart"^>License server restart^</a^>^</li^> >> %REPORT_FILE%
echo ^<li^> ^<a href="#lognew"^>New log file^</a^>^</li^> >> %REPORT_FILE%
echo ^</ul^> >> %REPORT_FILE%
echo ^<hr /^> >> %REPORT_FILE%

goto:eof

:htmlFooter
echo End of report. >> %REPORT_FILE%
echo ^</body^>^</html^> >> %REPORT_FILE%
goto:eof

:htmlSectionHeader
echo ^<a id="%~1"^> >> %REPORT_FILE%
echo ^<h2^>%~2^</h2^> >> %REPORT_FILE%
echo ^</a^> >> %REPORT_FILE%
goto:eof

:htmlSectionFooter
echo ^<a href="#top"^>Back to top^</a^> >> %REPORT_FILE%
echo ^<hr /^> >> %REPORT_FILE%
goto:eof