Skip to content

Commit

Permalink
Merge pull request #5 from riotkit-org/coding-standards-fix
Browse files Browse the repository at this point in the history
Coding standards fix
  • Loading branch information
blackandred committed May 18, 2019
2 parents 2a2c7cf + 1994c90 commit 3d9023e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/bash

echo " >> Assert that 2GB free (3/5GB used) is ok when max usage is 70%"
MOCK_FREE_RAM=3000 MOCK_TOTAL_RAM=5000 MAX_RAM_PERCENTAGE=70 ../checks/free_ram.py
MOCK_FREE_RAM=3000 MOCK_TOTAL_RAM=5000 MAX_RAM_PERCENTAGE=70 ../checks/free-ram
[[ $? == 1 ]] && echo " .. Test failed." && exit 1
[[ $? == 127 ]] && echo " .. Test lacking a parameter." && exit 127

echo " >> Assert that 2GB free (8/10GB used) is too much when max usage is 60%"
MOCK_FREE_RAM=2000 MOCK_TOTAL_RAM=10000 MAX_RAM_PERCENTAGE=60 ../checks/free_ram.py
MOCK_FREE_RAM=2000 MOCK_TOTAL_RAM=10000 MAX_RAM_PERCENTAGE=60 ../checks/free-ram
[[ $? == 0 ]] && echo " .. Test failed." && exit 0
[[ $? == 127 ]] && echo " .. Test lacking a parameter." && exit 127

echo " >> Assert that 5/10GB used is ok when max usage is 60%"
MOCK_FREE_RAM=5000 MOCK_TOTAL_RAM=10000 MAX_RAM_PERCENTAGE=60 ../checks/free_ram.py
MOCK_FREE_RAM=5000 MOCK_TOTAL_RAM=10000 MAX_RAM_PERCENTAGE=60 ../checks/free-ram
[[ $? == 1 ]] && echo " .. Test failed." && exit 1
[[ $? == 127 ]] && echo " .. Test lacking a parameter." && exit 127

Expand Down
54 changes: 54 additions & 0 deletions app/checks/free-ram
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3

import re
from os import environ
from sys import stderr

if 'MAX_RAM_PERCENTAGE' not in environ:
print("Missing MAX_RAM_PERCENTAGE parameter.\nUsage ./free_ram.py.", file=stderr)
exit(127)


def check_ram(max_percent: float, meminfo_path: str = '/proc/meminfo') -> int:
meminfo = open(meminfo_path).read()
mem_total_reg = re.compile("MemTotal:\s+(\d+)")
mem_avail_reg = re.compile("MemAvailable:\s+(\d+)")
mem_total = float( mem_total_reg.split(meminfo)[1])
mem_avail = float( mem_avail_reg.split(meminfo)[1])

if "MOCK_FREE_RAM" in environ and "MOCK_TOTAL_RAM" in environ:
mem_avail = float(environ["MOCK_FREE_RAM"]) * 1024
mem_total = float(environ["MOCK_TOTAL_RAM"]) * 1024

mem_used = mem_total - mem_avail
mem_used_percent = mem_used/mem_total * 100

if mem_used/mem_total * 100 > max_percent:
print(
("RAM usage too high: {0:.0f}MiB/{1:.0f}MiB ({2:.0f}% used, {3:.0f}MiB left. Max usage allowed: {4:.0f}% ("
+ "{5:.0f}MiB) ")
.format(
mem_used / 1024,
mem_total / 1024,
mem_used_percent,
mem_avail / 1024,
max_percent,
max_percent / 100.0 * mem_total / 1024
)
)
return 1

print(
"RAM usage OK: {0:.0f}MiB/{1:.0f}MiB ({2:.0f}% used, {3:.0f}MiB left. Max usage allowed: {4:.0f}% ({5:.0f}MiB)"
.format(
mem_used / 1024, mem_total / 1024,
mem_used_percent,
mem_avail / 1024,
max_percent,
max_percent / 100.0 * mem_total / 1024
)
)
return 0


exit(check_ram(float(environ["MAX_RAM_PERCENTAGE"])))
29 changes: 0 additions & 29 deletions app/checks/free_ram.py

This file was deleted.

8 changes: 8 additions & 0 deletions docs/source/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ Parameters:
- min_req_space (in gigabytes)
- dir (path)

free-ram
--------

Monitors RAM memory usage to notify that a maximum percent of memory was used.

Parameters:
- max_ram_percentage (in percents eg. 80)

replication-running
-------------------

Expand Down
6 changes: 6 additions & 0 deletions example/healthchecks/configured/ram.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "free-ram",
"input": {
"max_ram_percentage": "85"
}
}
4 changes: 3 additions & 1 deletion tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def test_returns_all_checks(self):
repository = Repository([path + '/../example/healthchecks', path])

self.assertEqual(
sorted(['is_dd_accessible', 'docker-health', 'some_port_is_open', 'disk-space', 'hello-test-custom-check-example']),
sorted([
'is_dd_accessible', 'docker-health', 'some_port_is_open',
'disk-space', 'hello-test-custom-check-example', 'ram']),
sorted(repository.get_configured_checks(with_disabled=False))
)

Expand Down

0 comments on commit 3d9023e

Please sign in to comment.