簡體   English   中英

使用api在zabbix中克隆服務器

[英]clone the server in zabbix using api

對不起,但是我不知道如何在zabbix中克隆服務器,所以請幫忙。 我在zabbix文檔中沒有找到具有此功能的json請求,因此嘗試創建個人腳本,但是當我從服務器獲取項目並嘗試將其添加到新腳本時,出現此錯誤:

{“ jsonrpc”:“ 2.0”,“錯誤”:{“代碼”:-32602,“消息”:“無效的參數。”,“數據”:“項目使用來自非父級宿主的宿主接口。”},“ ID“:1}

謝謝

當然,如果需要我的代碼:

create_host() { 
a='{ "jsonrpc": "2.0", "method": "host.create", "params": { "host": "'$selected_name'", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "'$IP'", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "1" } ], "templates": [ { "templateid": "10001" } ], "inventory": { "macaddress_a": "01234", "macaddress_b": "56768" } }, "auth": "'$AUTH_TOKEN'", "id": 1 }'
        wget -O- -o /dev/null $API --no-check-certificate --header 'Content-Type: application/json-rpc' --post-data "$a"
}

push_items() {
id_of_host=`echo $3 | tr -d ']' | tr -d '[u' | sed -s  "s/'//g"`
echo "name"$1
echo "key"$2
request_push='{
        "jsonrpc": "2.0",
        "method": "item.create",
        "params": {
                "name": "'$1'",
                "key_": "'$2'",
                "hostid": "'$id_of_host'",
                "type": 10,
                "value_type": 0,
                "interfaceid": "2",
                "delay": 300
                },
        "auth": "'$AUTH_TOKEN'",
        "id": 1
}'
        wget -O- -o /dev/null $API --no-check-certificate --header 'Content-Type: application/json-rpc' --post-data "$request_push"
}
get_items() {
request='{
    "jsonrpc": "2.0",
    "method": "item.get",
    "params": {
        "output": ["name", "key_"],
        "host": "'$ETALON_SERVER'",
        "sortfield": "name"
    },
    "auth": "'$AUTH_TOKEN'",
    "id": 1
}'
        wget -O- -o /dev/null $API --no-check-certificate --header 'Content-Type: application/json-rpc' --post-data "$request"
}


AUTH_TOKEN=$(authenticate)

if [ -z "$AUTH_TOKEN" ]; then
        echo "Connection not established"
        exit 1
else
        echo "everything is ok"
       echo $AUTH_TOKEN
fi

check_host=$(check_exist_host)
host_create=$(create_host)
#echo "$host_create"
id_host=`echo "$host_create" | python -c 'import json, sys; print json.load(sys.stdin)["result"]["hostids"]'`
items=$(get_items)
#echo $items
keys=`echo "$items" | python -c 'import json, sys; print ("".join(i["name"]+";"+i["key_"] +"|" for i in json.load(sys.stdin)["result"]))'`
while IFS='[;]' read -r s1 s2; do
        name_item=$s1
        item_key=$s2
        push_items "$s1" "$s2" "$id_host"
done < <(printf "%b" "${keys//|/\\n}")

這個問題年代久遠,但仍然出現在搜索結果的頂部...

該API不允許使用host.clone(),因為應該根據需要通過host.get()和host.create()執行該API。 以下代碼克隆

  • 主機名
  • 主機可見名稱
  • 第一個IP地址
  • 組,添加一個新的(靜態ID)

和變化

  • 模板(狀態ID)

重命名並禁用了舊主機,以維護數據歷史記錄。

from pyzabbix import ZabbixAPI
ZAPI = ZabbixAPI(api_url)
ZAPI.login(username, password)

batch_list = [
    "host1-asdasd",
    "host2-asdasd"
]

for hostname in batch_list:
    try:
        original_host = ZAPI.host.get(
            filter={'host': hostname},
            selectGroups='extend',
            selectInterfaces='extend',
            selectMacros='extend'
        )[0]
        disable = ZAPI.host.update(
            hostid=original_host['hostid'],
            status=1,
            host=original_host['host'] + '-history',
            name=original_host['name'] + ' (history)'
        )
        print(disable)
        clone = ZAPI.host.create(
            host=original_host['host'],
            name=original_host['name'],
            proxy_hostid=original_host['proxy_hostid'],
            groups=original_host['groups'] + [{'groupid': 802}],
            macros=original_host['macros'],
            interfaces=[{'main': '1', 'type': '1', 'useip': '1', 'dns': '', 'port': '10050', 'bulk': '1',
                         'ip': original_host['interfaces'][0]['ip']}],
            templates={'templateid': 25708}
        )
        print(clone)
    except:
        print('something went wrong with: ' + hostname)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM