Prometheus系列--使用node_exporter的collector.textfile 功能自定義監(jiān)控

      網(wǎng)友投稿 2966 2025-03-31

      一、簡介

      Node_exporter的--collector.textfile是一個(gè)收集器,這個(gè)收集器可以允許我們暴露自定義指標(biāo),比如某些pushgateway功能中自定義的指標(biāo),就可以使用--collector.textfile功能來實(shí)現(xiàn),而且,Node_exporter實(shí)現(xiàn)起來更加優(yōu)雅。用node_expoerter ,直接在現(xiàn)在基礎(chǔ)上做textfile collector即可。如果有pushgateway的話,可是使用pushgateway的,也可以使用textfilecollector。自己用那個(gè)舒服,就用吧。

      collector.text收集器通過掃描指定目錄中的文件,提取所有格式為prometheus指標(biāo)的字符串,然后暴露它們以便抓取。

      Prometheus系列--使用node_exporter的collector.textfile 功能自定義監(jiān)控

      二、Textfile Collector使用

      因?yàn)閚ode_exporter之前已經(jīng)安裝過,如果node_exporter啟動時(shí)沒有指定--collector.textfile.directory參數(shù),需要在啟動文件里面,添加上參數(shù),并確認(rèn)文件指的目錄存在。

      /usr/local/prometheus/node_exporter # 編輯systemd啟動文件,和下方的supervisor二選一即可。 cat >> /usr/lib/systemd/system/node_exporter.service << "EOF" [Unit] Description=Prometheus node_exporter Requires=network.target remote-fs.target After=network.target remote-fs.target [Service] Type=simple User=root Group=root ExecStart=/usr/local/prometheus/node_exporter --web.listen-address=0.0.0.0:9100 --collector.textfile.directory=$PWD/textfile ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target EOF # 使用supervisor管理node_exporter程序,和上方的systemd二選一即可。 cat >> /etc/supervisord.d/node_exporter.ini << "EOF" [program:node_exporter] # command=/usr/local/prometheus/node_exporter/node_exporter --web.listen-address=0.0.0.0:9100 --collector.textfile.directory=$PWD/textfile; the program (relative uses PATH, can take args) numprocs=1 ; number of processes copies to start (def 1) directory=/usr/local/prometheus/node_exporter ; directory to cwd to before exec (def no cwd) autostart=true ; start at supervisord start (default: true) autorestart=true ; retstart at unexpected quit (default: true) startsecs=30 ; number of secs prog must stay running (def. 1) startretries=3 ; max # of serial start failures (default 3) exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) stopsignal=QUIT ; signal used to kill process (default TERM) stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) user=root ; setuid to this UNIX account to run the program redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile=/usr/local/node_exporter/node_exporter.stdout.log ; stderr log path, NONE for none; default AUTO stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB) stdout_logfile_backups=4 ; # of stdout logfile backups (default 10) stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) stdout_events_enabled=false ; emit events on stdout writes (default false) stopasgroup=true killasgroup=true EOF #啟動 # systemd 方式啟動 systemctl daemon-reload systemctl enable node_exporter systemctl start node_exporter systemctl status node_exporter # supervisor方式啟動 supervisorctl update supervisorctl status supervisorctl start node_exporter supervisorctl restart node_exporter #檢查是否啟動成功 ss -untlp |grep 9100 ps -ef |grep node_exporter 如果啟動不成功,使用systemd的,使用journal -xe 檢查啟動報(bào)錯(cuò);使用supervisor,去日志文件檢查啟動報(bào)錯(cuò)。

      3、寫入自定義指標(biāo)

      寫入自定義指標(biāo),這個(gè)需要自己開發(fā)腳本,將數(shù)據(jù)寫入對應(yīng)的文件即可。也可以在github(點(diǎn)我點(diǎn)我...)中看實(shí)例文件,熟悉python或者shell都可以寫。

      GitHub 中有提供模板的,比如directory-size.sh

      ]# cat directory-size.sh #!/bin/sh # # Expose directory usage metrics, passed as an argument. # # Usage: add this to crontab: # # */5 * * * * prometheus directory-size.sh /var/lib/prometheus | sponge /var/lib/node_exporter/directory_size.prom # # sed pattern taken from https://www.robustperception.io/monitoring-directory-sizes-with-the-textfile-collector/ # # Author: Antoine Beaupré echo "# HELP node_directory_size_bytes Disk space used by some directories" echo "# TYPE node_directory_size_bytes gauge" du --block-size=1 --summarize "$@" \ | sed -ne 's/\\/\\\\/;s/"/\\"/g;s/^\([0-9]\+\)\t\(.*\)$/node_directory_size_bytes{directory="\2"} \1/p'

      示例的模板文件中,有HELP和TYPE兩行注釋,這兩行注釋需要將指標(biāo)名稱與下面的內(nèi)容完全對應(yīng),比如:“node_directory_size_bytes”,三行的指標(biāo)名稱一定要對應(yīng)起來。防止自定義多了,自己分辨不出來。主要是給自己看。

      自定義采集,需要自己寫crontab定時(shí)任務(wù),定時(shí)執(zhí)行。所以仿照剛剛實(shí)例中的定時(shí)任務(wù)格式寫就好了,sponge是寫入管道傳來的數(shù)據(jù)到文件中。tee也可以,tee會輸出內(nèi)容。

      之前碰到過一種現(xiàn)象,就是加入數(shù)據(jù)正在寫入,但是prometheus正好拉取,可能會出現(xiàn)拉取出現(xiàn)問題,這里呢,需要先將自定義指標(biāo)寫入一個(gè)臨時(shí)文件,然后將文件mv一下到正式的文件。

      以下是英文原版的說明。有能力可以看下。

      Textfile Collector

      The textfile collector is similar to the Pushgateway, in that it allows exporting of statistics from batch jobs. It can also be used to export static metrics, such as what role a machine has. The Pushgateway should be used for service-level metrics. The textfile module is for metrics that are tied to a machine.

      To use it, set the --collector.textfile.directory flag on the node_exporter commandline. The collector will parse all files in that directory matching the glob *.prom using the text format. Note: Timestamps are not supported.

      To atomically push completion time for a cron job:

      echo my_batch_job_completion_time $(date +%s) > /path/to/directory/my_batch_job.prom.$$ mv /path/to/directory/my_batch_job.prom.$$ /path/to/directory/my_batch_job.prom

      To statically set roles for a machine using labels:

      echo 'role{role="application_server"} 1' > /path/to/directory/role.prom.$$ mv /path/to/directory/role.prom.$$ /path/to/directory/role.prom

      以上摘抄的github中的文檔

      GitHub - prometheus/node_exporter: Exporter for machine metrics

      4、配置Grafana

      這里的數(shù)據(jù)源,是使用已有的數(shù)據(jù)源即可。配置pannel,需要寫一寫公式。請查看pushgateway那篇文章的Grafana的json。

      GitHub

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:如何在Excel中顯示計(jì)算步驟?
      下一篇:excel用函數(shù)進(jìn)行進(jìn)制轉(zhuǎn)換的方法
      相關(guān)文章
      亚洲AV色欲色欲WWW| 亚洲另类少妇17p| 中文字幕亚洲不卡在线亚瑟| 亚洲精品乱码久久久久蜜桃 | 亚洲国产成人VA在线观看| 亚洲人成77777在线播放网站不卡| 亚洲婷婷天堂在线综合| 亚洲一区二区成人| 无码欧精品亚洲日韩一区| 亚洲AV中文无码乱人伦下载 | 亚洲成人精品久久| 亚洲av无码潮喷在线观看| 亚洲gv白嫩小受在线观看| 亚洲gv猛男gv无码男同短文| 亚洲av无码一区二区三区乱子伦| 亚洲精品无码久久久久去q| 亚洲色自偷自拍另类小说| 亚洲精品V欧洲精品V日韩精品| 国产亚洲一区二区精品| 亚洲AV无码久久精品狠狠爱浪潮| 久久精品国产亚洲AV麻豆不卡 | 蜜芽亚洲av无码精品色午夜| 久久丫精品国产亚洲av不卡| 亚洲成人福利网站| 亚洲av一综合av一区| 亚洲成a人片在线观看中文动漫| 亚洲AV无码成人精品区蜜桃| 日韩精品一区二区亚洲AV观看| 亚洲网站免费观看| 在线观看亚洲AV日韩A∨| 亚洲高清有码中文字| 亚洲精品无码国产片| 亚洲AV香蕉一区区二区三区| 亚洲 小说区 图片区 都市| 久久精品国产亚洲精品| 久久精品国产精品亚洲蜜月| 亚洲黄色在线观看| 亚洲天然素人无码专区| 国内成人精品亚洲日本语音| 国产亚洲精品看片在线观看| 久久精品视频亚洲|