Prometheus(三)node-exporter

一、部署

1 容器方式

普罗米修斯官方所有的镜像:https://hub.docker.com/search?q=prom/&source=community

2 二进制方式

下载

官方下载地址 https://prometheus.io/download/
找到 node-export 下载即可

curl -o node-exporter.tar.gz -L https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz

配置 systemed

node-exporter.service

[Unit]
Description=The node-exporter 监控程序
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/node_exporter  --web.listen-address=:9111 --collector.textfile.directory=/apps/exporterData

KillSignal=SIGQUIT

Restart=always

RestartPreventExitStatus=1 6 SIGABRT

TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=1048576
LimitNPROC=1048576

[Install]
WantedBy=multi-user.target
  • –web.listen-address=:9111 指定监听端口为 任意主机地址的 9111端口
  • –collector.textfile.directory=/apps/exporterData 指定可以从目录 /apps/exporterData 中读取通过其他脚本程序获取的监控数据(注意是已经获取到的数据,不是其他脚本),比如使用脚本获取到的节点中的容器运行指标。
    在目录 /apps/exporterData 中可以有任意文件名的文件存在,但是文件中的内容数据格式比须遵循 Prometheus 的要求。

实例如下:

container_netwrite{name="mysql3",id="9fa421caef99"} 2583852325273.60
container_blkioread{name="mysql3",id="9fa421caef99"} 619549032448
container_blkiowrite{name="mysql3",id="9fa421caef99"} 206708186021888

生产脚本

#!/bin/bash
outputfile=/tmp/docker_stat.output
outInfoFile=/apps/exporterData/docker_s.prom

while true
do
    docker stats --format "{{.Name}} {{.ID}} {{.CPUPerc}} {{.MemPerc}} {{.MemUsage}} {{.NetIO}} {{.BlockIO}}" --no-stream |tr -d '/' > $outputfile

    cat $outputfile|while read LINE
    do
      container_name=$(echo $LINE|awk '{print $1}')
      container_id=$(echo $LINE|awk '{print $2}')
      container_cpuperc=$(echo $LINE|awk '{print $3}'|tr -d '%')
      container_memperc=$(echo $LINE|awk '{print $4}'|tr -d '%')

      if [[ "$(echo $LINE|awk '{print $5}')" =~ MiB$ ]];then
        container_memusage=$(echo $LINE|awk '{print $5}' |awk -F 'MiB' '{print $1}')
      elif [[ "$(echo $LINE|awk '{print $5}')" =~ GiB$ ]];then
        num=$(echo $LINE|awk '{print $5}' |awk -F 'GiB' '{print $1}')
        container_memusage=$(echo $num \* 1024|bc)
      fi

      if [[ "$(echo $LINE|awk '{print $6}')" =~ MiB$ ]];then
        container_memlimit=$(echo $LINE|awk '{print $6}' |awk -F 'MiB' '{print $1}')
      elif [[ "$(echo $LINE|awk '{print $6}')" =~ GiB$ ]];then
        num2=$(echo $LINE|awk '{print $6}' |awk -F 'GiB' '{print $1}')
        container_memlimit=$(echo $num2 \* 1024|bc)
      fi

      if [[ "$(echo $LINE|awk '{print $7}')" =~ kB$ ]];then
        container_netread=$(echo $LINE|awk '{print $7}'|awk -F 'kB' '{print $1}')
      elif [[ "$(echo $LINE|awk '{print $7}')" =~ [0-9]B$ ]];then
        container_netread=`echo $(echo $LINE|awk '{print $7}' |awk -F 'B' '{print $1}') \* 1048576|bc`
      elif [[ "$(echo $LINE|awk '{print $7}')" =~ MB$ ]];then
        container_netread=`echo $(echo $LINE|awk '{print $7}' |awk -F 'MB' '{print $1}') \* 1073741824|bc`
      elif [[ "$(echo $LINE|awk '{print $7}')" =~ GB$ ]];then
        container_netread=`echo $(echo $LINE|awk '{print $7}' |awk -F 'GB' '{print $1}') \* 1099511627776|bc`
      fi

      if [[ "$(echo $LINE|awk '{print $8}')" =~ kB$ ]];then
        container_netwrite=$(echo $LINE|awk '{print $8}'|awk -F 'kB' '{print $1}')
      elif [[ "$(echo $LINE|awk '{print $8}')" =~ [0-9]B$ ]];then
        container_netwrite=`echo $(echo $LINE|awk '{print $8}' |awk -F 'B' '{print $1}') \* 1048576|bc`
      elif [[ "$(echo $LINE|awk '{print $8}')" =~ MB$ ]];then
        container_netwrite=`echo $(echo $LINE|awk '{print $8}' |awk -F 'MB' '{print $1}') \* 1073741824|bc`
      elif [[ "$(echo $LINE|awk '{print $8}')" =~ GB$ ]];then
        container_netwrite=`echo $(echo $LINE|awk '{print $8}' |awk -F 'GB' '{print $1}') \* 1099511627776|bc`
      fi

      if [[ "$(echo $LINE|awk '{print $9}')" =~ kB$ ]];then
        container_blkioread=$(echo $LINE|awk '{print $9}'|awk -F 'kB' '{print $1}')
      elif [[ "$(echo $LINE|awk '{print $9}')" =~ [0-9]B$ ]];then
        container_blkioread=`echo $(echo $LINE|awk '{print $9}' |awk -F 'B' '{print $1}') \* 1048576|bc`
      elif [[ "$(echo $LINE|awk '{print $9}')" =~ MB$ ]];then
        container_blkioread=`echo $(echo $LINE|awk '{print $9}' |awk -F 'MB' '{print $1}') \* 1073741824|bc`
      elif [[ "$(echo $LINE|awk '{print $9}')" =~ GB$ ]];then
        container_blkioread=`echo $(echo $LINE|awk '{print $9}' |awk -F 'GB' '{print $1}') \* 1099511627776|bc`
      fi

      if [[ "$(echo $LINE|awk '{print $10}')" =~ kB$ ]];then
        container_blkiowrite=$(echo $LINE|awk '{print $10}'|awk -F 'kB' '{print $1}')
      elif [[ "$(echo $LINE|awk '{print $10}')" =~ [0-9]B$ ]];then
        container_blkiowrite=`echo $(echo $LINE|awk '{print $10}' |awk -F 'B' '{print $1}') \* 1048576|bc`
      elif [[ "$(echo $LINE|awk '{print $10}')" =~ MB$ ]];then
        container_blkiowrite=`echo $(echo $LINE|awk '{print $10}' |awk -F 'MB' '{print $1}') \* 1073741824|bc`
      elif [[ "$(echo $LINE|awk '{print $10}')" =~ GB$ ]];then
        container_blkiowrite=`echo $(echo $LINE|awk '{print $10}' |awk -F 'GB' '{print $1}') \* 1099511627776|bc`
      fi


      echo "container_cpuperc{name=\""$container_name"\",id=\""$container_id"\"} $container_cpuperc" >> $outInfoFile
      echo "container_memperc{name=\""$container_name"\",id=\""$container_id"\"} $container_memperc" >> $outInfoFile
      echo "container_memusage{name=\""$container_name"\",id=\""$container_id"\"} $container_memusage" >> $outInfoFile

      echo "container_memlimit{name=\""$container_name"\",id=\""$container_id"\"} $container_memlimit" >> $outInfoFile
      echo "container_netread{name=\""$container_name"\",id=\""$container_id"\"} $container_netread" >> $outInfoFile
      echo "container_netwrite{name=\""$container_name"\",id=\""$container_id"\"} $container_netwrite" >> $outInfoFile
      echo "container_blkioread{name=\""$container_name"\",id=\""$container_id"\"} $container_blkioread" >> $outInfoFile
      echo "container_blkiowrite{name=\""$container_name"\",id=\""$container_id"\"} $container_blkiowrite" >> $outInfoFile
    done
    sleep 10
    echo > $outInfoFile
