Skip to content

Commit

Permalink
Merge pull request #368 from alphagov/shw-add-disk-space-status-check
Browse files Browse the repository at this point in the history
Add disk space check for healthcheck
  • Loading branch information
samuelhwilliams authored Mar 14, 2018
2 parents 694d20e + 37869e2 commit 6cba77e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dmutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
import flask_featureflags # noqa


__version__ = '34.5.0'
__version__ = '34.6.0'
16 changes: 15 additions & 1 deletion dmutils/status.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import datetime
import math
import os
from .formats import DATE_FORMAT
from flask_featureflags import FEATURE_FLAGS_CONFIG

Expand All @@ -26,6 +27,19 @@ def get_flags(current_app):
return flags


def get_disk_space_status(low_disk_percent_threshold=5):
"""Accepts a single parameter that indicates the minimum percentage of disk space which should be free for the
instance to be considered healthy.
Returns a tuple containing two items: a status (OK or LOW) indicating whether the disk space remaining on the
instance is below the threshold and the integer percentage remaining disk space."""
disk_stats = os.statvfs('/')

disk_free_percent = 100 - int(math.ceil(((disk_stats.f_bfree * 1.0) / disk_stats.f_blocks) * 100))

return 'OK' if disk_free_percent >= low_disk_percent_threshold else 'LOW', disk_free_percent


def enabled_since(date_string):
if date_string:
# Check format like YYYY-MM-DD
Expand Down
29 changes: 29 additions & 0 deletions tests/test_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from collections import namedtuple

import mock
import pytest

from dmutils.status import get_disk_space_status


def statvfs_fixture(total_blocks, free_blocks):
statvfs = namedtuple('statvfs', 'f_blocks f_bfree')
return statvfs(total_blocks, free_blocks)


@mock.patch('dmutils.status.os.statvfs')
@pytest.mark.parametrize('kwargs, total_blocks, free_blocks, expected_status',
(
({}, 1024, 0, ('OK', 100)),
({}, 1024, 512, ('OK', 50)),
({}, 1024, 920, ('OK', 10)),
({}, 1024, 1000, ('LOW', 2)),
({}, 1024, 1024, ('LOW', 0)),
({'low_disk_percent_threshold': 10}, 1024, 950, ('LOW', 7)),
({'low_disk_percent_threshold': 20}, 1024, 920, ('LOW', 10)),
({'low_disk_percent_threshold': 75}, 1024, 512, ('LOW', 50)),
))
def test_disk_space_status(disk_usage, kwargs, total_blocks, free_blocks, expected_status):
disk_usage.return_value = statvfs_fixture(total_blocks, free_blocks)

assert get_disk_space_status(**kwargs) == expected_status

0 comments on commit 6cba77e

Please sign in to comment.