【问题标题】:Get beautiful website output using python for zabbix api?使用 python for zabbix api 获得漂亮的网站输出?
【发布时间】:2021-09-13 08:27:11
【问题描述】:

有一段python代码:

    import json
    
    from pyzabbix import ZabbixAPI
    
    username = "user"
    password = "*************"
    servername = "https://<name of the site.com>/api_jsonrpc.php"
    
    zapi = ZabbixAPI(servername)
    zapi.session.verify = False
    zapi.login(username, password)
    
    hosts = zapi.host.get(
        output=["host"], groupids=[25, 41, 43], selectInventory=["site_notes"],
    )
    
    print(json.dumps(hosts, indent=4, sort_keys=True))

我们得到输出(指定部分):

[
    {
        "host": "CR-20",
        "hostid": "10355",
        "inventory": {
            "site_notes": "_\n_\n128\n4112\nastronomyoracle.com\nastronomyoracle.com\nastrooracles.com\nastrooracles.com\nastro-stars.com\nastro-stars.com\ndwrf.justapi.cc\ndwrf.justapi.cc\ngrafana.justapi.cc\ngrafana.justapi.cc\nhoro.astrozens.com\nhoro.astrozens.com\njcontent.justservices.cc\njcontent.justservices.cc\njustcontent.services\njustcontent.services\njustfeeds.services\njustfeeds.services\njustinsight.services\njustinsight.services\njustservices.cc\njustservices.cc\njustsupport.services\njustsupport.services\njusttemplate.services\nln.astrozens.com\nln.astrozens.com\nln.trivia.buzz\nln.trivia.buzz\npush.justapi.cc\npush.justapi.cc\nsegments.justapi.cc\nsegments.justapi.cc\ns_hash_bucket_size\ns_hash_max_size\nsms.justapi.cc\nsms.justapi.cc\nstat.justapi.cc\nstat.justapi.cc\ntrk.justservices.cc\ntrk.justservices.cc\nuser.justcontent.services\nusers.justapi.cc\nusers.justapi.cc\nusers.justtemplate.services\nwww.astronomyoracle.com\nwww.astrooracles.com\nwww.astro-stars.com\nwww.dwrf.justapi.cc\nwww.grafana.justapi.cc\nwww.grafana.justapi.cc\nwww.horo.astrozens.com\nwww.jcontent.justservices.cc\nwww.jcontent.justservices.cc\nwww.justcontent.services\nwww.justcontent.services\nwww.justfeeds.services\nwww.justfeeds.services\nwww.justinsight.services\nwww.justinsight.services\nwww.justservices.cc\nwww.justservices.cc\nwww.justsupport.services\nwww.justsupport.services\nwww.justtemplate.services\nwww.ln.astrozens.com\nwww.ln.trivia.buzz\nwww.push.justapi.cc\nwww.push.justapi.cc\nwww.segments.justapi.cc\nwww.segments.justapi.cc\nwww.sms.justapi.cc\nwww.sms.justapi.cc\nwww.stat.justapi.cc\nwww.stat.justapi.cc\nwww.trk.justservices.cc\nwww.user.justcontent.services\nwww.users.justapi.cc\nwww.users.justapi.cc\nwww.users.justtemplate.services"
        }
    },
        {
        "host": "DO-08",
        "hostid": "10659",
        "inventory": {
            "site_notes": "_\nbeta.everydayhoroscopes.com\ncontent.everydayhoroscopes.com\ncontent.everydayhoroscopes.com\neveryday-astrology.com\neveryday-horoscopes.net\neverydayhoroscopes.com\neverydayhoroscopes.com\neverydayhoroscopes.org\neverydayzenastrology.com\nmoonboundhoroscopes.com\nregular-horoscopes.com\nregularastrology.com\nwww.beta.everydayhoroscopes.com\nwww.beta.everydayhoroscopes.com\nwww.content.everydayhoroscopes.com\nwww.content.everydayhoroscopes.com\nwww.everyday-astrology.com\nwww.everyday-horoscopes.net\nwww.everydayhoroscopes.com\nwww.everydayhoroscopes.com\nwww.everydayhoroscopes.org\nwww.everydayzenastrology.com\nwww.moonboundhoroscopes.com\nwww.regular-horoscopes.com\nwww.regularastrology.com"
        }
    }
]

如何在列中只显示网站列表(不包括括号、主机、主机 ID、\n 等)?

【问题讨论】:

  • 类似于[clean(host["inventory"]["site_notes"]) for host in hosts],其中clean 是您需要创建的函数以删除不需要的字符串部分(例如_\n、数字等,考虑到您自己的数据布局,由您来定义)

标签: python zabbix


【解决方案1】:

试试下面的。想法是分析包含数据的字段 (site_notes),将其拆分并清除不需要的条目。

data = [
    {
        "host": "CR-20",
        "hostid": "10355",
        "inventory": {
            "site_notes": "_\n_\n128\n4112\nastronomyoracle.com\nastronomyoracle.com\nastrooracles.com\nastrooracles.com\nastro-stars.com\nastro-stars.com\ndwrf.justapi.cc\ndwrf.justapi.cc\ngrafana.justapi.cc\ngrafana.justapi.cc\nhoro.astrozens.com\nhoro.astrozens.com\njcontent.justservices.cc\njcontent.justservices.cc\njustcontent.services\njustcontent.services\njustfeeds.services\njustfeeds.services\njustinsight.services\njustinsight.services\njustservices.cc\njustservices.cc\njustsupport.services\njustsupport.services\njusttemplate.services\nln.astrozens.com\nln.astrozens.com\nln.trivia.buzz\nln.trivia.buzz\npush.justapi.cc\npush.justapi.cc\nsegments.justapi.cc\nsegments.justapi.cc\ns_hash_bucket_size\ns_hash_max_size\nsms.justapi.cc\nsms.justapi.cc\nstat.justapi.cc\nstat.justapi.cc\ntrk.justservices.cc\ntrk.justservices.cc\nuser.justcontent.services\nusers.justapi.cc\nusers.justapi.cc\nusers.justtemplate.services\nwww.astronomyoracle.com\nwww.astrooracles.com\nwww.astro-stars.com\nwww.dwrf.justapi.cc\nwww.grafana.justapi.cc\nwww.grafana.justapi.cc\nwww.horo.astrozens.com\nwww.jcontent.justservices.cc\nwww.jcontent.justservices.cc\nwww.justcontent.services\nwww.justcontent.services\nwww.justfeeds.services\nwww.justfeeds.services\nwww.justinsight.services\nwww.justinsight.services\nwww.justservices.cc\nwww.justservices.cc\nwww.justsupport.services\nwww.justsupport.services\nwww.justtemplate.services\nwww.ln.astrozens.com\nwww.ln.trivia.buzz\nwww.push.justapi.cc\nwww.push.justapi.cc\nwww.segments.justapi.cc\nwww.segments.justapi.cc\nwww.sms.justapi.cc\nwww.sms.justapi.cc\nwww.stat.justapi.cc\nwww.stat.justapi.cc\nwww.trk.justservices.cc\nwww.user.justcontent.services\nwww.users.justapi.cc\nwww.users.justapi.cc\nwww.users.justtemplate.services"
        }
    },
    {
        "host": "DO-08",
        "hostid": "10659",
        "inventory": {
            "site_notes": "_\nbeta.everydayhoroscopes.com\ncontent.everydayhoroscopes.com\ncontent.everydayhoroscopes.com\neveryday-astrology.com\neveryday-horoscopes.net\neverydayhoroscopes.com\neverydayhoroscopes.com\neverydayhoroscopes.org\neverydayzenastrology.com\nmoonboundhoroscopes.com\nregular-horoscopes.com\nregularastrology.com\nwww.beta.everydayhoroscopes.com\nwww.beta.everydayhoroscopes.com\nwww.content.everydayhoroscopes.com\nwww.content.everydayhoroscopes.com\nwww.everyday-astrology.com\nwww.everyday-horoscopes.net\nwww.everydayhoroscopes.com\nwww.everydayhoroscopes.com\nwww.everydayhoroscopes.org\nwww.everydayzenastrology.com\nwww.moonboundhoroscopes.com\nwww.regular-horoscopes.com\nwww.regularastrology.com"
        }
    }
]

for entry in data:
    temp = entry['inventory']['site_notes'].split('\n')
    sites = [x for x in temp if len(x) > 1 and not x[0].isdigit()]
    print(sites)

输出

['astronomyoracle.com', 'astronomyoracle.com', 'astrooracles.com', 'astrooracles.com', 'astro-stars.com', 'astro-stars.com', 'dwrf.justapi.cc', 'dwrf.justapi.cc', 'grafana.justapi.cc', 'grafana.justapi.cc', 'horo.astrozens.com', 'horo.astrozens.com', 'jcontent.justservices.cc', 'jcontent.justservices.cc', 'justcontent.services', 'justcontent.services', 'justfeeds.services', 'justfeeds.services', 'justinsight.services', 'justinsight.services', 'justservices.cc', 'justservices.cc', 'justsupport.services', 'justsupport.services', 'justtemplate.services', 'ln.astrozens.com', 'ln.astrozens.com', 'ln.trivia.buzz', 'ln.trivia.buzz', 'push.justapi.cc', 'push.justapi.cc', 'segments.justapi.cc', 'segments.justapi.cc', 's_hash_bucket_size', 's_hash_max_size', 'sms.justapi.cc', 'sms.justapi.cc', 'stat.justapi.cc', 'stat.justapi.cc', 'trk.justservices.cc', 'trk.justservices.cc', 'user.justcontent.services', 'users.justapi.cc', 'users.justapi.cc', 'users.justtemplate.services', 'www.astronomyoracle.com', 'www.astrooracles.com', 'www.astro-stars.com', 'www.dwrf.justapi.cc', 'www.grafana.justapi.cc', 'www.grafana.justapi.cc', 'www.horo.astrozens.com', 'www.jcontent.justservices.cc', 'www.jcontent.justservices.cc', 'www.justcontent.services', 'www.justcontent.services', 'www.justfeeds.services', 'www.justfeeds.services', 'www.justinsight.services', 'www.justinsight.services', 'www.justservices.cc', 'www.justservices.cc', 'www.justsupport.services', 'www.justsupport.services', 'www.justtemplate.services', 'www.ln.astrozens.com', 'www.ln.trivia.buzz', 'www.push.justapi.cc', 'www.push.justapi.cc', 'www.segments.justapi.cc', 'www.segments.justapi.cc', 'www.sms.justapi.cc', 'www.sms.justapi.cc', 'www.stat.justapi.cc', 'www.stat.justapi.cc', 'www.trk.justservices.cc', 'www.user.justcontent.services', 'www.users.justapi.cc', 'www.users.justapi.cc', 'www.users.justtemplate.services']
['beta.everydayhoroscopes.com', 'content.everydayhoroscopes.com', 'content.everydayhoroscopes.com', 'everyday-astrology.com', 'everyday-horoscopes.net', 'everydayhoroscopes.com', 'everydayhoroscopes.com', 'everydayhoroscopes.org', 'everydayzenastrology.com', 'moonboundhoroscopes.com', 'regular-horoscopes.com', 'regularastrology.com', 'www.beta.everydayhoroscopes.com', 'www.beta.everydayhoroscopes.com', 'www.content.everydayhoroscopes.com', 'www.content.everydayhoroscopes.com', 'www.everyday-astrology.com', 'www.everyday-horoscopes.net', 'www.everydayhoroscopes.com', 'www.everydayhoroscopes.com', 'www.everydayhoroscopes.org', 'www.everydayzenastrology.com', 'www.moonboundhoroscopes.com', 'www.regular-horoscopes.com', 'www.regularastrology.com']

【讨论】:

  • 坦克!在“数据”中,信息可能会发生变化,我表示输出很小,但要大 10 倍。我会尝试实验。
  • 一位同事帮了大忙。也许有人需要它,我发布有效的代码。
【解决方案2】:
    import json
    import re
    from pyzabbix import ZabbixAPI
    
    username = "user"
    password = "*************"
    servername = "https://<name of the site.com>/api_jsonrpc.php"
    
    zapi = ZabbixAPI(servername)
    zapi.session.verify = False
    zapi.login(username, password)
    ef fqdn(
        name: str, test = re.compile(
            r"^(?!-)[\w-]{1,63}(?<!-)$", re.A
        ).fullmatch
):
    temp = name.split(".")
    return (
        all(test(i) for i in temp) if len(name) < 255 and len(temp) > 1 else False
    )


print(
    *sorted(
        {
            host for _ in zapi.host.get(
                output = ["host"], groupids = [25, 41, 43], selectInventory = ["site_notes"]
            ) for host in _["inventory"]["site_notes"].split("\n") if fqdn(host)
        }
    ), sep = "\n"
)

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 2014-04-21
相关资源
最近更新 更多