如何比较 Python 处的两个 Json 数组?

How can I compare two Json arrays at Python?

我正在创建一个脚本,它允许将两个 ssh 命令输出与远程 Netapp 进行比较,并且允许在当前值与 Netapp 机舱具有的 space 的最大值之间进行比较。

我已经在两个字典(rv 和 rv 2)中收集了这些值,然后我将它们转换为 JSON 格式,以便能够根据传递给它的警告参数来比较它们(如果超过这个限制,就必须通知)。

import subprocess
import argparse
import sys
import json
import re
from subprocess import check_output
def parse_args(argv):
    parser = argparse.ArgumentParser()  
    parser.add_argument("-u", "--user", action="store",
                        help="User for login",
                        dest="user")
    parser.add_argument("-p", "--pwd", action="store",
                        help="Password",
                        dest="pwd")
    parser.add_argument("-i", "--ip", action="store",
                        help="Machine ip",
                        dest="ip")
    parser.add_argument("-w", "--warning", action="store",
                        help="Warning threshold",
                        type = int,
                        dest="warning")
    parser.add_argument("-m", "--machine_command", action="store",
                        help="Command",
                        type = int,
                        dest="ssh_command")
    parser.add_argument("-M", "--machine_command_max", action="store",
                        help="Max value for command",
                        type = int,
                        dest="ssh_command_max")



    args = parser.parse_args()
    return args
args = parse_args(sys.argv[1:])

command = 'sshpass -p ' +args.pwd+ ' ssh ' + args.user + '@' +args.ip+ args.ssh_command 
output = check_output(command, shell=True)
#Command to retrieve the max_values
command_max = 'sshpass -p ' +args.pwd+ ' ssh ' + args.user + '@' +args.ip+ args.ssh_command_max
output_max = check_output(command_max, shell=True)


rv = {}
current_node = None

for match in re.findall(r'Machine_name (name.*\d+)|(Metric_name_.*)', output):
    node, metric = match
    if node and current_node != node:
       current_node = node
    if current_node and metric:
        name, value = metric.strip().split()
        rv[current_node.strip() + "." + name.replace("Alloc", "")] = [value] 
#print(';'.join(rv))

rv2 = {}
current_node = None

for match in re.findall(r'Machine_name (name.*\d+)|(Max_metric.*)', output_max):
    node, metric = match
    if node and current_node != node:
        current_node = node
    if current_node and metric:
        name, value = metric.strip().split()
        rv2[current_node.strip() + "." + name.replace("Max", "")] = [(value/100) * args.warning] 

json1=json.dumps(rv, sort_keys=True)
json2=json.dumps(rv2, sort_keys=True)



def get_values(json, lst):
  if isinstance(json, list):
    for item in json: get_values(item, lst)
  elif isinstance(json, dict):
    for item in json.values(): get_values(item, lst)
  else: lst.append(json)


list1 = []; get_values(json1, list1)
list2 = []; get_values(json2, list2)

diff = [(n, x, y) for n,(x,y) in enumerate(zip(list1, list2)) if x <= y]
print(diff)

我要比较的值的示例: RV1:

{'node1.storePool_Owner': ['160'], 'node1.storePool_Deleg': ['0'], 'node2.storePool_LockState': ['0']}

RV2:

{'node1.storePool_Owner': ['1024000'], 'node1.storePool_Deleg': ['1024000'], 'node2.storePool_LockState': ['1024000']}

我们的想法是将这些值中的每一个与其最大等效值进行比较。

非常感谢您的帮助。

如何比较的例子: 如果此节点具有该值:

node1.storePool_Owner': ['160']

达到以下的 X%(警告参数):

node1.storePool_Owner': ['1024000']

那么应该return:

WARNING: node1.storePool_Owner has exceeded the threshold (x%)

这将比较两个 json files/dictionaries。我注意到值是字符串列表...这是故意的吗?

import json


def open_json(path):
    with open(path, 'r') as file:
        return json.load(file)


def check_thresholds(data, thresholds):
    for k, v in thresholds.items():
        # your logic/output here
        # k[0] and v[0] because your values are lists of strings... should they be just int of float?
        difference = int(data.get(k)[0]) - int(v[0])
        if difference >= 0:
            print(f'WARNING: {k} has exceeded the threshold by {difference}')
        else:
            print(f'OK: {k}')


def main():
    # load the data into dictionaries
    data = open_json('./rv.json')
    thresholds = open_json('./rv2.json')

    # if your data is a dictionary and not json files then use these
    # data = {'node1.storePool_Owner': ['160'], 'node1.storePool_Deleg': ['0'], 'node2.storePool_LockState': ['0']}
    # thresholds = {'node1.storePool_Owner': ['1024000'], 'node1.storePool_Deleg': ['1024000'], 'node2.storePool_LockState': ['1024000']}

    # run the checks
    check_thresholds(data, thresholds)


main()

输出(我修改了一些值以显示警告):

WARNING: node1.storePool_Owner has exceeded the threshold by 1000
OK: node1.storePool_Deleg
OK: node2.storePool_LockState