bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/scollector/collectors/mem_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_simple_mem_windows}) 11 collectors = append(collectors, &IntervalCollector{F: c_windows_memory}) 12 collectors = append(collectors, &IntervalCollector{F: c_windows_pagefile}) 13 } 14 15 func c_simple_mem_windows() (opentsdb.MultiDataPoint, error) { 16 var dst []Win32_OperatingSystem 17 var q = wmi.CreateQuery(&dst, "") 18 err := queryWmi(q, &dst) 19 if err != nil { 20 return nil, err 21 } 22 var md opentsdb.MultiDataPoint 23 for _, v := range dst { 24 Add(&md, "win.mem.vm.total", v.TotalVirtualMemorySize*1024, nil, metadata.Gauge, metadata.Bytes, descWinMemVirtual_Total) 25 Add(&md, "win.mem.vm.free", v.FreeVirtualMemory*1024, nil, metadata.Gauge, metadata.Bytes, descWinMemVirtual_Free) 26 Add(&md, "win.mem.total", v.TotalVisibleMemorySize*1024, nil, metadata.Gauge, metadata.Bytes, descWinMemVisible_Total) 27 Add(&md, "win.mem.free", v.FreePhysicalMemory*1024, nil, metadata.Gauge, metadata.Bytes, descWinMemVisible_Free) 28 Add(&md, osMemTotal, v.TotalVisibleMemorySize*1024, nil, metadata.Gauge, metadata.Bytes, osMemTotalDesc) 29 Add(&md, osMemFree, v.FreePhysicalMemory*1024, nil, metadata.Gauge, metadata.Bytes, osMemFreeDesc) 30 Add(&md, osMemUsed, v.TotalVisibleMemorySize*1024-v.FreePhysicalMemory*1024, nil, metadata.Gauge, metadata.Bytes, osMemUsedDesc) 31 Add(&md, osMemPctFree, float64(v.FreePhysicalMemory)/float64(v.TotalVisibleMemorySize)*100, nil, metadata.Gauge, metadata.Pct, osMemPctFreeDesc) 32 } 33 return md, nil 34 } 35 36 const ( 37 descWinMemVirtual_Total = "Number, in bytes, of virtual memory." 38 descWinMemVirtual_Free = "Number, in bytes, of virtual memory currently unused and available." 39 descWinMemVisible_Total = "Total amount, in bytes, of physical memory available to the operating system." 40 descWinMemVisible_Free = "Number, in bytes, of physical memory currently unused and available." 41 ) 42 43 func c_windows_memory() (opentsdb.MultiDataPoint, error) { 44 var dst []Win32_PerfRawData_PerfOS_Memory 45 var q = wmi.CreateQuery(&dst, "") 46 err := queryWmi(q, &dst) 47 if err != nil { 48 return nil, err 49 } 50 var md opentsdb.MultiDataPoint 51 for _, v := range dst { 52 Add(&md, "win.mem.cache", v.CacheBytes, nil, metadata.Gauge, metadata.Bytes, descWinMemCacheBytes) 53 Add(&md, "win.mem.cache_peak", v.CacheBytesPeak, nil, metadata.Gauge, metadata.Bytes, descWinMemCacheBytesPeak) 54 Add(&md, "win.mem.committed", v.CommittedBytes, nil, metadata.Gauge, metadata.Bytes, descWinMemCommittedBytes) 55 Add(&md, "win.mem.committed_limit", v.CommitLimit, nil, metadata.Gauge, metadata.Bytes, descWinMemCommitLimit) 56 Add(&md, "win.mem.committed_percent", float64(v.PercentCommittedBytesInUse)/float64(v.PercentCommittedBytesInUse_Base)*100, nil, metadata.Gauge, metadata.Pct, descWinMemPercentCommittedBytesInUse) 57 Add(&md, "win.mem.modified", v.ModifiedPageListBytes, nil, metadata.Gauge, metadata.Bytes, descWinMemModifiedPageListBytes) 58 Add(&md, "win.mem.page_faults", v.PageFaultsPersec, nil, metadata.Counter, metadata.PerSecond, descWinMemPageFaultsPersec) 59 Add(&md, "win.mem.faults", v.CacheFaultsPersec, opentsdb.TagSet{"type": "cache"}, metadata.Counter, metadata.PerSecond, descWinMemCacheFaultsPersec) 60 Add(&md, "win.mem.faults", v.DemandZeroFaultsPersec, opentsdb.TagSet{"type": "demand_zero"}, metadata.Counter, metadata.PerSecond, descWinMemDemandZeroFaultsPersec) 61 Add(&md, "win.mem.faults", v.TransitionFaultsPersec, opentsdb.TagSet{"type": "transition"}, metadata.Counter, metadata.PerSecond, descWinMemTransitionFaultsPersec) 62 Add(&md, "win.mem.faults", v.WriteCopiesPersec, opentsdb.TagSet{"type": "write_copies"}, metadata.Counter, metadata.PerSecond, descWinMemWriteCopiesPersec) 63 Add(&md, "win.mem.page_operations", v.PageReadsPersec, opentsdb.TagSet{"type": "read"}, metadata.Counter, metadata.PerSecond, descWinMemPageReadsPersec) 64 Add(&md, "win.mem.page_operations", v.PageWritesPersec, opentsdb.TagSet{"type": "write"}, metadata.Counter, metadata.PerSecond, descWinMemPageWritesPersec) 65 Add(&md, "win.mem.page_operations", v.PagesInputPersec, opentsdb.TagSet{"type": "input"}, metadata.Counter, metadata.PerSecond, descWinMemPagesInputPersec) 66 Add(&md, "win.mem.page_operations", v.PagesOutputPersec, opentsdb.TagSet{"type": "output"}, metadata.Counter, metadata.PerSecond, descWinMemPagesOutputPersec) 67 Add(&md, "win.mem.pool.bytes", v.PoolNonpagedBytes, opentsdb.TagSet{"type": "nonpaged"}, metadata.Gauge, metadata.Bytes, descWinMemPoolNonpagedBytes) 68 Add(&md, "win.mem.pool.bytes", v.PoolPagedBytes, opentsdb.TagSet{"type": "paged"}, metadata.Gauge, metadata.Bytes, descWinMemPoolPagedBytes) 69 Add(&md, "win.mem.pool.bytes", v.PoolPagedResidentBytes, opentsdb.TagSet{"type": "paged_resident"}, metadata.Gauge, metadata.Bytes, descWinMemPoolPagedResidentBytes) 70 Add(&md, "win.mem.pool.allocations", v.PoolPagedAllocs, opentsdb.TagSet{"type": "paged"}, metadata.Gauge, metadata.Operation, descWinMemPoolPagedAllocs) 71 Add(&md, "win.mem.pool.allocations", v.PoolNonpagedAllocs, opentsdb.TagSet{"type": "nonpaged"}, metadata.Gauge, metadata.Operation, descWinMemPoolNonpagedAllocs) 72 } 73 return md, nil 74 } 75 76 const ( 77 descWinMemCacheBytes = "Cache Bytes the size, in bytes, of the portion of the system file cache which is currently resident and active in physical memory." 78 descWinMemCacheBytesPeak = "Cache Bytes Peak is the maximum number of bytes used by the system file cache since the system was last restarted. This might be larger than the current size of the cache." 79 descWinMemCacheFaultsPersec = "Cache Faults/sec is the rate at which faults occur when a page sought in the file system cache is not found and must be retrieved from elsewhere in memory (a soft fault) or from disk (a hard fault). The file system cache is an area of physical memory that stores recently used pages of data for applications. Cache activity is a reliable indicator of most application I/O operations. This counter shows the number of faults, without regard for the number of pages faulted in each operation." 80 descWinMemCommitLimit = "Commit Limit is the amount of virtual memory that can be committed without having to extend the paging file(s). It is measured in bytes. Committed memory is the physical memory which has space reserved on the disk paging files. If the paging file(s) are be expanded, this limit increases accordingly." 81 descWinMemCommittedBytes = "Committed Bytes is the amount of committed virtual memory, in bytes. Committed memory is the physical memory which has space reserved on the disk paging file(s)." 82 descWinMemDemandZeroFaultsPersec = "Demand Zero Faults/sec is the rate at which a zeroed page is required to satisfy the fault. Zeroed pages, pages emptied of previously stored data and filled with zeros, are a security feature of Windows that prevent processes from seeing data stored by earlier processes that used the memory space. Windows maintains a list of zeroed pages to accelerate this process. This counter shows the number of faults, without regard to the number of pages retrieved to satisfy the fault." 83 descWinMemModifiedPageListBytes = "Modified Page List Bytes is the amount of physical memory, in bytes, that is assigned to the modified page list. This memory contains cached data and code that is not actively in use by processes, the system and the system cache. This memory needs to be written out before it will be available for allocation to a process or for system use." 84 descWinMemPageFaultsPersec = "Page Faults/sec is the average number of pages faulted per second. It is measured in number of pages faulted per second because only one page is faulted in each fault operation, hence this is also equal to the number of page fault operations. This counter includes both hard faults (those that require disk access) and soft faults (where the faulted page is found elsewhere in physical memory.) Most processors can handle large numbers of soft faults without significant consequence. However, hard faults, which require disk access, can cause significant delays." 85 descWinMemPageReadsPersec = "Page Reads/sec is the rate at which the disk was read to resolve hard page faults. It shows the number of reads operations, without regard to the number of pages retrieved in each operation. Hard page faults occur when a process references a page in virtual memory that is not in working set or elsewhere in physical memory, and must be retrieved from disk. This counter is a primary indicator of the kinds of faults that cause system-wide delays. It includes read operations to satisfy faults in the file system cache (usually requested by applications) and in non-cached mapped memory files. Compare the value of Memory\\Pages Reads/sec to the value of Memory\\Pages Input/sec to determine the average number of pages read during each operation." 86 descWinMemPagesInputPersec = "Pages Input/sec is the rate at which pages are read from disk to resolve hard page faults. Hard page faults occur when a process refers to a page in virtual memory that is not in its working set or elsewhere in physical memory, and must be retrieved from disk. When a page is faulted, the system tries to read multiple contiguous pages into memory to maximize the benefit of the read operation. Compare the value of Memory\\Pages Input/sec to the value of Memory\\Page Reads/sec to determine the average number of pages read into memory during each read operation." 87 descWinMemPagesOutputPersec = "Pages Output/sec is the rate at which pages are written to disk to free up space in physical memory. Pages are written back to disk only if they are changed in physical memory, so they are likely to hold data, not code. A high rate of pages output might indicate a memory shortage. Windows writes more pages back to disk to free up space when physical memory is in short supply. This counter shows the number of pages, and can be compared to other counts of pages, without conversion." 88 descWinMemPageWritesPersec = "Page Writes/sec is the rate at which pages are written to disk to free up space in physical memory. Pages are written to disk only if they are changed while in physical memory, so they are likely to hold data, not code. This counter shows write operations, without regard to the number of pages written in each operation." 89 descWinMemPercentCommittedBytesInUse = "% Committed Bytes In Use is the ratio of Memory\\Committed Bytes to the Memory\\Commit Limit. Committed memory is the physical memory in use for which space has been reserved in the paging file should it need to be written to disk. The commit limit is determined by the size of the paging file. If the paging file is enlarged, the commit limit increases, and the ratio is reduced)." 90 descWinMemPoolNonpagedAllocs = "Pool Nonpaged Allocs is the number of calls to allocate space in the nonpaged pool. The nonpaged pool is an area of system memory area for objects that cannot be written to disk, and must remain in physical memory as long as they are allocated. It is measured in numbers of calls to allocate space, regardless of the amount of space allocated in each call." 91 descWinMemPoolNonpagedBytes = "Pool Nonpaged Bytes is the size, in bytes, of the nonpaged pool, an area of the system virtual memory that is used for objects that cannot be written to disk, but must remain in physical memory as long as they are allocated. Memory\\Pool Nonpaged Bytes is calculated differently than Process\\Pool Nonpaged Bytes, so it might not equal Process(_Total)\\Pool Nonpaged Bytes." 92 descWinMemPoolPagedAllocs = "Pool Paged Allocs is the number of calls to allocate space in the paged pool. The paged pool is an area of the system virtual memory that is used for objects that can be written to disk when they are not being used. It is measured in numbers of calls to allocate space, regardless of the amount of space allocated in each call." 93 descWinMemPoolPagedBytes = "Pool Paged Bytes is the size, in bytes, of the paged pool, an area of the system virtual memory that is used for objects that can be written to disk when they are not being used." 94 descWinMemPoolPagedResidentBytes = "Pool Paged Resident Bytes is the size, in bytes, of the portion of the paged pool that is currently resident and active in physical memory." 95 descWinMemTransitionFaultsPersec = "Transition Faults/sec is the rate at which page faults are resolved by recovering pages that were being used by another process sharing the page, or were on the modified page list or the standby list, or were being written to disk at the time of the page fault. The pages were recovered without additional disk activity. Transition faults are counted in numbers of faults; because only one page is faulted in each operation, it is also equal to the number of pages faulted." 96 descWinMemWriteCopiesPersec = "Write Copies/sec is the rate at which page faults are caused by attempts to write that have been satisfied by coping of the page from elsewhere in physical memory. This is an economical way of sharing data since pages are only copied when they are written to; otherwise, the page is shared. This counter shows the number of copies, without regard for the number of pages copied in each operation." 97 ) 98 99 type Win32_PerfRawData_PerfOS_Memory struct { 100 CacheBytes uint64 101 CacheBytesPeak uint64 102 CacheFaultsPersec uint32 103 CommitLimit uint64 104 CommittedBytes uint64 105 DemandZeroFaultsPersec uint32 106 ModifiedPageListBytes uint64 107 PageFaultsPersec uint32 108 PageReadsPersec uint32 109 PagesInputPersec uint32 110 PagesOutputPersec uint32 111 PageWritesPersec uint32 112 PercentCommittedBytesInUse uint32 113 PercentCommittedBytesInUse_Base uint32 114 PoolNonpagedAllocs uint32 115 PoolNonpagedBytes uint64 116 PoolPagedAllocs uint32 117 PoolPagedBytes uint64 118 PoolPagedResidentBytes uint64 119 TransitionFaultsPersec uint32 120 WriteCopiesPersec uint32 121 } 122 123 func c_windows_pagefile() (opentsdb.MultiDataPoint, error) { 124 var dst []Win32_PageFileUsage 125 var q = wmi.CreateQuery(&dst, "") 126 err := queryWmi(q, &dst) 127 if err != nil { 128 return nil, err 129 } 130 var md opentsdb.MultiDataPoint 131 for _, v := range dst { 132 driveletter := "unknown" 133 if len(v.Name) >= 1 { 134 driveletter = v.Name[0:1] 135 } 136 tags := opentsdb.TagSet{"drive": driveletter} 137 Add(&md, "win.mem.pagefile.size", int64(v.AllocatedBaseSize)*1024*1024, tags, metadata.Gauge, metadata.Bytes, descWinMemPagefileAllocatedBaseSize) 138 Add(&md, "win.mem.pagefile.usage_current", int64(v.CurrentUsage)*1024*1024, tags, metadata.Gauge, metadata.Bytes, descWinMemPagefileCurrentUsage) 139 Add(&md, "win.mem.pagefile.usage_peak", int64(v.PeakUsage)*1024*1024, tags, metadata.Gauge, metadata.Bytes, descWinMemPagefilePeakUsage) 140 Add(&md, "win.mem.pagefile.usage_percent", float64(v.CurrentUsage)/float64(v.AllocatedBaseSize)*100, tags, metadata.Gauge, metadata.Pct, descWinMemPagefilePercent) 141 } 142 return md, nil 143 } 144 145 const ( 146 descWinMemPagefileAllocatedBaseSize = "The actual amount of disk space in bytes allocated for use with this page file. This value corresponds to the range established in Win32_PageFileSetting under the InitialSize and MaximumSize properties, set at system startup." 147 descWinMemPagefileCurrentUsage = "How many bytes of the total reserved page file are currently in use." 148 descWinMemPagefilePeakUsage = "The maximum number of bytes used in the page file since the system was restarted." 149 descWinMemPagefilePercent = "The current used page file size / total page file size." 150 ) 151 152 type Win32_PageFileUsage struct { 153 AllocatedBaseSize uint32 154 CurrentUsage uint32 155 PeakUsage uint32 156 Name string 157 }