Skip to content

Instantly share code, notes, and snippets.

@michaelknurr
Last active September 20, 2018 07:44
Show Gist options
  • Save michaelknurr/9b9540c10692e1a5a1a1d39c2064155a to your computer and use it in GitHub Desktop.
Save michaelknurr/9b9540c10692e1a5a1a1d39c2064155a to your computer and use it in GitHub Desktop.
node exporter setup
#!/bin/sh
#
# /etc/init.d/node_exporter -- startup script for node_exporter
#
# Written by Michael Knurr
#
### BEGIN INIT INFO
# Provides: Node exporter
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Node exporter for prometheus
### END INIT INFO
DAEMON=/opt/node_exporter
NAME=node_exporter
USER=monitoring
PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log
ARGS=""
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
do_start_prepare()
{
mkdir -p `dirname $PIDFILE` || true
mkdir -p `dirname $LOGFILE` || true
chown -R $USER: `dirname $LOGFILE`
chown -R $USER: `dirname $PIDFILE`
}
do_start_cmd()
{
do_start_prepare
echo -n "Starting daemon: "$NAME
start-stop-daemon --chuid $USER -C --background --start --quiet --pidfile $PIDFILE --make-pidfile --exec $DAEMON -- $ARGS >> $LOGFILE 2>&1
echo "."
}
do_stop_cmd()
{
echo -n "Stopping daemon: "$NAME
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
rm $PIDFILE
echo "."
}
status() {
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
echo "Running, the PID is $PID"
fi
else
printf "%s\n" "Service not running"
fi
}
case "$1" in
start)
do_start_cmd
;;
stop)
do_stop_cmd
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: $1 {start|stop|status|restart}"
exit 1
esac
exit 0
#!/bin/bash
##########################
#
# Setup for node exporter
#
##########################
EXPORTER_RUNNING=`ps -ef|grep -v grep|grep -v setup_node_exporter.sh| grep node_exporter|wc -l`
if [ $EXPORTER_RUNNING -gt 0 ] ; then
echo "Node exporter already running, exiting."
exit 1;
fi
VERSION=0.15.2
EXPORTER_NAME=node_exporter-$VERSION.linux-amd64
cd /opt
wget https://github.com/prometheus/node_exporter/releases/download/v$VERSION/$EXPORTER_NAME.tar.gz
tar xzf $EXPORTER_NAME.tar.gz
rm $EXPORTER_NAME.tar.gz
useradd monitoring
chown -R monitoring:monitoring /opt/$EXPORTER_NAME/
if [ -x /opt/node_exporter ] ; then
rm /opt/node_exporter
fi
ln -s $EXPORTER_NAME node_exporter
wget https://gist.githubusercontent.com/michaelknurr/9b9540c10692e1a5a1a1d39c2064155a/raw/a5a190a1c0a83dd5a6bdb15573dcadd975d69233/node_exporter.init.d
mv node_exporter.init.d /etc/init.d/node_exporter
chmod a+x /etc/init.d/node_exporter
echo "[Unit]
Description=Node Exporter
[Service]
User=monitoring
ExecStart=/opt/node_exporter/node_exporter
[Install]
WantedBy=default.target" > /etc/systemd/system/node_exporter.service
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment