【问题标题】:Create LLD Zabbix discovery output创建 LLD Zabbix 发现输出
【发布时间】:2018-12-28 10:00:32
【问题描述】:

从这个脚本

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)

我明白了:

example_job_1

example_job_2

example_job_3

example_job_4

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

期望的输出

{
    "data": [
        {
            "{#job}": "example_job_1"
        },
        {
           "{#job}": "example_job_2"
        },        
        {
           "{#job}": "example_job_3"
        },
        {
           "{#job}": "example_job_3"
        },
        {
         "{#job}": "example_job_4" 
        }    
           ]
  }        

我修改了上面的脚本:

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}": "example_job_1"
        }
    ]
}
{
    "data": [
        {
            "{#job}": "example_job_2"
        }
    ]
}
{
    "data": [
        {
            "{#job}": "example_job_3"
        }
    ]
}
{
    "data": [
        {
            "{#job}": "example_job_4"
        }
    ]
}

【问题讨论】:

    标签: python json zabbix


    【解决方案1】:

    已解决:“忘记”在“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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多