Prometheus 和 Grafana - 有没有办法让用户使用机器?

2023-12-13

我一直在与 Prometheus 和 Grafana 合作,获取几个计算机实验室的状态和统计数据。有没有一种方法可以让我通过 Prometheus 登录计算机的用户并将其放到 Grafana 上?


我会列出2个选项。第一个将 Pushgateway 与 Prometheus 一起使用,第二个仅使用 Prometheus。

1 - 将 Pushgateway 与 Prometheus 结合使用

您可以使用的一种解决方案是Pushgateway。您将已登录和未登录的用户推送到Pushgateway and Prometheus刮擦它以收集值。这很快,因为您无需配置应用程序即可让 Prometheus 从中抓取数据。首先在您的设备上配置 PushGatewayprometheus.yml配置文件:

....
scrape_configs:
  - job_name: 'pushgateway'
    scrape_interval: 10s
    honor_labels: true # disable auto discover labels
    static_configs:
      - targets: ['pushgateway:9091']

然后使用此脚本收集所有用户并将其分为已登录用户和未登录用户:

#!/bin/bash

users=`cat /etc/passwd | cut -d: -f1`
users_logged=`who | cut -d' ' -f1 | sort | uniq`

for u in $users; do
  cmd="curl --data-binary @- http://admin:admin@localhost:9091/metrics/job/$u"
  if [[ "$users_logged" == *"$u"* ]]; then
    # echo "some_metric 3.14" | curl --data-binary @- http://admin:admin@localhost:9091/metrics/job/some_job
    echo "linux_user 1" | `echo $cmd`
  else
    echo "linux_user 0" | `echo $cmd`
  fi
done

Once you Pushgateway is up and running at http://127.0.0.1:9091/ you run the script in a corn job and it will push the values to Pushgateway. enter image description here

2 - 仅使用普罗米修斯

如果您不想使用 Pushgateway,您可以将您的应用程序配置为收集登录您计算机的用户并使用普罗米修斯客户端库它适合您的应用程序编程语言并公开指标端点并让 Prometheus 从中提取数据。这是一个使用的示例Spring Boot 在 Java 中使用千分尺.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Prometheus 和 Grafana - 有没有办法让用户使用机器? 的相关文章

随机推荐