Skip to content

Commit

Permalink
Add disk and memory usage checks
Browse files Browse the repository at this point in the history
  • Loading branch information
pikhovkin committed Sep 22, 2021
1 parent 8680d23 commit 9762527
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .
pip install -e .[psutil]
pip install "${{ matrix.django }}"
- name: Test
run: |
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='django-simple-health-check',
version='0.2.0',
version='0.3.0',
description='Simple Django health check',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
Expand All @@ -21,6 +21,9 @@
install_requires=[
'Django>=3.1,<4.0',
],
extras_require={
'psutil': ['psutil'],
},
python_requires='>=3.7.*, <4.0.*',
license='MIT',
zip_safe=False,
Expand Down
45 changes: 45 additions & 0 deletions simple_health_check/checks/ps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import locale
import socket

import psutil

from . import BaseHealthCheck
from ..exceptions import HealthCheckError


host = socket.gethostname()


class DiskUsage(BaseHealthCheck):
def __init__(self, max_usage_percent: int = 0):
super().__init__()

self.max_usage_percent = max_usage_percent

def check(self):
if not self.max_usage_percent:
return

du = psutil.disk_usage('/')
if du.percent >= self.max_usage_percent:
raise HealthCheckError(f'{host} {du.percent}% disk usage exceeds {self.max_usage_percent}%')


class MemoryUsage(BaseHealthCheck):
MB_1 = 1024 * 1024

def __init__(self, min_memory_mb: int = 0):
super().__init__()

self.min_memory_mb = min_memory_mb
self._min_memory_mb = min_memory_mb * self.MB_1

def check(self):
if not self.min_memory_mb:
return

memory = psutil.virtual_memory()
if memory.available < self._min_memory_mb:
locale.setlocale(locale.LC_ALL, '')
available = round(memory.available / self.MB_1, 2)
raise HealthCheckError(f'{host} {available:n} MB available RAM below {self.min_memory_mb:d} MB')
72 changes: 72 additions & 0 deletions simple_health_check/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,75 @@ def test_cache_no_rediness(self):
response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 500)
self.assertTrue(response.content == b'down')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.DiskUsage': None,
},
)
def test_ps_disk_usage_no_value(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.DiskUsage': dict(max_usage_percent=99),
},
)
def test_ps_disk_usage(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.DiskUsage': dict(max_usage_percent=0.001),
},
)
def test_ps_disk_usage_no_rediness(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 500)
self.assertTrue(response.content == b'down')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.MemoryUsage': None,
},
)
def test_ps_memory_usage_no_value(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.MemoryUsage': dict(min_memory_mb=10),
},
)
def test_ps_memory_usage(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.MemoryUsage': dict(min_memory_mb=100_000),
},
)
def test_ps_memory_usage_no_rediness(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 500)
self.assertTrue(response.content == b'down')
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ skip_missing_interpreters = True
envlist = py{37,38,39,310}-django{31,32}

[testenv]
commands = python manage.py test
extras =
psutil
deps =
django31: Django>=3.1,<3.2
django32: Django>=3.2,<3.3
Expand All @@ -14,3 +15,4 @@ basepython =
py38: {env:TOX_PYTHON_38}
py39: {env:TOX_PYTHON_39}
py310: {env:TOX_PYTHON_310}
commands = python manage.py test

0 comments on commit 9762527

Please sign in to comment.