Skip to content

Commit

Permalink
Checks: Add swap-usage-max-percent
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Oct 28, 2019
1 parent 7ff61d1 commit c4f2cd5
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 5 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pytz = "*"
whois = "*"
ovh = "*"
python-dateutil = "*"
psutil = "*"
18 changes: 17 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project = 'Infra Check'
project = 'InfraCheck'
copyright = '2019, RiotKit Team'
author = 'Wesolowski'

Expand Down Expand Up @@ -48,7 +48,7 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'InfraCheck.tex', 'Infra Check Documentation',
(master_doc, 'InfraCheck.tex', 'InfraCheck Documentation',
'Wolnosciowiec Team', 'manual'),
]

Expand All @@ -58,7 +58,7 @@
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'infracheck', 'Infra Check Documentation',
(master_doc, 'infracheck', 'InfraCheck Documentation',
[author], 1)
]

Expand All @@ -69,7 +69,7 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'InfraCheck', 'Infra Check Documentation',
(master_doc, 'InfraCheck', 'InfraCheck Documentation',
author, 'InfraCheck', 'One line description of project.',
'Miscellaneous'),
]
Expand Down
4 changes: 4 additions & 0 deletions docs/source/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,7 @@ Parameters:
.. include:: ../../infracheck/checks/load-average
:start-after: <sphinx>
:end-before: </sphinx>

.. include:: ../../infracheck/checks/swap-usage-max-percent
:start-after: <sphinx>
:end-before: </sphinx>
24 changes: 24 additions & 0 deletions infracheck/checks-tests/swap-usage-max-percent-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

echo " >> Assert that current usage of 50% is higher than maximum of 40%"
MOCK_SWAP_USAGE=50 MAX_ALLOWED_PERCENTAGE=40 ./infracheck/checks/swap-usage-max-percent
if [[ $? != 1 ]]; then
echo " >> Test failed."
exit 1
fi

echo " >> Assert that swap is not used"
MOCK_SWAP_USAGE=0 MAX_ALLOWED_PERCENTAGE=0 ./infracheck/checks/swap-usage-max-percent
if [[ $? != 0 ]]; then
echo " >> Test failed."
exit 1
fi

echo " >> Assert 10% is ok, when maximum is defined at 15%"
MOCK_SWAP_USAGE=10 MAX_ALLOWED_PERCENTAGE=15 ./infracheck/checks/swap-usage-max-percent
if [[ $? != 0 ]]; then
echo " >> Test failed."
exit 1
fi

exit 0
46 changes: 46 additions & 0 deletions infracheck/checks/swap-usage-max-percent
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3

"""
<sphinx>
swap-usage-max-percent
----------------------
Defines maximum percentage of allowed swap usage
Parameters:
- max_allowed_percentage (default: 0.0)
</sphinx>
"""


import psutil
import os
import sys


class SwapCheck:
def main(self, max_allowed_percentage: float):
used = self._get_used_swap_percentage()

if used > max_allowed_percentage:
return False, "Current swap usage of %f%% exceeds allowed usage of %f%%" % (used, max_allowed_percentage)

return True, "Current swap usage of %f%% is ok" % used

@staticmethod
def _get_used_swap_percentage() -> float:
if os.getenv('MOCK_SWAP_USAGE'):
return float(os.getenv('MOCK_SWAP_USAGE'))

return psutil.swap_memory().percent


if __name__ == '__main__':
app = SwapCheck()
status, message = app.main(max_allowed_percentage=float(os.getenv('MAX_ALLOWED_PERCENTAGE', 0.0)))

print(message)
sys.exit(0 if status else 1)

0 comments on commit c4f2cd5

Please sign in to comment.