【问题标题】:Troubleshooting Cron with Curl on Pushgateway (Prometheus)在 Pushgateway (Prometheus) 上使用 Curl 对 Cron 进行故障排除
【发布时间】:2020-02-10 20:51:06
【问题描述】:

我正在 Prometheus 上测试 Pushgateway,每分钟使用 bash 脚本和 curl 推送有关我的虚拟机服务的简单数据,由 crontab 触发。

我有两个 bash 脚本。 第一个,cpu_usage.sh,通过 curl 解析并发送关于 ps -aux 命令的数据到 pushgateway:

#!/usr/bin/env bash

z=$(ps aux)
while read -r z
do
   var=$var$(awk '{print "cpu_usage{process=\""$11"\", pid=\""$2"\", cpu=\""$3"\"}", $3z}');
done <<< "$z"

curl -X POST -H  "Content-Type: text/plain" --data "$var
" http://localhost:9091/metrics/job/top/instance/machine

这一切都很好,我的 crontab 每分钟都会将格式化的数据发送到localhost:9091,我的 pushgateway 实例。

现在,我正在尝试使用名为 services_list.sh 的脚本发送 service --status-all 命令的解析结果,这与我处理 cpu_usage.sh 脚​​本的方式完全相同:

#!/usr/bin/env bash

y=$(service --status-all)
while read -r y
do
   varx=$varx$(awk '{print "services_list{service=\""$y"\"}", 1}');
done <<< "$y"

curl -X POST -H  "Content-Type: text/plain" --data "$varx
" http://localhost:9091/metrics/job/top/instance/machine

当手动执行这两个脚本时,比如./cpu_usage.sh./services_list.sh,一切都很好,Pushgateway 正在成功地从两个脚本中检索数据。

但是当我通过 CRON 传递这些调用时,只有 cpu_usage.sh 正在向 pushgateway 发送数据(Pushgateway 上 services_list 方法上最后一次推送的时间戳保持不变)。

我的 crontab 语法是这样的:* * * * * cd /path/ &amp;&amp; ./script.sh

脚本位于777 / root:root,crontab 始终以 root 身份编辑。我试图将两个 scipts 连接到一个 bash 文件中,无论我放置它们的顺序如何,都不会发出对 services_list 方法的 curl 调用(但在 cpu_usage 方法上一切正常)。

我有点迷茫,因为这两个脚本非常相似,手动调用 services_list.sh 工作正常。有什么想法吗 ?谢谢。

在带有 Pushgateway 0.10 的 Debian 9 上运行。

【问题讨论】:

    标签: bash curl cron debian prometheus


    【解决方案1】:

    Fixed :

    命令service 不能从 crontab 中调用。从 cron 调用时,我必须提供 service 资源的完整路径,例如 /usr/sbin/service --options

    在这种情况下,在我的 services_list.sh 脚​​本中使用 y=$(/usr/sbin/service --status-all) 代替 y=$(service --status-all) 一切正常。

    【讨论】:

      猜你喜欢
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 2020-10-02
      • 2011-02-07
      • 2013-11-20
      相关资源
      最近更新 更多