zbx-utils: 简化 Zabbix API 操作的实用工具集
===========================================
简介
----
- 在现代 ``IT`` 企业中,监控系统是确保网络和应用程序高效运行的关键组件。
- ``Zabbix`` 是一款广泛使用的开源监控软件,它允许企业对各种服务、网络设备和服务器进行实时监控。
- 为了满足快速发展的业务需求, ``Zabbix API`` 提供了一个强大的接口来自动化监控任务,如批量获取主机信息、添加或更新监控项等。
- 在日常使用中发现,重复性地调用 ``Zabbix API`` 进行相似任务处理既繁琐又低效。
- 为了解决这个问题并优化操作流程,我们开发了 ``zbx-utils`` 工具集。
- 此工具集旨在简化常见操作而不重复编写请求方法和过程,提高效率并减少错误。
特点
----
- **易用性**:``zbx-utils`` 通过封装常见操作简化了与 ``Zabbix API`` 的交互。
- **功能丰富**:支持主机标签管理、监控项配置以及模板操作等。
- **兼容性**:支持从 ``Zabbix 5.0`` 以上版本开始的新特性,并兼容大多数历史版本。
先决条件
--------
- **环境要求**:使用 ``zbx-utils`` 之前,请确保您的 ``Zabbix`` 版本至少为 ``5.0`` 或更高。不同功能模块可能有不同的版本要求,请根据需要查阅官方文档确认。
- **依赖库**:本工具集通过利用第三方库 ``Zabbix_api`` 来与 ``Zabbix API`` 进行交互,因此需要预先安装该库。
操作指南
--------
主机标签管理
~~~~~~~~~~~~
- 创建一个含有初始化标签信息的主机标签对象:
.. code::
tags = [
{
"tag": "webserver",
"value": "nginx",
"automatic": True
},
{
"tag": "database",
"value": "mysql"
},
{
"tag": "Application",
"value": "db"
}
]
host_tags = HostTags(tags=tags)
- 更新特定标签并打印其内容:
.. code::
import json
print(
json.dumps(
host_tags.update_tag(
tag_name="webserver",
tag_value="apache"
),
ensure_ascii=False,
indent=4
)
)
- 结果展示:
.. code::
[
{
"tag": "webserver",
"value": "apache"
},
{
"tag": "database",
"value": "mysql"
},
{
"tag": "Application",
"value": "db"
}
]
- 批量更新标签(请确保键名和值一一对应):
.. code::
import json
print(
json.dumps(
host_tags.update_tag(
tag_name=["webserver", "database", "Application"],
tag_value=["apache", "mariadb", "web&db"]
),
ensure_ascii=False,
indent=4
)
)
- 结果展示:
.. code::
[
{
"tag": "webserver",
"value": "apache"
},
{
"tag": "database",
"value": "mariadb"
},
{
"tag": "Application",
"value": "web&db"
}
]
- 为主机添加一个新标签 ``cache``,不指定标签值:
.. code::
import json
print(
json.dumps(
host_tags.add_tag(
tag_name="cache",
tag_value=""
),
ensure_ascii=False,
indent=4
)
)
- 结果展示:
.. code::
[
{
"tag": "webserver",
"value": "nginx"
},
{
"tag": "database",
"value": "mysql"
},
{
"tag": "Application",
"value": "db"
},
{
"tag": "cache",
"value": ""
}
]
- 支持批量添加多个标签及其对应值:
.. code::
import json
print(
json.dumps(
host_tags.add_tag(
tag_name=["webserver", "database", "k1", "k2"],
tag_value=["apache", "mysql", "v1", "v2"]
),
ensure_ascii=False,
indent=4
)
)
- 结果展示:
.. code::
[
{
"tag": "webserver",
"value": "apache"
},
{
"tag": "database",
"value": "mysql"
},
{
"tag": "Application",
"value": "db"
},
{
"tag": "k1",
"value": "v1"
},
{
"tag": "k2",
"value": "v2"
}
]
- 最后便是针对于主机标签的删除操作,同样支持删除一个或多个标签,只需要传入标签名即可,如下所示:
.. code::
import json
print(
json.dumps(
host_tags.delete_tag(tag_name="webserver"),
ensure_ascii=False,
indent=4
)
)
- 结果展示:
.. code::
[
{
"tag": "database",
"value": "mysql"
},
{
"tag": "Application",
"value": "db"
}
]
- 注意:处理监控项标签与主机标签相同,因此不再详述。
主机模板管理
~~~~~~~~~~~~
- 在 ``Zabbix`` 监控系统中,主机模板是一组预定义的监控项、触发器、图形等的集合,用于快速配置和部署监控策略。
- 下面是如何通过 ``Zabbix API`` 来操作这些主机模板的步骤。
- 首先,您需要创建一个与 ``Zabbix API`` 交互的初始化对象。
- 这要求您提供 ``Zabbix`` 服务器的 ``URL``,以及相应的用户认证信息,包括用户名和密码。
.. code::
zbx = ZbxTpls(
server="your zabbix server url",
user="your zabbix username",
passwd="your zabbix username password"
)
- 在进行主机模板相关操作之前,请确保您已经准备好了目标主机的名称( ``host`` ),因为模板必须关联到特定的主机上。
- 不同于标签(通常使用键值对来唯一标识),模板是通过它们的唯一 ``"id"`` 来识别和管理的。
- 这样就需要通过 ``Zabbix API`` 进行查询和操作,因此在初始化时必须提供用户认证信息。
- 以下是如何添加新模板到指定主机:
.. code::
import json
add_tpls = zbx.add_tpl(
host="ZabbixServer",
tpl_name="Docker by Zabbix agent 2"
)
print(
json.dumps(
add_tpls,
ensure_ascii=False,
indent=4
)
)
- 执行上述操作后,您应该会看到一个包含新添加模板信息的 ``JSON`` 结构响应:
.. code::
[
{
"host": "Linux by Zabbix agent",
"templateid": "10047"
},
{
"host": "Docker by Zabbix agent 2",
"templateid": "12723"
}
]
- 如果您想要移除某个已关联到主机上的模板,则可以使用以下代码:
.. code::
import json
clear_tpls = zbx.delete_tpl(
host="ZabbixServer",
tpl_name="Docker by Zabbix agent 2"
)
print(
json.dumps(
clear_tpls,
ensure_ascii=False,
indent=4
)
)
- 成功移除模板后,返回结果将不再显示已删除的条目:
.. code::
[
{
"host": "Linux by Zabbix agent",
"templateid": "10047"
}
]
- 如果您需要替换某个现有模板为另一个新模板,则应先删除既有模板,并添加新模版。以下示例展示了这一过程:
.. code::
import json
replace_tpls = zbx.replace_tpl(
host="ZabbixServer",
old_tpl_name="Linux by Zabbix agent",
new_tpl_name="Docker by Zabbix agent 2"
)
print(
json.dumps(
replace_tpls,
ensure_ascii=False,
indent=4
)
)
- 替换完成后,相关联主机将仅关联到新指定的模版,反映在返回结果中:
.. code::
[
{
"host": "Docker by Zabbix agent 2",
"templateid": "12723"
}
]
使用主机信息清单标签(Host Inventory Tags)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 由于 ``Zabbix`` 监控系统的历史版本中不支持主机标签( ``Host Tags`` )功能,监控项的分类和组织是依靠 ``Application`` 实体来实现的。
- 为了给主机添加类似 "标签" 或者 "分类" 的属性,可以利用 ``Host Inventory Tag`` (主机信息清单)功能。
- 这一功能允许用户存储有关每个监控主机详细信息的键值对数据。
- 以下是如何在低版本的 ``Zabbix`` 中借助 ``Host Inventory Tag`` 实现该功能的方法:
.. code::
- 创建 `InvTags` 对象:
>>> tags = InvTags(
>>> "env=prod;region=east;"
>>> )
>>> print(tags)
"env=prod;region=east;"
- 添加新标签或修改现有标签:
>>> tags["new_key"] = "value"
>>> print(tags)
"env=prod;region=east;new_key=value;"
- 删除一个标签:
>>> del tags["env"]
>>> print(tags)
"region=east;new_key=value;"
- 若键值为 None,则只输出键名:
>>> tags["only_key"] = None
>>> print(tags)
"region=east;new_key=value;only_key;"
- 获取某个键对应的值:
>>> value = tags.get("region")
>>> print(value)
"east"
- 抛出 TypeError 异常,如果尝试设置非法类型作为 value:
>>> tags["invalid"] = 123
TypeError: 主机清单标签内容必须是字符串,但接收到: int。
- 当 `InvTags` 为空时, 返回空字符串:
>>> empty_tags = InvTags()
>>> print(empty_tags)
高级用法:重复利用 ZabbixAPI 对象
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 在更复杂或高级的使用场景中,如操作主机模板时,我们通常会实例化模板对象并传入登录凭证(用户名和密码)。
- 但如果在当前代码环境中已经存在一个由 ``Zabbix_api`` 库创建且正确初始化好用户认证信息的 ``ZabbixAPI`` 对象,则无需重新进行初始化。
- 相反,可以直接将现有对象传递给要使用它进行进一步操作的构造函数或方法。
- 例如:
.. code::
# 假设 `zapi` 是已经提前创建好的 `ZabbixAPI` 对象
zbx = ZbxTpls(zapi=zapi)
- 在这种情况下,请确保传入构造函数或方法中的 ``ZabbixAPI`` 对象确实是从 ``zabbix-api Python`` 库创建出来,并且其会话仍然有效。
- 这样做可以减少网络请求、避免不必要的资源消耗,并加快脚本执行速度。
- 以上内容旨在提供一种优化代码、提高效率并确保与 ``Zabbix API`` 交互过程科学、专业和合理性方面建议。
Raw data
{
"_id": null,
"home_page": "https://github.com/gary714/zbx-utils",
"name": "zbx-utils",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "Zabbix, Utilities",
"author": "gary",
"author_email": "mepmb@sina.com",
"download_url": "https://files.pythonhosted.org/packages/82/4f/ff21648853b3c8f5303723f835ffc154d66fd8b6861bb5157ddee1a9ed68/zbx_utils-1.0.0.tar.gz",
"platform": "Linux",
"description": "zbx-utils: \u7b80\u5316 Zabbix API \u64cd\u4f5c\u7684\u5b9e\u7528\u5de5\u5177\u96c6\r\n===========================================\r\n\r\n\u7b80\u4ecb\r\n----\r\n\r\n- \u5728\u73b0\u4ee3 ``IT`` \u4f01\u4e1a\u4e2d\uff0c\u76d1\u63a7\u7cfb\u7edf\u662f\u786e\u4fdd\u7f51\u7edc\u548c\u5e94\u7528\u7a0b\u5e8f\u9ad8\u6548\u8fd0\u884c\u7684\u5173\u952e\u7ec4\u4ef6\u3002\r\n\r\n- ``Zabbix`` \u662f\u4e00\u6b3e\u5e7f\u6cdb\u4f7f\u7528\u7684\u5f00\u6e90\u76d1\u63a7\u8f6f\u4ef6\uff0c\u5b83\u5141\u8bb8\u4f01\u4e1a\u5bf9\u5404\u79cd\u670d\u52a1\u3001\u7f51\u7edc\u8bbe\u5907\u548c\u670d\u52a1\u5668\u8fdb\u884c\u5b9e\u65f6\u76d1\u63a7\u3002\r\n\r\n- \u4e3a\u4e86\u6ee1\u8db3\u5feb\u901f\u53d1\u5c55\u7684\u4e1a\u52a1\u9700\u6c42\uff0c ``Zabbix API`` \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u63a5\u53e3\u6765\u81ea\u52a8\u5316\u76d1\u63a7\u4efb\u52a1\uff0c\u5982\u6279\u91cf\u83b7\u53d6\u4e3b\u673a\u4fe1\u606f\u3001\u6dfb\u52a0\u6216\u66f4\u65b0\u76d1\u63a7\u9879\u7b49\u3002\r\n\r\n- \u5728\u65e5\u5e38\u4f7f\u7528\u4e2d\u53d1\u73b0\uff0c\u91cd\u590d\u6027\u5730\u8c03\u7528 ``Zabbix API`` \u8fdb\u884c\u76f8\u4f3c\u4efb\u52a1\u5904\u7406\u65e2\u7e41\u7410\u53c8\u4f4e\u6548\u3002\r\n\r\n- \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5e76\u4f18\u5316\u64cd\u4f5c\u6d41\u7a0b\uff0c\u6211\u4eec\u5f00\u53d1\u4e86 ``zbx-utils`` \u5de5\u5177\u96c6\u3002\r\n\r\n- \u6b64\u5de5\u5177\u96c6\u65e8\u5728\u7b80\u5316\u5e38\u89c1\u64cd\u4f5c\u800c\u4e0d\u91cd\u590d\u7f16\u5199\u8bf7\u6c42\u65b9\u6cd5\u548c\u8fc7\u7a0b\uff0c\u63d0\u9ad8\u6548\u7387\u5e76\u51cf\u5c11\u9519\u8bef\u3002\r\n\r\n\u7279\u70b9\r\n----\r\n\r\n- **\u6613\u7528\u6027**\uff1a``zbx-utils`` \u901a\u8fc7\u5c01\u88c5\u5e38\u89c1\u64cd\u4f5c\u7b80\u5316\u4e86\u4e0e ``Zabbix API`` \u7684\u4ea4\u4e92\u3002\r\n\r\n- **\u529f\u80fd\u4e30\u5bcc**\uff1a\u652f\u6301\u4e3b\u673a\u6807\u7b7e\u7ba1\u7406\u3001\u76d1\u63a7\u9879\u914d\u7f6e\u4ee5\u53ca\u6a21\u677f\u64cd\u4f5c\u7b49\u3002\r\n\r\n- **\u517c\u5bb9\u6027**\uff1a\u652f\u6301\u4ece ``Zabbix 5.0`` \u4ee5\u4e0a\u7248\u672c\u5f00\u59cb\u7684\u65b0\u7279\u6027\uff0c\u5e76\u517c\u5bb9\u5927\u591a\u6570\u5386\u53f2\u7248\u672c\u3002\r\n\r\n\u5148\u51b3\u6761\u4ef6\r\n--------\r\n\r\n- **\u73af\u5883\u8981\u6c42**\uff1a\u4f7f\u7528 ``zbx-utils`` \u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u7684 ``Zabbix`` \u7248\u672c\u81f3\u5c11\u4e3a ``5.0`` \u6216\u66f4\u9ad8\u3002\u4e0d\u540c\u529f\u80fd\u6a21\u5757\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u7248\u672c\u8981\u6c42\uff0c\u8bf7\u6839\u636e\u9700\u8981\u67e5\u9605\u5b98\u65b9\u6587\u6863\u786e\u8ba4\u3002\r\n\r\n- **\u4f9d\u8d56\u5e93**\uff1a\u672c\u5de5\u5177\u96c6\u901a\u8fc7\u5229\u7528\u7b2c\u4e09\u65b9\u5e93 ``Zabbix_api`` \u6765\u4e0e ``Zabbix API`` \u8fdb\u884c\u4ea4\u4e92\uff0c\u56e0\u6b64\u9700\u8981\u9884\u5148\u5b89\u88c5\u8be5\u5e93\u3002\r\n\r\n\u64cd\u4f5c\u6307\u5357\r\n--------\r\n\r\n\u4e3b\u673a\u6807\u7b7e\u7ba1\u7406\r\n~~~~~~~~~~~~\r\n\r\n- \u521b\u5efa\u4e00\u4e2a\u542b\u6709\u521d\u59cb\u5316\u6807\u7b7e\u4fe1\u606f\u7684\u4e3b\u673a\u6807\u7b7e\u5bf9\u8c61\uff1a\r\n\r\n .. code:: \r\n\r\n tags = [\r\n {\r\n \"tag\": \"webserver\",\r\n \"value\": \"nginx\",\r\n \"automatic\": True\r\n },\r\n {\r\n \"tag\": \"database\",\r\n \"value\": \"mysql\"\r\n },\r\n {\r\n \"tag\": \"Application\",\r\n \"value\": \"db\"\r\n }\r\n ]\r\n host_tags = HostTags(tags=tags)\r\n\r\n- \u66f4\u65b0\u7279\u5b9a\u6807\u7b7e\u5e76\u6253\u5370\u5176\u5185\u5bb9\uff1a\r\n\r\n .. code:: \r\n\r\n import json\r\n\r\n print(\r\n json.dumps(\r\n host_tags.update_tag(\r\n tag_name=\"webserver\",\r\n tag_value=\"apache\"\r\n ),\r\n ensure_ascii=False,\r\n indent=4\r\n )\r\n )\r\n\r\n - \u7ed3\u679c\u5c55\u793a\uff1a\r\n\r\n .. code:: \r\n\r\n [\r\n {\r\n \"tag\": \"webserver\",\r\n \"value\": \"apache\"\r\n },\r\n {\r\n \"tag\": \"database\",\r\n \"value\": \"mysql\"\r\n },\r\n {\r\n \"tag\": \"Application\",\r\n \"value\": \"db\"\r\n }\r\n ]\r\n\r\n- \u6279\u91cf\u66f4\u65b0\u6807\u7b7e\uff08\u8bf7\u786e\u4fdd\u952e\u540d\u548c\u503c\u4e00\u4e00\u5bf9\u5e94\uff09\uff1a\r\n\r\n .. code:: \r\n\r\n import json\r\n\r\n print(\r\n json.dumps(\r\n host_tags.update_tag(\r\n tag_name=[\"webserver\", \"database\", \"Application\"],\r\n tag_value=[\"apache\", \"mariadb\", \"web&db\"]\r\n ),\r\n ensure_ascii=False,\r\n indent=4\r\n )\r\n )\r\n\r\n - \u7ed3\u679c\u5c55\u793a\uff1a\r\n\r\n .. code:: \r\n\r\n [\r\n {\r\n \"tag\": \"webserver\",\r\n \"value\": \"apache\"\r\n },\r\n {\r\n \"tag\": \"database\",\r\n \"value\": \"mariadb\"\r\n },\r\n {\r\n \"tag\": \"Application\",\r\n \"value\": \"web&db\"\r\n }\r\n ]\r\n\r\n- \u4e3a\u4e3b\u673a\u6dfb\u52a0\u4e00\u4e2a\u65b0\u6807\u7b7e ``cache``\uff0c\u4e0d\u6307\u5b9a\u6807\u7b7e\u503c\uff1a\r\n\r\n .. code:: \r\n\r\n import json\r\n\r\n print(\r\n json.dumps(\r\n host_tags.add_tag(\r\n tag_name=\"cache\",\r\n tag_value=\"\"\r\n ),\r\n ensure_ascii=False,\r\n indent=4\r\n )\r\n )\r\n\r\n - \u7ed3\u679c\u5c55\u793a\uff1a\r\n\r\n .. code:: \r\n\r\n [\r\n {\r\n \"tag\": \"webserver\",\r\n \"value\": \"nginx\"\r\n },\r\n {\r\n \"tag\": \"database\",\r\n \"value\": \"mysql\"\r\n },\r\n {\r\n \"tag\": \"Application\",\r\n \"value\": \"db\"\r\n },\r\n {\r\n \"tag\": \"cache\",\r\n \"value\": \"\"\r\n }\r\n ]\r\n\r\n- \u652f\u6301\u6279\u91cf\u6dfb\u52a0\u591a\u4e2a\u6807\u7b7e\u53ca\u5176\u5bf9\u5e94\u503c\uff1a\r\n\r\n .. code:: \r\n\r\n import json\r\n\r\n print(\r\n json.dumps(\r\n host_tags.add_tag(\r\n tag_name=[\"webserver\", \"database\", \"k1\", \"k2\"],\r\n tag_value=[\"apache\", \"mysql\", \"v1\", \"v2\"]\r\n ),\r\n ensure_ascii=False,\r\n indent=4\r\n )\r\n )\r\n\r\n - \u7ed3\u679c\u5c55\u793a\uff1a\r\n\r\n .. code:: \r\n\r\n [\r\n {\r\n \"tag\": \"webserver\",\r\n \"value\": \"apache\"\r\n },\r\n {\r\n \"tag\": \"database\",\r\n \"value\": \"mysql\"\r\n },\r\n {\r\n \"tag\": \"Application\",\r\n \"value\": \"db\"\r\n },\r\n {\r\n \"tag\": \"k1\",\r\n \"value\": \"v1\"\r\n },\r\n {\r\n \"tag\": \"k2\",\r\n \"value\": \"v2\"\r\n }\r\n ]\r\n\r\n- \u6700\u540e\u4fbf\u662f\u9488\u5bf9\u4e8e\u4e3b\u673a\u6807\u7b7e\u7684\u5220\u9664\u64cd\u4f5c\uff0c\u540c\u6837\u652f\u6301\u5220\u9664\u4e00\u4e2a\u6216\u591a\u4e2a\u6807\u7b7e\uff0c\u53ea\u9700\u8981\u4f20\u5165\u6807\u7b7e\u540d\u5373\u53ef\uff0c\u5982\u4e0b\u6240\u793a\uff1a\r\n\r\n .. code:: \r\n\r\n import json\r\n\r\n print(\r\n json.dumps(\r\n host_tags.delete_tag(tag_name=\"webserver\"),\r\n ensure_ascii=False,\r\n indent=4\r\n )\r\n )\r\n\r\n - \u7ed3\u679c\u5c55\u793a\uff1a\r\n\r\n .. code:: \r\n\r\n [\r\n {\r\n \"tag\": \"database\",\r\n \"value\": \"mysql\"\r\n },\r\n {\r\n \"tag\": \"Application\",\r\n \"value\": \"db\"\r\n }\r\n ]\r\n\r\n- \u6ce8\u610f\uff1a\u5904\u7406\u76d1\u63a7\u9879\u6807\u7b7e\u4e0e\u4e3b\u673a\u6807\u7b7e\u76f8\u540c\uff0c\u56e0\u6b64\u4e0d\u518d\u8be6\u8ff0\u3002\r\n\r\n\u4e3b\u673a\u6a21\u677f\u7ba1\u7406\r\n~~~~~~~~~~~~\r\n\r\n- \u5728 ``Zabbix`` \u76d1\u63a7\u7cfb\u7edf\u4e2d\uff0c\u4e3b\u673a\u6a21\u677f\u662f\u4e00\u7ec4\u9884\u5b9a\u4e49\u7684\u76d1\u63a7\u9879\u3001\u89e6\u53d1\u5668\u3001\u56fe\u5f62\u7b49\u7684\u96c6\u5408\uff0c\u7528\u4e8e\u5feb\u901f\u914d\u7f6e\u548c\u90e8\u7f72\u76d1\u63a7\u7b56\u7565\u3002\r\n\r\n- \u4e0b\u9762\u662f\u5982\u4f55\u901a\u8fc7 ``Zabbix API`` \u6765\u64cd\u4f5c\u8fd9\u4e9b\u4e3b\u673a\u6a21\u677f\u7684\u6b65\u9aa4\u3002\r\n\r\n- \u9996\u5148\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e0e ``Zabbix API`` \u4ea4\u4e92\u7684\u521d\u59cb\u5316\u5bf9\u8c61\u3002\r\n\r\n- \u8fd9\u8981\u6c42\u60a8\u63d0\u4f9b ``Zabbix`` \u670d\u52a1\u5668\u7684 ``URL``\uff0c\u4ee5\u53ca\u76f8\u5e94\u7684\u7528\u6237\u8ba4\u8bc1\u4fe1\u606f\uff0c\u5305\u62ec\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\r\n\r\n .. code:: \r\n\r\n zbx = ZbxTpls(\r\n server=\"your zabbix server url\",\r\n user=\"your zabbix username\",\r\n passwd=\"your zabbix username password\"\r\n )\r\n\r\n- \u5728\u8fdb\u884c\u4e3b\u673a\u6a21\u677f\u76f8\u5173\u64cd\u4f5c\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\u76ee\u6807\u4e3b\u673a\u7684\u540d\u79f0\uff08 ``host`` \uff09\uff0c\u56e0\u4e3a\u6a21\u677f\u5fc5\u987b\u5173\u8054\u5230\u7279\u5b9a\u7684\u4e3b\u673a\u4e0a\u3002\r\n\r\n- \u4e0d\u540c\u4e8e\u6807\u7b7e\uff08\u901a\u5e38\u4f7f\u7528\u952e\u503c\u5bf9\u6765\u552f\u4e00\u6807\u8bc6\uff09\uff0c\u6a21\u677f\u662f\u901a\u8fc7\u5b83\u4eec\u7684\u552f\u4e00 ``\"id\"`` \u6765\u8bc6\u522b\u548c\u7ba1\u7406\u7684\u3002\r\n\r\n- \u8fd9\u6837\u5c31\u9700\u8981\u901a\u8fc7 ``Zabbix API`` \u8fdb\u884c\u67e5\u8be2\u548c\u64cd\u4f5c\uff0c\u56e0\u6b64\u5728\u521d\u59cb\u5316\u65f6\u5fc5\u987b\u63d0\u4f9b\u7528\u6237\u8ba4\u8bc1\u4fe1\u606f\u3002\r\n\r\n- \u4ee5\u4e0b\u662f\u5982\u4f55\u6dfb\u52a0\u65b0\u6a21\u677f\u5230\u6307\u5b9a\u4e3b\u673a\uff1a\r\n\r\n .. code:: \r\n\r\n import json\r\n\r\n add_tpls = zbx.add_tpl(\r\n host=\"ZabbixServer\",\r\n tpl_name=\"Docker by Zabbix agent 2\"\r\n )\r\n print(\r\n json.dumps(\r\n add_tpls,\r\n ensure_ascii=False,\r\n indent=4\r\n )\r\n )\r\n\r\n - \u6267\u884c\u4e0a\u8ff0\u64cd\u4f5c\u540e\uff0c\u60a8\u5e94\u8be5\u4f1a\u770b\u5230\u4e00\u4e2a\u5305\u542b\u65b0\u6dfb\u52a0\u6a21\u677f\u4fe1\u606f\u7684 ``JSON`` \u7ed3\u6784\u54cd\u5e94\uff1a\r\n\r\n .. code:: \r\n\r\n [\r\n {\r\n \"host\": \"Linux by Zabbix agent\",\r\n \"templateid\": \"10047\"\r\n },\r\n {\r\n \"host\": \"Docker by Zabbix agent 2\",\r\n \"templateid\": \"12723\"\r\n }\r\n ]\r\n\r\n- \u5982\u679c\u60a8\u60f3\u8981\u79fb\u9664\u67d0\u4e2a\u5df2\u5173\u8054\u5230\u4e3b\u673a\u4e0a\u7684\u6a21\u677f\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a\r\n\r\n .. code:: \r\n\r\n import json\r\n\r\n clear_tpls = zbx.delete_tpl(\r\n host=\"ZabbixServer\",\r\n tpl_name=\"Docker by Zabbix agent 2\"\r\n )\r\n print(\r\n json.dumps(\r\n clear_tpls,\r\n ensure_ascii=False,\r\n indent=4\r\n )\r\n )\r\n\r\n - \u6210\u529f\u79fb\u9664\u6a21\u677f\u540e\uff0c\u8fd4\u56de\u7ed3\u679c\u5c06\u4e0d\u518d\u663e\u793a\u5df2\u5220\u9664\u7684\u6761\u76ee\uff1a\r\n\r\n .. code:: \r\n\r\n [\r\n {\r\n \"host\": \"Linux by Zabbix agent\",\r\n \"templateid\": \"10047\"\r\n }\r\n ]\r\n\r\n- \u5982\u679c\u60a8\u9700\u8981\u66ff\u6362\u67d0\u4e2a\u73b0\u6709\u6a21\u677f\u4e3a\u53e6\u4e00\u4e2a\u65b0\u6a21\u677f\uff0c\u5219\u5e94\u5148\u5220\u9664\u65e2\u6709\u6a21\u677f\uff0c\u5e76\u6dfb\u52a0\u65b0\u6a21\u7248\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u8fd9\u4e00\u8fc7\u7a0b\uff1a\r\n\r\n .. code:: \r\n\r\n import json\r\n\r\n replace_tpls = zbx.replace_tpl(\r\n host=\"ZabbixServer\",\r\n old_tpl_name=\"Linux by Zabbix agent\",\r\n new_tpl_name=\"Docker by Zabbix agent 2\"\r\n )\r\n print(\r\n json.dumps(\r\n replace_tpls,\r\n ensure_ascii=False,\r\n indent=4\r\n )\r\n )\r\n\r\n - \u66ff\u6362\u5b8c\u6210\u540e\uff0c\u76f8\u5173\u8054\u4e3b\u673a\u5c06\u4ec5\u5173\u8054\u5230\u65b0\u6307\u5b9a\u7684\u6a21\u7248\uff0c\u53cd\u6620\u5728\u8fd4\u56de\u7ed3\u679c\u4e2d\uff1a\r\n\r\n .. code:: \r\n\r\n [\r\n {\r\n \"host\": \"Docker by Zabbix agent 2\",\r\n \"templateid\": \"12723\"\r\n }\r\n ]\r\n\r\n\r\n\u4f7f\u7528\u4e3b\u673a\u4fe1\u606f\u6e05\u5355\u6807\u7b7e\uff08Host Inventory Tags\uff09\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\n- \u7531\u4e8e ``Zabbix`` \u76d1\u63a7\u7cfb\u7edf\u7684\u5386\u53f2\u7248\u672c\u4e2d\u4e0d\u652f\u6301\u4e3b\u673a\u6807\u7b7e\uff08 ``Host Tags`` \uff09\u529f\u80fd\uff0c\u76d1\u63a7\u9879\u7684\u5206\u7c7b\u548c\u7ec4\u7ec7\u662f\u4f9d\u9760 ``Application`` \u5b9e\u4f53\u6765\u5b9e\u73b0\u7684\u3002\r\n\r\n- \u4e3a\u4e86\u7ed9\u4e3b\u673a\u6dfb\u52a0\u7c7b\u4f3c \"\u6807\u7b7e\" \u6216\u8005 \"\u5206\u7c7b\" \u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u5229\u7528 ``Host Inventory Tag`` \uff08\u4e3b\u673a\u4fe1\u606f\u6e05\u5355\uff09\u529f\u80fd\u3002\r\n\r\n- \u8fd9\u4e00\u529f\u80fd\u5141\u8bb8\u7528\u6237\u5b58\u50a8\u6709\u5173\u6bcf\u4e2a\u76d1\u63a7\u4e3b\u673a\u8be6\u7ec6\u4fe1\u606f\u7684\u952e\u503c\u5bf9\u6570\u636e\u3002\r\n\r\n- \u4ee5\u4e0b\u662f\u5982\u4f55\u5728\u4f4e\u7248\u672c\u7684 ``Zabbix`` \u4e2d\u501f\u52a9 ``Host Inventory Tag`` \u5b9e\u73b0\u8be5\u529f\u80fd\u7684\u65b9\u6cd5\uff1a\r\n\r\n .. code:: \r\n\r\n - \u521b\u5efa `InvTags` \u5bf9\u8c61\uff1a\r\n >>> tags = InvTags(\r\n >>> \"env=prod;region=east;\"\r\n >>> )\r\n >>> print(tags)\r\n \"env=prod;region=east;\"\r\n\r\n - \u6dfb\u52a0\u65b0\u6807\u7b7e\u6216\u4fee\u6539\u73b0\u6709\u6807\u7b7e\uff1a\r\n >>> tags[\"new_key\"] = \"value\"\r\n >>> print(tags)\r\n \"env=prod;region=east;new_key=value;\"\r\n\r\n - \u5220\u9664\u4e00\u4e2a\u6807\u7b7e\uff1a\r\n >>> del tags[\"env\"]\r\n >>> print(tags)\r\n \"region=east;new_key=value;\"\r\n\r\n - \u82e5\u952e\u503c\u4e3a None\uff0c\u5219\u53ea\u8f93\u51fa\u952e\u540d\uff1a\r\n >>> tags[\"only_key\"] = None\r\n >>> print(tags)\r\n \"region=east;new_key=value;only_key;\"\r\n\r\n - \u83b7\u53d6\u67d0\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\uff1a\r\n >>> value = tags.get(\"region\")\r\n >>> print(value)\r\n \"east\"\r\n\r\n - \u629b\u51fa TypeError \u5f02\u5e38\uff0c\u5982\u679c\u5c1d\u8bd5\u8bbe\u7f6e\u975e\u6cd5\u7c7b\u578b\u4f5c\u4e3a value\uff1a\r\n >>> tags[\"invalid\"] = 123\r\n TypeError: \u4e3b\u673a\u6e05\u5355\u6807\u7b7e\u5185\u5bb9\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\uff0c\u4f46\u63a5\u6536\u5230: int\u3002\r\n\r\n - \u5f53 `InvTags` \u4e3a\u7a7a\u65f6, \u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\uff1a\r\n >>> empty_tags = InvTags()\r\n >>> print(empty_tags)\r\n\r\n\r\n\u9ad8\u7ea7\u7528\u6cd5\uff1a\u91cd\u590d\u5229\u7528 ZabbixAPI \u5bf9\u8c61\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\n- \u5728\u66f4\u590d\u6742\u6216\u9ad8\u7ea7\u7684\u4f7f\u7528\u573a\u666f\u4e2d\uff0c\u5982\u64cd\u4f5c\u4e3b\u673a\u6a21\u677f\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5b9e\u4f8b\u5316\u6a21\u677f\u5bf9\u8c61\u5e76\u4f20\u5165\u767b\u5f55\u51ed\u8bc1\uff08\u7528\u6237\u540d\u548c\u5bc6\u7801\uff09\u3002\r\n\r\n- \u4f46\u5982\u679c\u5728\u5f53\u524d\u4ee3\u7801\u73af\u5883\u4e2d\u5df2\u7ecf\u5b58\u5728\u4e00\u4e2a\u7531 ``Zabbix_api`` \u5e93\u521b\u5efa\u4e14\u6b63\u786e\u521d\u59cb\u5316\u597d\u7528\u6237\u8ba4\u8bc1\u4fe1\u606f\u7684 ``ZabbixAPI`` \u5bf9\u8c61\uff0c\u5219\u65e0\u9700\u91cd\u65b0\u8fdb\u884c\u521d\u59cb\u5316\u3002\r\n\r\n- \u76f8\u53cd\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u73b0\u6709\u5bf9\u8c61\u4f20\u9012\u7ed9\u8981\u4f7f\u7528\u5b83\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u7684\u6784\u9020\u51fd\u6570\u6216\u65b9\u6cd5\u3002\r\n\r\n- \u4f8b\u5982\uff1a\r\n\r\n .. code:: \r\n\r\n # \u5047\u8bbe `zapi` \u662f\u5df2\u7ecf\u63d0\u524d\u521b\u5efa\u597d\u7684 `ZabbixAPI` \u5bf9\u8c61\r\n zbx = ZbxTpls(zapi=zapi)\r\n\r\n- \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bf7\u786e\u4fdd\u4f20\u5165\u6784\u9020\u51fd\u6570\u6216\u65b9\u6cd5\u4e2d\u7684 ``ZabbixAPI`` \u5bf9\u8c61\u786e\u5b9e\u662f\u4ece ``zabbix-api Python`` \u5e93\u521b\u5efa\u51fa\u6765\uff0c\u5e76\u4e14\u5176\u4f1a\u8bdd\u4ecd\u7136\u6709\u6548\u3002\r\n\r\n- \u8fd9\u6837\u505a\u53ef\u4ee5\u51cf\u5c11\u7f51\u7edc\u8bf7\u6c42\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8d44\u6e90\u6d88\u8017\uff0c\u5e76\u52a0\u5feb\u811a\u672c\u6267\u884c\u901f\u5ea6\u3002\r\n\r\n- \u4ee5\u4e0a\u5185\u5bb9\u65e8\u5728\u63d0\u4f9b\u4e00\u79cd\u4f18\u5316\u4ee3\u7801\u3001\u63d0\u9ad8\u6548\u7387\u5e76\u786e\u4fdd\u4e0e ``Zabbix API`` \u4ea4\u4e92\u8fc7\u7a0b\u79d1\u5b66\u3001\u4e13\u4e1a\u548c\u5408\u7406\u6027\u65b9\u9762\u5efa\u8bae\u3002\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Zabbix \u8f85\u52a9\u4e0e\u901a\u7528\u529f\u80fd\u64cd\u4f5c\u5e93\u3002",
"version": "1.0.0",
"project_urls": {
"Documentation": "https://github.com/gary714/zbx-utils",
"Homepage": "https://github.com/gary714/zbx-utils",
"Source": "https://github.com/gary714/zbx-utils"
},
"split_keywords": [
"zabbix",
" utilities"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "33ed1075d0c0654f85059b7adefc03b7ff06b48629dd0732cebacaae008183a6",
"md5": "8ed528c43b792f5ad683082b669baa29",
"sha256": "816ab80ea94739d6f05ea5cafa00509f490b8bf07999c5f4e99492b393ab7b2c"
},
"downloads": -1,
"filename": "zbx_utils-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8ed528c43b792f5ad683082b669baa29",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 18235,
"upload_time": "2024-12-16T05:30:33",
"upload_time_iso_8601": "2024-12-16T05:30:33.371386Z",
"url": "https://files.pythonhosted.org/packages/33/ed/1075d0c0654f85059b7adefc03b7ff06b48629dd0732cebacaae008183a6/zbx_utils-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "824fff21648853b3c8f5303723f835ffc154d66fd8b6861bb5157ddee1a9ed68",
"md5": "12b7920ecfd37dc03b8f7bbe12c02f70",
"sha256": "e94c1140db965d5f0f013c1dc6ff6fcd2f61083e5f531d48eed652639a047352"
},
"downloads": -1,
"filename": "zbx_utils-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "12b7920ecfd37dc03b8f7bbe12c02f70",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 21284,
"upload_time": "2024-12-16T05:30:35",
"upload_time_iso_8601": "2024-12-16T05:30:35.025206Z",
"url": "https://files.pythonhosted.org/packages/82/4f/ff21648853b3c8f5303723f835ffc154d66fd8b6861bb5157ddee1a9ed68/zbx_utils-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-16 05:30:35",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gary714",
"github_project": "zbx-utils",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "zabbix_api",
"specs": [
[
">=",
"0.5.4"
]
]
}
],
"lcname": "zbx-utils"
}