Installing node_exporter on an EC2 Instance

I had a difficult time finding information on how to install node_exporter on an EC2 instance. The following will be rough, but hopefully educational…

Just as an FYI I am doing this on an EC2 instance running with amzn-ami-2018.03.f-amazon-ecs-optimized.

Install Script

My preferred method of running node_exporter is as a service. You can run the following in a bash shell or as a script:

#!/bin/bash
sudo useradd --shell /bin/false prometheus
sudo mkdir /var/log/prometheus/
sudo touch /var/log/prometheus/node_exporter.log
sudo chown -R prometheus:prometheus /var/log/prometheus
sudo cat > initscript <<'endmsg'
#!/bin/bash
#
# chkconfig: 2345 20 80 Read
# description: node_exporter will export system metrics in a Prometheus format
# processname: node_exporter

# Source function library.
. /etc/rc.d/init.d/functions

PROGNAME=node_exporter
PROGDIR=/home/prometheus/node_exporter
PROG=$PROGDIR/$PROGNAME
USER=prometheus
LOGFILE=/var/log/prometheus/$PROGNAME.log
LOCKFILE=/var/run/$PROGNAME.pid

start() {
echo -n "Starting $PROGNAME: "
cd $PROGDIR
daemon --user $USER --pidfile="$LOCKFILE" "$PROG &>$LOGFILE &"
echo $(pidofproc $PROGNAME) >$LOCKFILE
echo
}

stop() {
echo -n "Shutting down $PROGNAME: "
killproc $PROGNAME
rm -f $LOCKFILE
echo
}


case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $PROGNAME
;;
restart)
stop
start
;;
reload)
echo "Sending SIGHUP to $PROGNAME"
kill -SIGHUP $(pidofproc $PROGNAME)
;;
*)
echo "Usage: service $PROGNAME {start|stop|status|reload|restart}"
exit 1
;;
esac
endmsg
sudo mv initscript /etc/init.d/node_exporter
sudo chmod +x /etc/init.d/node_exporter
sudo chown prometheus:prometheus /etc/init.d/node_exporter
curl -LO https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz
sudo mkdir /home/prometheus/node_exporter
sudo tar -xf node_exporter-*.tar.gz
sudo mv node_exporter-*/* /home/prometheus/node_exporter/
sudo chown -R prometheus:prometheus /home/prometheus/node_exporter
sudo chkconfig --add node_exporter
sudo service node_exporter start

 

The commands above will do the following

  1. Create a service user named prometheus
  2. Create a log file owned by the prometheus user to be used by the node_exporter service
  3. Create a script in the /etc/init.d directory to allow running the node_exporter executable as a service
  4. Download version 0.16.0 of node_exporter and untar it into the /home/prometheus/node_exporter directory
  5. Set the node_exporter service to automatically start on boot
  6. Start the node_exporter service

 

After the script has successfully executed, you can start/stop the service using the command service node_exporter start or service node_exporter stop. You can also check the status of the service using service node_exporter status.

Advertisement

2 thoughts on “Installing node_exporter on an EC2 Instance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s