We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
system.mem.pct_usable is missing on Windows
We don't compute this very useful metric on Windows. We should to have more consistency with Unix.
Need to hack around there: 
https://github.com/DataDog/dd-agent/blob/master/checks/system/win32.py#L166


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `checks/system/win32.py`
Content:
```
1 # project
2 from checks import Check
3 
4 # 3rd party
5 try:
6     import psutil
7 except ImportError:
8     psutil = None
9 
10 try:
11     import wmi
12     w = wmi.WMI()
13 except Exception:
14     wmi, w = None, None
15 
16 # Device WMI drive types
17 class DriveType(object):
18     UNKNOWN, NOROOT, REMOVEABLE, LOCAL, NETWORK, CD, RAM = (0, 1, 2, 3, 4, 5, 6)
19 B2MB  = float(1048576)
20 KB2MB = B2KB = float(1024)
21 
22 def should_ignore_disk(name, blacklist_re):
23     # blacklist_re is a compiled regex, compilation done at config loading time
24     return name =='_total' or blacklist_re is not None and blacklist_re.match(name)
25 
26 class Processes(Check):
27     def __init__(self, logger):
28         Check.__init__(self, logger)
29         self.gauge('system.proc.queue_length')
30         self.gauge('system.proc.count')
31 
32     def check(self, agentConfig):
33         try:
34             os = w.Win32_PerfFormattedData_PerfOS_System()[0]
35         except AttributeError:
36             self.logger.info('Missing Win32_PerfFormattedData_PerfOS_System WMI class.' \
37                              ' No process metrics will be returned.')
38             return
39 
40         try:
41             cpu = w.Win32_PerfFormattedData_PerfOS_Processor(name="_Total")[0]
42         except AttributeError:
43             self.logger.info('Missing Win32_PerfFormattedData_PerfOS_Processor WMI class.' \
44                              ' No process metrics will be returned.')
45             return
46         if os.ProcessorQueueLength is not None:
47             self.save_sample('system.proc.queue_length', os.ProcessorQueueLength)
48         if os.Processes is not None:
49             self.save_sample('system.proc.count', os.Processes)
50 
51         return self.get_metrics()
52 
53 class Memory(Check):
54     def __init__(self, logger):
55         Check.__init__(self, logger)
56         self.logger = logger
57         self.gauge('system.mem.free')
58         self.gauge('system.mem.used')
59         self.gauge('system.mem.total')
60         self.gauge('system.mem.cached')
61         self.gauge('system.mem.committed')
62         self.gauge('system.mem.paged')
63         self.gauge('system.mem.nonpaged')
64 
65     def check(self, agentConfig):
66         try:
67             os = w.Win32_OperatingSystem()[0]
68         except AttributeError:
69             self.logger.info('Missing Win32_OperatingSystem. No memory metrics will be returned.')
70             return
71 
72         if os.TotalVisibleMemorySize is not None and os.FreePhysicalMemory is not None:
73             total = int(os.TotalVisibleMemorySize) / KB2MB
74             free = int(os.FreePhysicalMemory) / KB2MB
75             self.save_sample('system.mem.total', total)
76             self.save_sample('system.mem.free', free)
77             self.save_sample('system.mem.used', total - free)
78 
79         mem = w.Win32_PerfFormattedData_PerfOS_Memory()[0]
80         if mem.CacheBytes is not None:
81             self.save_sample('system.mem.cached', int(mem.CacheBytes) / B2MB)
82         if mem.CommittedBytes is not None:
83             self.save_sample('system.mem.committed', int(mem.CommittedBytes) / B2MB)
84         if mem.PoolPagedBytes is not None:
85             self.save_sample('system.mem.paged', int(mem.PoolPagedBytes) / B2MB)
86         if mem.PoolNonpagedBytes is not None:
87             self.save_sample('system.mem.nonpaged', int(mem.PoolNonpagedBytes) / B2MB)
88 
89         return self.get_metrics()
90 
91 class Cpu(Check):
92     def __init__(self, logger):
93         Check.__init__(self, logger)
94         self.logger = logger
95         self.counter('system.cpu.user')
96         self.counter('system.cpu.idle')
97         self.gauge('system.cpu.interrupt')
98         self.counter('system.cpu.system')
99 
100     def check(self, agentConfig):
101         try:
102             cpu = w.Win32_PerfFormattedData_PerfOS_Processor()
103         except AttributeError:
104             self.logger.info('Missing Win32_PerfFormattedData_PerfOS_Processor WMI class.' \
105                              ' No CPU metrics will be returned.')
106             return
107 
108         cpu_interrupt = self._average_metric(cpu, 'PercentInterruptTime')
109         if cpu_interrupt is not None:
110             self.save_sample('system.cpu.interrupt', cpu_interrupt)
111 
112         cpu_percent = psutil.cpu_times()
113 
114         self.save_sample('system.cpu.user', 100 * cpu_percent.user / psutil.NUM_CPUS)
115         self.save_sample('system.cpu.idle', 100 * cpu_percent.idle / psutil.NUM_CPUS)
116         self.save_sample('system.cpu.system', 100 * cpu_percent.system/ psutil.NUM_CPUS)
117 
118         return self.get_metrics()
119 
120     def _average_metric(self, wmi_class, wmi_prop):
121         ''' Sum all of the values of a metric from a WMI class object, excluding
122             the value for "_Total"
123         '''
124         val = 0
125         counter = 0
126         for wmi_object in wmi_class:
127             if wmi_object.Name == '_Total':
128                 # Skip the _Total value
129                 continue
130 
131             if getattr(wmi_object, wmi_prop) is not None:
132                 counter += 1
133                 val += float(getattr(wmi_object, wmi_prop))
134 
135         if counter > 0:
136             return val / counter
137 
138         return val
139 
140 
141 class Network(Check):
142     def __init__(self, logger):
143         Check.__init__(self, logger)
144         self.logger = logger
145         self.gauge('system.net.bytes_rcvd')
146         self.gauge('system.net.bytes_sent')
147 
148     def check(self, agentConfig):
149         try:
150             net = w.Win32_PerfFormattedData_Tcpip_NetworkInterface()
151         except AttributeError:
152             self.logger.info('Missing Win32_PerfFormattedData_Tcpip_NetworkInterface WMI class.' \
153                              ' No network metrics will be returned')
154             return
155 
156         for iface in net:
157             name = self.normalize_device_name(iface.name)
158             if iface.BytesReceivedPerSec is not None:
159                 self.save_sample('system.net.bytes_rcvd', iface.BytesReceivedPerSec,
160                     device_name=name)
161             if iface.BytesSentPerSec is not None:
162                 self.save_sample('system.net.bytes_sent', iface.BytesSentPerSec,
163                     device_name=name)
164         return self.get_metrics()
165 
166 class Disk(Check):
167     def __init__(self, logger):
168         Check.__init__(self, logger)
169         self.logger = logger
170         self.gauge('system.disk.free')
171         self.gauge('system.disk.total')
172         self.gauge('system.disk.in_use')
173         self.gauge('system.disk.used')
174 
175     def check(self, agentConfig):
176         try:
177             disk = w.Win32_LogicalDisk()
178         except AttributeError:
179             self.logger.info('Missing Win32_LogicalDisk WMI class.'  \
180                              ' No disk metrics will be returned.')
181             return
182 
183         blacklist_re = agentConfig.get('device_blacklist_re', None)
184         for device in disk:
185             name = self.normalize_device_name(device.name)
186             if device.DriveType in (DriveType.CD, DriveType.UNKNOWN) or should_ignore_disk(name, blacklist_re):
187                 continue
188             if device.FreeSpace is not None and device.Size is not None:
189                 free = float(device.FreeSpace) / B2KB
190                 total = float(device.Size) / B2KB
191                 used = total - free
192                 self.save_sample('system.disk.free', free, device_name=name)
193                 self.save_sample('system.disk.total', total, device_name=name)
194                 self.save_sample('system.disk.used', used, device_name=name)
195                 self.save_sample('system.disk.in_use', (used / total),
196                     device_name=name)
197         return self.get_metrics()
198 
199 class IO(Check):
200     def __init__(self, logger):
201         Check.__init__(self, logger)
202         self.logger = logger
203         self.gauge('system.io.wkb_s')
204         self.gauge('system.io.w_s')
205         self.gauge('system.io.rkb_s')
206         self.gauge('system.io.r_s')
207         self.gauge('system.io.avg_q_sz')
208 
209     def check(self, agentConfig):
210         try:
211             disk = w.Win32_PerfFormattedData_PerfDisk_LogicalDisk()
212         except AttributeError:
213             self.logger.info('Missing Win32_PerfFormattedData_PerfDisk_LogicalDiskUnable WMI class.' \
214                              ' No I/O metrics will be returned.')
215             return
216         blacklist_re = agentConfig.get('device_blacklist_re', None)
217         for device in disk:
218             name = self.normalize_device_name(device.name)
219             if should_ignore_disk(name, blacklist_re):
220                 continue
221             if device.DiskWriteBytesPerSec is not None:
222                 self.save_sample('system.io.wkb_s', int(device.DiskWriteBytesPerSec) / B2KB,
223                     device_name=name)
224             if device.DiskWritesPerSec is not None:
225                 self.save_sample('system.io.w_s', int(device.DiskWritesPerSec),
226                     device_name=name)
227             if device.DiskReadBytesPerSec is not None:
228                 self.save_sample('system.io.rkb_s', int(device.DiskReadBytesPerSec) / B2KB,
229                     device_name=name)
230             if device.DiskReadsPerSec is not None:
231                 self.save_sample('system.io.r_s', int(device.DiskReadsPerSec),
232                     device_name=name)
233             if device.CurrentDiskQueueLength is not None:
234                 self.save_sample('system.io.avg_q_sz', device.CurrentDiskQueueLength,
235                     device_name=name)
236         return self.get_metrics()
237
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/checks/system/win32.py b/checks/system/win32.py
--- a/checks/system/win32.py
+++ b/checks/system/win32.py
@@ -57,10 +57,23 @@
         self.gauge('system.mem.free')
         self.gauge('system.mem.used')
         self.gauge('system.mem.total')
+        # area of physical memory that stores recently used pages of data
+        # for applications
         self.gauge('system.mem.cached')
+        # Committed memory is physical memory for which space has been
+        # reserved on the disk paging file in case it must be written
+        # back to disk
         self.gauge('system.mem.committed')
+        # physical memory used by the operating system, for objects
+        # that can be written to disk when they are not being used
         self.gauge('system.mem.paged')
+        # physical memory used by the operating system for objects that
+        # cannot be written to disk, but must remain in physical memory
+        # as long as they are allocated.
         self.gauge('system.mem.nonpaged')
+        # usable = free + cached
+        self.gauge('system.mem.usable')
+        self.gauge('system.mem.pct_usable')
 
     def check(self, agentConfig):
         try:
@@ -69,6 +82,10 @@
             self.logger.info('Missing Win32_OperatingSystem. No memory metrics will be returned.')
             return
 
+        total = 0
+        free = 0
+        cached = 0
+
         if os.TotalVisibleMemorySize is not None and os.FreePhysicalMemory is not None:
             total = int(os.TotalVisibleMemorySize) / KB2MB
             free = int(os.FreePhysicalMemory) / KB2MB
@@ -76,9 +93,11 @@
             self.save_sample('system.mem.free', free)
             self.save_sample('system.mem.used', total - free)
 
+
         mem = w.Win32_PerfFormattedData_PerfOS_Memory()[0]
         if mem.CacheBytes is not None:
-            self.save_sample('system.mem.cached', int(mem.CacheBytes) / B2MB)
+            cached = int(mem.CacheBytes) / B2MB
+            self.save_sample('system.mem.cached', cached)
         if mem.CommittedBytes is not None:
             self.save_sample('system.mem.committed', int(mem.CommittedBytes) / B2MB)
         if mem.PoolPagedBytes is not None:
@@ -86,6 +105,12 @@
         if mem.PoolNonpagedBytes is not None:
             self.save_sample('system.mem.nonpaged', int(mem.PoolNonpagedBytes) / B2MB)
 
+        usable = free + cached
+        self.save_sample('system.mem.usable', usable)
+        if total > 0:
+            pct_usable = float(usable) / total
+            self.save_sample('system.mem.pct_usable', pct_usable)
+
         return self.get_metrics()
 
 class Cpu(Check):

{"golden_diff": "diff --git a/checks/system/win32.py b/checks/system/win32.py\n--- a/checks/system/win32.py\n+++ b/checks/system/win32.py\n@@ -57,10 +57,23 @@\n         self.gauge('system.mem.free')\n         self.gauge('system.mem.used')\n         self.gauge('system.mem.total')\n+        # area of physical memory that stores recently used pages of data\n+        # for applications\n         self.gauge('system.mem.cached')\n+        # Committed memory is physical memory for which space has been\n+        # reserved on the disk paging file in case it must be written\n+        # back to disk\n         self.gauge('system.mem.committed')\n+        # physical memory used by the operating system, for objects\n+        # that can be written to disk when they are not being used\n         self.gauge('system.mem.paged')\n+        # physical memory used by the operating system for objects that\n+        # cannot be written to disk, but must remain in physical memory\n+        # as long as they are allocated.\n         self.gauge('system.mem.nonpaged')\n+        # usable = free + cached\n+        self.gauge('system.mem.usable')\n+        self.gauge('system.mem.pct_usable')\n \n     def check(self, agentConfig):\n         try:\n@@ -69,6 +82,10 @@\n             self.logger.info('Missing Win32_OperatingSystem. No memory metrics will be returned.')\n             return\n \n+        total = 0\n+        free = 0\n+        cached = 0\n+\n         if os.TotalVisibleMemorySize is not None and os.FreePhysicalMemory is not None:\n             total = int(os.TotalVisibleMemorySize) / KB2MB\n             free = int(os.FreePhysicalMemory) / KB2MB\n@@ -76,9 +93,11 @@\n             self.save_sample('system.mem.free', free)\n             self.save_sample('system.mem.used', total - free)\n \n+\n         mem = w.Win32_PerfFormattedData_PerfOS_Memory()[0]\n         if mem.CacheBytes is not None:\n-            self.save_sample('system.mem.cached', int(mem.CacheBytes) / B2MB)\n+            cached = int(mem.CacheBytes) / B2MB\n+            self.save_sample('system.mem.cached', cached)\n         if mem.CommittedBytes is not None:\n             self.save_sample('system.mem.committed', int(mem.CommittedBytes) / B2MB)\n         if mem.PoolPagedBytes is not None:\n@@ -86,6 +105,12 @@\n         if mem.PoolNonpagedBytes is not None:\n             self.save_sample('system.mem.nonpaged', int(mem.PoolNonpagedBytes) / B2MB)\n \n+        usable = free + cached\n+        self.save_sample('system.mem.usable', usable)\n+        if total > 0:\n+            pct_usable = float(usable) / total\n+            self.save_sample('system.mem.pct_usable', pct_usable)\n+\n         return self.get_metrics()\n \n class Cpu(Check):\n", "issue": "system.mem.pct_usable is missing on Windows\nWe don't compute this very useful metric on Windows. We should to have more consistency with Unix.\nNeed to hack around there: \nhttps://github.com/DataDog/dd-agent/blob/master/checks/system/win32.py#L166\n\n", "before_files": [{"content": "# project\nfrom checks import Check\n\n# 3rd party\ntry:\n    import psutil\nexcept ImportError:\n    psutil = None\n\ntry:\n    import wmi\n    w = wmi.WMI()\nexcept Exception:\n    wmi, w = None, None\n\n# Device WMI drive types\nclass DriveType(object):\n    UNKNOWN, NOROOT, REMOVEABLE, LOCAL, NETWORK, CD, RAM = (0, 1, 2, 3, 4, 5, 6)\nB2MB  = float(1048576)\nKB2MB = B2KB = float(1024)\n\ndef should_ignore_disk(name, blacklist_re):\n    # blacklist_re is a compiled regex, compilation done at config loading time\n    return name =='_total' or blacklist_re is not None and blacklist_re.match(name)\n\nclass Processes(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.gauge('system.proc.queue_length')\n        self.gauge('system.proc.count')\n\n    def check(self, agentConfig):\n        try:\n            os = w.Win32_PerfFormattedData_PerfOS_System()[0]\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_PerfOS_System WMI class.' \\\n                             ' No process metrics will be returned.')\n            return\n\n        try:\n            cpu = w.Win32_PerfFormattedData_PerfOS_Processor(name=\"_Total\")[0]\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_PerfOS_Processor WMI class.' \\\n                             ' No process metrics will be returned.')\n            return\n        if os.ProcessorQueueLength is not None:\n            self.save_sample('system.proc.queue_length', os.ProcessorQueueLength)\n        if os.Processes is not None:\n            self.save_sample('system.proc.count', os.Processes)\n\n        return self.get_metrics()\n\nclass Memory(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.gauge('system.mem.free')\n        self.gauge('system.mem.used')\n        self.gauge('system.mem.total')\n        self.gauge('system.mem.cached')\n        self.gauge('system.mem.committed')\n        self.gauge('system.mem.paged')\n        self.gauge('system.mem.nonpaged')\n\n    def check(self, agentConfig):\n        try:\n            os = w.Win32_OperatingSystem()[0]\n        except AttributeError:\n            self.logger.info('Missing Win32_OperatingSystem. No memory metrics will be returned.')\n            return\n\n        if os.TotalVisibleMemorySize is not None and os.FreePhysicalMemory is not None:\n            total = int(os.TotalVisibleMemorySize) / KB2MB\n            free = int(os.FreePhysicalMemory) / KB2MB\n            self.save_sample('system.mem.total', total)\n            self.save_sample('system.mem.free', free)\n            self.save_sample('system.mem.used', total - free)\n\n        mem = w.Win32_PerfFormattedData_PerfOS_Memory()[0]\n        if mem.CacheBytes is not None:\n            self.save_sample('system.mem.cached', int(mem.CacheBytes) / B2MB)\n        if mem.CommittedBytes is not None:\n            self.save_sample('system.mem.committed', int(mem.CommittedBytes) / B2MB)\n        if mem.PoolPagedBytes is not None:\n            self.save_sample('system.mem.paged', int(mem.PoolPagedBytes) / B2MB)\n        if mem.PoolNonpagedBytes is not None:\n            self.save_sample('system.mem.nonpaged', int(mem.PoolNonpagedBytes) / B2MB)\n\n        return self.get_metrics()\n\nclass Cpu(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.counter('system.cpu.user')\n        self.counter('system.cpu.idle')\n        self.gauge('system.cpu.interrupt')\n        self.counter('system.cpu.system')\n\n    def check(self, agentConfig):\n        try:\n            cpu = w.Win32_PerfFormattedData_PerfOS_Processor()\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_PerfOS_Processor WMI class.' \\\n                             ' No CPU metrics will be returned.')\n            return\n\n        cpu_interrupt = self._average_metric(cpu, 'PercentInterruptTime')\n        if cpu_interrupt is not None:\n            self.save_sample('system.cpu.interrupt', cpu_interrupt)\n\n        cpu_percent = psutil.cpu_times()\n\n        self.save_sample('system.cpu.user', 100 * cpu_percent.user / psutil.NUM_CPUS)\n        self.save_sample('system.cpu.idle', 100 * cpu_percent.idle / psutil.NUM_CPUS)\n        self.save_sample('system.cpu.system', 100 * cpu_percent.system/ psutil.NUM_CPUS)\n\n        return self.get_metrics()\n\n    def _average_metric(self, wmi_class, wmi_prop):\n        ''' Sum all of the values of a metric from a WMI class object, excluding\n            the value for \"_Total\"\n        '''\n        val = 0\n        counter = 0\n        for wmi_object in wmi_class:\n            if wmi_object.Name == '_Total':\n                # Skip the _Total value\n                continue\n\n            if getattr(wmi_object, wmi_prop) is not None:\n                counter += 1\n                val += float(getattr(wmi_object, wmi_prop))\n\n        if counter > 0:\n            return val / counter\n\n        return val\n\n\nclass Network(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.gauge('system.net.bytes_rcvd')\n        self.gauge('system.net.bytes_sent')\n\n    def check(self, agentConfig):\n        try:\n            net = w.Win32_PerfFormattedData_Tcpip_NetworkInterface()\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_Tcpip_NetworkInterface WMI class.' \\\n                             ' No network metrics will be returned')\n            return\n\n        for iface in net:\n            name = self.normalize_device_name(iface.name)\n            if iface.BytesReceivedPerSec is not None:\n                self.save_sample('system.net.bytes_rcvd', iface.BytesReceivedPerSec,\n                    device_name=name)\n            if iface.BytesSentPerSec is not None:\n                self.save_sample('system.net.bytes_sent', iface.BytesSentPerSec,\n                    device_name=name)\n        return self.get_metrics()\n\nclass Disk(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.gauge('system.disk.free')\n        self.gauge('system.disk.total')\n        self.gauge('system.disk.in_use')\n        self.gauge('system.disk.used')\n\n    def check(self, agentConfig):\n        try:\n            disk = w.Win32_LogicalDisk()\n        except AttributeError:\n            self.logger.info('Missing Win32_LogicalDisk WMI class.'  \\\n                             ' No disk metrics will be returned.')\n            return\n\n        blacklist_re = agentConfig.get('device_blacklist_re', None)\n        for device in disk:\n            name = self.normalize_device_name(device.name)\n            if device.DriveType in (DriveType.CD, DriveType.UNKNOWN) or should_ignore_disk(name, blacklist_re):\n                continue\n            if device.FreeSpace is not None and device.Size is not None:\n                free = float(device.FreeSpace) / B2KB\n                total = float(device.Size) / B2KB\n                used = total - free\n                self.save_sample('system.disk.free', free, device_name=name)\n                self.save_sample('system.disk.total', total, device_name=name)\n                self.save_sample('system.disk.used', used, device_name=name)\n                self.save_sample('system.disk.in_use', (used / total),\n                    device_name=name)\n        return self.get_metrics()\n\nclass IO(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.gauge('system.io.wkb_s')\n        self.gauge('system.io.w_s')\n        self.gauge('system.io.rkb_s')\n        self.gauge('system.io.r_s')\n        self.gauge('system.io.avg_q_sz')\n\n    def check(self, agentConfig):\n        try:\n            disk = w.Win32_PerfFormattedData_PerfDisk_LogicalDisk()\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_PerfDisk_LogicalDiskUnable WMI class.' \\\n                             ' No I/O metrics will be returned.')\n            return\n        blacklist_re = agentConfig.get('device_blacklist_re', None)\n        for device in disk:\n            name = self.normalize_device_name(device.name)\n            if should_ignore_disk(name, blacklist_re):\n                continue\n            if device.DiskWriteBytesPerSec is not None:\n                self.save_sample('system.io.wkb_s', int(device.DiskWriteBytesPerSec) / B2KB,\n                    device_name=name)\n            if device.DiskWritesPerSec is not None:\n                self.save_sample('system.io.w_s', int(device.DiskWritesPerSec),\n                    device_name=name)\n            if device.DiskReadBytesPerSec is not None:\n                self.save_sample('system.io.rkb_s', int(device.DiskReadBytesPerSec) / B2KB,\n                    device_name=name)\n            if device.DiskReadsPerSec is not None:\n                self.save_sample('system.io.r_s', int(device.DiskReadsPerSec),\n                    device_name=name)\n            if device.CurrentDiskQueueLength is not None:\n                self.save_sample('system.io.avg_q_sz', device.CurrentDiskQueueLength,\n                    device_name=name)\n        return self.get_metrics()\n", "path": "checks/system/win32.py"}], "after_files": [{"content": "# project\nfrom checks import Check\n\n# 3rd party\ntry:\n    import psutil\nexcept ImportError:\n    psutil = None\n\ntry:\n    import wmi\n    w = wmi.WMI()\nexcept Exception:\n    wmi, w = None, None\n\n# Device WMI drive types\nclass DriveType(object):\n    UNKNOWN, NOROOT, REMOVEABLE, LOCAL, NETWORK, CD, RAM = (0, 1, 2, 3, 4, 5, 6)\nB2MB  = float(1048576)\nKB2MB = B2KB = float(1024)\n\ndef should_ignore_disk(name, blacklist_re):\n    # blacklist_re is a compiled regex, compilation done at config loading time\n    return name =='_total' or blacklist_re is not None and blacklist_re.match(name)\n\nclass Processes(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.gauge('system.proc.queue_length')\n        self.gauge('system.proc.count')\n\n    def check(self, agentConfig):\n        try:\n            os = w.Win32_PerfFormattedData_PerfOS_System()[0]\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_PerfOS_System WMI class.' \\\n                             ' No process metrics will be returned.')\n            return\n\n        try:\n            cpu = w.Win32_PerfFormattedData_PerfOS_Processor(name=\"_Total\")[0]\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_PerfOS_Processor WMI class.' \\\n                             ' No process metrics will be returned.')\n            return\n        if os.ProcessorQueueLength is not None:\n            self.save_sample('system.proc.queue_length', os.ProcessorQueueLength)\n        if os.Processes is not None:\n            self.save_sample('system.proc.count', os.Processes)\n\n        return self.get_metrics()\n\nclass Memory(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.gauge('system.mem.free')\n        self.gauge('system.mem.used')\n        self.gauge('system.mem.total')\n        # area of physical memory that stores recently used pages of data\n        # for applications\n        self.gauge('system.mem.cached')\n        # Committed memory is physical memory for which space has been\n        # reserved on the disk paging file in case it must be written\n        # back to disk\n        self.gauge('system.mem.committed')\n        # physical memory used by the operating system, for objects\n        # that can be written to disk when they are not being used\n        self.gauge('system.mem.paged')\n        # physical memory used by the operating system for objects that\n        # cannot be written to disk, but must remain in physical memory\n        # as long as they are allocated.\n        self.gauge('system.mem.nonpaged')\n        # usable = free + cached\n        self.gauge('system.mem.usable')\n        self.gauge('system.mem.pct_usable')\n\n    def check(self, agentConfig):\n        try:\n            os = w.Win32_OperatingSystem()[0]\n        except AttributeError:\n            self.logger.info('Missing Win32_OperatingSystem. No memory metrics will be returned.')\n            return\n\n        total = 0\n        free = 0\n        cached = 0\n\n        if os.TotalVisibleMemorySize is not None and os.FreePhysicalMemory is not None:\n            total = int(os.TotalVisibleMemorySize) / KB2MB\n            free = int(os.FreePhysicalMemory) / KB2MB\n            self.save_sample('system.mem.total', total)\n            self.save_sample('system.mem.free', free)\n            self.save_sample('system.mem.used', total - free)\n\n\n        mem = w.Win32_PerfFormattedData_PerfOS_Memory()[0]\n        if mem.CacheBytes is not None:\n            cached = int(mem.CacheBytes) / B2MB\n            self.save_sample('system.mem.cached', cached)\n        if mem.CommittedBytes is not None:\n            self.save_sample('system.mem.committed', int(mem.CommittedBytes) / B2MB)\n        if mem.PoolPagedBytes is not None:\n            self.save_sample('system.mem.paged', int(mem.PoolPagedBytes) / B2MB)\n        if mem.PoolNonpagedBytes is not None:\n            self.save_sample('system.mem.nonpaged', int(mem.PoolNonpagedBytes) / B2MB)\n\n        usable = free + cached\n        self.save_sample('system.mem.usable', usable)\n        if total > 0:\n            pct_usable = float(usable) / total\n            self.save_sample('system.mem.pct_usable', pct_usable)\n\n        return self.get_metrics()\n\nclass Cpu(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.counter('system.cpu.user')\n        self.counter('system.cpu.idle')\n        self.gauge('system.cpu.interrupt')\n        self.counter('system.cpu.system')\n\n    def check(self, agentConfig):\n        try:\n            cpu = w.Win32_PerfFormattedData_PerfOS_Processor()\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_PerfOS_Processor WMI class.' \\\n                             ' No CPU metrics will be returned.')\n            return\n\n        cpu_interrupt = self._average_metric(cpu, 'PercentInterruptTime')\n        if cpu_interrupt is not None:\n            self.save_sample('system.cpu.interrupt', cpu_interrupt)\n\n        cpu_percent = psutil.cpu_times()\n\n        self.save_sample('system.cpu.user', 100 * cpu_percent.user / psutil.NUM_CPUS)\n        self.save_sample('system.cpu.idle', 100 * cpu_percent.idle / psutil.NUM_CPUS)\n        self.save_sample('system.cpu.system', 100 * cpu_percent.system/ psutil.NUM_CPUS)\n\n        return self.get_metrics()\n\n    def _average_metric(self, wmi_class, wmi_prop):\n        ''' Sum all of the values of a metric from a WMI class object, excluding\n            the value for \"_Total\"\n        '''\n        val = 0\n        counter = 0\n        for wmi_object in wmi_class:\n            if wmi_object.Name == '_Total':\n                # Skip the _Total value\n                continue\n\n            if getattr(wmi_object, wmi_prop) is not None:\n                counter += 1\n                val += float(getattr(wmi_object, wmi_prop))\n\n        if counter > 0:\n            return val / counter\n\n        return val\n\n\nclass Network(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.gauge('system.net.bytes_rcvd')\n        self.gauge('system.net.bytes_sent')\n\n    def check(self, agentConfig):\n        try:\n            net = w.Win32_PerfFormattedData_Tcpip_NetworkInterface()\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_Tcpip_NetworkInterface WMI class.' \\\n                             ' No network metrics will be returned')\n            return\n\n        for iface in net:\n            name = self.normalize_device_name(iface.name)\n            if iface.BytesReceivedPerSec is not None:\n                self.save_sample('system.net.bytes_rcvd', iface.BytesReceivedPerSec,\n                    device_name=name)\n            if iface.BytesSentPerSec is not None:\n                self.save_sample('system.net.bytes_sent', iface.BytesSentPerSec,\n                    device_name=name)\n        return self.get_metrics()\n\nclass Disk(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.gauge('system.disk.free')\n        self.gauge('system.disk.total')\n        self.gauge('system.disk.in_use')\n        self.gauge('system.disk.used')\n\n    def check(self, agentConfig):\n        try:\n            disk = w.Win32_LogicalDisk()\n        except AttributeError:\n            self.logger.info('Missing Win32_LogicalDisk WMI class.'  \\\n                             ' No disk metrics will be returned.')\n            return\n\n        blacklist_re = agentConfig.get('device_blacklist_re', None)\n        for device in disk:\n            name = self.normalize_device_name(device.name)\n            if device.DriveType in (DriveType.CD, DriveType.UNKNOWN) or should_ignore_disk(name, blacklist_re):\n                continue\n            if device.FreeSpace is not None and device.Size is not None:\n                free = float(device.FreeSpace) / B2KB\n                total = float(device.Size) / B2KB\n                used = total - free\n                self.save_sample('system.disk.free', free, device_name=name)\n                self.save_sample('system.disk.total', total, device_name=name)\n                self.save_sample('system.disk.used', used, device_name=name)\n                self.save_sample('system.disk.in_use', (used / total),\n                    device_name=name)\n        return self.get_metrics()\n\nclass IO(Check):\n    def __init__(self, logger):\n        Check.__init__(self, logger)\n        self.logger = logger\n        self.gauge('system.io.wkb_s')\n        self.gauge('system.io.w_s')\n        self.gauge('system.io.rkb_s')\n        self.gauge('system.io.r_s')\n        self.gauge('system.io.avg_q_sz')\n\n    def check(self, agentConfig):\n        try:\n            disk = w.Win32_PerfFormattedData_PerfDisk_LogicalDisk()\n        except AttributeError:\n            self.logger.info('Missing Win32_PerfFormattedData_PerfDisk_LogicalDiskUnable WMI class.' \\\n                             ' No I/O metrics will be returned.')\n            return\n        blacklist_re = agentConfig.get('device_blacklist_re', None)\n        for device in disk:\n            name = self.normalize_device_name(device.name)\n            if should_ignore_disk(name, blacklist_re):\n                continue\n            if device.DiskWriteBytesPerSec is not None:\n                self.save_sample('system.io.wkb_s', int(device.DiskWriteBytesPerSec) / B2KB,\n                    device_name=name)\n            if device.DiskWritesPerSec is not None:\n                self.save_sample('system.io.w_s', int(device.DiskWritesPerSec),\n                    device_name=name)\n            if device.DiskReadBytesPerSec is not None:\n                self.save_sample('system.io.rkb_s', int(device.DiskReadBytesPerSec) / B2KB,\n                    device_name=name)\n            if device.DiskReadsPerSec is not None:\n                self.save_sample('system.io.r_s', int(device.DiskReadsPerSec),\n                    device_name=name)\n            if device.CurrentDiskQueueLength is not None:\n                self.save_sample('system.io.avg_q_sz', device.CurrentDiskQueueLength,\n                    device_name=name)\n        return self.get_metrics()\n", "path": "checks/system/win32.py"}]}