Community Effort - Clean Old Sites from BES Relays

Firstly, I’d like to thank @steve for creating the original content on BigFix.me for this problem
[ORIGINAL WINDOWS FIXLET]

I’d also like to thank @strawgate for his very helpful guide on this subject too

Now to the point, I’m no expert, there’s probably stuff I’ve missed and yeah I love exit codes :smiley:

I’ve updated the content as there doesn’t seem to be any Unix version of this out there (which is a real pain) however I’m sure there are probably better ways of doing this so I’m putting it on here before adding it to bigfix.me to get a community effort going in making this better and more efficient (cause I know there will be ways).

Relevance will be whether or not the BESRelay is installed.

if {windows of operating system}

// restart the BES Relay first
waithidden cmd.exe /C net stop besrelay
if {exit code of action = 0}
else exit 12
endif

// set starting time
setting "_BESClient_PauseWhile"="{now as string}" on "{parameter "action issue date" of action}" for client

// PAUSE until the relay service stops or 3 minutes has passed.
pause while {(exists running application whose (name of it as lowercase = "besrelay.exe") OR exists running service whose (service name of it as lowercase = "besrelay")) AND (now - (value of setting "_BESClient_PauseWhile" of client as string as time)) < 3*minute}

delete "{value "InboxLocation" of key "HKLM\SOFTWARE\BigFix\Enterprise Server\GatherService" of registry}\GatherState.xml.bak"
if {exit code of action = 0}
else exit 13
endif

move "{value "InboxLocation" of key "HKLM\SOFTWARE\BigFix\Enterprise Server\GatherService" of registry}\GatherState.xml" "{value "InboxLocation" of key "HKLM\SOFTWARE\BigFix\Enterprise Server\GatherService" of registry}\GatherState.xml.bak"
if {exit code of action = 0}
else exit 14
endif

delete "{value "InboxLocation" of key "HKLM\SOFTWARE\BigFix\Enterprise Server\GatherService" of registry}\bfemapfile.xml.bak"

if {exit code of action = 0}
else exit 15
endif

move "{value "InboxLocation" of key "HKLM\SOFTWARE\BigFix\Enterprise Server\GatherService" of registry}\bfemapfile.xml" "{value "InboxLocation" of key "HKLM\SOFTWARE\BigFix\Enterprise Server\GatherService" of registry}\bfemapfile.xml.bak"
if {exit code of action = 0}
else exit 16
endif

waithidden cmd.exe /C rmdir "{value "ContentLocation" of key "HKLM\SOFTWARE\BigFix\Enterprise Server\GatherService" of registry}" /S /Q
if {exit code of action = 0}
else exit 17
endif

// set starting time
setting "_BESClient_PauseWhile"="{now as string}" on "{parameter "action issue date" of action}" for client

// PAUSE until the bfsites directory is gone or 5 minutes has passed.
pause while {(exists folder (value "ContentLocation" of key "HKLM\SOFTWARE\BigFix\Enterprise Server\GatherService" of registry as string)) AND (now - (value of setting "_BESClient_PauseWhile" of client as string as time)) < 5*minute}

waithidden cmd.exe /C mkdir "{value "ContentLocation" of key "HKLM\SOFTWARE\BigFix\Enterprise Server\GatherService" of registry as string}"
if {exit code of action = 0}
else exit 18
endif

waithidden cmd.exe /C net start besrelay
if {exit code of action = 0}
else exit 19
endif

// if for some reason the BES Relay hasn't started yet, fail here
if{exists running service "BESRelay"}
else exit 20
endif

// then restart the BES Client
delete __appendfile
appendfile @echo off
appendfile echo Your administrator is restarting the BES Client... Please Wait...
appendfile net stop BESClient
appendfile net start BESClient

delete clientRestart.bat
if {exit code of action = 0}
else exit 21
endif

copy __appendfile clientRestart.bat
if {exit code of action = 0}
else exit 22
endif

runhidden cmd.exe /C clientRestart.bat
if {exit code of action = 0}
else exit 23
endif

elseif {unix of operating system}

//Stop the Relay Service Running 
waithidden service besrelay stop
if {exit code of action = 0}
else exit 1
endif

// allow time for relay to stop
parameter "startTime"="{now}"
pause while { (now-time(parameter "startTime") < 180*second) }

