0x2142 / frigate-notify

Event notifications for a standalone Frigate NVR instance
https://frigate-notify.0x2142.com/
MIT License
107 stars 9 forks source link

Webhook alerts, several warnings #170

Open woody4165 opened 2 weeks ago

woody4165 commented 2 weeks ago

Hi

I have set webhook alerts and I get several WARN in the log

This is part of the log with warnings

2024/11/03 18:54:25 +0100 WRN util/httpclient.go:185 > HTTP Request failed, retries exceeded error="Post \"http://192.168.1.160:1880/webhook-frigate\": http: ContentLength=66 with Body length 0" attempt=6 max_tries=6
2024/11/03 18:54:25 +0100 WRN notifier/webhook.go:53 > Unable to send alert error="Post \"http://192.168.1.160:1880/webhook-frigate\": http: ContentLength=66 with Body length 0" event_id=1730656447.78467-smy8sq provider=Webhook
2024/11/03 18:54:25 +0100 WRN util/httpclient.go:192 > HTTP Request failed, retrying in 10 seconds... error="Post \"http://192.168.1.160:1880/webhook-frigate\": http: ContentLength=70 with Body length 0" attempt=4 max_tries=6
2024/11/03 18:54:25 +0100 WRN util/httpclient.go:192 > HTTP Request failed, retrying in 10 seconds... error="Post \"http://192.168.1.160:1880/webhook-frigate\": http: ContentLength=67 with Body length 0" attempt=2 max_tries=6
2024/11/03 18:54:26 +0100 WRN util/httpclient.go:192 > HTTP Request failed, retrying in 10 seconds... error="Post \"http://192.168.1.160:1880/webhook-frigate\": http: ContentLength=70 with Body length 0" attempt=5 max_tries=6

and this is my configuration

frigate:
  server: xxxxxxxx:5000
  ignoressl: true
  startup_check:
    attempts: 5
    interval: 30

  mqtt: 
    enabled: true
    server: xxxxxx
    port: 1883
    clientid: frigate-notify
    username: user
    password: pass
    topic_prefix: frigate

  cameras:
    exclude:
      - patio
      - porta

alerts:
  general:
    title: Frigate Alert
    timeformat: 15:04:05 del 02 Gen 2006 
    nosnap:
    snap_bbox: true
    snap_timestamp:

    snap_crop:
    notify_once: true

  quiet:
    start:
    end:

  zones:
    unzoned: allow
    allow:
     - viale
     - scala
     - prato

  labels:
    min_score: 20
    allow:
     - person
     - car
     - motorcycle
     - bicycle
    block:
     - dog
     - cat

  telegram:
    enabled: true
    chatid: xxxxxx
    token: xxxxxxxx
    template: |
      Motion alle {{ .Extra.FormattedTime }}
      Camera: {{ .Camera }}
      Label: {{ .Label }} ({{ .Extra.TopScorePercent }})
      Links:
      {{ if ne .Extra.PublicURL "" }}{{if ge .Extra.FrigateMajorVersion 14 }} - Camera: {{ .Extra.PublicURL }}/#{{ .Camera }}{{else}} - Camera: {{ .Extra.PublicURL }}/cameras/{{ .Camera }}{{end}}
      {{ if .HasClip }} - Event Clip: {{ .Extra.PublicURL }}/api/events/{{ .ID }}/clip.mp4{{ end }}
      {{ else }}{{if ge .Extra.FrigateMajorVersion 14 }} - Camera: {{ .Extra.LocalURL }}/#{{ .Camera }}{{ else }} - Camera: {{ .Extra.LocalURL }}/cameras/{{ .Camera }}{{ end }}
      {{ if .HasClip }} - Event Clip: {{ .Extra.LocalURL }}/api/events/{{ .ID }}/clip.mp4{{ end }}
      {{ end }}

      {{ if not .HasSnapshot }}Nessuno snapshot disponibile.{{end}}

  webhook: 
    enabled: true
    server: http://xxxxxxx:1880/webhook-frigate
    header:
      Content-Type: "application/json"
    template: 
       camera: "{{ .Camera }}" 
       label: "{{ .Label }}" 
       id: "{{ .ID }}"   

What can I check/do?

Thanks

freefd commented 2 weeks ago

What can I check/do?

You missed the pipe symbol for the template in the webhook notifier, it should look like:

...
  webhook: 
    enabled: true
    server: http://xxxxxxx:1880/webhook-frigate
    header:
      Content-Type: "application/json"
    template: |
       camera: "{{ .Camera }}" 
       label: "{{ .Label }}" 
       id: "{{ .ID }}"   

See more: https://yaml-multiline.info/

woody4165 commented 2 weeks ago

Thanks @freefd

Strangely was working, not all the times maybe. Now I've placed the pipe after the template: and I will check it asap

woody4165 commented 2 weeks ago

@freefd After adding the pipe I get this error while loading the config

Failed to load config from file! error="1 error(s) decoding:\n\n* 'alerts.webhook.template' expected a map, got 'string'"

I've also placed correctly the indentation

    template: |
      camera: "{{ .Camera }}"
      label: "{{ .Label }}"
      id: "{{ .ID }}"

If I remove the pipe, the config loads correctly and I get the proper webhook in nodered

