bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/scollector/collectors/disk_windows.go (about)

     1  package collectors
     2  
     3  import (
     4  	"bosun.org/metadata"
     5  	"bosun.org/opentsdb"
     6  	"github.com/StackExchange/wmi"
     7  )
     8  
     9  func init() {
    10  	collectors = append(collectors, &IntervalCollector{F: c_physical_disk_windows})
    11  	collectors = append(collectors, &IntervalCollector{F: c_diskspace_windows})
    12  }
    13  
    14  const (
    15  	//Converts 100nS samples to 1S samples
    16  	winDisk100nS_1S = 10000000
    17  
    18  	//Converts 100nS samples to 1mS samples
    19  	winDisk100nS_1mS = 1000000
    20  
    21  	//Converts 100nS samples to 0-100 Percent samples
    22  	winDisk100nS_Pct = 100000
    23  )
    24  
    25  func c_diskspace_windows() (opentsdb.MultiDataPoint, error) {
    26  	var dst []Win32_LogicalDisk
    27  	var q = wmi.CreateQuery(&dst, "WHERE DriveType = 3 AND FreeSpace <> null")
    28  	err := queryWmi(q, &dst)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	var md opentsdb.MultiDataPoint
    33  	for _, v := range dst {
    34  		tags := opentsdb.TagSet{"disk": v.Name}
    35  		space_used := v.Size - v.FreeSpace
    36  		Add(&md, "win.disk.fs.space_free", v.FreeSpace, tags, metadata.Gauge, metadata.Bytes, osDiskFreeDesc)
    37  		Add(&md, "win.disk.fs.space_total", v.Size, tags, metadata.Gauge, metadata.Bytes, osDiskTotalDesc)
    38  		Add(&md, "win.disk.fs.space_used", space_used, tags, metadata.Gauge, metadata.Bytes, osDiskUsedDesc)
    39  		Add(&md, osDiskFree, v.FreeSpace, tags, metadata.Gauge, metadata.Bytes, osDiskFreeDesc)
    40  		Add(&md, osDiskTotal, v.Size, tags, metadata.Gauge, metadata.Bytes, osDiskTotalDesc)
    41  		Add(&md, osDiskUsed, space_used, tags, metadata.Gauge, metadata.Bytes, osDiskUsedDesc)
    42  		if v.Size != 0 {
    43  			percent_free := float64(v.FreeSpace) / float64(v.Size) * 100
    44  			Add(&md, "win.disk.fs.percent_free", percent_free, tags, metadata.Gauge, metadata.Pct, osDiskPctFreeDesc)
    45  			Add(&md, osDiskPctFree, percent_free, tags, metadata.Gauge, metadata.Pct, osDiskPctFreeDesc)
    46  		}
    47  		if v.VolumeName != "" {
    48  			metadata.AddMeta("", tags, "label", v.VolumeName, true)
    49  		}
    50  	}
    51  	return md, nil
    52  }
    53  
    54  type Win32_LogicalDisk struct {
    55  	FreeSpace  uint64
    56  	Name       string
    57  	Size       uint64
    58  	VolumeName string
    59  }
    60  
    61  func c_physical_disk_windows() (opentsdb.MultiDataPoint, error) {
    62  	var dst []Win32_PerfRawData_PerfDisk_PhysicalDisk
    63  	var q = wmi.CreateQuery(&dst, `WHERE Name <> '_Total'`)
    64  	err := queryWmi(q, &dst)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	var md opentsdb.MultiDataPoint
    69  	for _, v := range dst {
    70  		Add(&md, "win.disk.duration", v.AvgDiskSecPerRead/winDisk100nS_1mS, opentsdb.TagSet{"disk": v.Name, "type": "read"}, metadata.Counter, metadata.MilliSecond, "Time, in milliseconds, of a read from the disk.")
    71  		Add(&md, "win.disk.duration", v.AvgDiskSecPerWrite/winDisk100nS_1mS, opentsdb.TagSet{"disk": v.Name, "type": "write"}, metadata.Counter, metadata.MilliSecond, "Time, in milliseconds, of a write to the disk.")
    72  		Add(&md, "win.disk.queue", v.AvgDiskReadQueueLength/winDisk100nS_1S, opentsdb.TagSet{"disk": v.Name, "type": "read"}, metadata.Counter, metadata.Operation, "Number of read requests that were queued for the disk.")
    73  		Add(&md, "win.disk.queue", v.AvgDiskWriteQueueLength/winDisk100nS_1S, opentsdb.TagSet{"disk": v.Name, "type": "write"}, metadata.Counter, metadata.Operation, "Number of write requests that were queued for the disk.")
    74  		Add(&md, "win.disk.ops", v.DiskReadsPerSec, opentsdb.TagSet{"disk": v.Name, "type": "read"}, metadata.Counter, metadata.PerSecond, "Number of read operations on the disk.")
    75  		Add(&md, "win.disk.ops", v.DiskWritesPerSec, opentsdb.TagSet{"disk": v.Name, "type": "write"}, metadata.Counter, metadata.PerSecond, "Number of write operations on the disk.")
    76  		Add(&md, "win.disk.bytes", v.DiskReadBytesPerSec, opentsdb.TagSet{"disk": v.Name, "type": "read"}, metadata.Counter, metadata.BytesPerSecond, "Number of bytes read from the disk.")
    77  		Add(&md, "win.disk.bytes", v.DiskWriteBytesPerSec, opentsdb.TagSet{"disk": v.Name, "type": "write"}, metadata.Counter, metadata.BytesPerSecond, "Number of bytes written to the disk.")
    78  		Add(&md, "win.disk.percent_time", v.PercentDiskReadTime/winDisk100nS_Pct, opentsdb.TagSet{"disk": v.Name, "type": "read"}, metadata.Counter, metadata.Pct, "Percentage of time that the disk was busy servicing read requests.")
    79  		Add(&md, "win.disk.percent_time", v.PercentDiskWriteTime/winDisk100nS_Pct, opentsdb.TagSet{"disk": v.Name, "type": "write"}, metadata.Counter, metadata.Pct, "Percentage of time that the disk was busy servicing write requests.")
    80  		Add(&md, "win.disk.spltio", v.SplitIOPerSec, opentsdb.TagSet{"disk": v.Name}, metadata.Counter, metadata.PerSecond, "Number of requests to the disk that were split into multiple requests due to size or fragmentation.")
    81  	}
    82  	return md, nil
    83  }
    84  
    85  //See msdn for counter types http://msdn.microsoft.com/en-us/library/ms804035.aspx
    86  type Win32_PerfRawData_PerfDisk_PhysicalDisk struct {
    87  	AvgDiskReadQueueLength  uint64
    88  	AvgDiskSecPerRead       uint32
    89  	AvgDiskSecPerWrite      uint32
    90  	AvgDiskWriteQueueLength uint64
    91  	DiskReadBytesPerSec     uint64
    92  	DiskReadsPerSec         uint32
    93  	DiskWriteBytesPerSec    uint64
    94  	DiskWritesPerSec        uint32
    95  	Name                    string
    96  	PercentDiskReadTime     uint64
    97  	PercentDiskWriteTime    uint64
    98  	SplitIOPerSec           uint32
    99  }