Skip to content

Commit

Permalink
Merge pull request #110 from 0x2142/add-alert-filters
Browse files Browse the repository at this point in the history
Add new alert filters
  • Loading branch information
0x2142 authored Aug 9, 2024
2 parents b48c6c8 + f4b53d9 commit c73fbad
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 97 deletions.
73 changes: 60 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ type Cameras struct {
}

type Alerts struct {
General General `fig:"general"`
Zones Zones `fig:"zones"`
Labels Labels `fig:"labels"`
Discord Discord `fig:"discord"`
Gotify Gotify `fig:"gotify"`
SMTP SMTP `fig:"smtp"`
Telegram Telegram `fig:"telegram"`
Pushover Pushover `fig:"pushover"`
Nfty Nfty `fig:"nfty"`
Ntfy Ntfy `fig:"ntfy"`
Webhook Webhook `fig:"webhook"`
General General `fig:"general"`
Quiet Quiet `fig:"quiet"`
Zones Zones `fig:"zones"`
Labels Labels `fig:"labels"`
SubLabels Labels `fig:"sublabels"`
Discord Discord `fig:"discord"`
Gotify Gotify `fig:"gotify"`
SMTP SMTP `fig:"smtp"`
Telegram Telegram `fig:"telegram"`
Pushover Pushover `fig:"pushover"`
Nfty Nfty `fig:"nfty"`
Ntfy Ntfy `fig:"ntfy"`
Webhook Webhook `fig:"webhook"`
}