freefd commented 2 weeks ago

'alerts.webhook.template' expected a map, got 'string'"

And now we need to summon @0x2142 here.

0x2142 commented 1 week ago

Hi, So for the moment, there are two ways of configuring a custom webhook payload via template.

The first is what you've already tried - which is providing a mapping of key / value pairs:

    template: 
      camera: "{{ .Camera }}"

Which then generates a message like:

{ "camera": "test_cam" }

Alternatively, you can enter a full JSON string, but at the moment it must be on one line:

    template:  { "camera": "{{ .Camera }}" }

Which would generate the same payload as above.

Looks like because of the way I've implemented this, the typical YAML multiline string input isn't working - so I'll have to figure out a way to address that. I should also update the docs to add more info about this one, since it's a bit different than the other notification providers.

woody4165 commented 1 week ago

Ok, thanks @0x2142

So, my configutation, that is lile the first one, is correct

My issue, I don't know if is an issue, is the warning messages that I always get that are like

2024/11/04 19:18:02 +0100 WRN util/httpclient.go:192 > HTTP Request failed, retrying in 10 seconds... error="Post \"http://192.168.1.160:1880/webhook-frigate\": http: ContentLength=70 with Body length 0" attempt=4 max_tries=6
2024/11/04 19:18:03 +0100 WRN util/httpclient.go:192 > HTTP Request failed, retrying in 10 seconds... error="Post \"http://192.168.1.160:1880/webhook-frigate\": http: ContentLength=70 with Body length 0" attempt=5 max_tries=6
2024/11/04 19:18:04 +0100 WRN util/httpclient.go:185 > HTTP Request failed, retries exceeded error="Post \"http://192.168.1.160:1880/webhook-frigate\": http: ContentLength=70 with Body length 0" attempt=6 max_tries=6
2024/11/04 19:18:04 +0100 WRN notifier/webhook.go:53 > Unable to send alert error="Post \"http://192.168.1.160:1880/webhook-frigate\": http: ContentLength=70 with Body length 0" event_id=1730744265.934368-b50rn7 provider=Webhook

They are just warning or should I worry about?

0x2142 commented 1 week ago

Hi @woody4165 - So, there are two things going on here...

First, I tried spinning up a node red instance to see if I can replicate this - and sure enough it was pretty easy. I've never used node red before, so I added an "http in" for the incoming webhook, then some logic after to forward the request elsewhere. It looks like with this config, node red never responds to frigate-notify - so the app sits there until the request times out, which then initiates he retry errors you're seeing.

In order to fix this, I needed to add a "http response" with a status code of 200. Seems like without this, node red never responds with a status code - so the connection just hangs and times out.

Here's what my quick flow looks like:

image

So perhaps this might be your issue as well? If so, you should just need to add a response to the initial inbound webhook from frigate-notify.

Second - the warnings you are getting are in fact a bug in the HTTP retry logic. I'll get this resolved in the next release.

freefd commented 1 week ago

ContentLength=66 with Body length 0

@0x2142 I'm more worried about this one ^^^

@woody4165 can you please run your Frigate-notify instance in the trace logging mode (https://frigate-notify.0x2142.com/latest/config/options/) and provide more logs about the request? Based on your configuration this code will be invoked: https://github.com/0x2142/frigate-notify/blob/main/notifier/webhook.go#L45 And thus the entire request will be logged by https://github.com/0x2142/frigate-notify/blob/main/util/httpclient.go#L158-L163 lines.

0x2142 commented 1 week ago

@freefd Yeah that's the issue I found. I'm not handling the request body properly in the retry, so once that first request is sent, subsequent retries are actually sending an empty body. I have a fix already in my current local branch & it will be out in the dev branch for you soon 😊

freefd commented 1 week ago

@0x2142, oh... got the problem. @woody4165, please ignore my request about the trace log level.

woody4165 commented 1 week ago

So perhaps this might be your issue as well? If so, you should just need to add a response to the initial inbound webhook from frigate-notify.

Thanks @0x2142 I've just added an http response node with 200 and I'll check with next motion

My flow does not include http request node, since I get the data sent from the webhook directly from http in node

@freefd thanks for your suggestion, I'll wait for your fix

woody4165 commented 1 week ago

@0x2142 , @freefd It seems that all the webhook warnings are gone So I've just added the http response node with 200 as value

Thanks !

woody4165 commented 1 week ago

Just one more question, not related to webhook, in case it's needed I'll open a new issue

Some times, most of the times, I get two telegram message for same event, one without the event clips link, one with. The sent snapshots are almost the same, maybe some frame later the second message. Is it possible to avoid the message without the clip?

Screenshot_20241105-131641

freefd commented 1 week ago

@woody4165 I wrote the message about this behavior but finally decided to ask you to create a new issue so as not to mix topics, please.

woody4165 commented 1 week ago

Hi @freefd

let me know if I can close this issue, since now it seems that everything is ok

freefd commented 1 week ago

@woody4165, perhaps @0x2142 might want to make some commits with this issue as the ref, and close it after fixing, idk.

0x2142 commented 1 week ago

I fixed the issue here and forgot to add a reference. But as far as I'm concerned, feel free to close it if you like - and thanks for reporting the issue!