done

仪表盘

下载

下载地址 https://grafana.com/grafana/dashboards/
node-exporter 推荐 https://grafana.com/grafana/dashboards/8919

在这里插入图片描述

导入

在这里插入图片描述

离线导入

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在线导入

在这里插入图片描述
在这里插入图片描述

占 位 符 描述
.Container 容器名称或 ID(用户输入)
.Name 容器名称
.ID 容器标识
.CPUPerc 中央处理器百分比
.MemUsage 内存使用情况
.NetIO 网络接口
.BlockIO 块 IO
.MemPerc 内存百分比(在 Windows 上不可用)
.PIDs PID 数量(在 Windows 上不可用)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
如果你想使用podman部署node-exporter,可以按照以下步骤进行操作: 1. 创建一个名为node-exporter的目录,用于存放node-exporter的配置文件和数据。使用以下命令创建: ``` mkdir node-exporter ``` 2. 创建一个名为node-exporter.service的systemd服务单元文件,用于启动node-exporter服务。使用以下命令创建: ``` sudo vi /etc/systemd/system/node-exporter.service ``` 将以下内容复制粘贴到文件中: ``` [Unit] Description=Node Exporter [Service] Restart=always ExecStartPre=podman rm node-exporter ExecStart=/usr/bin/podman run --name node-exporter --net=host --pid=host --privileged=true -v /proc:/host/proc:ro -v /sys:/host/sys:ro -v /:/rootfs:ro -v /etc/node-exporter:/etc/node-exporter:z quay.io/prometheus/node-exporter [Install] WantedBy=multi-user.target ``` 保存并退出文件。 3. 创建一个名为node-exporter的配置文件目录,用于存放node-exporter的配置文件。使用以下命令创建: ``` sudo mkdir /etc/node-exporter ``` 4. 创建一个名为node-exporter.yml的node-exporter配置文件,用于指定node-exporter的参数和监控对象。使用以下命令创建: ``` sudo vi /etc/node-exporter/node-exporter.yml ``` 将以下内容复制粘贴到文件中: ``` global: scrape_interval: 15s scrape_configs: - job_name: 'node' metrics_path: /metrics static_configs: - targets: ['localhost:9100'] ``` 保存并退出文件。 5. 重新加载systemd守护程序并启动node-exporter服务。使用以下命令执行: ``` sudo systemctl daemon-reload sudo systemctl enable node-exporter sudo systemctl start node-exporter ``` 6. 确认node-exporter服务已经成功启动。使用以下命令查看服务状态: ``` sudo systemctl status node-exporter ``` 如果服务状态显示为“active (running)”则表示服务已经启动成功。 希望以上步骤能够帮助你成功部署node-exporter服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shark_西瓜甜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值