Last active
March 30, 2022 20:26
-
-
Save t0xicCode/515fb1c057db13796d5d6f9b2c03046c to your computer and use it in GitHub Desktop.
Script to install, enable, and start node-exporter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Copyright (c) 2020 MagnaX Software | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
NODE_EXPORTER_VERSION="1.2.2" | |
os="" | |
arch="" | |
case "$(uname -s | tr '[:upper:]' '[:lower:]')" in | |
linux) | |
os=linux | |
;; | |
*) | |
echo "Unknown kernel: $(uname -s)" >&2 | |
exit 1 | |
;; | |
esac | |
case "$(uname -m | tr '[:upper:]' '[:lower:]')" in | |
x86_64) | |
arch=amd64 | |
;; | |
armv7l) | |
arch=armv7 | |
;; | |
*) | |
echo "Unknown architecture: $(uname -m)" >&2 | |
exit 1 | |
;; | |
esac | |
filename="node_exporter-${NODE_EXPORTER_VERSION}.${os}-${arch}.tar.gz" | |
url="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/${filename}" | |
init="" | |
exec 3>&1 | |
log() { | |
echo "$(date "+%Y.%m.%d-%H:%M:%S"): $@" >&3 | |
} | |
which_init() { | |
log "Determining init system" | |
if [ -d "/run/systemd/system" ]; then | |
log " found systemd" | |
init="systemd" | |
return 0 | |
fi | |
echo "Unsupported init system" >&2 | |
exit 1 | |
} | |
which_firewall() { | |
log "Determining firewall" | |
} | |
should_install() { | |
if [ -f "/opt/node_exporter/version" ]; then | |
installed=$(cat /opt/node_exporter/version) | |
if [ "${NODE_EXPORTER_VERSION}.${os}-${arch}" = "${installed}" ]; then | |
return 0 | |
fi | |
fi | |
return 1 | |
} | |
store_version() { | |
echo "${NODE_EXPORTER_VERSION}.${os}-${arch}" > /opt/node_exporter/version | |
} | |
download_tarball() { | |
log "Downloading node_exporter ${NODE_EXPORTER_VERSION}" | |
if [ `which curl` ]; then | |
curl -sS -L "${url}" -o "/opt/${filename}" | |
elif [ `which wget` ]; then | |
wget -nv "${url}" -O "/opt/${filename}" | |
else | |
log "Neither curl nor wget are available" | |
exit 1 | |
fi | |
} | |
extract_tarball() { | |
log "Extracting..." | |
tar xzf "/opt/${filename}" -C /opt/node_exporter --unlink-first --strip-components=1 | |
} | |
ensure_group() { | |
if getent group node_exporter >/dev/null; then | |
return 0 | |
fi | |
log "Creating node_exporter group" | |
groupadd -r node_exporter | |
} | |
ensure_user() { | |
if getent passwd node_exporter >/dev/null; then | |
return 0 | |
fi | |
log "Creating node_exporter user" | |
useradd -d /var/opt/node_exporter -g node_exporter -m -r -s /usr/sbin/nologin node_exporter | |
} | |
init_systemd_unit() { | |
if [ -f "/etc/systemd/system/node-exporter.service" ]; then | |
return 0 | |
fi | |
log "Creating systemd unit for node-exporter service" | |
cat > "/etc/systemd/system/node-exporter.service" <<EOF | |
[Unit] | |
Description=Node Exporter | |
Wants=network-online.target | |
After=network-online.target | |
[Service] | |
User=node_exporter | |
Group=node_exporter | |
WorkingDirectory=~ | |
Type=simple | |
ExecStart=/opt/node_exporter/node_exporter | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
systemctl daemon-reload | |
} | |
init_systemd_enable_restart() { | |
log "Enabling and restarting node-exporter service" | |
systemctl enable node-exporter | |
systemctl restart node-exporter | |
} | |
setup_service() { | |
case $init in | |
systemd) | |
init_systemd_unit | |
init_systemd_enable_restart | |
;; | |
*) | |
echo "Unsupported init system: ${init}" >&2 | |
exit 1 | |
;; | |
esac | |
} | |
firewall_firewalld_service() { | |
log "Creating service file for firewalld" | |
cat > /etc/firewalld/services/node-exporter.xml <<EOF | |
<?xml version="1.0" encoding="utf-8"?> | |
<service> | |
<short>Node Exporter</short> | |
<description>Node Exporter presents stats about the running OS</description> | |
<port protocol="tcp" port="9100"/> | |
</service> | |
EOF | |
sleep 5 | |
} | |
if should_install; then | |
exit 0 | |
fi | |
which_init | |
download_tarball | |
mkdir -p /opt/node_exporter | |
extract_tarball | |
ensure_group | |
ensure_user | |
setup_service | |
store_version | |
log "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1 thanks