// Create a backup of the sites folder, just in case you need it
waithidden cp -R "/var/opt/BESRelay/wwwrootbes/bfmirror/bfsites" "/var/opt/BESRelay/wwwrootbes/bfmirror/bfsites_bak"
if {exit code of action = 0}
else exit 2
endif

//Allow time to copy the folder and files before deleting
parameter "startTime2"="{now}"
pause while { (now-time(parameter "startTime2") < 300*second) }

waithidden rm -rf "/var/opt/BESRelay/wwwrootbes/bfmirror/bfsites/*"
if {exit code of action = 0}
else exit 3
endif

//Create a backup of the files
waithidden mv "/var/opt/BESRelay/Mirror Server/inbox/bfemapfile.xml" "/var/opt/BESRelay/Mirror Server/inbox/bfemapfile.xml.bak"
if {exit code of action = 0}
else exit 4
endif

waithidden mv "/var/opt/BESRelay/Mirror Server/inbox/GatherState.xml" "/var/opt/BESRelay/Mirror Server/inbox/GatherState.xml.bak"
if {exit code of action = 0}
else exit 5
endif

//Remove the original files
waithidden rm "/var/opt/BESRelay/Mirror Server/inbox/bfemapfile.xml"
if {exit code of action = 0}
else exit 6
endif

waithidden rm "/var/opt/BESRelay/Mirror Server/inbox/GatherState.xml"
if {exit code of action = 0}
else exit 7
endif

//Create new GatherState file
createfile until end
<?xml version= "1.0"?>
<GatherStatus> </GatherStatus>
end
delete "/var/opt/BESRelay/Mirror Server/inbox/GatherState.xml"
if {exit code of action = 0}
else exit 8
endif

copy __createfile "/var/opt/BESRelay/Mirror Server/inbox/GatherState.xml"
if {exit code of action = 0}
else exit 9
endif

//Create new BFE Map file
createfile until end
<?xml version="1.0"?>
<URLtoID NextID="1"> </URLtoID>
end
delete "/var/opt/BESRelay/Mirror Server/inbox/bfemapfile.xml"
if {exit code of action = 0}
else exit 10
endif

copy __createfile "/var/opt/BESRelay/Mirror Server/inbox/bfemapfile.xml"
if {exit code of action = 0}
else exit 11
endif

//Start the BESRelay service 
waithidden service besrelay stop
if {exit code of action = 0}
else exit 24
endif

endif
5 Likes

I’ve posted my version at https://bigfix.me/fixlet/details/26858

It does a bit more to try to handle locked directories, but doesn’t explicitly create the XML with a new SiteID “1”. In my experience I haven’t had to do that yet, but please do test and let me know if that needs to be added.

It should also handle AIX Relays, but I don’t actually have one of those on which to test right now.

6 Likes

I am not nearly at the relevance level of you guys, but perfectly happy to test any solutions.
This has been needed functionality in Bigfix for as long as I have been a user and has essentially been ignored.

4 Likes

Just tried this on an RHEL 7 box and got this :frowning:

Command succeeded parameter “timestamp”=“20210713104846” (action:6873774)
Command failed (Relevance substitution failed) parameter “gatherstate”="{pathname of file “gatherstate.xml” of folders “Mirror Server\Inbox” of parent folders of folders (value of setting “_BESRelay_HTTPServer_ServerRootPath” of client as string)}" (action:6873774)

It needs to be updated to

parameter “timestamp”="{(year of item 0 of it as string & month of item 0 of it as two digits & day_of_month of item 0 of it as two digits & two digit hour of item 1 of it & two digit minute of item 1 of it & two digit second of item 1 of it) of (date (local time zone) of it, time (local time zone) of it) of now}"

parameter “gatherstate”="{pathname of file “GatherState.xml” of folders “Mirror Server/inbox” of parent folders of folders (value of setting “_BESRelay_HTTPServer_ServerRootPath” of client as string)}"

parameter “bfemapfile”="{pathname of file “bfemapfile.xml” of folders “Mirror Server/inbox” of parent folders of folders (value of setting “_BESRelay_HTTPServer_ServerRootPath” of client as string)}"

parameter “bfsites”="{pathname of folder “bfmirror/bfsites” of folders (value of setting “_BESRelay_HTTPServer_ServerRootPath” of client as string)}"

Thank you! I’ll update later today

1 Like