【发布时间】:2018-01-14 15:36:54
【问题描述】:
我编写了一个 python 代码来生成一个 YAML 文件(称为targets.yml),该文件由一个名为 prometheus 的流行监控应用程序读取。 prometheus 成功读取了 YAML 文件及其内容,但它也会在日志中引发错误,如下所示。
level=error msg="Error reading file "/var/targets/targets.yml": yaml:
line 218: found unexpected end of stream" source="file.go:199"
我无法摆脱这个错误,尽管我适当地关闭了 YAML 文件,下面是它的代码:-
while True:
create()
with open('/var/targets/targets.yml', 'w') as output:
print "opened the file to write"
i=0
for item in result:
if(item != None and item['status'] == "ACTIVE"):
print item['domains']['partner']
print item['status']
output.write("\n\n")
output.write("- targets: ['" + "https://" + item["domains"]["agency"] + "']\n")
output.write(" labels:\n")
output.write(" alias: " + item["alias"])
foo=item["name"]
#print foo
if isinstance(foo,basestring):
foo=foo.encode('utf8')
else:
foo=unicode(foo).encode('utf8')
output.close()
print("Waiting for 300 seconds, before relooping")
time.sleep(100)
我也不认为我的文件扩展名有什么不同。有人可以建议吗?
【问题讨论】:
-
prometheus 期望文件的结构如何?你需要单独的 yaml 文件吗?这些用 --- 或 ... 划定(我不记得了)。您是否可能需要附加到文件而不是覆盖它?
-
@PhilipStark 我想我编写 yaml 文件的方式应该没问题,因为如果它不是普罗米修斯预期的,它会给我一个错误写掉。相反,它会以随机间隔抛出一个错误,我想知道它是否与
refresh interval(默认值为 300 秒)和每 100 秒后我的应用程序生成 yaml 文件有关(如上面的应用程序代码)。请注意,在我的情况下,我必须覆盖该文件。 -
那么我真的建议你使用 pyyaml 模块来构建你的 yaml 文档。在某些情况下,您可能会遇到 yaml 语法错误。 item["alias"] 或 item["domains"]["agency"] 的可能值是什么?
-
@PhilipStark item[alias] 只是一个类似 "example" 的字符串,而 item["domains"]["agency"] 也是一个类似 "ui.xyz.abc.net" 的字符串。就像你说的,也许 pyyaml 是一个更好的选择。
标签: python python-2.7 python-3.x yaml prometheus