我在Linux上安装了RabbitMQ,它是一个很棒的软件。
当我运行此命令时:
sudo rabbitmqctl status
我得到了一堆输出:
[{pid,18665},
{running_applications,
[{rabbitmq_management,"RabbitMQ Management Console","3.1.5"},
{rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.1.5"},
{webmachine,"webmachine","1.10.3-rmq3.1.5-gite9359c7"},
{mochiweb,"MochiMedia Web Server","2.7.0-rmq3.1.5-git680dba8"},
{rabbitmq_management_agent,"RabbitMQ Management Agent","3.1.5"},
{rabbit,"RabbitMQ","3.1.5"},
{os_mon,"CPO CXC 138 46","2.2.7"},
{inets,"INETS CXC 138 49","5.7.1"},
{xmerl,"XML parser","1.2.10"},
{mnesia,"MNESIA CXC 138 12","4.5"},
{amqp_client,"RabbitMQ AMQP Client","3.1.5"},
{sasl,"SASL CXC 138 11","2.1.10"},
{stdlib,"ERTS CXC 138 10","1.17.5"},
{kernel,"ERTS CXC 138 10","2.14.5"}]},
{os,{unix,linux}},
{erlang_version,
"Erlang R14B04 (erts-5.8.5) [source] [64-bit] [rq:1] [async-threads:30] [kernel-poll:true]\n"},
{memory,
[{total,179426464},
{connection_procs,300224},
{queue_procs,14434024},
{plugins,474968},
{other_proc,9607952},
{mnesia,89264},
{mgmt_db,1539936},
{msg_index,85175152},
{other_ets,29060560},
{binary,18243208},
{code,17504466},
{atom,1602617},
{other_system,1394093}]},
{vm_memory_high_watermark,0.4},
{vm_memory_limit,1522479923},
{disk_free_limit,1000000000},
{disk_free,58396659712},
{file_descriptors,
[{total_limit,924},{total_used,17},{sockets_limit,829},{sockets_used,4}]},
{processes,[{limit,1048576},{used,233}]},
{run_queue,0},
{uptime,5169640}]
它看起来像JSON,但它不是。
这是什么数据格式?你是怎么发现的?
我能找到的最近的事情是:http://erlang.org/doc/man/yecc.html
答案 0 :(得分:3)
而不是查询rabbitctrl进程,我建议查询将返回JSON的REST api。
GET: http://localhost:15672/api/overview
以下是文档:
http://hg.rabbitmq.com/rabbitmq-management/raw-file/3646dee55e02/priv/www-api/help.html
答案 1 :(得分:2)
from tkinter import *
root = Tk()
root.geometry("600x600-400+50")
root.title("Game")
class App:
def __init__(self, master):
self.master = master
self.rbvar = IntVar()
self.stonerb = Radiobutton(self.master, text="Stone",
variable=self.rbvar, value=1, command=self.figurefunc)
self.stonerb.pack()
self.blackrb = Radiobutton(self.master, text="Black",
variable=self.rbvar, value=2, command=self.figurefunc)
self.blackrb.pack()
self.board = Canvas(self.master, bg='#b35900', width=500, height=500)
self.board.pack()
self.figure = self.board.create_polygon(0,0, 0,0, 0,0, 0,0, 0,0, 0,0)
self.stoneCoords = 60,200, 100,200, 120,230, 100,260, 60,260, 40,230
self.blackCoords = 440,200, 400,200, 380,230, 400,260, 440,260, 460,230
def figurefunc(self):
if self.rbvar.get() == 1:
self.board.coords(self.figure, self.stoneCoords)
self.board.itemconfig(self.figure, fill='#f2f2f2')
else:
self.board.coords(self.figure, self.blackCoords)
self.board.itemconfig(self.figure, fill='black')
myapp = App(root)
的输出格式是Erlang术语或Erlang ETF(外部术语格式)。
您可以使用python库erl_terms将输出转换为Python可用的内容:
rabbitmqctl
答案 2 :(得分:1)
如果有人对使用命令行util而不是UI插件感兴趣,则可以使用我编写的这个开源简单解析器: https://github.com/yuvaltir/RabbitMQNet 使用示例:
DATE(DATE)
答案 3 :(得分:1)
rabbitmqctl
具有--formatter
标志,以请求可选的JSON格式的输出。例如:
sudo rabbitmqctl status --formatter json | jq .disk_free_limit
50000000
答案 4 :(得分:0)
这是我在python中为此目的制作的方法,它只需要PyYAML(在pypi中可用)。第一次修订,可能是次优或错误,但对我有用:
import re
import subprocess
import yaml
def fix_dicts(json_str_list, pos):
'''this recursive function puts all comma-separted values into square
brackets to make data look like normal 'key: value' dicts'''
quoted_string = False
value = True
value_pos = 0
commas = False
is_list = False
in_list = 0
while pos < len(json_str_list):
if not quoted_string:
if json_str_list[pos] == '{':
json_str_list, pos = fix_dicts(json_str_list, pos+1)
elif json_str_list[pos] == '"':
quoted_string = True
elif json_str_list[pos] == ':':
value = True
value_pos = pos + 1
elif json_str_list[pos] == '[':
if value and not commas:
is_list = True
in_list += 1
elif json_str_list[pos] == ']':
in_list -= 1
elif json_str_list[pos] == ',':
commas = True
if not in_list:
is_list = False
elif json_str_list[pos] == '}':
if not is_list and commas:
json_str_list = (json_str_list[:value_pos] + ['['] +
json_str_list[value_pos:pos] + [']'] +
json_str_list[pos:])
pos += 2
return json_str_list, pos
elif json_str_list[pos] == '"':
quoted_string = False
pos += 1
return json_str_list, pos
def squash_dicts(input_data):
# recursively converts [{a:1},{b:2},{c:3}...] into {a:1, b:2, c:3}'''
if type(input_data) is list:
for i in range(len(input_data)):
input_data[i] = squash_dicts(input_data[i])
if all([type(e) is dict for e in input_data]):
input_data = dict([(k,v) for e in input_data for k,v in e.items()])
elif type(input_data) is dict:
for k, v in input_data.items():
input_data[k] = squash_dicts(v)
return input_data
text = subprocess.check_output(['rabbitmqctl','status'])
text = text.splitlines()
text = text[1:] # skip the first line "Status of node..."
text = ''.join(text) # back into string for regex processing
# quote strings
bad_yaml = re.sub(r'([,{])([a-z_A-Z]+)([,}])', r'\1"\2"\3', text)
# change first element into a key - replacing ',' with ':'
bad_yaml = re.sub(r'({[^,]+),',r'\1:', bad_yaml)
bad_yaml_list = list(bad_yaml) # into a list for another fix
good_yaml, _ = fix_dicts(bad_yaml_list, 0)
status_list = yaml.load(''.join(good_yaml))
status_dict = squash_dicts(status_list)
# now we can use "status_dict" - it's an ordinary dict
print(yaml.safe_dump(status_dict, default_flow_style=False))
答案 5 :(得分:-1)
这是一个很长的单线,除了“标准内容”外没有任何依赖项。它将输出转换为json:
rabbitmqctl -q cluster_status | tr '{}()<>'\' '[][][]"' | sed -e 's|\([\[,]\)\([A-Za-z][^,\]*\)|\1"\2"|g'