1. 程式人生 > >zabbix:zabbix api中hostgroup.get、host.get和host.update的詳解

zabbix:zabbix api中hostgroup.get、host.get和host.update的詳解

1.hostgroup.get獲取組資訊
該方法允許根據給定的引數檢索主機組。
Parameters 引數:

引數 型別 描述
graphids string/array Return only host groups that contain hosts or templates with the given graphs.只返回包含具有給定圖表的主機或模板的主機組。
groupids string/array Return only host groups with the given host group IDs. 只返回具有給定主機組ID的主機組。
hostids string/array Return only host groups that contain the given hosts.只返回包含給定主機的主機組。
maintenanceids string/array Return only host groups that are affected by the given maintenances. 僅返回受指定維護影響的主機組。
monitored_hosts flag Return only host groups that contain monitored hosts.僅返回包含受監視主機的主機組。
real_hosts flag Return only host groups that contain hosts.僅返回包含主機的主機組。
templated_hosts flag Return only host groups that contain templates.只返回包含模板的主機組。
templateids string/array Return only host groups that contain the given templates.只返回包含給定模板的主機組。
triggerids string/array Return only host groups that contain hosts or templates with the given triggers.僅返回包含具有給定觸發器的主機或模板的主機組。
with_applications flag Return only host groups that contain hosts with applications.僅返回包含具有應用程式主機的主機組。
with_graphs flag Return only host groups that contain hosts with graphs.僅返回包含具有圖表的主機的主機組。
with_hosts_and_templates flag Return only host groups that contain hosts or templates. 只返回包含主機或模板的主機組
with_httptests flag Return only host groups that contain hosts with web checks.僅返回包含具有Web檢查的主機的主機組。 Overrides the with_monitored_httptests parameter.覆蓋“with_monitored_httptests”引數。
with_items flag Return only host groups that contain hosts or templates with items.僅返回包含主題或包含專案的模板的主機組。 Overrides the with_monitored_items andwith_simple_graph_items parameters.覆蓋“with_monitored_items”和“with_simple_graph_items“引數。
with_monitored_httptests flag Return only host groups that contain hosts with enabled web checks.僅返回包含啟用Web檢查的主機的主機組。
with_monitored_items flag Return only host groups that contain hosts or templates with enabled items.僅返回包含啟用專案的主機或模板的主機組。 Overrides the with_simple_graph_items parameter.覆蓋with_simple_graph_items引數。
with_monitored_triggers flag Return only host groups that contain hosts with enabled triggers. All of the items used in the trigger must also be enabled. 僅返回包含啟用觸發器的主機的主機組。 觸發器中使用的所有專案也必須啟用。
with_simple_graph_items flag Return only host groups that contain hosts with numeric items. 僅返回包含具有數字專案的主機的主機組。
with_triggers flag Return only host groups that contain hosts with triggers.僅返回包含具有觸發器的主機的主機組。 Overrides the with_monitored_triggers parameter. 覆蓋“with_monitored_triggers”引數。
selectDiscoveryRule query Return the LLD rule that created the host group in the discoveryRule property.返回在“discoveryRule”屬性中建立主機組的LLD規則。
selectGroupDiscovery query Return the host group discovery object in the groupDiscovery property. 在“groupDiscovery”屬性中返回主機組發現物件。 The host group discovery object links a discovered host group to a host group prototype and has the following properties: 主機組發現物件將發現的主機組連結到主機組原型,並具有以下屬性:groupid - (string) ID of the discovered host group; 發現的主機組的ID;lastcheck - (timestamp) time when the host group was last discovered;主機組最後發現的時間; name - (string) name of the host goup prototype;主機組名稱原型; parent_group_prototypeid - (string) ID of the host group prototype from which the host group has been created;建立主機組的主機組原型的ID; ts_delete - (timestamp) time when a host group that is no longer discovered will be deleted.不再發現主機組將被刪除的時間。
selectHosts query Return the hosts that belong to the host group in the hosts property.在“hosts”屬性中返回屬於主機組的主機。 Supports count.
selectTemplates query Return the templates that belong to the host group in the templates property. 在“”模板“”屬性中返回屬於主機組的模板。Supports count.
limitSelects integer Limits the number of records returned by subselects. 限制子選擇返回的記錄數。Applies to the following subselects:適用於以下子選項: selectHosts - results will be sorted by host; 結果將按“主機”排序;selectTemplates - results will be sorted by host.結果將按“主機”排序。sortfield string/array Sort the result by the given properties. 按照給定的屬性對結果進行排序。Possible values are: groupid, name. 可能的值為:groupid,name。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------

