Compare commits

..

No commits in common. "8633bd072440270e1cdc953e0c63b4ffa5218640" and "904c77fc299a9ea61089525e2c814a8d24d56f01" have entirely different histories.

3 changed files with 30 additions and 85 deletions

@ -1,8 +1,5 @@
import sys
import datetime
import time
import os
import signal
import io
import json
import subprocess
@ -20,48 +17,7 @@ from typing import (
logger = logging.getLogger(__name__)
def get_info(
sh: list[str],
timeout: int | float,
):
t1: list[str] = []
for sh_index, o in enumerate(
[
*sh,
*[
r"""
A=$(free -h | grep -P Mem: | grep -Po '[\w\.\d]+');
echo -n $A | awk '{print $2, $7}';
""",
r"""
date +'%Y-%m-%d %l:%M:%S %p';
""",
],
]
):
try:
t1.append(
subprocess.check_output(
o,
shell=True,
timeout=timeout,
)
.decode('utf-8')
.strip()
)
except Exception:
t1.append('fail %d' % sh_index)
t3 = ' | '.join(t1).replace('\n\r', '')
sys.stdout.write(t3)
sys.stdout.flush()
def run(argv: list[str]):
logging.basicConfig(level=logging.INFO)
assert isinstance(argv, list) and all([isinstance(o, str) for o in argv])
class c1(optparse.IndentedHelpFormatter):
@ -125,13 +81,6 @@ def run(argv: list[str]):
default=None,
type=float,
)
add_option(
parser,
'--repeat_interval',
dest='repeat_interval',
default=None,
type=float,
)
add_option(
parser,
'--config',
@ -173,37 +122,39 @@ printf '% 3.0f%%' $(upower -d | grep -Po 'percentage:\\s+\\d+(\\.\\d+)?%' | grep
options.sh.extend(config.get('sh', []))
last_ts = datetime.datetime.now()
t1: list[str] = []
shutdown: bool = False
for sh_index, o in enumerate(
[
*options.sh,
*[
r"""
A=$(free -h | grep -P Mem: | grep -Po '[\w\.\d]+');
echo -n $A | awk '{print $2, $7}';
""",
r"""
date +'%Y-%m-%d %l:%M:%S %p';
""",
],
]
):
try:
t1.append(
subprocess.check_output(
o,
shell=True,
timeout=timeout2,
)
.decode('utf-8')
.strip()
)
except Exception:
t1.append('fail %d' % sh_index)
def on_signal(*args: Any, **kwargs: Any):
nonlocal shutdown
t3 = ' | '.join(t1).replace('\n\r', '')
shutdown = True
signal.signal(signal.SIGINT, on_signal)
signal.signal(signal.SIGTERM, on_signal)
while not shutdown:
get_info(
timeout=timeout2,
sh=options.sh,
)
if not options.repeat_interval:
break
else:
sys.stdout.write('\n')
sys.stdout.flush()
now_ts = datetime.datetime.now()
spent = (now_ts - last_ts).total_seconds()
last_ts = last_ts + datetime.timedelta(seconds=options.repeat_interval)
if spent < options.repeat_interval:
time.sleep(options.repeat_interval - spent)
sys.stdout.write(t3)
sys.stdout.flush()
if __name__ == '__main__':

Binary file not shown.