example script
This commit is contained in:
@@ -1,3 +1,9 @@
|
|||||||
# ryzen7-script
|
# ryzen7-script
|
||||||
|
|
||||||
Customization test script for the RYZEN-7 gateway on castro — reads host signals through the sandbox sidecar and runs in an image-builder image.
|
Customization test script for the RYZEN-7 gateway on castro — reads host signals through the sandbox sidecar and runs in an image-builder image.
|
||||||
|
|
||||||
|
Reads `ryzen7-cpu`, `ryzen7-ram` and `ryzen7-disk` (43, 44, 45) once a second, and writes their maximum to `ryzen7-load-index` (54) as an indicator of how loaded the host is. All three inputs are percentages on the same scale, so the maximum is meaningful without normalisation.
|
||||||
|
|
||||||
|
`ryzen7-load-index` is an i7state `state_signal` with a timeout, so it reads NaN when the job stops writing. A stale value is therefore impossible: if the number is there, the job is alive. When an input is missing or not yet reporting, the script skips the write rather than writing a value derived from partial data, which lets the signal time out honestly.
|
||||||
|
|
||||||
|
Runs in `docker.izazpi.cloud/infra/infra-python:0.1.12`, which carries the `i7lib` wheel. There is no library folder on the host — `i7lib` reaches the script only through the image, so a new `i7lib` version means a new image tag here.
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
from i7lib.client import run
|
||||||
|
from i7lib.timeutils import now, to_datetime
|
||||||
|
|
||||||
|
INPUTS = ["ryzen7-cpu", "ryzen7-ram", "ryzen7-disk"]
|
||||||
|
ID_LOAD_INDEX = 54
|
||||||
|
NAME_LOAD_INDEX = "ryzen7-load-index"
|
||||||
|
LOG_EVERY = 60
|
||||||
|
|
||||||
|
tick = 0
|
||||||
|
|
||||||
|
|
||||||
|
def log(msg):
|
||||||
|
stamp = to_datetime(now()).strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
print(f"[{stamp}] {msg}")
|
||||||
|
|
||||||
|
|
||||||
|
def read_inputs(plant):
|
||||||
|
values = []
|
||||||
|
for name in INPUTS:
|
||||||
|
value = plant.state.get(name)
|
||||||
|
if value is None or math.isnan(value):
|
||||||
|
return None
|
||||||
|
values.append(value)
|
||||||
|
return values
|
||||||
|
|
||||||
|
|
||||||
|
def report_missing(plant):
|
||||||
|
missing = [name for name in INPUTS if name not in plant.state]
|
||||||
|
if missing:
|
||||||
|
log(f"WARNING: not exposed to this job: {', '.join(missing)}")
|
||||||
|
|
||||||
|
|
||||||
|
def write_load_index(plant, load_index):
|
||||||
|
batch = plant.new_write_batch()
|
||||||
|
batch.add(ID_LOAD_INDEX, load_index, NAME_LOAD_INDEX)
|
||||||
|
plant.send(batch)
|
||||||
|
|
||||||
|
|
||||||
|
def logic(plant):
|
||||||
|
global tick
|
||||||
|
|
||||||
|
if tick == 0:
|
||||||
|
report_missing(plant)
|
||||||
|
tick += 1
|
||||||
|
|
||||||
|
values = read_inputs(plant)
|
||||||
|
if values is None:
|
||||||
|
log("skipping write: an input is missing or not yet reporting")
|
||||||
|
return
|
||||||
|
|
||||||
|
load_index = max(values)
|
||||||
|
write_load_index(plant, load_index)
|
||||||
|
|
||||||
|
if tick % LOG_EVERY == 1:
|
||||||
|
readings = " ".join(f"{n}={v:.2f}" for n, v in zip(INPUTS, values))
|
||||||
|
log(f"{readings} -> {NAME_LOAD_INDEX}={load_index:.2f}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run(logic, interval_ms=1000, offset_ms=200)
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
name: ryzen7-script
|
||||||
|
active: true
|
||||||
|
schedule: "* * * * *"
|
||||||
|
action: ensure
|
||||||
|
runOnStartup: true
|
||||||
|
imageName: docker.izazpi.cloud/infra/infra-python:0.1.12
|
||||||
|
command: ["python", "/app/ryzen7_script.py"]
|
||||||
|
maxCpuCores: 0.5
|
||||||
|
maxMemoryMb: 128
|
||||||
|
autoremove: false
|
||||||
|
logOption: 1
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
- { id: 43, name: 'ryzen7-cpu', read: 1, write: 0 }
|
||||||
|
- { id: 44, name: 'ryzen7-ram', read: 1, write: 0 }
|
||||||
|
- { id: 45, name: 'ryzen7-disk', read: 1, write: 0 }
|
||||||
|
- { id: 46, name: 'ryzen7-net-tx', read: 1, write: 0 }
|
||||||
|
- { id: 47, name: 'ryzen7-net-rx', read: 1, write: 0 }
|
||||||
|
- { id: 48, name: 'ryzen7-temp', read: 1, write: 0 }
|
||||||
|
- { id: 54, name: 'ryzen7-load-index', read: 0, write: 1 }
|
||||||
Reference in New Issue
Block a user