Request:

{
    "jsonrpc": "2.0",
    "method": "hostgroup.get",
    "params": {
        "output": "extend",
        "filter": {
            "name": [
                "Zabbix servers",
                "Linux servers"
            ]
        }
    },
    "auth": "6f38cddc44cfbb6c1bd186f9a220b5a0",
    "id": 1
}

Response:

{
    "jsonrpc": "2.0",
    "result": [
        {
            "groupid": "2",
            "name": "Linux servers",
            "internal": "0"
        },
        {
            "groupid": "4",
            "name": "Zabbix servers",
            "internal": "0"
        }
    ],
    "id": 1
}

2.host.get
此方法允許根據指定的引數獲取主機。
Parameters引數

引數 型別 描述
groupids string/array 僅返回指定主機組所屬的主機。
applicationids string/array 僅返回含有指定應用集的主機。
dserviceids string/array 僅返回與指定自動發現服務相關的主機。
graphids string/array 僅返回含有指定圖表的主機。
hostids string/array 僅返回指定主機ID的主機。
httptestids string/array 僅返回含有指定網頁監測的主機。
interfaceids string/array 僅返回使用指定介面的主機。
itemids string/array 僅返回含有指定監控項的主機。
maintenanceids string/array 僅返回被指定管理影響到的主機。
monitored_hosts flag 僅返回被監控的主機。
proxy_hosts flag 僅返回代理伺服器。
proxyids string/array 僅返回被代理伺服器監控的主機。
templated_hosts flag 同時返回主機和模板。
templateids string/array 僅返回與指定模板連結的主機。
triggerids string/array 僅返回含有指定觸發器的主機。
with_items flag 僅返回含有監控項的主機。Overrides the with_monitored_items and with_simple_graph_items parameters. 覆蓋“with_monitored_items”和“with_simple_graph_items”引數。
with_applications flag 僅返回含有應用集的主機。
with_graphs flag 僅返回含有圖表的主機。
with_httptests flag 僅返回含有網頁監測的主機。Overrides the with_monitored_httptests parameter.覆蓋“with_monitored_httptests”引數。
with_monitored_httptests flag 僅返回啟用網頁監測的主機。
with_monitored_items flag .僅返回啟用監控項的主機。Overrides the with_simple_graph_items parameter.覆蓋with_simple_graph_items引數。
with_monitored_triggers flag Return only hosts that have enabled triggers. All of the items used in the trigger must also be enabled.僅返回啟用觸發器的主機。所有在觸發器中使用到的監控項必須也要啟用。
with_simple_graph_items flag 僅返回含有數字類資訊監控項的主機。with_triggers flag 僅返回含有觸發器的主機。Overrides the with_monitored_triggers parameter.覆蓋“with_monitored_triggers”引數。
withInventory flag 僅返回含有資產清單資料的主機。
selectGroups query 返回在groups屬性中主機所屬的主機組。
selectApplications query 返回在applications屬性中來自主機的應用集。Supports count.支援count
selectDiscoveries query 返回在discoveries屬性中來自主機的底層自動發現。Supports count.支援count。
selectDiscoveryRule query R 返回在discoveryRule屬性中建立主機的底層自動發現規則。
selectGraphs query 返回在graphs屬性中來自主機的圖表。Supports count.支援count。
selectHostDiscovery query 返回在hostDiscovery屬性中的主機自動發現物件。 主機自動發現物件將一個自動發現的主機和一個原型主機連線起來,或者把一個原型主機和一個底層自動發現規則連線起來,並且含有以下屬性: host - (string) host of the host prototype;字串)原型主機的主機。 hostid - (string) ID of the discovered host or host prototype;(字串)自動發現的主機或原型主機的ID。 parent_hostid - (string) ID of the host prototype from which the host has been created; 字串)建立主機的原型主機ID。parent_itemid - (string) ID of the LLD rule that created the discovered host;(字串)建立自動發現主機的底層自動發現規則ID。 lastcheck - (timestamp) time when the host was last discovered; (時間戳)當最近一次自動發現主機時的時間。ts_delete - (timestamp) time when a host that is no longer discovered will be deleted.(時間戳)當不再自動發現的主機將被刪除的時間。
selectHttpTests query Return the web scenarios from the host in the httpTests property. 返回在httpTests屬性中主機裡的網頁應用場景Supports count.支援count。
selectInterfaces query 返回在interfaces屬性中的主機介面。 Supports count.支援count。
selectInventory query 返回在inventory屬性中主機裡的主機資產清單。
selectItems query 返回在items屬性中主機裡的監控項。 Supports count. 支援count。
selectMacros query 返回在macros屬性中主機裡的巨集。
selectParentTemplates query 返回在parentTemplates屬性中與主機關聯的模板。Supports count.支援count。
selectScreens query 返回在screens屬性中主機裡的螢幕。Supports count.支援count。
selectTriggers query 返回在triggers屬性中主機裡的觸發器。Supports count. 支援count。
filter object 僅返回完全匹配指定篩選後的結果。接受一個數組,其鍵值為屬性名,其值要麼是一個單一的值,要麼是一個匹配的陣列值。 Allows filtering by interface properties.允許通過介面屬性篩選。
limitSelects integer 限定由子查詢返回的記錄數量。 適用於以下子查詢:selectParentTemplates - results will be sorted by host;結果將由host排序; selectInterfaces; selectItems - sorted by name;由name排序; selectDiscoveries - sorted by name;由name排序; selectTriggers - sorted by description;由description排序; selectGraphs - sorted by name;由name排序; selectApplications - sorted by name; 由name排序;selectScreens - sorted by name. 由name排序;
search object Return results that match the given wildcard search.返回與指定萬用字元搜尋匹配的結果。Accepts an array, where the keys are property names, and the values are strings to search for. If no additional options are given, this will perform a LIKE “%…%” search.接受一個數組,其中鍵值是屬性名,其值為搜尋到的字串。如果沒有指定的額外選項,將會以LIKE “%…%”方式執行搜尋。Allows searching by interface properties. Works only with text fields.允許通過介面屬性搜尋,僅對文字域產生影響。
searchInventory object 僅返回與指定萬用字元搜尋資產清單資料匹配的主機。 This parameter is affected by the same additional parameters as search. 這個引數被相同其他引數,比如引數search所影響。
sortfield string/array 用指定的屬性排序結果。Possible values are: hostid, host, name, status.可能的值為:hostid,host,name,status。
countOutput flag These parameters being common for all get methods are described in detail in the reference commentary. 在reference commentary中詳細描述了所有“get”方法的共同引數.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

