Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swap stats] add swap in/out check to checks.d #1549

Merged
merged 2 commits into from
Apr 16, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions checks.d/system_swap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from checks import AgentCheck
try:
import psutil
except ImportError:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know i told you something different yesterday but after all i think we shouldn't manage the case where psutil is not present.

The only case where it's not present is when someone installs the agent from source on a system that doesn't have a compiler. And if that's the case, the check initialization will fail and report the missing import which is an acceptable behavior.

psutil = None

B2MB = float(1048576)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like it's not needed anymore :)


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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use a rate for these.
According to the psutil documentation, sin is

the number of bytes the system has swapped in from disk (cumulative)

By submitting the metric as a rate, you will compute the # of bytes per second swapped in.
Otherwise the graphs will just display a monotonically increasing value.

Also you should submit this value in bytes as per our discussion earlier.

self.gauge('system.swap.swapped_out', swap_mem.sout / B2MB)
30 changes: 30 additions & 0 deletions tests/test_system_swap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import mock
import psutil

from tests.common import AgentCheckTest

B2MB = float(1048576)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed


class _PSUtilSwapStatsMock(object):
def __init__(self, sin, sout):
self.sin = sin
self.sout = sout

MOCK_PSUTIL_SWAP_STATS = [
_PSUtilSwapStatsMock(115332743168, 22920884224),
_PSUtilSwapStatsMock(115332743170, 22920884230),
]

class SystemSwapTestCase(AgentCheckTest):

CHECK_NAME = 'system_swap'

@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)