nagios 监控windows 磁盘 IO

script

报警的阀值没有进行处理,只是一个初级的版本

注释过两天再写吧,先把功能实现了。。。

from __future__ import division
import time,wmi,win32com
from optparse import OptionParser

parser = OptionParser(usage="%prog -w <warning threshold> -c <critical threshold> -p ",version="%prog ")
        
parser.add_option("-w", "--warning",action="store", type="string", dest="warn_threshold", help="Warning threshold in percentage")
        
parser.add_option("-c", "--critical",action="store", type="string", dest="crit_threshold", help="Critical threshold in percentage")

parser.add_option("-p", "--partion",action="store", type="string", dest="partion", help="Partion Name")
       
(options, args) = parser.parse_args()

def DISK(drive):  
    c = wmi.WMI ()  
  
    data_dict = {}  
    data_dict['total_available'] = 0  
    data_dict['total_capacity'] = 0  
    data_dict['total_free'] = 0  
  
    #  DriveType=3 : "Local Disk",
    disk = wmi.WMI(moniker='//./root/cimv2:Win32_LogicalDisk.DeviceID="%s:"' %drive)  
#    for disk in c.Win32_LogicalDisk (DriveType=3):  
    data_dict['total_available'] += round(float(disk.FreeSpace) / (1024*1024*1024), 2)  
    data_dict['total_capacity'] += round(float(disk.Size) / (1024*1024*1024), 2)  
    data_dict['total_free'] += round(float(disk.FreeSpace) / (1024*1024*1024), 2)  
  
    dev_tmp = {}  
    dev_tmp['dev'] = disk.DeviceID  
    dev_tmp['available'] = {'volume':round(float(disk.FreeSpace) / (1024*1024*1024), 2), 'unit':'GB'}  
    dev_tmp['capacity'] = {'volume':round(float(disk.Size) / (1024*1024*1024), 2), 'unit':'GB'}  
    dev_tmp['free'] = {'volume':round(float(disk.FreeSpace) / (1024*1024*1024), 2), 'unit':'GB'}  
    dev_tmp['fstype'] = disk.FileSystem  
    dev_tmp['mnt'] = ''  
    dev_tmp['used'] = round(long(disk.FreeSpace) / long(disk.Size), 2)  
  
    data_dict[disk.DeviceID] = dev_tmp  
  
    com = win32com.client.Dispatch("WbemScripting.SWbemRefresher")  
    obj = win32com.client.GetObject('''winmgmts:\\root\cimv2''')  
    diskitems = com.AddEnum(obj, "Win32_PerfFormattedData_PerfDisk_LogicalDisk").objectSet  
      
    com.Refresh()  
    for item in diskitems:  
        if item.Name in data_dict:  
            data_dict[item.Name]['io_stat'] = {}  
            data_dict[item.Name]['io_stat']['r/s'] = {'volume':float(item.DiskReadsPerSec), 'unit':''}  
            data_dict[item.Name]['io_stat']['w/s'] = {'volume':float(item.DiskWritesPerSec), 'unit':''}  
            data_dict[item.Name]['io_stat']['rkB/s'] = {'volume':(float(item.DiskReadBytesPerSec) / 1024), 'unit':'KB/s'}  
            data_dict[item.Name]['io_stat']['wkB/s'] = {'volume':(float(item.DiskWriteBytesPerSec) / 1024), 'unit':'KB/s'}  
#    return {'data':data_dict, 'timestamp':time.asctime(time.localtime())}
    return data_dict

#print DISK('C')['C:']['io_stat']['wkB/s']['volume'],DISK('C')['C:']['io_stat']['rkB/s']['volume']
def printr():
    disk = DISK(options.partion)
    diskw = disk[options.partion+':']['io_stat']['wkB/s']['volume']
    diskr = disk[options.partion+':']['io_stat']['rkB/s']['volume']
    print "Partion %s IO: READ %2.f KB/s Write %2.f KB/s | READ=%2.fKB WRITE=%2.fKB" %(options.partion,diskr,diskw,diskr,diskw)

if __name__ == '__main__':
    printr()


你可能感兴趣的:(nagios 监控windows 磁盘 IO)