Hello, everyone!
I enjoy writing script since I learned it.
But please don't mock me, as I don't know yet the rules. I mean I'm not sure if this is the common and professional way of writing scripts.
I am not actually an IT (my college course is really far from this field, lol).
Anyways, hope this can help.
#!/bin/bash
# Installing Node Exporter
# Output yellow text
echo_yellow() {
echo - "\e[93m$1\e[0m"
}
# Specify the name of the systemd service
SERVICE_NAME="node_exporter"
# Check if the service file exists
if [ -e "/usr/lib/systemd/system/$SERVICE_NAME.service" ]; then
# Check if the service is active
if sudo systemctl is-active --quiet "$SERVICE_NAME"; then
echo_yellow "There is an active $SERVICE_NAME."
# Check the version of the active node_exporter
NODE_EXPORTER_PATH="/usr/local/$SERVICE_NAME/$SERVICE_NAME"
VERSION_INFO="$($NODE_EXPORTER_PATH --version | awk '/node_exporter/ {print $3}')"
echo_yellow "Active Node ExporterVersion: $VERSION_INFO"
echo
echo "Do you want to remove it and replace with a new one? [ 1 / 2 ]"
echo
echo_yellow "1: Remove the active node_exporter and replace it with a new one."
echo
echo "2: Don't do anything and exit."
echo
read -rp "> " ACTION
# Check the action to do
if [ -z "$ACTION" ]; then
echo
echo_yellow "Removing all node_exporter files..."
echo
# Remove node_exporter related files
sudo systemctl stop $SERVICE_NAME
sudo systemctl disable $SERVICE_NAME
sudo rm /usr/lib/systemd/system/$SERVICE_NAME.service
sudo rm -rf /usr/local/node_exporter*
echo
echo "Related files removed."
echo
echo "Installation will continue..."
echo
elif [ "$ACTION" -eq 2 ]; then
echo
echo "No action done."
echo
exit
else
echo
echo "Invalid input. Please enter 1 or 2."
echo
exit 1
fi
else
echo "There's a $SERVICE_NAME service that is not active. Removing related files..."
sudo systemctl stop $SERVICE_NAME
sudo systemctl disable $SERVICE_NAME
sudo rm /usr/lib/systemd/system/$SERVICE_NAME.service
sudo rm -rf /usr/local/node_exporter*
echo
echo "Related files removed."
echo
echo "Installation will continue..."
echo
fi
else
echo "No $SERVICE_NAME service file found."
echo
fi
# Curling Google to check if connected to a network
echo "Looking for a network..."
echo
if curl -sSf https://www.google.com > /dev/null; then
echo "Network connected."
echo
else
echo "The server is not connected to the network. Please connect and try again.";
echo
exit 1
fi
echo_yellow -n "Insert the version you would lilke to be installed, default is [ 1.2.2 ] :"
read VERSION
VERSION=${VERSION:-1.2.2}
echo
# Download the file
wget https://github.com/prometheus/node_exporter/releases/download/v$VERSION/node_exporter-$VERSION.linux-amd64.tar.gz -P /opt
# Extract the downloaded tarball in user directory with a new name
tar -xzvf /opt/node_exporter-$VERSION.linux-amd64.tar.gz -C /usr/local && mv /usr/local/node_exporter-$VERSION.linux-amd64 /usr/local_node_exporter
# Create a systemd service file for Node Exporter
cat >/usr/lib/systemd/system/node_exporter.service<<EOF
[Unit]
Description= Node Exporter
Documentation= https://prometheus.io/
Wants=network.target
After=network.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
# Create the node_exporter user
sudo useradd -M -r -s /bin/false node_exporter
# Set proper permissions
sudo chown -R node_exporter. /usr/local/node_exporter/node_exporter
sudo chmod 755 /usr/local/node_exporter/node_exporter
# Reload systemd and start Node Exporter
sudo systemctl daemon-reload
sudo systemctl start node_exporter.service
sudo systemctl enable node_exporter.service
sudo systemctl status node_exporter.service
# Cleanup downloaded file
rm -f /opt/node_exporter-$VERSION.linux-amd64.tar.gz*
echo
if sudo systemctl is-active --quiet "$SERVICE_NAME"; then
echo_yellow "Node Exporter installed successfully!"
echo
else
echo "Node Exporter installation failed."
echo
fi
Top comments (3)
This looks mostly fine - it does what you want; it automates installing the service for you which is exactly what a script is good for.
That said, here're a couple of tips:
done
but you haven't started awhile
loop - did you have an earlier draft where the user input was repeated if the user gave an invalid value?tput
commandprintf
is more portable thanecho
and you can use newlines in it expliclity:General notes:
The way this is formatted, if for some reason
SERVICE_NAME
is unset - say you changed it and made a typo - then you'll wipe out the entirety of/usr/local
if
blocks gets unmanageable quite quickly in shell scripts, so it's probably best to factor more things out to their own functions or to exit early if the condition isn't met.Final note:
Did you know you can syntax-highlight the code here if you put the language after the three backticks (in this case either "bash" or "sh")!
waaah, I didn't expect someone would notice. Thanks a lot. I'll study more
REVISED NODE EXPORTER INSTALLTION BASH SCRIPT