Advertisement
zefie

collectd_rtlamr

Feb 24th, 2019
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.90 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## WIP - Check back to this pastebin often (like once a day or so) for updates while this string is here. ##
  4.  
  5. METER_ID="${1}" # first arg required
  6. RTL_PPM="${2:-0}" # send arg, optional, rtl ppm data
  7. INTERVAL="${3:-${COLLECTD_INTERVAL}}" # third arg, interval. fall back to default collectd value if blank
  8. INTERVAL="${INTERVAL:-60}" # third arg second fallback, set to 60 if all else fails
  9. MAX_WAIT="${3:-10}" # forth arg, optional, how long to run radio waiting for a packet per instance
  10. RTL_WAIT=3 # How long to give rtl_tcp to fire up
  11.  
  12. HOSTNAME="${COLLECTD_HOSTNAME:-`hostname -f`}"
  13.  
  14. function zdbg() { if [ ! -z "${DEBUG_TEST}" ]; then echo "${@}"; fi };
  15.  
  16. if [ $(echo "${INTERVAL}" | grep -c "\.") -gt 0 ]; then
  17.         # don't want your float value, collectd
  18.         INTERVAL=$(printf -v int %.0f "${INTERVAL}")
  19. fi
  20.  
  21. while [ "${METER_ID}" != "" ]; do
  22.         TIME="$(date +%s)"
  23.         METER_READING=""
  24.         while [ "${METER_READING}" == "" ]; do
  25.                 # start radio
  26.                 zdbg "Starting Radio with ppm offset ${RTL_PPM}"
  27.                 rtl_tcp -P ${RTL_PPM} 2>/dev/null >/dev/null &
  28.                 RTLERR="$?";
  29.                 RTLPID="$!";
  30.                 if [ ${RTLERR} -ne 0 ]; then
  31.                         # TODO: human intervention is probably required but try some sys hacks to power cycle dongle
  32.                         # TODO NOTE: this will require the collectd user to have sudo NOPASSWD priv
  33.  
  34.                         # for now, sleep and restart the loop
  35.                         zdbg "rtl_tcp failed to start"
  36.                         sleep ${RTL_WAIT}
  37.                         continue;
  38.                 fi
  39.  
  40.                 zdbg "rtl_tcp started with PID ${RTLPID}, waiting ${RTL_WAIT} seconds to settle...";
  41.                 sleep ${RTL_WAIT} # time for radio to start
  42.  
  43.                 zdbg "Attempting Meter ${METER_ID} Read (Max wait time: $((MAX_WAIT - RTL_WAIT))s)..."
  44.                 METER_READING=$(stdbuf -i0 -o0 -e0 rtlamr -freqcorrection=${RTL_PPM} -filterid=${METER_ID} -single=true -duration=$((MAX_WAIT - RTL_WAIT))s -format=json 2>/dev/null)
  45.                 if [ "${METER_READING}" != "" ]; then
  46.                         zdbg "Read meter... parsing..."
  47.                         METER_READING=$(echo "${METER_READING}" | jq -Mrc .Message.Consumption | tail -n1)
  48.                         zdbg "Result: ${METER_READING}"
  49.                 fi
  50.  
  51.                 # stop radio .. we do it this way to stop kill from echoing to collectd
  52.                 zdbg "Stopping Radio..."
  53.                 kill -9 "${RTLPID}"
  54.                 wait $! &>/dev/null
  55.  
  56.                 if [ "${METER_READING}" == "" ]; then
  57.                         # Meters beacon often, but this code might still get hit if we don't get a beacon within our timeframe
  58.                         # Broken radio will no longer reach here since we handle rtl_tcp ourselves
  59.                         zdbg "Could not read meter. Check meter ID (${METER_ID}) and antenna."
  60.                         sleep ${RTL_WAIT}
  61.                 fi
  62.         done
  63.  
  64.         echo "PUTVAL ${HOSTNAME}/rtlamr-${METER_ID}/kwh_usage interval=${INTERVAL} N:${METER_READING}"
  65.         echo "PUTVAL ${HOSTNAME}/rtlamr-${METER_ID}/kwh_reading interval=${INTERVAL} N:${METER_READING}"
  66.         TIMEEND="$(date +%s)"
  67.         TIMERUN=$((TIMEEND - TIME))
  68.         CALC_INTERVAL=$((INTERVAL - TIME_RUN))
  69.         if [ ${CALC_INTERVAL} -gt 0 ]; then
  70.                 sleep ${CALC_INTERVAL};
  71.         fi
  72. done
  73.  
  74.  
  75. # types.db addition
  76. #kwh_usage               usage:DERIVE:0:U
  77. #kwh_reading             reading:GAUGE:0:U
  78.  
  79. # collectd.conf.d/rtlamr.conf (replace Exec line as needed)
  80. #<Plugin exec>
  81. #  Exec "user" "path-to-this-script" "your-scm-meter-id" "rtlsdr-ppm-offset" "update-interval"
  82. ##  Example: Exec "zefie" "/usr/local/sbin/collectd_rtlamr" "39244000 "22" "60"
  83. #</Plugin>
  84.  
  85. # See also: https://github.com/zefie/CGP (modified with rtlamr plugin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement