我正在创建一个脚本,允许将两个 ssh 命令输出与远程 Netapp 进行比较,并允许在当前值和 Netapp 机舱拥有的空间最大值之间进行比较。
我已经在两个字典(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']}
房车2:
{'node1.storePool_Owner': ['1024000'], 'node1.storePool_Deleg': ['1024000'], 'node2.storePool_LockState': ['1024000']}
想法是将这些值中的每一个与它们的最大等效值进行比较。
非常感谢你的帮助。
应该如何进行比较的示例: 如果此节点,具有该值:
node1.storePool_Owner': ['160']
达到 X%(警告 arg):
node1.storePool_Owner': ['1024000']
然后它应该返回:
WARNING: node1.storePool_Owner has exceeded the threshold (x%)