Skip to content

Instantly share code, notes, and snippets.

@dale-c-anderson
Created October 3, 2018 20:37
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save dale-c-anderson/efcdda55eedcc7d6f8b6056d6805ed7d to your computer and use it in GitHub Desktop.
Save dale-c-anderson/efcdda55eedcc7d6f8b6056d6805ed7d to your computer and use it in GitHub Desktop.
Install and configure Prometheus Node Exporter on a CentOS 6 box
#!/bin/bash
# Install Prometheus Node Exporter on CentOS 6.
set -eu
set -o pipefail
set -x
REPOFILE='/etc/yum.repos.d/coprs.ibotty.prometheus-exporters.repo'
INSTALLED=0
if test -f "$REPOFILE"; then
INSTALLED=1
service node_exporter stop || true
fi
if [[ $INSTALLED -eq 0 ]]; then
curl -sS https://copr.fedorainfracloud.org/coprs/ibotty/prometheus-exporters/repo/epel-6/ibotty-prometheus-exporters-epel-6.repo > $REPOFILE
# this will create the node_exporter user / group
yum install node_exporter
iptables -I INPUT -p tcp --dport 9100 -j ACCEPT
service iptables save
fi
touch /var/log/node_exporter.log
chmod 640 /var/log/node_exporter.log
chown node_exporter:adm /var/log/node_exporter.log
# Adjust permissions slightly from what the repo provided.
chown root:node_exporter /var/lib/node_exporter/textfile_collector/
chmod 2771 /var/lib/node_exporter/textfile_collector/
cat << 'EOF' > /etc/logrotate.d/node_exporter
/var/log/node_exporter.log {
weekly
missingok
notifempty
compress
delaycompress
create 640 node_exporter adm
}
EOF
cat << 'EOF' > /etc/init.d/node_exporter
#!/bin/bash
#
# node_exporter Export statistics to be consumed by a remote Prometheus server
#
# chkconfig: 345 70 30
# description: Prometheus node exporter
# processname: node_exporter
### BEGIN INIT INFO
# Provides:
# Required-Start:
# Required-Stop:
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
### END INIT INFO
#
#
# Prometheus node exporter
# /etc/init.d/node_exporter
#
#
# Source function library.
. /etc/init.d/functions
PROGNAME=node_exporter
PROG=/usr/sbin/$PROGNAME
USER=node_exporter
LOGFILE=/var/log/node_exporter.log
LOCKFILE=/var/run/$PROGNAME.pid
start() {
echo -n "Starting $PROGNAME: "
cd /var/lib/node_exporter/textfile_collector/
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)#!/bin/bash
;;
*)
echo "Usage: service node_exporter {start|stop|status|reload|restart}"
exit 1
;;
esac
EOF
chmod 755 /etc/init.d/node_exporter
if [[ $INSTALLED -eq 0 ]]; then
chkconfig node_exporter --add
fi
service node_exporter start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment