selfhostedops-stack/filesync/server/sampler.sh

25 lines
1.1 KiB
Bash

#!/usr/bin/env bash
# sampler.sh start NAME | stop NAME : Host-CPU% und RAM im 1-s-Takt während eines Laufs, Zusammenfassung bei stop
D=/root/ep19fs/load; mkdir -p $D
case $1 in
start) nohup python3 - "$D/$2.tsv" "$D/$2.stop" >/dev/null 2>&1 <<'PY' &
import sys,time,os
out=open(sys.argv[1],'w'); stop=sys.argv[2]
def cpu():
v=list(map(int,open('/proc/stat').readline().split()[1:])); return sum(v), v[3]+v[4]
def mem():
d={l.split(':')[0]:int(l.split()[1]) for l in open('/proc/meminfo')}; return (d['MemTotal']-d['MemAvailable'])/1024
a=cpu()
while not os.path.exists(stop):
time.sleep(1); b=cpu(); t=b[0]-a[0]; i=b[1]-a[1]; a=b
out.write(f"{time.strftime('%H:%M:%S',time.gmtime())}\t{100*(t-i)/max(t,1):.1f}\t{mem():.0f}\n"); out.flush()
PY
;;
stop) touch $D/$2.stop; sleep 1.5; python3 - "$D/$2.tsv" <<'PY'
import sys
r=[l.split('\t') for l in open(sys.argv[1]) if l.strip()]
c=[float(x[1]) for x in r]; m=[float(x[2]) for x in r]
print(f"server load: {len(r)} samples, CPU mean {sum(c)/len(c):.1f} %, CPU max {max(c):.1f} % (2 vCPU = 100 %), RAM used mean {sum(m)/len(m):.0f} MiB, max {max(m):.0f} MiB")
PY
;;
esac