Skip to content

[Bug]: Matrix notification sender issue #1130

Description

@byteonabeach

Bug description

Hey,
I'm running Netdata on my home lab and trying to set up Matrix notifications for alerts. Everything seems configured right in health_alarm_notify.conf (homeserver URL, access token and room ID), but when I test it with /usr/libexec/netdata/plugins.d/alarm-notify.sh test, it fails with HTTP 404 and this Matrix error: {"errcode":"M_UNRECOGNIZED","error":"M_UNRECOGNIZED: Not Found"}.

What happens:

The curl command in debug looks like this (sanitized):

/usr/bin/curl -X POST --data '{...}' https://myhomeserver.example.com/_matrix/client/r0/rooms/%21roomid%3Amyhomeserver.address.tld/send/m.room.message?access_token=valid_token_here

And the response is always that M_UNRECOGNIZED not found thing.

I tested manually with curl using the right format (PUT instead of POST, add a txnId like timestamp, and put the token in Authorization header), and it works fine:

txnid=$(date +%s%N)
curl -X PUT -d '{"msgtype":"m.text", "body":"Test"}' \
"https://myhomeserver.example.com/_matrix/client/r0/rooms/%21roomid:myhomeserver.address.tld/send/m.room.message/${txnid}" \
-H "Authorization: Bearer valid_token_here"

Gets a proper event_id back.

Expected behavior

Notifications should post to the Matrix room without errors.

Steps to reproduce

  1. Enabled Matrix in health_alarm_notify.conf like that:
    • SEND_MATRIX="YES"
    • MATRIX_HOMESERVER="https://myhomeserver.address.tld"
    • MATRIX_ACCESSTOKEN="valid_token_here"
    • DEFAULT_RECIPIENT_MATRIX="!roomid:myhomeserver.address.tld"
  2. Bot account is joined to the room.
  3. Ran the test script as the netdata user: sudo su -s /bin/bash netdata then /usr/libexec/netdata/plugins.d/alarm-notify.sh test
  4. Or with debug: export NETDATA_ALARM_NOTIFY_DEBUG=1 and run again.

Screenshots

No response

Error Logs

No response

Desktop

Netdata version: v2.8.4
OS: Fedora 43
Matrix homeserver: Tuwunel 1.4.7

Additional context

That version of a send_matrix function fixed issue:

send_matrix() {
  local homeserver="${1}" rooms="${2}" httpcode sent=0 payload txnid

  [ "${SEND_MATRIX}" != "YES" ] && return 1
  [ -z "${MATRIX_ACCESSTOKEN}" ] && return 1

  accesstoken="${MATRIX_ACCESSTOKEN}"

  case "${status}" in
  WARNING) emoji="warning" ;;
  CRITICAL) emoji="red_circle" ;;
  CLEAR) emoji="white_check_mark" ;;
  *) emoji="white_circle" ;;
  esac

  txnid=$(date +%s%N)

  for room in ${rooms}; do
    webhook="$homeserver/_matrix/client/r0/rooms/$(urlencode $room)/send/m.room.message/${txnid}" 
    payload="$(
  cat <<EOF
  {
    "msgtype": "m.notice",
    "format": "org.matrix.custom.html",
      "formatted_body": "${emoji} ${host} ${status_message} - <b>${name//_/ }</b><br>${chart}<br><a href=\"${goto_url}\">${alarm}</a><br><i>${info}</i>",
      "body": "${emoji} ${host} ${status_message} - ${name//_/ } ${chart} ${goto_url} ${alarm} ${info}"
  }
EOF
    )"

    httpcode=$(docurl -X PUT --data "${payload}" -H "Authorization: Bearer ${accesstoken}" "${webhook}") 
    if [ "${httpcode}" == "200" ]; then
      info "sent Matrix notification to '${room}' for ${notification_description}"
      sent=$((sent + 1))
    else
      error "failed to send Matrix notification to '${room}' for ${notification_description}, with HTTP response status code ${httpcode}."
    fi
  done

  [ ${sent} -gt 0 ] && return 0

  return 1
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions