node_exporter安装和配置

1.二进制包安装

#创建目录
mkdir -p /opt/exporter
cd /opt/exporter
#下载安装包
wget https://github.com/prometheus/node_exporter/releases/download/v0.14.0/node_exporter-0.14.0.linux-amd64.tar.gz
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-arm64.tar.gz
#解压
tar -xvzf  node_exporter-0.14.0.linux-amd64.tar.gz
#修改名称
mv node_exporter-0.14.0.linux-amd64 node_exportercd /opt/exporter/node_exporter
#修改权限
chmod 777 node_exporter
#启动服务
nohup /opt/exporter/node_exporter/node_exporter &
#访问
curl http://IP:9100/metrics

2.docker化安装

 注意:前提是当前环境已安装docker环境

#执行显示docker版本信息,则说明已安装docker
docker -v 

 1)node_exporter镜像下载

#查看镜像: 
docker search node-exporter

#拉取镜像: 
docker pull prom/node-exporter

#查看拉取的镜像:
docker images

#查看镜像: 
docker images|grep node-exporter

#根据对应的镜像id启动:

docker start 3a6e851e4dc2

#查看监听端口:
netstat -nlp|grep 9100

#访问:
curl http://IP:9100/metrics

 3.node_exporter自定义exporter 监控key

#创建目录: 
mkdir -p /opt/exporter/node_exporter/key
#切换目录
cd /opt/exporter/node_exporter/key

1)创建脚本:

vim key_runner 

#! /bin/bash
prom_file=/opt/exporter/node_exporter/key/key.prom

IFS=";"

export TERM=vt100

key_value="
Logical_CPU_core_total  `cat /proc/cpuinfo| grep "processor"| wc -l`;
logined_users_total     `who | wc -l`;
procs_total             `/bin/top -b -n 1|grep Tasks|sed 's/,/\n/g'|grep total|awk '{ print $(NF-1) }'`;
procs_zombie            `/bin/top -b -n 1|grep Tasks|sed 's/,/\n/g'|grep zombie|awk '{ print $(NF-1) }'`"

for i in $key_value
do
    IFS=" "
    j=(`echo $i`)
    key=${j[0]}
    value=${j[1]}
    echo $key $value >> "$prom_file".tmp
done

cat "$prom_file".tmp > $prom_file
rm -rf "$prom_file".tmp
IFS=$OLD_IFS
 

#*****************
 

2)添加权限

 chmod 755 /opt/exporter/node_exporter/key/*

3) 设置定时任务

 
vim /etc/cron.d/node-exporter

*/5 * * * * sh /opt/exporter/node_exporter/key/key_runner 

4) 添加启动参数

#启动node_exporter,指定新加key值的prom路径

nohup /opt/exporter/node_exporter/node_exporter --collector.textfile.directory=/opt/exporter/node_exporter/key &

#访问:
curl http://ip:9100/metrics  > metrics.txt

cat metrics.txt

 

posted @ 2019-09-14 22:59  漠漠颜  阅读(25080)  评论(0编辑  收藏  举报