用例:
Request:

{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": "extend",
        "filter": {
            "host": [
                "Zabbix server",
                "Linux server"
            ]
        }
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}

Response:

{
    "jsonrpc": "2.0",
    "result": [
        {
            "maintenances": [],
            "hostid": "10160",
            "proxy_hostid": "0",
            "host": "Zabbix server",
            "status": "0",
            "disable_until": "0",
            "error": "",
            "available": "0",
            "errors_from": "0",
            "lastaccess": "0",
            "ipmi_authtype": "-1",
            "ipmi_privilege": "2",
            "ipmi_username": "",
            "ipmi_password": "",
            "ipmi_disable_until": "0",
            "ipmi_available": "0",
            "snmp_disable_until": "0",
            "snmp_available": "0",
            "maintenanceid": "0",
            "maintenance_status": "0",
            "maintenance_type": "0",
            "maintenance_from": "0",
            "ipmi_errors_from": "0",
            "snmp_errors_from": "0",
            "ipmi_error": "",
            "snmp_error": "",
            "jmx_disable_until": "0",
            "jmx_available": "0",
            "jmx_errors_from": "0",
            "jmx_error": "",
            "name": "Zabbix server",
            "description": "The Zabbix monitoring server.",
            "tls_connect": "1",
            "tls_accept": "1",
            "tls_issuer": "",
            "tls_subject": "",
            "tls_psk_identity": "",
            "tls_psk": ""
        },
        {
            "maintenances": [],
            "hostid": "10167",
            "proxy_hostid": "0",
            "host": "Linux server",
            "status": "0",
            "disable_until": "0",
            "error": "",
            "available": "0",
            "errors_from": "0",
            "lastaccess": "0",
            "ipmi_authtype": "-1",
            "ipmi_privilege": "2",
            "ipmi_username": "",
            "ipmi_password": "",
            "ipmi_disable_until": "0",
            "ipmi_available": "0",
            "snmp_disable_until": "0",
            "snmp_available": "0",
            "maintenanceid": "0",
            "maintenance_status": "0",
            "maintenance_type": "0",
            "maintenance_from": "0",
            "ipmi_errors_from": "0",
            "snmp_errors_from": "0",
            "ipmi_error": "",
            "snmp_error": "",
            "jmx_disable_until": "0",
            "jmx_available": "0",
            "jmx_errors_from": "0",
            "jmx_error": "",
            "name": "Linux server",
            "description": "",
            "tls_connect": "1",
            "tls_accept": "1",
            "tls_issuer": "",
            "tls_subject": "",
            "tls_psk_identity": "",
            "tls_psk": ""
        }
    ],
    "id": 1
}

檢索主機組:
Request:

{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid"],
        "selectGroups": "extend",
        "filter": {
            "host": [
                "Zabbix server"
            ]
        }
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 2
}

Response:

{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostid": "10085",
            "groups": [
                {
                    "groupid": "2",
                    "name": "Linux servers",
                    "internal": "0",
                    "flags": "0"
                },
                {
                    "groupid": "4",
                    "name": "Zabbix servers",
                    "internal": "0",
                    "flags": "0"
                }
            ]
        }
    ],
    "id": 2
}

檢索相關模板:

Request:

{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid"],
        "selectParentTemplates": [
            "templateid",
            "name"
        ],
        "hostids": "10084"
    },
    "id": 1,
    "auth": "70785d2b494a7302309b48afcdb3a401"
}

Response:

{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostid": "10084",
            "parentTemplates": [
                {
                    "name": "Template OS Linux",
                    "templateid": "10001"
                },
                {
                    "name": "Template App Zabbix Server",
                    "templateid": "10047"
                }
            ]
        }
    ],
    "id": 1
}

3.host.update
該方法允許更新目前的主機。
Parameters引數:
每一個主機的hostid屬性必須已定義過,其他屬性為可選項。僅指定屬性會被更新,其他屬性保持不變。

引數 型別 說明
groups object/array Host groups to replace the current host groups the host belongs to. 替換當前主機所屬主機組。The host groups must have the groupid property defined.主機組必須已定義過groupid屬性
interfaces object/array Host interfaces to replace the current host interfaces. 替換當前主機介面。
inventory object Host inventory properties.主機資產清單屬性。
macros object/array User macros to replace the current user macros.替換當前使用者巨集。
templates object/array Templates to replace the currently linked templates. Templates that are not passed are only unlinked. 替換當前連結的模板。 模板沒有傳遞僅刪除連結。The templates must have the templateid property defined.模板必須已定義過templateid屬性。
templates_clear object/array Templates to unlink and clear from the host. 從主機中刪除模板連結並清除。The templates must have the templateid property defined.模板必須已定義過templateid屬性。
--------------------------------------------------- ---------------------------------------------------------------------------

用例:

Enabling a host啟用一個主機
Enable host monitoring, i.e. set its status to 0.啟用主機監控,例如,把主機狀態設定為0。

Request:

{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10092",
        "status": 0
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}

Response:

{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10126"
        ]
    },
    "id": 1
}

Unlinking templates 刪除模板連結
Unlink and clear two templates from host.從主機中刪除連結並清除兩個模板。

Request:

{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10126",
        "templates_clear": [
            {
                "templateid": "10124"
            },
            {
                "templateid": "10125"
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}

Response:

{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10126"
        ]
    },
    "id": 1
}

Updating host macros更新主機的巨集
Replace all host macros with two new ones.用兩個新的巨集替換主機所有的巨集。

Request:

{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10126",
        "macros": [
            {
                "macro": "{$PASS}",
                "value": "password"
            },
            {
                "macro": "{$DISC}",
                "value": "sda"
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}

Response:

{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10126"
        ]
    },
    "id": 1
}

Updating host inventory 更新主機資產清單
Change inventory mode and add location更改資產清單模式並新增地點。

Request:

{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10387",
        "inventory_mode": 0,
        "inventory": {
            "location": "Latvia, Riga"
        }
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}

Response:

{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10387"
        ]
    },
    "id": 2
}