创建LLD Zabbix发现输出

时间:2018-12-28 10:00:32

标签: python json zabbix

通过此脚本

items = items[0].split()
for emailid in items:
    resp, data = conn.uid("fetch",emailid, "(RFC822)")
    if resp == 'OK':
        email_body = data[0][1].decode('utf-8')
        mail = email.message_from_string(email_body)
        #print email_body
        if mail["Subject"].find("PA1") > 0 or mail["Subject"].find("PA2") > 0:
          regex1 = r'(?<!^)JOB:\s*(\S+)'
          #regex2 = r'Job finished'
          #c=re.findall(regex2, email_body, re.IGNORECASE)
          a=re.findall(regex1 ,email_body) #re.IGNORECASE)
          if a:
           b=set(a)
           seen = set()
           result = []
           for item in a:
              if item not in seen:
               seen.add(item)
               result.append(item)
               output = " ".join(result)

我得到:

p1_risk_mandates_otc_count_breaches

p1_risk_mandates_calc_levels_rerun_late

p1_run_autofix_fx_1030_nyc_mxpp01

p1_risk_mandates_upcoming_limit_changes

我想从上面的输出中创建以下格式的JSON输出。从这个输出中,我试图创建Zabbix外部脚本

所需的输出

{
    "data": [
        {
            "{#job}": "p1_risk_mandates_otc_count_breaches"
        },
        {
           "{#job}": "p1_risk_mandates_calc_levels_rerun_late"
        },        
        {
           "{#job}": "p1_run_autofix_fx_1030_nyc_mxpp01"
        },
        {
           "{#job}": "p1_run_autofix_fx_1030_nyc_mxpp01"
        },
        {
         "{#job}": "p1_risk_mandates_upcoming_limit_changes" 
        }    
           ]
  }        

我修改了上面的脚本:

items = items[0].split()
for emailid in items:
    resp, data = conn.uid("fetch",emailid, "(RFC822)")
    if resp == 'OK':
        email_body = data[0][1].decode('utf-8')
        mail = email.message_from_string(email_body)
        #print email_body
        if mail["Subject"].find("PA1") > 0 or mail["Subject"].find("PA2") > 0:
          regex1 = r'(?<!^)JOB:\s*(\S+)'
          #regex2 = r'Job finished'
          #c=re.findall(regex2, email_body, re.IGNORECASE)
          a=re.findall(regex1 ,email_body) #re.IGNORECASE)
          if a:
           b=set(a)
           seen = set()
           result = []
           for item in a:
              if item not in seen:
               seen.add(item)
               result.append(item)
               output = " ".join(result)
    data = [{"{#job}": output}]
    print json.dumps({"data": data}, indent=4)

并获得实际输出

{
    "data": [
        {
            "{#job}": "p1_risk_mandates_otc_count_breaches"
        }
    ]
}
{
    "data": [
        {
            "{#job}": "p1_risk_mandates_calc_levels_rerun_late"
        }
    ]
}
{
    "data": [
        {
            "{#job}": "p1_run_autofix_fx_1030_nyc_mxpp01"
        }
    ]
}
{
    "data": [
        {
            "{#job}": "p1_risk_mandates_upcoming_limit_changes"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

已解决:“忘记”在“ for循环”之前初始化空列表,因此列表在每次迭代时都会重置

tdata=[]



items = items[0].split()
for emailid in items:
    resp, data = conn.uid("fetch",emailid, "(RFC822)")
    if resp == 'OK':
        email_body = data[0][1].decode('utf-8')
        mail = email.message_from_string(email_body)
        if mail["Subject"].find("PA1") > 0 or mail["Subject"].find("PA2") > 0:
          regex1 = r'(?<!^)JOB:\s*(\S+)'
          #regex2 = r'Job finished'
          #c=re.findall(regex2, email_body, re.IGNORECASE)
          a=re.findall(regex1 ,email_body) #re.IGNORECASE)
          if a:
           b=set(a)
           seen = set()
           result = []
           for item in a:
              if item not in seen:
               seen.add(item)
               result.append(item)
               output = " ".join(result)
               tdata.append({'#job':output})
print json.dumps({"data": tdata}, indent=4)