Skip to content

Commit da13af3

Browse files
Update hostCorrect.sh
Update to determine if the local "inputs.conf" exists and to create it if it's missing.
1 parent d16d86c commit da13af3

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

bin/hostCorrect.sh

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
#!/bin/bash
22
### Capture the existing host and serverName values and define path to restart checkpoint file
3-
INPUTS_FILE=$SPLUNK_HOME/etc/system/local/inputs.conf
4-
SERVER_FILE=$SPLUNK_HOME/etc/system/local/server.conf
5-
CURRENT_HOST=$(cat $INPUTS_FILE | grep "host =" | awk '{printf $3}')
6-
CURRENT_SERVER=$(cat $SERVER_FILE | grep "serverName =" | awk '{printf $3}')
3+
INPUTS_FILE="$SPLUNK_HOME/etc/system/local/inputs.conf"
4+
SERVER_FILE="$SPLUNK_HOME/etc/system/local/server.conf"
75
RESTART_INPUT_CHECK="$SPLUNK_HOME/etc/restartinput.txt"
86
RESTART_SERVER_CHECK="$SPLUNK_HOME/etc/restartserver.txt"
7+
CURRENT_SERVER=$(cat "$SERVER_FILE" | grep "serverName =" | awk '{printf $3}')
8+
9+
### Check for the existence of the inputs file and create if it doesn't exist
10+
if [ -f "$INPUTS_FILE" ]; then
11+
CURRENT_HOST=$(cat "$INPUTS_FILE" | grep "host =" | awk '{printf $3}')
12+
else
13+
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Inputs file is missing from local config. Creating it now..."
14+
touch "$INPUTS_FILE"
15+
echo "[default]
16+
host = IntentionallyWrong" > "$INPUTS_FILE"
17+
CURRENT_HOST=$(cat "$INPUTS_FILE" | grep "host =" | awk '{printf $3}')
18+
fi
919

1020
### Compare those values and correct if necessary
11-
if [ $CURRENT_HOST = $HOSTNAME ]; then
21+
if [ "$CURRENT_HOST" = "$HOSTNAME" ]; then
1222
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Currently configured host name: $CURRENT_HOST. No correction necessary..."
1323
else
1424
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Currently configured host name: $CURRENT_HOST. Reconfiguring inputs.conf..."
15-
cp $INPUTS_FILE $INPUTS_FILE.$(date +"%m%d%Y")
16-
sed -i "s/$CURRENT_HOST/$HOSTNAME/" $INPUTS_FILE
17-
touch $RESTART_INPUT_CHECK
25+
cp "$INPUTS_FILE" "$INPUTS_FILE".$(date +"%m%d%Y")
26+
sed -i "s/$CURRENT_HOST/$HOSTNAME/" "$INPUTS_FILE"
27+
touch "$RESTART_INPUT_CHECK"
1828
fi
19-
if [ $CURRENT_SERVER = $HOSTNAME ]; then
29+
if [ "$CURRENT_SERVER" = "$HOSTNAME" ]; then
2030
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Currently configured server name: $CURRENT_SERVER. No correction necessary..."
2131
else
2232
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") ${HOSTNAME}: Currently configured server name: $CURRENT_SERVER. Reconfiguring inputs.conf..."
23-
cp $SERVER_FILE $SERVER_FILE.$(date +"%m%d%Y")
24-
sed -i "s/$CURRENT_SERVER/$HOSTNAME/" $SERVER_FILE
25-
touch $RESTART_SERVER_CHECK
33+
cp "$SERVER_FILE" "$SERVER_FILE".$(date +"%m%d%Y")
34+
sed -i "s/$CURRENT_SERVER/$HOSTNAME/" "$SERVER_FILE"
35+
touch "$RESTART_SERVER_CHECK"
2636
fi

0 commit comments

Comments
 (0)