python3监控硬件设备的维保到期时间

#!/usr/bin/python3
# -*- coding: utf-8 -*-

'''
此脚本用于zabbix监控所有硬件设备的维保到期时间
'''

import xlrd,datetime,time,os
from xlrd import xldate_as_tuple
import json

class Maintence:

    def __init__(self):
        self.file = '/Users/fanht/Desktop/机房设备20190726.xlsx'
        self.today = time.time()

    @property
    def read_excel(self):

        wb = xlrd.open_workbook(filename=self.file)   # 打开文件
        sheet1 = wb.sheet_by_index(0)  # 通过索引获取表格,表格对象
        row_message = {}
        for row in range(1,sheet1.nrows):
            line = sheet1.row_values(row) # 每一行的内容

            serial_number = line[4] # 设备序列号

            if sheet1.cell(row, 9).ctype == 3:
                date = xldate_as_tuple(sheet1.cell(row,9).value,0)
                maintenance_time = datetime.datetime(*date)  # 维保到期时间
                ans_time = time.mktime(maintenance_time.timetuple()) # 维保到期时间时间戳
                row_message[serial_number] = ans_time
        return row_message


    @property
    def time_diff(self):

        time_dic = {}
        for key,value in self.read_excel.items():
            time_poor = int((value - self.today)/86400)# 维保时间截止今天的天数差
            time_dic[key] = time_poor
        return time_dic


if __name__ == '__main__':

    maintence = Maintence()
    a = []
    for key in maintence.time_diff.keys():
        b= {}
        b["{#SERIAL}"] = key
        a.append(b)

    print(json.dumps(a,sort_keys=True, indent=6))

你可能感兴趣的:(python,日常运维)