prometheus是一套监控工具
exporter是像服务发送监控数据的客户端服务,node-exporter是对主机的常规监控
可以编译启动或docker启动node-exporter
https://github.com/prometheus/node_exporter
但是怎么把这个node-exporter做到系统服务里?
1)编写一个启动脚本:
[root@xxx ~]# cat /usr/local/node_exporter/node-exporter.sh#!/bin/sh[ "$1" == "start" ] || [ "$1" == "stop" ] || ( echo "$0 need parameter:[start] or [stop]?" && exit -1 )pidfile=/var/run/docker/node-exporter.pidcid=`docker ps |grep node-exporter |awk '{print $1}'`cida=`docker ps -a|grep node-exporter|awk '{print $1}'`if [ "$1" == "start" ];thenif [ "$cid" != "" ];thenecho node-exporter is running.Need not start.elif [ "$cida" != "" ];thendocker start $cidapid=`pgrep node-exporter`echo pid=$pidecho $pid > $pidfileelsedocker run -d --net="host" --pid="host" --name=node-exporter -v "/:/host:ro,rslave" quay.io/prometheus/node-exporter --path.rootfs=/hostpid=`pgrep node_exporter`echo pid=$pidecho $pid > $pidfilefielif [ "$1" == "stop" ];thenif [ "$cid" != "" ];thendocker stop $ciddocker rm $cidelif [ "$cida" != "" ];thendocker rm $cidaelseecho node-exporter is not running.Need not stop.fifi
2)编写系统服务配置文件:
[root@xxx ~]# cat /etc/systemd/system/multi-user.target.wants/node-exporter.service[Unit]Description=prometheus/node-exporterAfter=docker.service[Service]Type=forkingPIDFile=/var/run/docker/node-exporter.pidExecStart=/usr/bin/sh /usr/local/node_exporter/node-exporter.sh startExecStop=/usr/bin/sh /usr/local/node_exporter/node-exporter.sh stop[Install]WantedBy=multi-user.target
3)systemctl daemon-reload后可以使用systemctl start/stop 服务
