【问题标题】:P4Python Perforce API does not honor P4_TRUST when running as a Collectd Python (2.7) PluginP4Python Perforce API 在作为 Collectd Python (2.7) 插件运行时不支持 P4_TRUST
【发布时间】:2017-04-27 17:20:28
【问题描述】:

使用 Collectd Python 插件通过 Perforce 的 P4Python API 收集 Graphite Metrics。除了需要 P4_TRUST 命令在 login() 命令之前为给定服务器断言指纹的基于 ssl 的连接之外,一切都很好。

具体来说:

import collectd
from P4 import P4

def configer(configobj):
    collectd.info('Configuring Perforce Plugin')


def initer():
    collectd.info('Initializing Perforce Plugin')


def reader():
    p4 = P4()
    p4.port = "ssl:perforce1.lab.dell.com:1666"
    p4.user = "perforce"
    p4.charset = 'utf8'
    p4.password = "Password"
    p4.connect()
    p4.run_trust("-i", '60:27:3E:E0:A6:58:8E:13:4F:9E:8C:C8:BF:C3:26:C2:91:2F:78:33')
    p4.run_login()

    result = p4.run('license', '-u')
    collectd.info("User limit is :" + result[0].get('userLimit'))

#
    p4.disconnect()

# -- Hook Callbacks, Order is important! ==#
collectd.register_config(configer)
collectd.register_init(initer)
collectd.register_read(reader)

将导致 collectd.log 中记录的以下运行时错误

`[2017-04-27 11:33:58] Unhandled python exception in read callback: P4Exception: [P4#run] Errors during command execution( "p4 login" )

    [Error]: "The authenticity of '10.99.248.42:1666' can't be established,\nthis may be your first attempt to connect to this P4PORT.\nThe fingerprint for the key sent to your client is\n60:27:3E:E0:A6:58:8E:13:4F:9E:8C:C8:BF:C3:26:C2:91:2F:78:33\nTo allow connection use the 'p4 trust' command."


[2017-04-27 11:33:58] read-function of plugin `python.PerforceMetrics' failed. Will suspend it for 20.000 seconds.
`

在 collectd 上下文之外运行的相同代码按预期运行。

from P4 import P4
import logging

FORMAT = '%(asctime)-15s: %(name)s: %(levelname)s :  %(message)s'
logging.basicConfig(format=FORMAT, filename='/var/log/pytest.log', level=logging.INFO)

def reader():
    p4 = P4()
    p4.port = "ssl:perforce1.cec.lab.emc.com:1666"
    p4.user = "perforce"
    p4.charset = 'utf8'
    p4.password = "Password"
    p4.connect()
    p4.run_trust("-i", '80:25:3E:E0:A6:58:8E:13:4F:9E:8C:C8:BF:C3:26:C2:91:2F:78:33')
    p4.run_login()

    result = p4.run('license', '-u')
    logging.info ("User limit is :" + result[0].get('userLimit'))

    #
    p4.disconnect()

reader()

注意:不需要“P4 信任”的 SSL 连接在 collectd 下无法正常工作。

任何线索、想法或如何解决这个问题?

【问题讨论】:

  • 您确定您的“p4.run_trust”命令有效吗?您是否查看了 ~/.p4trust 文件以验证它是否包含您期望的信息,与您从命令行运行“p4 trust”时的外观相比?
  • Bryan,是的,我确定 p4 信任运行正常...注意:没有 collectd 的第二个代码集...这工作正常。事实上,当我从 PyCharm 运行我的代码时,即使加载了 collectd python 库,它也会正确执行......它只是失败并在 Collectd 守护程序加载 Python 插件时引发信任错误。顺便说一句...使用 p4python API 不使用 .p4trust 文件...而是直接指定指纹p4.run_trust("-i", '80:25:3E:E0:A6:58:8E:13:4F:9E:8C:C8:BF:C3:26:C2:91:2F:78:33')

标签: python-2.7 perforce collectd


【解决方案1】:

好的,所以与 Perforce 支持合作,当他们努力让 collectd 运行时,他们确实为我指明了正确的方向。具体来说,可以将 P4TRUST 作为环境变量定义为指向通常使用的 .p4trust 文件,默认情况下,该文件是使用 p4.run_trust p4Python API 调用的应用程序创建的。 所以诀窍是在你的 python 插件中定义环境 P4TRUST 环境变量:

import collectd
import os
from P4 import P4

def configer(configobj):
    collectd.info('Configuring Perforce Plugin')


def initer():
    collectd.info('Initializing Perforce Plugin')


def reader():
    os.environ["P4TRUST"]="/root/.p4trust"
    p4 = P4()
    p4.port = "ssl:perforce1.lab.dell.com:1666"
    p4.user = "perforce"
    p4.charset = 'utf8'
    p4.password = "Password"
    p4.connect()
    p4.run_trust("-i", '60:27:3E:E0:A6:58:8E:13:4F:9E:8C:C8:BF:C3:26:C2:91:2F:78:33')
    p4.run_login()

result = p4.run('license', '-u')
collectd.info("User limit is :" + result[0].get('userLimit'))

#
p4.disconnect()

# -- Hook Callbacks, Order is important! ==#
collectd.register_config(configer)
collectd.register_init(initer)
collectd.register_read(reader) 

注意:os库的包含和使用

os.environ["P4TRUST"]="/root/.p4trust"

就是这样!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多