| 1 |
#!/usr/bin/env python3 |
| 2 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 3 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 4 |
# |
| 5 |
# This program is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
"""Report system utilisation information. |
| 18 |
|
| 19 |
(This command is for internal use.) |
| 20 |
|
| 21 |
This is for use in situations where Cylc needs to extract information from |
| 22 |
the `psutil` on remote platforms. |
| 23 |
|
| 24 |
Exits: |
| 25 |
0 - If successful. |
| 26 |
2 - For errors in extracting results from psutil |
| 27 |
1 - For all other errors. |
| 28 |
""" |
| 29 |
|
| 30 |
import json |
| 31 |
import sys |
| 32 |
|
| 33 |
import psutil |
| 34 |
|
| 35 |
# from cylc.flow.host_select import _simple_eval |
| 36 |
from cylc.flow.option_parsers import OptionParser |
| 37 |
from cylc.flow.terminal import ( |
| 38 |
cli_function, |
| 39 |
parse_dirty_json |
| 40 |
) |
| 41 |
|
| 42 |
INTERNAL = True |
| 43 |
|
| 44 |
|
| 45 |
def get_option_parser(): |
| 46 |
return OptionParser(__doc__) |
| 47 |
|
| 48 |
|
| 49 |
def _call_psutil_interface(interface, *args): |
| 50 |
"""Call a psutil interface with the provided arguments. |
| 51 |
|
| 52 |
Args: |
| 53 |
interface: |
| 54 |
The psutil method we want to call, e.g. "cpu_percent". |
| 55 |
|
| 56 |
In the case of objects, this may include attributes, e.g. |
| 57 |
"Process.cmdline". |
| 58 |
args: |
| 59 |
The arguments to provide to the psutil method. |
| 60 |
|
| 61 |
Returns: |
| 62 |
The result of the psutil method call. |
| 63 |
|
| 64 |
""" |
| 65 |
result = psutil |
| 66 |
is_first = True |
| 67 |
for fcn in interface.split('.'): |
| 68 |
try: |
| 69 |
method = getattr(result, fcn) |
| 70 |
except AttributeError as exc: |
| 71 |
# error obtaining interfaces from psutil e.g: |
| 72 |
# * requesting a method which does not exist |
| 73 |
print(exc, file=sys.stderr) |
| 74 |
sys.exit(2) |
| 75 |
|
| 76 |
if is_first: |
| 77 |
is_first = False |
| 78 |
else: |
| 79 |
args = () |
| 80 |
|
| 81 |
result = method(*args) |
| 82 |
return result |
| 83 |
|
| 84 |
|
| 85 |
def _psutil(metrics_json): |
| 86 |
metrics = parse_dirty_json(metrics_json) |
| 87 |
|
| 88 |
try: |
| 89 |
ret = [_call_psutil_interface(*key) for key in metrics] |
| 90 |
except Exception as exc: |
| 91 |
# error extracting metrics from psutil e.g: |
| 92 |
# * requesting information on a resource which does not exist |
| 93 |
print(exc, file=sys.stderr) |
| 94 |
sys.exit(2) |
| 95 |
|
| 96 |
# serialise |
| 97 |
for ind, item in enumerate(ret): |
| 98 |
if hasattr(item, '_asdict'): |
| 99 |
ret[ind] = item._asdict() |
| 100 |
elif hasattr(item, 'as_dict'): |
| 101 |
ret[ind] = item.as_dict() |
| 102 |
|
| 103 |
return ret |
| 104 |
|
| 105 |
|
| 106 |
@cli_function(get_option_parser) |
| 107 |
def main(*_): |
| 108 |
print( |
| 109 |
json.dumps( |
| 110 |
_psutil(sys.stdin.read()) |
| 111 |
) |
| 112 |
) |