真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

利用ZABBIX進(jìn)行服務(wù)器自動(dòng)巡檢并導(dǎo)出報(bào)表

實(shí)現(xiàn)思路

主要是利用zabbix的api來對(duì)數(shù)據(jù)進(jìn)行獲取處理,實(shí)現(xiàn)思路如下:
利用ZABBIX進(jìn)行服務(wù)器自動(dòng)巡檢并導(dǎo)出報(bào)表

創(chuàng)新互聯(lián)長(zhǎng)期為上千多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為光澤企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、做網(wǎng)站,光澤網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

  1. zabbix提供了豐富的api,可以根據(jù)此api獲取zabbix得主機(jī)信息,監(jiān)控項(xiàng)ID,監(jiān)控項(xiàng)的趨勢(shì)數(shù)據(jù)和歷史數(shù)據(jù)
  2. 首先根據(jù)主機(jī)組ID獲取組內(nèi)的所有主機(jī)信息,包括主機(jī)名和IP地址
  3. 循環(huán)主機(jī)組內(nèi)的主機(jī)ID,并在循環(huán)里再嵌套一個(gè)根據(jù)監(jiān)控項(xiàng)鍵值獲取監(jiān)控項(xiàng)ID的請(qǐng)求
  4. 根據(jù)獲取到的監(jiān)控項(xiàng)ID分別獲取歷史數(shù)據(jù)和趨勢(shì)數(shù)據(jù)
  5. 將歷史數(shù)據(jù)和趨勢(shì)數(shù)據(jù)的值寫到一個(gè)字典里,并把循環(huán)之后的所有字典添加到列表中
  6. 將列表中的信息寫入到Excel中,把腳本放到定時(shí)任務(wù)中定時(shí)執(zhí)行

定義獲取的時(shí)間間隔

x=(datetime.datetime.now()-datetime.timedelta(minutes=120)).strftime("%Y-%m-%d %H:%M:%S")
y=(datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
def timestamp(x,y):
    p=time.strptime(x,"%Y-%m-%d %H:%M:%S")
    starttime = str(int(time.mktime(p)))
    q=time.strptime(y,"%Y-%m-%d %H:%M:%S")
    endtime= str(int(time.mktime(q)))
    return starttime,endtime

根據(jù)主機(jī)組ID獲取主機(jī)信息

def get_hosts(groupids,auth):
    data ={
            "jsonrpc": "2.0",
             "method": "host.get",
             "params": {
             "output": [ "name"],
             "groupids": groupids,
             "filter":{
                 "status": "0"
             },
             "selectInterfaces": [   
                        "ip"
                    ],
            },
            "auth": auth,  # theauth id is what auth script returns, remeber it is string
            "id": 1
        }
    gethost=requests.post(url=ApiUrl,headers=header,json=data)
    return json.loads(gethost.content)["result"]

根據(jù)獲取到的主機(jī)信息構(gòu)建循環(huán),獲取主機(jī)監(jiān)控項(xiàng)的數(shù)據(jù)

獲取歷史數(shù)據(jù)
host=[]
    print(hosts)
    for i in hosts:
        item1=[]
        item2=[]
        #print(i)
        dic1={}
        for j in ['vfs.fs.size[C:,total]','vm.memory.size[total]','system.cpu.num']:
            data={
                "jsonrpc": "2.0",
                "method": "item.get",
                "params": {
                    "output": [
                        "itemid"

                    ],
                    "search": {
                        "key_": j  
                    },
                    "hostids": i['hostid']
                },
                "auth":auth,
                "id": 1
            }
            getitem=requests.post(url=ApiUrl,headers=header,json=data)
            item=json.loads(getitem.content)['result']

            hisdata={
                "jsonrpc":"2.0",
                "method":"history.get",
                "params":{
                    "output":"extend",                    
                    "time_from":timestamp[0],
                    #"time_till":timestamp[1],
                    "history":0,
                    "sortfield": "clock",
                    "sortorder": "DESC",
                    "itemids": '%s' %(item[0]['itemid']),
                    "limit":1
                },
                "auth": auth,
                "id":1
                }
            gethist=requests.post(url=ApiUrl,headers=header,json=hisdata)
            hist=json.loads(gethist.content)['result']
            item1.append(hist)
獲取趨勢(shì)數(shù)據(jù)
for j in ['vfs.fs.size[C:,used]','vm.memory.size[used]','system.cpu.load']:
            data={
                "jsonrpc": "2.0",
                "method": "item.get",
                "params": {
                    "output": [
                        "itemid"

                    ],
                    "search": {
                        "key_": j  
                    },
                    "hostids": i['hostid']
                },
                "auth":auth,
                "id": 1
            }
            getitem=requests.post(url=ApiUrl,headers=header,json=data)
            item=json.loads(getitem.content)['result']

            trendata={
                "jsonrpc":"2.0",
                "method":"trend.get",
                "params":{
                    "output": [
                        "itemid",
                        "value_max",
                        "value_avg"
                    ],                    
                    "time_from":timestamp[0],
                    "time_till":timestamp[1],
                    "itemids": '%s' %(item[0]['itemid']),
                    "limit":1
                },
                "auth": auth,
                "id":1
                }
            gettrend=requests.post(url=ApiUrl,headers=header,json=trendata)
            trend=json.loads(gettrend.content)['result']
            item2.append(trend) 

對(duì)獲取到的數(shù)據(jù)進(jìn)行處理,并導(dǎo)出到csv文件中

dic1['Hostname']=i['name']
        dic1['IP']=i['interfaces'][0]['ip']
        dic1['磁盤C:Total(B)']=round(float(item1[0][0]['value'])/1024**3,2)
        dic1['磁盤最大C:Used(B)']=round(float(item2[0][0]['value_max'])/1024**3,2)
        dic1['內(nèi)存Total(B)']=round(float(item1[1][0]['value'])/1024**3,2)
        dic1['內(nèi)存最大Used(B)']=round(float(item2[1][0]['value_max'])/1024**3,2)
        dic1['內(nèi)存平均used(B)']=round(float(item2[1][0]['value_avg'])/1024**3,2)
        dic1['CPU負(fù)載最大值']=item2[2][0]['value_max']
        dic1['CPU負(fù)載平均值']=item2[2][0]['value_avg']
        dic1['CPU 核數(shù)']=item1[2][0]['value']
        x = time.localtime(int(item1[2][0]['clock']))
        item1[2][0]['clock'] = time.strftime("%Y-%m-%d %H:%M:%S", x)
        dic1['clock']=item1[2][0]['clock']
        host.append(dic1)  
        print(item)
    print(host)
    return host       
def writecsv(getitem1):
    with open('data.csv','w',encoding='utf-8-sig') as f:
        #f.write(codecs.BOM_UTF8)
        writer = csv.DictWriter(f,csvheader)
        writer.writeheader()
        for row in getitem1:
            writer.writerow(row)

實(shí)現(xiàn)效果如下:

利用ZABBIX進(jìn)行服務(wù)器自動(dòng)巡檢并導(dǎo)出報(bào)表

完整代碼可以訪問github地址:“https://github.com/sunsharing-note/zabbix/blob/master/xunjian_auto.py”
zabbix API地址:https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/history/get


歡迎各位關(guān)注個(gè)人公號(hào)“沒有故事的陳師傅”
利用ZABBIX進(jìn)行服務(wù)器自動(dòng)巡檢并導(dǎo)出報(bào)表


新聞名稱:利用ZABBIX進(jìn)行服務(wù)器自動(dòng)巡檢并導(dǎo)出報(bào)表
網(wǎng)站路徑:http://weahome.cn/article/jhccjp.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部