如何解决 Telegraf 中的此错误?

问题描述

我有一个自定义的 Python 插件,用于将数据拉入 Telegraf。它按预期打印出线路协议输出

在我的 Ubuntu 18.04 环境中,当这个插件运行时,我在我的日志中看到一行:

2020-12-28T21:55:00Z E! [inputs.exec] Error in plugin: exec: exit status 1 for command '/my_company/plugins-enabled/plugin-mysystem/poll_mysystem.py': Traceback (most recent call last):...

就是这样。我不知道如何获得实际的回溯。

如果我运行 sudo -u telegraf /usr/bin/telegraf -config /etc/telegraf/telegraf.conf插件会按预期工作。它完全按照应有的方式轮询和加载数据。

当 Telegraf 自行执行插件时,我不确定如何解决错误

我已经重新启动了 Telegraf 服务。我已经验证了权限(我认为上面的执行表明它应该可以工作)。

根据收到的评论和答案提供的一些其他详细信息:

插件代码 (/my_company/plugins-enabled/plugin-mysystem/poll_mysystem.py):

from google.auth.transport.requests import Request
from google.oauth2 import id_token
import requests
import os

RUNTIME_URL = INTERNAL_URL
MEASUREMENT = "MY_MEASUREMENT"
CREDENTIALS = "GOOGLE_SERVICE_FILE.json"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = CREDENTIALS  # ENV VAR required BY GOOGLE CODE BELOW
CLIENT_ID = VALUE_FROM_GOOGLE

exclude_fields = ["name","version"] # Don't try to put these into influxdb from json response

def make_iap_request(url,client_id,method="GET",**kwargs):
    # Code provided by Google docs
    # Set the default timeout,if missing
    if "timeout" not in kwargs:
        kwargs["timeout"] = 90

    # Obtain an OpenID Connect (OIDC) token from Metadata server or using service
    # account.
    open_id_connect_token = id_token.fetch_id_token(Request(),client_id)

    # Fetch the Identity-Aware Proxy-protected URL,including an
    # Authorization header containing "Bearer " followed by a
    # Google-issued OpenID Connect token for the service account.
    resp = requests.request(method,url,headers={"Authorization": "Bearer {}".format(open_id_connect_token)},**kwargs)
    if resp.status_code == 403:
        raise Exception("Service account does not have permission to " "access the IAP-protected application.")
    elif resp.status_code != 200:
        raise Exception(
            "Bad response from application: {!r} / {!r} / {!r}".format(resp.status_code,resp.headers,resp.text)
        )
    else:
        return resp.json()


def print_results(results):
    """
    Take the results of a Dolores call and print influx line protocol results
    """
    for item in results["workflow"]:
        line_protocol_line_base = f"{MEASUREMENT},name={item['name']}"
        values = ""
        for key,value in item.items():
            if key not in exclude_fields:
                values = values + f",{key}={value}"
        values = values[1:]
        line_protocol_line = f"{line_protocol_line_base} {values}"
        print(line_protocol_line)


def main():
    current_runtime = make_iap_request(URL,CLIENT_ID,timeout=30)
    print_results(current_runtime)


if __name__== "__main__":
    main()

telegraf.conf 文件的相关部分:

[[inputs.exec]]
  ## Commands array
  commands = [
    "/my_company/plugins-enabled/plugin-*/poll_*.py",]

配置文件的代理部分

[agent]
  interval = "60s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = "/var/log/telegraf/telegraf.log"
  hostname = ""
  omit_hostname = true

接下来我该怎么办?

解决方法

exec 插件会在换行符处截断您的异常消息。如果您将 make_iap_request 的调用包含在 try/except 块中,然后 print(e,file=sys.stderr) 而不是让 Exception 一直冒泡,这应该会告诉您更多信息。

def main():
    """
    Query URL and print line protocol
    """
    try:
        current_runtime = make_iap_request(URL,CLIENT_ID,timeout=30)
        print_results(current_runtime)
    except Exception as e:
        print(e,file=sys.stderr)

或者,您的脚本可以将错误消息记录到它自己的日志文件中,而不是将它们传回 Telegraf。这会让您更好地控制记录的内容。

我怀疑您遇到了环境问题,即您的运行方式有所不同。如果不是权限,可能是环境变量不同。

,

请检查权限。 好像是权限错误。由于 Telegraf 具有运行 sudo -u telegraf 的必要权限。但是您尝试使用的 user 没有访问 /my_company/plugins-enabled/ 中文件的必要权限。

因此,我建议您查看它们并将权限更改为 Other can access and write 或您尝试使用 Telegraf 的用户名。

为了解决此问题,请运行转到目录的命令:

cd /my_company/plugins-enabled/

然后将所有权更改为您并且只有您:

sudo chown -R $(whoami)

然后更改对所有文件和文件夹的读/写权限,否则:

sudo chmod -R u+w

如果您希望系统上的每个人实际上每个人都有权读取/写入这些文件和文件夹,并且只想将所有权限授予每个人:

sudo chmod -R 777

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...