zabbix 硬盘状态收集,制作表格
将zabbix首页复制到a文件里,这里主要是用到首页里 最近出现的问题 的信息
# -*- coding:utf-8 -*-
import time
import os
from openpyxl import Workbook
from openpyxl import load_workbook
#获取状态信息
def zabbixdisk():
ip=os.popen("grep 'DiskHealthy' a | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' |sort -t'.' -k1,1nr -k2,2nr -k3,3nr -k4,4nr|uniq").read().split('\n')
#一个端口重复一次,避免由于异常导致错过正常端口
zabbixport=['10050','10050']
data=[]
#遍历服务器ip
for i in ip:
if i == '':
continue
name=os.popen("grep -w '%s' a | awk '{print $1}'|uniq"%i).read().replace('\n',"")
num=0
healthy=0
disk=[]
status=[]
errore=[]
#遍历端口
while len(zabbixport) > num:
healthy=os.popen("/usr/local/zabbix/bin/zabbix_get -s%s -p%s -k'disk.healthy'"%(i,zabbixport[num])).read()
num=num + 1
if num == len(zabbixport) and healthy == "" :
errore.append(i)
break
elif healthy != "" :
num=len(zabbixport)+1
#将状态信息分类
for ii in healthy.split('\n'):
htmp=[]
for iii in ii.split(' '):
if "/dev" in iii :
disk.append(iii)
else:
htmp.append(iii)
status.append(' '.join(htmp))
data.append([i,name,'%s'%'\n'.join(disk).rstrip("\n"),"%s"%'\n'.join(status).rstrip("\n")])
return data,ip,'\n'.join(errore)
#获取时间
def newdate(sj):
ISOTIMEFORMAT='%Y-%m-%d %X'
if sj == 'Obtain':
timedate=time.strftime(ISOTIMEFORMAT, time.localtime())
return timedate
else:
timedate=time.strftime(ISOTIMEFORMAT,time.localtime(sj.st_mtime))
return timedate
#写入表格
def exsl(data):
name="sample-%s.xlsx"%newdate('Obtain').replace(' ','').replace(':','')
wb = Workbook()
# grab the active worksheet
ws = wb.active
# Data can be assigned directly to cells
ws['A1'] = 'IP'
ws['B1'] = 'HostName'
ws['C1'] = 'Disk'
ws['D1'] = 'status'
for i in data:
# Rows can also be appended
ws.append([i[0], i[1], i[2], i[3] ])
# Save the file
wb.save("/tmp/%s"%name)
return "/tmp/%s"%name
data,ip,error =zabbixdisk()
print "The number of servers that have detected a total hard disk: %s"%(len(ip)-1)
print "ZABBIX access to the number of: %s"%len(data)
if len(ip)-1 > len(data):
print "The following IP failed to succeed: \n%s"%error
print exsl(data)