python3 調用zabbix API實現批量增加刪除主機,主機各種監控項


 前端時間在工作中需要配置zabbix,配置起來特別的麻煩。當時想用ZABBIX API來搞,但是一直沒時間,最近有時間來研究研究,並記下筆記,以便復用!

在python3爬蟲中,我喜歡用request 模塊,所以這里也用request來弄!

  首先貼上zabbix官網和中文官網的地址:

  https://www.zabbix.com/documentation/3.4/manual/api/reference/item/object

  https://www.zabbix.com/documentation/3.4/zh/manual/api  任何時候任何難題在官網上幾乎都能找到答案,只是花費的時間多少問題了。

  首先,我們想用利用zabbix的restful api來訪問zabbix,肯定是需要登陸認證的。在zabbix的后續操作中,必須要有一個TOKEN,這也是官方介紹的:官方上實現的方法如下

{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1,
    "auth": null
}
  • jsonrpc - API使用的JSON-RPC協議的版本; Zabbix API實現JSON-RPC版本2.0;  (必須的)
  • method - 調用的API方法;    
  • params - 將被傳遞給API方法的參數;
  • id - 請求的任意標識符;  這個的id需要特別理解下,我之前一直不是很理解。這里的id其實就是一個標志,設置多少無所謂,主要是做返回標志用,也就是這里設置多少,返回數據也會有一個相同的ID 用來標志這個返回對應那個請求!
  • auth -用戶認證令牌; 因為我們還沒有一個,它的設置null。

  了解了之后我們構造一個請求函數----目的是獲取token

    def __init__():
        self.url = 'http://192.168.230.164/zabbix/api_jsonrpc.php'
        self.headers = {'Content-Type': 'application/json'}
        auth = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": "Admin", ###驗證
                "password":"zfno11"
            },
            "id": 1,
            "auth":None,
        }
        response = requests.post(self.url, data=json.dumps(auth), headers=self.headers)
        authid = json.loads(response.text)['result']  ### auth的id  也就是token

OK 我們已經獲取了TOKEN 了接下來就可以為所欲為了!

第二步我們獲取所有主機list信息(其實也可以去數據庫里面取--但是如果去數據庫里面取 還得寫數據庫函數 輸入賬號密碼等等等。。。。)這里我們假設第一步獲取的ID(TOKEN)為AAAAA

    def get_hosts():
        neirong={
            "jsonrpc": "2.0",
            "method": "host.get",
            "params": {
                "output": [
                    "hostid",
                    "host"
                ],
                "selectInterfaces": [
                    "interfaceid",
                    "ip"
                ]
            },
            "id": 2,
            "auth": ”AAA“
        }
        response = requests.post(self.url, data=json.dumps(neirong), headers=self.headers)
        print(response.text)

這里就會返回類似如下的列表:

{"jsonrpc":"2.0","result":[{"hostid":"10255","host":"QH-PS-IM-qq65","interfaces":[{"interfaceid":"3","ip":"192.168.230.165"}]},{"hostid":"10084","host":"Zabbix server","interfaces":[{"interfaceid":"1","ip":"1
27.0.0.1"}]}],"id":2}

我們發現了返回了兩台主機(我這里只有兩台)第一台hostid 10255 hostname:QH-PS-IM-qq65   interfaceid:3  注意這里的interfacse很重要 因為以后對主機項的操作(比如添加item)就需要這個玩意兒。。  我在這里卡了很久 以為這個不重要 官網的說法如下:

 

interfaceid 
(required)
string ID of the item's host interface. Used only for host items. 

Optional for Zabbix agent (active), Zabbix internal, Zabbix trapper, Dependent item, Zabbix aggregate, database monitor and calculated items.

第三步:創建主機ITEM

  機上上一步我們說到了創建主機 item,我們就來創建一個  上一步我們已經獲取到了 主機的 hostid  10255  interfaceid 3 這兩個是添加item必須的對於創建item官網的實例為:

{
    "jsonrpc": "2.0", "method": "item.create", "params": { "name": "Free disk space on $1", "key_": "vfs.fs.size[/home/joe/,free]", "hostid": "10084", "type": 0, "value_type": 3, "interfaceid": "1", "delay": 30 }, "auth": "0424bd59b807674191e7d77572075f33", "id": 3 }
好了 我們稍稍改一下:
    def item_create():
        neirong ={
            "jsonrpc": "2.0",
            "method": "item.create",
            "params": {
                "name": "Free disk space on $1",
                "key_": "vfs.fs.size[/boot,pused]",
                "hostid": "10255",
                "type": 0,
                "value_type": 0,
                "interfaceid": "3",
                "delay": 5
            },
            "auth": authid ,
            "id": 3
        }
        response1 = requests.post(self.url, data=json.dumps(neirong), headers=self.headers)print(response1.text)
        print("OK")

這里面的type  value_type的意思可以去官網詳細看看 我這里設置的是0 0  也就是代表 zabbix_agent float  相信有zabbix基礎的同學是秒懂

接下來我們去zabbix圖形化界面上看一看吧:

而且有數據。。完美!!!

 

OK 我們知道我們一般創建一個監控項之后都會對這個監控項添加圖形!

第四步:創建監控項圖形

    我們上一步創建了監控項,那么返回值是什么么? 我們最后print(response1.text)發現如下:

    {"jsonrpc":"2.0","result":{"itemids":["28257"]},"id":3}

    這里面的itemids非常重要,我們添加這個監控項對於的圖形就需要用到他!

直接上代碼

    def graf_create(self, authid):
        neirong = {
                "jsonrpc": "2.0",
                "method": "graph.create",
                "params": {
                    "name": "test1",
                    "width": 900,
                    "height": 200,
                    "gitems": [
                        {
                            "itemid": "28257",
                            "color": "00AA00"
                        }
                    ]
                },
                "auth": authid,
                "id": 4
            }

        response1 = requests.post(self.url, data=json.dumps(neirong), headers=self.headers)
        print(response1)
        print(response1.text)
        print("OK")

有數據並且OK 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM