-
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.
Load avg checks: Corrected invalid casting in timeout parameter and e…
…xtracted code into the checklib
- Loading branch information
1 parent
d55c8f7
commit ead7ce5
Showing
4 changed files
with
91 additions
and
75 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
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,66 @@ | ||
|
||
import abc | ||
import os | ||
import re | ||
|
||
|
||
class BaseLoadAverageCheck(abc.ABC): | ||
@staticmethod | ||
def get_complete_avg() -> str: | ||
avg = os.getloadavg() | ||
return '{:.2f}, {:.2f}, {:.2f}'.format(avg[0], avg[1], avg[2]) | ||
|
||
@staticmethod | ||
def get_load_average(timing: str) -> float: | ||
if os.getenv('MOCK_LOAD_AVERAGE'): | ||
return float(os.getenv('MOCK_LOAD_AVERAGE')) | ||
|
||
if timing not in ['1', '5', '15']: | ||
raise Exception('Invalid argument, expected type to be: 1, 5 or 15') | ||
|
||
load = {'1': os.getloadavg()[0], '5': os.getloadavg()[1], '15': os.getloadavg()[2]} | ||
return load[timing] | ||
|
||
@staticmethod | ||
def available_cpu_count() -> int: | ||
""" Number of available virtual or physical CPUs on this system, i.e. | ||
user/real as output by time(1) when called with an optimally scaling | ||
userspace-only program | ||
:url: https://stackoverflow.com/a/1006301/6782994 | ||
""" | ||
|
||
if os.getenv('MOCK_CPU_COUNT'): | ||
return int(os.getenv('MOCK_CPU_COUNT')) | ||
|
||
# cpuset | ||
# cpuset may restrict the number of *available* processors | ||
try: | ||
with open('/proc/self/status') as f: | ||
m = re.search(r'(?m)^Cpus_allowed:\s*(.*)$', f.read()) | ||
|
||
if m: | ||
res = bin(int(m.group(1).replace(',', ''), 16)).count('1') | ||
if res > 0: | ||
return int(res) | ||
except IOError: | ||
pass | ||
|
||
# Python 2.6+ | ||
try: | ||
import multiprocessing | ||
return multiprocessing.cpu_count() | ||
except (ImportError, NotImplementedError): | ||
pass | ||
|
||
# Linux | ||
try: | ||
with open('/proc/cpuinfo') as f: | ||
res = f.read().count('processor\t:') | ||
|
||
if res > 0: | ||
return int(res) | ||
except IOError: | ||
pass | ||
|
||
raise Exception('Can not determine number of CPUs on this system') |