22# ##################
33# MQTT Shell Logger
44
5+ temp_dir=$( mktemp -d)
6+
7+ mkfifo " $temp_dir /out" " $temp_dir /err"
8+ < " $temp_dir /out" logger -p user.notice -t " $( basename " $0 " ) " &
9+ < " $temp_dir /err" logger -p user.error -t " $( basename " $0 " ) " &
10+ exec > " $temp_dir /out" 2> " $temp_dir /err"
11+
512outputroot=" /mnt/mmcblk0p1" # Scope to put and rotate the logs in, thus oldest file inside this might get deleted, even if not lyed out by this script!
613outputsubdirs=1 # whether to create subdirectories for each topic
714filenameprefix=" log_"
@@ -12,60 +19,74 @@ minfreespace=2097 # in KiB
1219# maxfilesize=1048 # in B (for testing)
1320maxfilesize=1048576 # in B
1421
15- backpipe=" /tmp /mqttlogger_backpipe"
16- pidfile=" /tmp /mqttlogger_pidfile"
22+ backpipe=" $temp_dir /mqttlogger_backpipe"
23+ pidfile=" $temp_dir /mqttlogger_pidfile"
1724
1825ctrl_c () {
19- echo " Cleaning up..."
20- kill $( cat $pidfile ) 2> /dev/null
21- rm -f $backpipe $pidfile
22- [ " $? " -eq " 0" ] && { echo " Exit success" ; exit 0; } || exit 1
26+ kill $( cat " $pidfile " )
27+ rm -rf $temp_dir
28+ [ " $? " -eq " 0" ] && {
29+ echo " Exit success."
30+ exit 0
31+ } || exit 1
2332}
2433
25- listen (){
34+ listen () {
2635
36+ while true
37+ do
38+ curl -s mqtt://localhost:1883
39+ ret=$?
40+ [ $ret -eq 3 ] && unset tmp && unset ret && break
41+ [ -z $tmp ] && echo " waiting for mqtt broker" && tmp=1
42+ sleep 1
43+ done
44+
2745 [ ! -p " $backpipe " ] && mkfifo $backpipe
28- (mosquitto_sub -v $* > $backpipe 2> /dev/null ) &
29- echo " $! " > $pidfile
46+ (mosquitto_sub $* 1 >$backpipe ) &
47+ echo " $! " > " $pidfile "
3048
31- while read line < $backpipe
32- do
49+ echo " connected."
50+
51+ while read line; do
3352
34- # timestamp=$(date -Ins) # option ns not available on busybox
35- ns= $( adjtimex | awk ' NR==12 {print $NF} ' ) # get nanoseconds on busybox
36- timestamp =$( date " +%Y-%m-%dT%H:%M:%S. $ns %z " ) # workaround for not having +%N or -Ins options in date
37- # timestamp=$(date "+%Y-%m-%dT%H:%M:%S%z") # give up on nanoseconds
53+ # get nanoseconds on busybox
54+ # (workaround for not having +%N or -Ins options in date)
55+ us =$( adjtimex | awk ' NR==12 {print $NF} ' )
56+ timestamp=$( date " +%Y-%m-%dT%H:%M:%S. $us %z" )
3857
3958 topic=${line%% " " * }
4059 message=${line#* " " }
4160 outputpath=" $outputroot " $( [ $outputsubdirs -ne 0 ] && echo " /$topic " )
4261
4362 # make new directory if necessary
4463 # otherwise get last changed logfile inside the subfolder
45- [ ! -d $outputpath ] && { mkdir -p $outputpath ; unset filename; } || \
46- filename=$( find $outputpath -maxdepth 1 -type f -name " $filenameprefix *.$fileextension " -print0 | xargs -0r ls -t | head -n1)
64+ [ ! -d $outputpath ] && {
65+ mkdir -p $outputpath
66+ unset filename
67+ } ||
68+ filename=$( find $outputpath -maxdepth 1 -type f -name " $filenameprefix *.$fileextension " -print0 | xargs -0r ls -t | head -n1)
4769
4870 # debugging:
49- # echo "topic=$topic"
50- # echo "message=$message"
51- # echo "outputpath: $outputpath"
71+ # echo "received: $topic = $message"
72+ # echo "outputpath: $outputpath"
5273
5374 # new file if necessary:
54- ([ -z " $filename " ] || \
55- (filesize=$( wc -c " $filename " ) && filesize=${filesize%% " " * } && [ " $filesize " -gt " $maxfilesize " ])) && \
56- filename=" $outputpath /$filenameprefix $( date $filenamesuffixformat ) .$fileextension "
75+ ([ -z " $filename " ] ||
76+ (filesize=$( wc -c " $filename " ) && filesize=${filesize%% " " * } && [ " $filesize " -gt " $maxfilesize " ])) &&
77+ filename=" $outputpath /$filenameprefix $( date $filenamesuffixformat ) .$fileextension "
5778
5879 # delete oldest, if necessary:
5980 avail=$( df " $outputroot " | awk ' NR == 2 { print $4 }' ) # get free disk space
6081 [ " $avail " -lt " $minfreespace " ] && rm " $( find $outputroot -type f -name " $filenameprefix *.$fileextension " -print0 | xargs -0r ls -tr | head -n1) "
6182
6283 # write log entry:
63- echo " \" $timestamp \" $( [ $outputsubdirs -eq 0 ] && echo " ,\" $topic \" " ) , \" $message \" " >> " $filename "
84+ echo " $timestamp $( [ $outputsubdirs -eq 0 ] && echo " ,$topic " ) , $message " >> " $filename "
6485
65- done
86+ done < $backpipe
6687}
6788
68- usage (){
89+ usage () {
6990 echo " Mqtt-Exec Logger Via Bash"
7091 echo " based on https://unix.stackexchange.com/a/274224"
7192 echo " Logger writes messages from subscribed topics into a file."
@@ -76,12 +97,11 @@ usage(){
7697
7798case " $1 " in
7899--h)
79- usage
80- exit 1
81- ;;
100+ usage
101+ exit 1
102+ ;;
82103* )
83- trap ctrl_c INT
84- listen $*
85- ;;
104+ trap ctrl_c INT TERM QUIT
105+ listen -v -t womo/ # $*
106+ ;;
86107esac
87-
0 commit comments