Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to install, enable, and start node-exporter
#!/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.0.1"
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
}
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}"
curl -sS -L "${url}" -o "/opt/${filename}"
}
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 /home/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
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
}
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
You can’t perform that action at this time.