type General struct {
Expand All @@ -80,15 +82,21 @@ type General struct {
NoSnap string `fig:"nosnap" default:"allow"`
}

type Quiet struct {
Start string `fig:"start" default:""`
End string `fig:"end" default:""`
}

type Zones struct {
Unzoned string `fig:"unzoned" default:"allow"`
Allow []string `fig:"allow" default:[]`
Block []string `fig:"block" default:[]`
}

type Labels struct {
Allow []string `fig:"allow" default:[]`
Block []string `fig:"block" default:[]`
MinScore float64 `fig:"min_score" default:0`
Allow []string `fig:"allow" default:[]`
Block []string `fig:"block" default:[]`
}

type Discord struct {
Expand Down Expand Up @@ -306,6 +314,24 @@ func validateConfig() {
}
}

// Check quiet hours config
if ConfigData.Alerts.Quiet.Start != "" || ConfigData.Alerts.Quiet.End != "" {
timeformat := "15:04"
validstart := true
validend := true
if _, ok := time.Parse(timeformat, ConfigData.Alerts.Quiet.Start); ok != nil {
configErrors = append(configErrors, "Start time for quiet hours does not match format: 00:00")
validstart = false
}
if _, ok := time.Parse(timeformat, ConfigData.Alerts.Quiet.End); ok != nil {
configErrors = append(configErrors, "End time for quiet hours does not match format: 00:00")
validend = false
}
if validstart && validend {
log.Debug().Msgf("Quiet hours enabled. Start: %v, End: %v", ConfigData.Alerts.Quiet.Start, ConfigData.Alerts.Quiet.End)
}
}

// Check action on no snapshot available
if strings.ToLower(ConfigData.Alerts.General.NoSnap) != "allow" && strings.ToLower(ConfigData.Alerts.General.NoSnap) != "drop" {
configErrors = append(configErrors, "Option for nosnap must be 'allow' or 'drop'")
Expand Down Expand Up @@ -338,6 +364,9 @@ func validateConfig() {
}

// Check Label filtering config
if ConfigData.Alerts.Labels.MinScore > 0 {
log.Debug().Msgf("Label required minimum score: %v", ConfigData.Alerts.Labels.MinScore)
}
if len(ConfigData.Alerts.Labels.Allow) > 0 {
log.Debug().Msg("Labels to generate alerts for:")
for _, c := range ConfigData.Alerts.Labels.Allow {
Expand All @@ -355,6 +384,24 @@ func validateConfig() {
log.Debug().Msg("No labels excluded")
}

// Check Subabel filtering config
if len(ConfigData.Alerts.SubLabels.Allow) > 0 {
log.Debug().Msg("Sublabels to generate alerts for:")
for _, c := range ConfigData.Alerts.SubLabels.Allow {
log.Debug().Msgf(" - %v", c)
}
} else {
log.Debug().Msg("All Sublabels included in alerts")
}
if len(ConfigData.Alerts.SubLabels.Block) > 0 {
log.Debug().Msg("Sublabels to exclude from alerting:")
for _, c := range ConfigData.Alerts.SubLabels.Block {
log.Debug().Msgf(" - %v", c)
}
} else {
log.Debug().Msg("No Sublabels excluded")
}

// Check / Load alerting configuration
if ConfigData.Alerts.Discord.Enabled {
log.Debug().Msg("Discord alerting enabled.")
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [v0.3.3](https://github.com/0x2142/frigate-notify/releases/tag/v0.3.3) - Upcoming Release
- Additional filtering options for alerts:
- [Sublabels](https://frigate-notify.0x2142.com/latest/config/file/#sublabels)
- [Minimum label score](https://frigate-notify.0x2142.com/latest/config/file/#labels)
- [Quiet hours](https://frigate-notify.0x2142.com/latest/config/file/#quiet-hours)

## [v0.3.2](https://github.com/0x2142/frigate-notify/releases/tag/v0.3.2) - Jun 13 2024

- Allow config to be provided via environment variables (Thanks [@ryan-willis](https://github.com/ryan-willis)!!)
Expand Down
48 changes: 47 additions & 1 deletion docs/config/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,31 @@ frigate:
- Set to `drop` to silently drop these events & not send notifications

```yaml title="Config File Snippet"
alerts:
alerts:
general:
title: Frigate Alert
timeformat: Mon, 02 Jan 2006 15:04:05 MST
nosnap:
```

### Quiet Hours

Define a quiet period & supress alerts during this time.

- **start** (Optional)
- When quiet period begins, in 24-hour format
- Required if `end` is configured
- **end** (Optional)
- When quiet period ends, in 24-hour format
- Required if `start` is configured

```yaml title="Config File Snippet"
alerts:
quiet:
start: 08:00
end: 17:00
```

### Zones

This config section allows control over whether to generate alerts on all zones, or only specific ones. By default, the app will generate notifications on **all** Frigate events, regardless of whether the event includes zone info.
Expand Down Expand Up @@ -180,6 +198,11 @@ alerts:

Similar to [zones](#zones), notifications can be filtered based on labels. By default, the app will generate notifications regardless of any labels received from Frigate. Using this config section, certain labels can be blocked from sending notifications - or an allowlist can be provided to only generate alerts from specified labels.

- **min_score** (Optional - Default: `0`)
- Filter by minimum label score, based on Frigate `top_score` value
- Scores are a percent accuracy of object identification (0-100)
- For example, to filter objects under 80% accuracy, set `min_score: 80`
- By default, any score above 0 will generate an alert
- **allow** (Optional)
- Specify a list of labels to allow notifications
- If set, all other labels will be ignored
Expand All @@ -191,13 +214,36 @@ Similar to [zones](#zones), notifications can be filtered based on labels. By de
```yaml title="Config File Snippet"
alerts:
labels:
min_score: 80
allow:
- person
- dog
block:
- bird
```

### Sublabels

Filter by sublabels, just like normal [labels](#labels).

- **allow** (Optional)
- Specify a list of sublabels to allow notifications
- If set, all other sublabels will be ignored
- If not set, all sublabels will generate notifications
- **block** (Optional)
- Specify a list of sublabels to always ignore
- This takes precedence over the `allow` list

```yaml title="Config File Snippet"
alerts:
sublabels:
allow:
- ABCD
- EFGH
block:
- XYZ
```

### Discord

- **enabled** (Optional - Default: `false`)
Expand Down
11 changes: 11 additions & 0 deletions docs/config/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ alerts:
timeformat:
nosnap:

quiet:
start:
end:

zones:
unzoned: allow
allow:
Expand All @@ -43,12 +47,19 @@ alerts:
- test_zone_02

labels:
min_score:
allow:
- person
- dog
block:
- bird

sublabels:
allow:
- ABCD
block:
- EFGH

discord:
enabled: false
webhook:
Expand Down
4 changes: 2 additions & 2 deletions events/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func CheckForEvents() {
Str("event_id", event.ID).
Msgf("Event start time: %s", eventTime)

// Check that event passes the zone & label filters
if !isAllowedZone(event.ID, event.Zones) || !isAllowedLabel(event.ID, event.Label) {
// Check that event passes configured filters
if !checkEventFilters(event) {
return
}

Expand Down
77 changes: 0 additions & 77 deletions events/events.go

This file was deleted.

Loading

0 comments on commit c73fbad

Please sign in to comment.