Skip to content

Commit 59c2c3e

Browse files
Merge pull request #4 from jimmyatSplunk/script-updates
Script updates
2 parents dd77cdc + 3df524a commit 59c2c3e

13 files changed

+143
-52
lines changed

README/inputs.conf.spec

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Configurations to pass btool checks without throwing errors
2+
3+
[script:<uniqueName>]
4+
deploymentServerUri = [string]
5+
* Correct URI that should be configured
6+
7+
deploymentClientApp = [string]
8+
* App name that contains the correct deploymentclient.conf configuration
9+
10+
[powershell:<uniqueName>]
11+
deploymentServerUri = [string]
12+
* Correct URI that should be configured with port
13+
14+
deploymentClientApp = [string]
15+
* App name that contains the correct deploymentclient.conf configuration

bin/appContext.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22
## Grab the current app context
33
SCRIPT_PATH=$(realpath $0)
44
APP_PATH=$(dirname ${SCRIPT_PATH})
5+
SCRIPT_NAME=$(echo ${SCRIPT_PATH} | sed "s|${APP_PATH}/||")
6+
APP_NAME=$(echo ${SCRIPT_PATH} | sed "s|${SPLUNK_HOME}/etc/apps" | sed "s|/${SCRIPT_NAME}||")
7+
8+
## Capture the deploymentServerUri from the inputs stanza
9+
if [ "${SCRIPT_NAME}" = "dsRemove.sh" ]; then
10+
CORRECT_DS=$(${SPLUNK_HOME}/bin/splunk btool --app=$APP_NAME inputs list script://./bin/$SCRIPT_NAME | grep deploymentServerUri | sed "s/deploymentServerUri = //")
11+
CORRECT_APP=$(${SPLUNK_HOME}/bin/splunk btool --app=$APP_NAME inputs list script://./bin/$SCRIPT_NAME | grep deploymentClientApp | sed "s/deploymentClientApp = //")
12+
fi

bin/dateTimeCorrect.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $referenceDateTime = "$SPLUNKHOME\apps\SplunkForwarderRepairKit\datetime.xml"
44
$restartDateTimeCheck = "$SPLUNKHOME\etc\restartdatetime.txt"
55

66
### Filter to attach timestamps where necessary
7-
filter timestamp {"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff') ${env:COMPUTERNAME}: $_"}
7+
filter timestamp {"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff zzz') ${env:COMPUTERNAME}: $_"}
88

99
### Check flags and take appropriate actions for host name
1010
if(Compare-Object -ReferenceObject $(Get-Content $existingDateTime) -DifferenceObject $(Get-Content $referenceDateTime)) {

bin/dateTimeCorrect.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ RESTART_DATETIME_CHECK="$SPLUNK_HOME/etc/restartdatetime.txt"
77

88
### Determine if a correction is necessary
99
if [ $DATETIME_DIFFERENCE = 0 ]; then
10-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: The datetime.xml is the updated version. No correction necessary..."
10+
echo "$(date -R) ${HOSTNAME}: The datetime.xml is the updated version. No correction necessary..."
1111
else
12-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: The datetime.xml file needs to be updated. Updating..."
12+
echo "$(date -R) ${HOSTNAME}: The datetime.xml file needs to be updated. Updating..."
1313
mv $EXISTING_DATETIME $EXISTING_DATETIME.$(date +"%m%d%Y")
1414
cp $REFERENCE_DATETIME $EXISTING_DATETIME
1515
touch $RESTART_DATETIME_CHECK

bin/dsRemove.ps1

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,57 @@
1+
### Grab variables from inputs.conf
2+
$BTOOL_INPUT = & $SPLUNKHOME\bin\splunk.exe cmd btool inputs list powershell://dsRemove --debug
3+
$SFRK_APP = ($BTOOL_INPUT -replace [regex]::Escape("$SPLUNKHOME\etc\apps\"),"" -replace "\\(default|local)\\inputs.conf","").Split(" ")[0]
4+
$CORRECT_DS_LINE = ($BTOOL_INPUT | findstr deploymentServerUri)
5+
$CORRECT_DS_APP_LINE = ($BTOOL_INPUT | findstr deploymentClientApp)
6+
$CORRECT_DS = ("$CORRECT_DS_LINE" -replace [regex]::Escape("$SPLUNKHOME\etc\apps\"),"").Split(" ")[3]
7+
$CORRECT_DS_APP = ("$CORRECT_DS_APP_LINE" -replace [regex]::Escape("$SPLUNKHOME\etc\apps\"),"").Split(" ")[3]
8+
9+
if (!$CORRECT_DS) {
10+
Write-output "deploymentServerUri not configured in inputs.conf" | timestamp
11+
exit
12+
}
13+
14+
if (!$CORRECT_DS_APP) {
15+
Write-output "deploymentClientApp not configured in inputs.conf" | timestamp
16+
exit
17+
}
18+
119
### Configure file paths for the system
2-
$LOCAL = {$((Get-ChildItem -Path "$SPLUNKHOME\etc\system\local" -Include deploymentclient.conf -File -Recurse).Count)}
3-
$DEPLOYED = {$((Get-ChildItem -Path "$SPLUNKHOME\etc\apps" -Include deploymentclient.conf -File -Recurse).Count)}
20+
$LOCAL = $((Get-ChildItem -Path "$SPLUNKHOME\etc\system\local" -Include deploymentclient.conf -File -Recurse).Count)
21+
$DEPLOYED = $((Get-ChildItem -Path "$SPLUNKHOME\etc\apps" -Include deploymentclient.conf -File -Recurse).Count)
22+
$LIST_APPS = $(Get-ChildItem -Path "$SPLUNKHOME\etc\apps" -Include deploymentclient.conf -File -Recurse)
23+
$BAD_APPS = @(($LIST_APPS -replace "(deafult|local)\\deploymentclient.conf","").where{$_ -notmatch [regex]::Escape("$CORRECT_DS_APP") })
424
$RESTART_CHECK = "$SPLUNKHOME\etc\restartds.txt"
525

626
### Filter to attach timestamps where necessary
7-
filter timestamp {"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff') ${env:COMPUTERNAME}: $_"}
27+
filter timestamp {"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff zzz') ${env:COMPUTERNAME}: $_"}
28+
29+
## Capture the current configuration that Splunk is using from btool
30+
$BTOOL = & $SPLUNKHOME\bin\splunk.exe cmd btool deploymentclient list --debug | FINDSTR 'targetUri'
31+
$CURRENT_DS = ($BTOOL -replace [regex]::Escape("$SPLUNKHOME"),"").Split(" ")[3]
32+
$CURRENT_APP_PATH = "$SPLUNKHOME"+($BTOOL -replace [regex]::Escape("$SPLUNKHOME"),"" -replace "\\(deafult|local)\\deploymentclient.conf","").Split(" ")[0]
33+
$CURRENT_APP_NAME = ($CURRENT_APP_PATH -replace [regex]::Escape("$SPLUNKHOME\etc\apps\"),"")
834

935
### Check to see if there is a deploymentclient.conf file under $SPLUNKHOME\etc\apps and bail out if there isn't
1036
if ($DEPLOYED -eq "0") {
11-
Write-output "No deploymentclient.conf detected in $SPLUNKHOME\etc\apps. Bailing out so the fowarder doesn't get orphaned." | timestamp
12-
} elseif ($DEPLOYED -gt "1") {
13-
Write-output "Multiple deploymentclient.conf detected in $SPLUNKHOME\etc\apps. Check all deployed apps to ensure you\'re only using one." | timestamp
14-
} elseif ($LOCAL -eq "1" -AND $DEPLOYED -eq "1") {
15-
### Remove the local "deploymentclient.conf" and flag
16-
Write-output "Removed deploymentclient.conf from local system." | timestamp
17-
Remove-Item -Path "$LOCAL"
18-
Out-File -FilePath "$RESTART_CHECK"
19-
} else {
20-
Write-output "No deploymentclient.conf correction necessary." | timestamp
37+
Write-output "No deploymentclient.conf detected in $SPLUNKHOME\etc\apps. Bailing out so the fowarder doesn't get orphaned." | timestamp
38+
Write-output "Deploy $CORRECT_APP to this server to correct this issue." | timestamp
39+
}
40+
elseif ($DEPLOYED -gt "1") {
41+
Write-output "Multiple deploymentclient.conf detected in $SPLUNKHOME\etc\apps." | timestamp
42+
Write-output "Removing bad app(s) to ensure there is no contention with $CORRECT_DS_APP" | timestamp
43+
foreach ($item in $BAD_APPS)
44+
{
45+
Remove-Item -Path "$item" -Force -Recurse -Confirm
46+
Write-output "Removed app: $item" | timestamp
47+
}
48+
Out-File -FilePath "$RESTART_CHECK"
49+
}
50+
elseif ($LOCAL -eq "1" -AND $DEPLOYED -eq "1") {
51+
Write-output "Removed deploymentclient.conf from local system." | timestamp
52+
Remove-Item -Path "$LOCAL" -Force -Confirm
53+
Out-File -FilePath "$RESTART_CHECK"
54+
}
55+
else {
56+
Write-output "No deploymentclient.conf correction necessary." | timestamp
2157
}

bin/dsRemove.sh

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
11
#!/bin/bash
2+
3+
## Run appContext to capture details from inputs stanza
4+
. $(dirname $0)/appContext.sh
5+
26
### Look for a deploymentclient.conf file in the apps directory and define the path to the restartds.txt file
7+
CORRECT_APP_DEPLOYED=$(find $SPLUNK_HOME/etc/apps -type d -name ${CORRECT_APP} | wc -l)
38
DEPLOYED_APP=$(find $SPLUNK_HOME/etc/apps -type f -name deploymentclient.conf | wc -l)
9+
BAD_APPS=($(find $SPLUNK_HOME/etc/apps -type f -name deploymentclient.conf | grep -v ${CORRECT_APP} | sed "s/\/(default|local)\/deploymentclient.conf//"))
410
LOCAL=$(find $SPLUNK_HOME/etc/system/local -type f -name deploymentclient.conf | wc -l)
511
RESTART_CHECK=$SPLUNK_HOME/etc/restartds.txt
612

7-
### Check variables and take action accordingly
13+
## Capture the current deployment server URI from btool
14+
BTOOL=$(${SPLUNK_HOME}/bin/splunk btool deploymentclient list --debug | grep targetUri)
15+
CURRENT_DS=$(echo ${BTOOL} | awk '{print $4}')
16+
CURRENT_APP_PATH=$(echo ${BTOOL} | awk '{print $1}' | sed "s/\/(default|local)\/deploymentclient.conf//")
17+
CURRENT_APP_NAME=$(echo ${CURRENT_APP_PATH} | sed "s|${SPLUNK_HOME}\/etc\/apps\/||")
18+
19+
## Check for inputs configurations and bail out if they don't exist
20+
if [ -z "${CORRECT_DS}" ] || [ -z "${CORRECT_APP}" ]; then
21+
echo "$(date -R) $HOSTNAME: Missing configurations in inputs.conf."
22+
exit 1
23+
fi
24+
825
## If there is no deployed app with a deployment client, bail out
926
if [ $DEPLOYED_APP = "0" ]; then
10-
echo $(date -R) $HOSTNAME: No deploymentclient.conf detected in $SPLUNK_HOME/etc/apps. Bailing out so the fowarder doesn\'t get orphaned.
11-
exit 1
12-
## If more than one deploymentclient.conf file is deployed, bail out
13-
elif [ $DEPLOYED_APP > "1" ]; then
14-
echo $(date -R) $HOSTNAME: Multiple deploymentclient.conf detected in $SPLUNK_HOME/etc/apps. Check all deployed apps to ensure you\'re only using one.
15-
exit 1
16-
## If there's 1 local config and 1 deployed config, remove the local one and set the checkpoint file
17-
elif [ $LOCAL = "1" ] & [ $DEPLOYED_APP = "1" ]; then
18-
# Remove the deploymentclient.conf from $SPLUNK_HOME/etc/system/local
19-
rm -f $SPLUNK_HOME/etc/system/local/deploymentclient.conf > /dev/null 2>&1
20-
echo $(date -R) $HOSTNAME: Removed deploymentclient.conf from local system.
21-
touch $RESTART_CHECK
22-
else
23-
echo $(date -R) $HOSTNAME: No deploymentclient.conf correction necessary.
27+
echo "$(date -R) $HOSTNAME: No deploymentclient.conf detected in $SPLUNK_HOME/etc/apps. Bailing out so the fowarder doesn\'t get orphaned."
28+
echo "$(date -R) $HOSTNAME: Deploy \"${CORRECT_APP}\" to this server to correct this issue."
29+
exit 1
30+
fi
31+
32+
## If more than one deploymentclient.conf file is deployed, nuke the wrong app and set the checkpoint file
33+
if [ $DEPLOYED_APP > "1" ]; then
34+
echo "$(date -R) $HOSTNAME: Multiple apps with deploymentclient.conf detected in $SPLUNK_HOME/etc/apps."
35+
echo "$(date -R) $HOSTNAME: Removing bad apps to ensure there is no contention with \"${CORRECT_APP}\""
36+
for i in "${BAD_APPS[@]}"; do
37+
rm -rf $i
38+
done
39+
touch $RESTART_CHECK
40+
fi
41+
42+
## If there's 1 local config, remove the local one and set the checkpoint file
43+
if [ $LOCAL = "1" ]; then
44+
rm -f $SPLUNK_HOME/etc/system/local/deploymentclient.conf > /dev/null 2>&1
45+
echo $(date -R) $HOSTNAME: Removed deploymentclient.conf from local system.
46+
touch $RESTART_CHECK
47+
fi
48+
49+
## If the current deployment server is configured and the correct app is installed, bail out
50+
if [ "${CURRENT_DS}" = "${CORRECT_DS}" ] && [ "${CURRENT_APP}" = "${CORRECT_APP}" ]; then
51+
echo $(date -R) $HOSTNAME: No deploymentclient.conf correction necessary.
2452
fi

bin/hostCorrect.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ $restartInputCheck = "$SPLUNKHOME\etc\restartinput.txt"
66
$restartServerCheck = "$SPLUNKHOME\etc\restartserver.txt"
77

88
### Filter to attach timestamps where necessary
9-
filter timestamp {"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff') ${env:COMPUTERNAME}: $_"}
9+
filter timestamp {"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff zzz') ${env:COMPUTERNAME}: $_"}
1010

1111
### Compare values to actual host value and flag accordingly
1212
if (-not ($currentHost -eq $env:COMPUTERNAME)){

bin/hostCorrect.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ RESTART_SERVER_CHECK="$SPLUNK_HOME/etc/restartserver.txt"
99
if [ -f "$INPUTS_FILE" ]; then
1010
CURRENT_HOST=$(cat "$INPUTS_FILE" | grep "host =" | awk '{printf $3}')
1111
else
12-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Inputs file is missing from local config. Creating it now..."
12+
echo "$(date -R) ${HOSTNAME}: Inputs file is missing from local config. Creating it now..."
1313
touch "$INPUTS_FILE"
1414
echo "[default]
1515
host = IntentionallyWrong" > "$INPUTS_FILE"
@@ -19,7 +19,7 @@ fi
1919
### Check for the existence of a serverName value and insert one if one isn't configured
2020
CURRENT_SERVER=$(cat "$SERVER_FILE" | grep "serverName =" | awk '{printf $3}')
2121
if [ -z "$CURRENT_SERVER" ]; then
22-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: There is no serverName value configured. Inserting one..."
22+
echo "$(date -R) ${HOSTNAME}: There is no serverName value configured. Inserting one..."
2323
echo "
2424
[general]
2525
serverName = IntentionallyWrong" >> "$SERVER_FILE"
@@ -28,17 +28,17 @@ fi
2828

2929
### Check the current values of the configured files and correct if necessary
3030
if [ "$CURRENT_HOST" = "$HOSTNAME" ]; then
31-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Currently configured host name: $CURRENT_HOST. No correction necessary..."
31+
echo "$(date -R) ${HOSTNAME}: Currently configured host name: $CURRENT_HOST. No correction necessary..."
3232
else
33-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Currently configured host name: $CURRENT_HOST. Reconfiguring inputs.conf..."
33+
echo "$(date -R) ${HOSTNAME}: Currently configured host name: $CURRENT_HOST. Reconfiguring inputs.conf..."
3434
cp "$INPUTS_FILE" "$INPUTS_FILE".$(date +"%m%d%Y")
3535
sed -i "s/$CURRENT_HOST/$HOSTNAME/" "$INPUTS_FILE"
3636
touch "$RESTART_INPUT_CHECK"
3737
fi
3838
if [ "$CURRENT_SERVER" = "$HOSTNAME" ]; then
39-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Currently configured server name: $CURRENT_SERVER. No correction necessary..."
39+
echo "$(date -R) ${HOSTNAME}: Currently configured server name: $CURRENT_SERVER. No correction necessary..."
4040
else
41-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Currently configured server name: $CURRENT_SERVER. Reconfiguring server.conf..."
41+
echo "$(date -R) ${HOSTNAME}: Currently configured server name: $CURRENT_SERVER. Reconfiguring server.conf..."
4242
cp "$SERVER_FILE" "$SERVER_FILE".$(date +"%m%d%Y")
4343
sed -i "s/$CURRENT_SERVER/$HOSTNAME/" "$SERVER_FILE"
4444
touch "$RESTART_SERVER_CHECK"

bin/regenGUID.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $INSTANCE_CHECK = {$(Test-Path "$INSTANCE_FILE.*")}
44
$RESTART_CHECK = "$SPLUNKHOME\etc\restartguid.txt"
55

66
### Filter to attach timestamps where necessary
7-
filter timestamp {"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff') ${env:COMPUTERNAME}: $_"}
7+
filter timestamp {"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff zzz') ${env:COMPUTERNAME}: $_"}
88

99
### Check to see if the GUID has already been replaced on this host previously by this script
1010
if ($INSTANCE_CHECK -eq "True") {
@@ -15,4 +15,4 @@ if ($INSTANCE_CHECK -eq "True") {
1515
Copy-Item -Path "$INSTANCE_FILE" -Destination "$INSTANCE_FILE.$(Get-Date -Format 'MMddyyyy').bak"
1616
Remove-Item -Path "$INSTANCE_FILE"
1717
Out-File -FilePath "$RESTART_CHECK"
18-
}
18+
}

bin/regenGUID.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ RESTART_CHECK="$SPLUNK_HOME/etc/restartguid.txt"
55

66
### Check if there is already a backup of the instance.cfg and take the appropriate action.
77
if [ -f "${INSTANCE}.*" ]; then
8-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Instance GUID has already been changed."
8+
echo "$(date -R) ${HOSTNAME}: Instance GUID has already been changed."
99
CHECK="0"
1010
else
11-
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Backing up instance.cfg"
11+
echo "$(date -R) ${HOSTNAME}: Backing up instance.cfg"
1212
mv "$INSTANCE" "${INSTANCE}.$(date +"%m%d%Y")"
1313
touch $RESTART_CHECK
14-
fi
14+
fi

0 commit comments

Comments
 (0)