Benjamin Zhang

目前就职于中科院计算机网络信息中心,主要兴趣集中在数据库,操作系统,中间件。

【开源监控】node-exporter自启动

13 Oct 2019 » 开源监控

node-exporter自启动

0.安装

cd /opt/
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
tar zxvf node_exporter-0.18.1.linux-amd64.tar.gz 

1.超链接

ln -s /opt/node_exporter-0.18.1.linux-amd64/node_exporter /usr/bin/

2.安装daemonize

yum install daemonize

3.配置自启动脚本

文件位置/etc/init.d/node_exporter

[root@localhost run]# vi /etc/init.d/node_exporter
#!/bin/bash
# node_exporter     This shell script takes care of starting and stopping exporter
# chkconfig: 2345 80 80
# description: Prometheus apache exporter  start script
# processname: node_exporter
# pidfile: /var/run/node_exporter.pid
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
PROGNAME=node_exporter
PROG=/usr/bin/${PROGNAME}
RUNAS=prometheus
LOCKFILE=/var/lock/subsys/${PROGNAME}
PIDFILE=/var/run/${PROGNAME}.pid
LOGFILE=/var/log/${PROGNAME}.log
DAEMON_SYSCONFIG=/etc/sysconfig/${PROGNAME}

# GO CPU core Limit
#GOMAXPROCS=$(grep -c ^processor /proc/cpuinfo)
GOMAXPROCS=1

# Source config
. ${DAEMON_SYSCONFIG}
start() {
if [[ -f $PIDFILE ]] > /dev/null; then
echo "node_exporter  is already running"
exit 0
fi
echo -n "Starting node_exporter  service…"
daemonize -u ${USER} -p ${PIDFILE} -l ${LOCKFILE} -a -e ${LOGFILE} -o ${LOGFILE} ${PROG} ${ARGS}
RETVAL=$?
echo ""
return $RETVAL
}

stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo "Service not running"
return 1
fi
echo 'Stopping node_exporter service…'
#kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
killproc -p ${PIDFILE} -d 10 ${PROG}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE}
return $RETVAL
}

status() {
if [ -f "$PIDFILE" ] || kill -0 $(cat "$PIDFILE"); then
echo "apache exporter  service running..."
echo "Service PID: `cat $PIDFILE`"
else
echo "Service not running"
fi
RETVAL=$?
return $RETVAL
}

# Call function
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 2
esac

配置文件/etc/sysconfig/node_exporter

[root@localhost bin]# vi /etc/sysconfig/node_exporter
ARGS=""
:wq

测试自启动脚本

[root@localhost run]# /etc/init.d/node_exporter restart
Stopping node_exporter service…
[确定]
Starting node_exporter  service…
[root@localhost run]# /etc/init.d/node_exporter stop
Stopping node_exporter service…
[确定]
[root@localhost run]# /etc/init.d/node_exporter start
Starting node_exporter  service…
[root@localhost run]# /etc/init.d/node_exporter status
apache exporter  service running...
Service PID: 1090

查看进程状态

[root@localhost run]# ps -ef | grep node_exporter
root      1090     1  0 16:08 ?        00:00:00 /usr/bin/node_exporter
root      1114 31528  0 16:09 pts/1    00:00:00 grep node_exporter

开机自启动

chkconfig --level 35 node_exporter on