【发布时间】:2021-02-01 23:11:17
【问题描述】:
我有这个键:json 中的值
"text": "CRITICAL: Alert for device amst-asw1 - Port status Down\nSeverity: critical\nTimestamp: 2021-02-01 10:53:16\nUnique-ID: 307849\nRule: Port status Down Faults:\n #1: sysObjectID = .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr = Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id = 17; port_id = 4101; ifDescr = vme; \n #2: sysObjectID = .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr = Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id = 17; port_id = 4128; ifDescr = ge-0/0/4; \n #3: sysObjectID = .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr = Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id = 17; port_id = 4136; ifDescr = ge-0/0/8; \nAlert sent to:\n",
我只想解析该值并从中获取 ifDescr 字符串。
import json
import os
import requests
import re
with open('alerts.json', 'r') as json_file:
data = json.load(json_file)
for a in data['history']:
text = a.get('text')
if re.search(r"ifDescr", text):
print(text)
我明白了
CLEARED: Device amst-asw1 recovered from Port status Down
Severity: critical
Time elapsed: 3d 20h 25m 34s Timestamp: 2021-02-01 10:41:39
Unique-ID: 307845
Rule: Port status Down Faults:
#1: sysObjectID => .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr => Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id => 17; port_id => 4101; ifDescr => vme;
#2: sysObjectID => .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr => Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id => 17; port_id => 4128; ifDescr => ge-0/0/4;
#3: sysObjectID => .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr => Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id => 17; port_id => 4136; ifDescr => ge-0/0/8;
Alert sent to:
我只想要这个
ifDescr = ge-0/0/8
【问题讨论】:
标签: json python-3.x python-re