Files
Arduino/uno-stats-monitor/monitor_sender.sh
2024-01-16 19:31:14 -05:00

103 lines
2.6 KiB
Bash

#!/bin/bash
# UNO Monitor Sender v.0.1.0
# Kidacro <kidacro@archamedis.net>
# https://kidacro.archamedis.net/git/kidacro/Arduino/src/branch/main/uno-stats-monitor
#
# monitor_sender.sh - Bash stats sender suitable for cron
#
# Input Schema:
# Start char is: '#'
# End char is: '@'
# 2nd char denotes which line number/mode to use
#
# Setting Schema:
# LCDLength:LCDRows:LCDDelay:LEDBrightness:LEDDelay
# 16:2:5:0:3#
#
# Example:
# Line 1: #1Archamedis Status@
# Line 2: #299% free / etc.@
# LAVG -: #324@
# LSET 0: #016:2:5:0:3@
#
# Output Schema:
# Multimedia Controls use single characters to denote which button was pressed
# P power
# M mute
# m mode
# p pause/play
# b back
# f forward
# e eq
# - vol down
# + vol up
# r return
# u usb scan
# 0 number 0
# 1 number 1
# 2 number 2
# 3 number 3
# 4 number 4
# 5 number 5
# 6 number 6
# 7 number 7
# 8 number 8
# 9 number 9
# R repeat
# Z other button pressed
# TTY Device to use
TTY="/dev/ttyACM0"
# Arduino Settings
LCDLength=16
LCDRows=2
LCDDelay=5
LEDBrightness=0
LEDDelay=3
##########################################################
round() {
printf "%.${2}f" "${1}"
}
# Stats
# we are limited to 2 lines and 16 chars per line so, light stats & no labels
#CLOAD=$(mpstat | awk '$3 ~ /CPU/ { for(i=1;i<=NF;i++) { if ($i ~ /%idle/) field=i } } $3 ~ /all/ { printf("%d%%",100 - $field) }')
#CLOAD=$(cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}')
CLOAD=$(awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) "%"; }' <(grep 'cpu ' /proc/stat) <(sleep 0.5;grep 'cpu ' /proc/stat))
CLOAD=${CLOAD:0:-1}
CLOAD=$(round ${CLOAD} 0)
CTEMP=$(sensors | grep TSI0_TEMP: | tr '°+' ' ' | awk '{print $2}' | paste -sd ', ')C
MFREE=$(free -h | grep Mem | awk '{print $4}' | paste -sd ', ')
RFREE=$(df -h | grep /dev/nvme0n1p3 | awk '{print $4}' | paste -sd ', ')
LFREE=$(df -h | grep /dev/sdc1 | awk '{print $4}' | paste -sd ', ')
SFREE=$(df -h | grep /dev/mapper/store0-lvol1 | awk '{print $4}' | paste -sd ', ')
# setup device
stty -F $TTY ispeed 9600 ospeed 9600 -ignpar cs8 -cstopb -echo -hupcl
# send settings
LINE0="#0$LCDLength:$LCDRows:$LCDDelay:$LEDBrightness:$LEDDelay@"
/usr/bin/echo "$LINE0" > $TTY
echo Sending to $TTY: $LINE0
# line 1
LINE1="#1$CLOAD% $CTEMP $MFREE@"
/usr/bin/echo "$LINE1" > $TTY
echo Sending to $TTY: $LINE1
# line 2
LINE2="#2$RFREE $LFREE $SFREE@"
/usr/bin/echo "$LINE2" > $TTY
echo Sending to $TTY: $LINE2
# load average
LINE3="#-$CLOAD@"
/usr/bin/echo "$LINE3" > $TTY
echo Sending to $TTY: $LINE3