Browse Source

add old tools already published elsewhere

boyska 2 years ago
parent
commit
3b143d3805
3 changed files with 137 additions and 0 deletions
  1. 24 0
      rds_check
  2. 76 0
      sonda
  3. 37 0
      sonda-audio

+ 24 - 0
rds_check

@@ -0,0 +1,24 @@
+#!/bin/bash
+
+set -u
+
+freq='87.9M'
+
+if ! lsusb -d '0bda:2838' > /dev/null; then
+    echo "UNKNOWN - no RTL-SDR found" 2> /dev/null
+    exit 3
+fi
+
+# TODO: save all output in a tempfile and output it after first line
+# dovremmo cercare .ps, ma non trovandolo cerchiamo .pi che esce piu spesso
+lines=$(rtl_fm -M fm -l 0 -A std -p 0 -s '171k' -g 20 -F 9 -f "$freq" 2> /dev/null | \
+        /usr/bin/timeout -s INT 10 redsea | \
+        jq -r 'select(.pi != null) | .pi'|uniq|wc -l)
+
+if [[ "$lines" -gt 0 ]]; then
+    echo "OK: rds $lines times"
+    exit 0
+else
+    echo "CRITICAL: no rds found"
+    exit 2
+fi

+ 76 - 0
sonda

@@ -0,0 +1,76 @@
+#!/bin/bash
+
+instance=asd
+auth='-u user:pass'
+curl="curl $auth"
+remote='https://push.example.net/'
+snrduration=10
+
+if ! lsusb -d '0bda:2838' > /dev/null; then
+    echo "no RTL-SDR found" 2> /dev/null
+    exit 0
+fi
+
+mkdir -p /tmp/samples/
+
+get_power() {
+	rtl_power -f $1:$2:20k -c 0.3 -1 -i 3 2> /dev/null| \
+		cut -d, -f7-|tr , '\n'|awk '{sum += $1; lines+=1; } END {print sum/lines }'
+}
+
+do_station() {
+	freq=$1
+	station=$2
+	start=$((freq - 100000))
+	end=$((freq + 100000))
+	shortfreq=$(bc <<< "scale=1; $freq/1000000")
+
+	power=$(get_power $start $end)
+	cat <<EOF | $curl --data-binary @- "$remote/metrics/job/radio/instance/$instance/freq/$shortfreq/modulation/fm/station/$station"
+#TYPE radio_power gauge
+#HELP radio_power valore assoluto
+radio_power $power
+EOF
+}
+
+
+snr() {
+	freq=$1
+	station=$2
+	shortfreq=$(bc <<< "scale=1; $freq/1000000")
+	timeout -s INT $snrduration rtl_fm -g 50 -f "$freq" -M wfm -s 180k -E deemp | \
+	       	sox -traw -r180k -es -b16 -c1 -V1 - -t ogg - | \
+	       	pee "sponge /tmp/samples/$station.ogg" "sox - -n stats" "sox - -n stat" 2>&1 | \
+		grep -vw -e Bit-depth -e NaN | grep -E '[0-9]$' |
+		sed -e 's/\([^ ]\)  *\([-0-9]\)/\1=\2/;s/  */ /;s/^/radio_sox_/'| \
+		tr ' ' _|tr -d '():' | \
+		tr = ' ' | \
+		while read line
+		do
+			echo $line | $curl --data-binary @- "$remote/metrics/job/radio/instance/$instance/freq/$shortfreq/modulation/fm/station/$station"
+		done
+
+	}
+
+#(
+#	if !  flock -n 9 ; then
+#echo 'Error: sdr locked'
+#exit 1
+#fi
+
+	do_station 87900000 ror
+	do_station 87600000 rai1
+	do_station 98400000 rai3
+	do_station 102100000 freccia
+	do_station 88100000 elle
+	snr 87900000 ror
+	snr 102100000 freccia
+	snr 98400000 rai3
+#	flock -u 9
+#) 9> /var/lock/sdr.lock
+
+# rds_out=$(./rds_check)
+# ret=$?
+# if [[ "$ret" -ne 3 ]]; then
+#     echo -e "$(date +%s)\\t$ret\t$rds_out" >> "$datadir/rds.csv"
+# fi

+ 37 - 0
sonda-audio

@@ -0,0 +1,37 @@
+#!/bin/bash
+
+#defaults
+instance="$(hostname)"
+remote='https://push.example.net'
+freq=87900000
+station=ror
+user=
+password=
+
+# override defaults with sonda.conf
+# important: set $user and $password (to push metrics)
+conffile="$(dirname "$0")/sonda.conf"
+test -f "$conffile" && source "$conffile"
+
+auth="-u ${user}:${password}"
+curl="curl $auth"
+snrduration=10
+
+shortfreq=$(bc <<< "scale=1; $freq/1000000")
+
+mkdir -p "/tmp/samples/$station"
+
+
+export AUDIODEV=default:CARD=SB
+
+timeout $snrduration sox -q -c 1 -t alsa -d -t ogg - | sponge "/tmp/samples/${station}.ogg"
+	sox /tmp/samples/$station.ogg -n stats 2>&1 |
+	grep -vw -e Bit-depth -e NaN | grep -E '[0-9]$' |
+	sed -e 's/\([^ ]\)  *\([-0-9]\)/\1=\2/;s/  */ /;s/^/radio_sox_/'| \
+	tr ' ' _|tr -d '():' | \
+	tr = ' ' | \
+	$curl --data-binary @- "$remote/metrics/job/radio/instance/$instance/freq/$shortfreq/modulation/fm/station/$station"
+
+cp "/tmp/samples/${station}.ogg" "/tmp/samples/$station/$(date '+%F_%T').ogg"
+
+find /tmp/samples/ -mtime +2 -delete