username = “ a dmin”
password = “ password “
zapi = ZabbixAPI(zabbix_url)
zapi.login(username, password)

獲取所有模板

templates = zapi.template.get(output=”extend”)

此代碼用于從Zabbix 服務(wù)器獲取模板信息。通過 template.get API,我們可以獲取包括監(jiān)控項(xiàng)、觸發(fā)器等在內(nèi)的模板詳細(xì)數(shù)據(jù)。

3.2 調(diào)用 Local LLM 進(jìn)行翻譯

假設(shè)我們有一個(gè)本地部署的LLM 服務(wù),地址為 http://127.0.0.1:11434/,使用模型 mistral:7b-instruct。我們將通過 API 將模板中的文本傳遞給 LLM,獲得翻譯后的內(nèi)容。

LLM API的URL

llm_url = http://127.0.0.1:11434/api/generate

準(zhǔn)備翻譯請求

def translate_text(text, short_version=False):
prompt = f”你是一個(gè)專業(yè)的計(jì)算機(jī)相關(guān)語言翻譯者,幫助我翻譯語言為中文,除了翻譯結(jié)果,不需要其他內(nèi)容和解釋。請注意 , 當(dāng)你接收到內(nèi)容為空時(shí),回復(fù)內(nèi)容為空。請將以下文本翻譯成中文:

\“{text}\“”
if short_version:
prompt += “ 重新翻譯,去掉備注,不要輸出翻譯結(jié)果外的內(nèi)容。”
payload = {
“model”: “mistral:7b-instruct”,
“prompt”: prompt,
“max_tokens”: 100,
“stream”: False
}
response = requests.post(llm_url, json=payload)
try:
result = response.json()
if result.get(‘done’):
return result.get(‘response’, ‘’).strip()
else:
return “翻譯未完成”
except requests.exceptions.JSONDecodeError as e:
print(f”JSON 解析失敗: {e}”)
return text
def check_and_translate(text, max_length=50):
translated_text = translate_text(text)
while len(translated_text) > max_length:
print(f”翻譯內(nèi)容過長 ({len(translated_text)} 字符),重新生成簡短版本…”)
translated_text = translate_text(text, short_version=True)
return translated_text

此處的translate_text 函數(shù)將傳入的模板文本發(fā)送到本地 LLM 服務(wù)進(jìn)行翻譯,并返回翻譯后的內(nèi)容。

3.3 更新 Zabbix 模板

翻譯完成后,我們將通過Zabbix API 更新模板。如下代碼展示了如何使用 template.update 接口將翻譯后的模板信息更新到 Zabbix 服務(wù)器:

def update_template():
translated_name = check_and_translate(template_name, max_length=50)
translated_description = check_and_translate(template_description, max_length=3000)
try:
update_result = zapi.template.update(
templateid=template_id,
name=translated_name,
description=translated_description
)
print(f”模板的名稱更新成功: {update_result}”)
except Exception as e:
print(f”跳過模板的名稱更新: {translated_name},原因: {str(e)}”)
def update_items():
items = zapi.item.get(templateids=template_id, output=[‘itemid’, ‘name’, ‘description’])
for item in items:
item_name = item[‘name’]
item_description = item.get(‘description’, ‘No Description’)
translated_item_name = check_and_translate(item_name, max_length=50)
translated_item_description = check_and_translate(item_description, max_length=3000)
try:
update_item_result = zapi.item.update(
itemid=item[‘itemid’],
name=translated_item_name,
description=translated_item_description
)
print(f”監(jiān)控項(xiàng)更新成功: {update_item_result}”)
except Exception as e:
print(f”跳過監(jiān)控項(xiàng)更新: {translated_item_name},原因: {str(e)}”)
def update_triggers():
triggers = zapi.trigger.get(templateids=template_id, output=[‘triggerid’, ‘description’, ‘comments’])
for trigger in triggers:
trigger_description = trigger[‘description’]
trigger_comments = trigger.get(‘comments’, ‘No Comments’)
translated_trigger_description = check_and_translate(trigger_description, max_length=255)
translated_trigger_comments = translate_text(trigger_comments)
try:
update_trigger_result = zapi.trigger.update(
triggerid=trigger[‘triggerid’],
description=translated_trigger_description,
comments=translated_trigger_comments
)
print(f”觸發(fā)器更新成功: {update_trigger_result}”)
except Exception as e:
print(f”跳過觸發(fā)器更新: {trigger_description},原因: {str(e)}”)
def update_discovery_rules():
discovery_rules = zapi.discoveryrule.get(templateids=template_id, output=[‘itemid’, ‘name’, ‘description’])
for rule in discovery_rules:
rule_name = rule[‘name’]
rule_description = rule.get(‘description’, ‘No Description’)
translated_rule_name = check_and_translate(rule_name, max_length=50)
translated_rule_description = check_and_translate(rule_description, max_length=3000)
try:
update_rule_result = zapi.discoveryrule.update(
itemid=rule[‘itemid’],
name=translated_rule_name,
description=translated_rule_description
)
print(f”自動(dòng)發(fā)現(xiàn)規(guī)則更新成功: {update_rule_result}”)
except Exception as e:print(f”跳過自動(dòng)發(fā)現(xiàn)規(guī)則更新: {translated_rule_name},原因: {str(e)}”)

4. 實(shí)現(xiàn)效果

4.1 獲取Zabbix模板信息

4.2 翻譯Zabbix模板信息

4.3 翻譯前后對比

5. 總結(jié)

通過結(jié)合Zabbix API 與 Local LLM,我們實(shí)現(xiàn)了 Zabbix 模板的自動(dòng)化翻譯。這一解決方案通過自動(dòng)獲取模板、調(diào)用本地語言模型進(jìn)行智能翻譯,并最終更新 Zabbix 模板的全流程自動(dòng)化,極大提升了翻譯效率,減少了手動(dòng)操作帶來的錯(cuò)誤風(fēng)險(xiǎn)。

這種方法不僅適用于Zabbix 模板的翻譯,還可以擴(kuò)展至其他類似場景,例如配置文件的翻譯、監(jiān)控規(guī)則的國際化等。通過結(jié)合自動(dòng)化和 AI 技術(shù),企業(yè)可以更高效地應(yīng)對全球化的運(yùn)維需求。

本文章轉(zhuǎn)載微信公眾號(hào)@twt企業(yè)IT社區(qū)

上一篇:

大語言模型友好的 API:借助集體智慧構(gòu)建更好的軟件架構(gòu)

下一篇:

Elasticsearch Inference API 增加對阿里云 AI 的支持
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門場景實(shí)測,選對API

#AI文本生成大模型API

對比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對比試用API 限時(shí)免費(fèi)