-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ff61d1
commit c4f2cd5
Showing
6 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ pytz = "*" | |
whois = "*" | ||
ovh = "*" | ||
python-dateutil = "*" | ||
psutil = "*" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |