简体   繁体   English

如何使用linux将多个值从kapacitor存储到python变量中?

[英]how to store multiple values from kapacitor into python variable using linux?

This command gives multiple values.此命令给出多个值。

kapacitor list tasks | grep -i enabled

I need to iterate through and store the output of the above command.我需要遍历并存储上述命令的输出。 I tried我试过

enabled = os.system("kapacitor list tasks | grep -i enabled")
print enabled

Output for this command kapacitor list tasks |此命令的输出 kapacitor list tasks | grep -i enabled grep -i 启用

Alert_ALL_metrics_cpu stream enabled true ["metrics_NWNA"."autogen" "metrics_NN"."autogen"] tAlert_ALL_metrics_memory_usage stream enabled true ["metrics_NWNA"."autogen" "metrics_NN"."autogen"] tAlert_ALL_oracle_TBS_offline stream enabled true ["oracle_NWNA"."autogen" "oracle_NN"."autogen"] tAlert_NN_WMS_endpoint-message-count_MSE stream enabled true ["metrics"."autogen"] tAlert_NN_ecom_version_check stream enabled true ["metrics_NN"."autogen"] tAlert_NN_ecom_version_check_all_farms stream enabled true ["metrics_NN"."autogen"] tAlert_NN_metrics_fileSystem stream enabled true ["metrics_NN"."autogen"] tAlert_NWNA_metrics_fileSystem stream enabled true ["metrics_NWNA"."autogen"] Alert_ALL_metrics_cpu 流启用真 ["metrics_NWNA"."autogen" "metrics_NN"."autogen"] tAlert_ALL_metrics_memory_usage 流启用真 ["metrics_NWNA"."autogen" "metrics_NN"."autogen"] tAlert_ALL_oracle_TBS_NWNA_offline 启用流autogen" "oracle_NN"."autogen"] tAlert_NN_WMS_endpoint-message-count_MSE 流启用 true ["metrics"."autogen"] tAlert_NN_ecom_version_check 流启用 true ["metrics_NN"."autogen"] tAlert_NN_ecom_version_check_all_farms 流启用 true ["metrics"."autogen"] autogen"] tAlert_NN_metrics_fileSystem 流启用 true ["metrics_NN"."autogen"] tAlert_NWNA_metrics_fileSystem 流启用 true ["metrics_NWNA"."autogen"]

Instead of using the os module, try using the subprocess module.不要使用 os 模块,而是尝试使用 subprocess 模块。

import subprocess导入子流程

ele = subprocess.Popen(['kapacitor', 'list', 'tasks', '|', 'grep', '-i', 'enabled'] shell=True)
output, err = ele.communicate()
print(output)

This will store the output of the command ran inside the output(as a variable) then you can call it and do whatever you want with it from there这将存储在输出中运行的命令的输出(作为变量)然后你可以调用它并从那里做任何你想做的事情

Not sure what variable come with your "enable" but try below.不确定“启用”附带什么变量,但请尝试以下方法。

from subprocess import check_output

datas = []

data = check_output("kapacitor list tasks | grep -i enabled")

print (data)

for i, j, k in data:    # i,j,k are just three variables in example. Add more if needed.
    datas.append((i,j),(k))

print (datas[0])  # prints (i,j)

For other type of subprocess command check SO question: Linux command-line call not returning what it should from os.system?对于其他类型的子进程命令检查 SO 问题: Linux 命令行调用未从 os.system 返回它应该返回的内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM