Skip to content

Commit

Permalink
[swap stats] change gauge to rate
Browse files Browse the repository at this point in the history
  • Loading branch information
talwai committed Apr 16, 2015
1 parent 3f595ba commit 2963984
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
12 changes: 3 additions & 9 deletions checks.d/system_swap.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
from checks import AgentCheck
try:
import psutil
except ImportError:
psutil = None
import psutil

B2MB = float(1048576)

class SystemSwap(AgentCheck):

def check(self, instance):
if not psutil:
return {}

swap_mem = psutil.swap_memory()
self.gauge('system.swap.swapped_in', swap_mem.sin / B2MB)
self.gauge('system.swap.swapped_out', swap_mem.sout / B2MB)
self.rate('system.swap.swapped_in', swap_mem.sin)
self.rate('system.swap.swapped_out', swap_mem.sout)
20 changes: 11 additions & 9 deletions tests/test_system_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ def __init__(self, sin, sout):
self.sin = sin
self.sout = sout

ORIG_SWAP_IN = 115332743168
ORIG_SWAP_OUT = 22920884224

SWAP_IN_INCR = 2
SWAP_OUT_INCR = 4

MOCK_PSUTIL_SWAP_STATS = [
_PSUtilSwapStatsMock(115332743168, 22920884224),
_PSUtilSwapStatsMock(115332743170, 22920884230),
_PSUtilSwapStatsMock(ORIG_SWAP_IN, ORIG_SWAP_OUT),
_PSUtilSwapStatsMock(ORIG_SWAP_IN + SWAP_IN_INCR, ORIG_SWAP_OUT + SWAP_OUT_INCR),
]

class SystemSwapTestCase(AgentCheckTest):
Expand All @@ -21,10 +27,6 @@ class SystemSwapTestCase(AgentCheckTest):

@mock.patch('psutil.swap_memory', side_effect=MOCK_PSUTIL_SWAP_STATS)
def test_system_swap(self, mock_swap_stats):
self.run_check({"instances": [{}]})
self.assertMetric('system.swap.swapped_in', 115332743168 / B2MB)
self.assertMetric('system.swap.swapped_out', 22920884224 / B2MB)

self.run_check({"instances": [{}]})
self.assertMetric('system.swap.swapped_in', 115332743170 / B2MB)
self.assertMetric('system.swap.swapped_out', 22920884230 / B2MB)
self.run_check_twice({"instances": [{}]})
self.assertMetric('system.swap.swapped_in', value=SWAP_IN_INCR, count=1)
self.assertMetric('system.swap.swapped_out', value=SWAP_OUT_INCR, count=1)

0 comments on commit 2963984

Please sign in to comment.