# Subscribe to AXIS Image Health Analytics events, pretty-print them # to stdout (the Data Agent Logs tab), and POST each event to ntfy. # # Extra env (Data Agent UI): # NTFY_TOPIC_URL=https://ntfy.sh/your-topic # STATS_PERIOD=1h # ALARM_QUIET_PERIOD=1h # NOTIFY_MAX_BEFORE_PAUSE=5 [agent] debug = ${TELEGRAF_DEBUG:-false} # ---------------------------------------------------------------------- # Subscribe and process the events. # ---------------------------------------------------------------------- # Subscribe to events from AXIS Image Health Analytics. [[inputs.execd]] command = [ "${EXECUTABLES_DIR}/event_handler", "--subscribe", "--config-inline", """ [topics] topic_0 = AcapOnvifEvent topic_0_namespace = tns1 topic_1 = AXISImageHealthAnalytics topic_2 = Any-Active-Events [settings] namespace = unqualified [item.Cause] namespace = tns1 data_type = string [item.active] namespace = tns1 data_type = bool """ ] data_format = "influx" # Create a message counter tells how many metrics have arrived # since the start of the agent. This can be used to ignore the # first metric which is always sent when subscribing to a # stateful event. [[processors.defaults]] namepass = ["Any-Active-Events"] [processors.defaults.fields] state_change = 1 [[processors.cumulative_sum]] namepass = ["Any-Active-Events"] fields = ["state_change"] # Split activations and clears onto dedicated measurement names. # Rename alarm activation event to "health_event_activated" # and alarm deactivation event to "health_event_deactivated". # This makes is easier to work with them downstream. [[processors.override]] namepass = ["Any-Active-Events"] metricpass = "fields.active == true" name_override = "health_event_activated" [[processors.override]] namepass = ["Any-Active-Events"] name_override = "health_event_deactivated" # Promote the "Cause" field to a tag. This is needed for the # cumulative_sum and valuecounter processors to split the different # causes into separate metrics so that statistics are reported # separately. [[processors.converter]] namepass = ["health_event_activated", "health_event_deactivated"] [processors.converter.fields] tag = ["Cause"] drop = ["active"] # Create regular statistics with a report of the number of alarms # for each cause during that interval. E.g. if STATS_PERIOD is set # to 1h, then this will emit a metric every hour with the number of # alarms for each cause during that interval. [[aggregators.valuecounter]] namepass = ["health_event_activated"] name_override = "image_health_stats" period = "${STATS_PERIOD:-1h}" fields = ["active"] # Leave the original metric untouched so it can be used again # by other plugins. drop_original = false # Add a static "incident=1" field. This is needed for the # cumulative_sum processor since it operates on numeric fields. # Activations and clears are counted separately. [[processors.defaults]] namepass = ["health_event_activated", "health_event_deactivated"] [processors.defaults.fields] incident = 1 # Add a dummy metric that flows every second. This is used to trigger the # expiration of old alarms in the cumulative_sum processor. Without a metric # driving it, it will never expire an alarm. [[inputs.mock]] name_override = "health_event_expiry_tick" interval = "1s" [[inputs.mock.constant]] name = "tick" value = 1 # Running totals for activations and clears. Each measurement and Cause # has its own counter. The counter resets after ALARM_QUIET_PERIOD with no new # metric for that series. This processor adds the 'incident_sum' field. [[processors.cumulative_sum]] namepass = ["health_event_activated", "health_event_deactivated", "health_event_expiry_tick"] fields = ["incident"] expiry_interval = "${ALARM_QUIET_PERIOD:-1h}" # Check the incident_sum field. If too many activations have occurred # within ALARM_QUIET_PERIOD, create a new notification paused metric. # This can be used to notify the user about a flickering alarm. [[processors.clone]] namepass = ["health_event_activated"] metricpass = "fields.incident_sum == ${NOTIFY_MAX_BEFORE_PAUSE:-5} + 1" name_override = "health_event_notifications_paused" # Check the incident_sum field and create new copies of the events that # are below the threshold. This makes it easier for downstream logic # to decide whether to use all events are only non-muted ones. [[processors.clone]] namepass = ["health_event_activated"] metricpass = "fields.incident_sum <= ${NOTIFY_MAX_BEFORE_PAUSE:-5}" name_override = "health_event_activated_notify" [[processors.clone]] namepass = ["health_event_deactivated"] metricpass = "fields.incident_sum <= ${NOTIFY_MAX_BEFORE_PAUSE:-5}" name_override = "health_event_deactivated_notify" # ---------------------------------------------------------------------- # Log events to the Data Agent Logs tab. # ---------------------------------------------------------------------- # Pretty-print live activations to stdout (the Data Agent Logs tab). [[outputs.file]] namepass = ["health_event_activated_notify"] files = ["stdout"] data_format = "template" metric_batch_size = 1 template = ''' Image health event activated (cause: {{ .Tag "Cause" }}, count: {{ .Field "incident_sum" }}) (name: {{ .Name }}) ''' # Pretty-print clears to stdout (the Data Agent Logs tab). [[outputs.file]] namepass = ["health_event_deactivated_notify"] files = ["stdout"] data_format = "template" metric_batch_size = 1 template = ''' Image health event cleared ''' # Pretty-print a notification about a flickering alarm being paused. [[outputs.file]] namepass = ["health_event_notifications_paused"] files = ["stdout"] data_format = "template" metric_batch_size = 1 template = ''' ------------------------------------------------------------ Image health notifications paused cause: {{ .Tag "Cause" }} reason: flickering (more than ${NOTIFY_MAX_BEFORE_PAUSE:-5} activations) ------------------------------------------------------------ ''' # Pretty-print regular statistics to stdout (the Data Agent Logs tab). [[outputs.file]] namepass = ["image_health_stats"] files = ["stdout"] data_format = "template" metric_batch_size = 1 template = ''' ------------------------------------------------------------ Image health (${STATS_PERIOD:-1h}) cause: {{ .Tag "Cause" }} count: {{ .Field "active_true" }} ------------------------------------------------------------ ''' # ---------------------------------------------------------------------- # Send events to the ntfy.sh event notification service. # The NTFY_TOPIC_URL environment variable must be set in the # Configuration->Variables tab under the # "Extra Telegraf variables" section. # ---------------------------------------------------------------------- # Send each alarm activation to the ntfy.sh event notification service. [[outputs.http]] namepass = ["health_event_activated_notify"] url = "${NTFY_TOPIC_URL}" method = "POST" timeout = "10s" data_format = "template" use_batch_format = false metric_batch_size = 1 template = '{{ .Tag "Cause" }} alarm activated ({{ .Field "incident_sum" }} times)' [outputs.http.headers] Content-Type = "text/plain; charset=utf-8" Title = "Image health event" Tags = "camera" Priority = "high" # Send alarm clears to the ntfy.sh event notification service # to tell the user that all is good again. # If the metric number is one, we ignore it since the first # metric is always received when subscribing to a stateful event. # This does not indicate a change from alarming to normal, it # is just saying that there was no alarm when we subscribed. # That's irrelevant to the user, so no need to notify them. [[outputs.http]] namepass = ["health_event_deactivated_notify"] metricpass = "fields.state_change_sum > 1" url = "${NTFY_TOPIC_URL}" method = "POST" timeout = "10s" data_format = "template" use_batch_format = false metric_batch_size = 1 template = "Image health is no longer alarming" [outputs.http.headers] Content-Type = "text/plain; charset=utf-8" Title = "Image health event" Tags = "camera" Priority = "default" # Send a notification about a flickering alarm being paused. It's # important that the user knows that the alarms are being ignored, # so this pause started notification is sent with the max priority. [[outputs.http]] namepass = ["health_event_notifications_paused"] url = "${NTFY_TOPIC_URL}" method = "POST" timeout = "10s" data_format = "template" use_batch_format = false metric_batch_size = 1 template = '{{ .Tag "Cause" }} alarm notifications paused due to flickering' [outputs.http.headers] Content-Type = "text/plain; charset=utf-8" Title = "Image health notifications paused" Tags = "camera,warning" Priority = "max" # Send regular statistics to the ntfy.sh event notification service. # This sends a notification every interval with the number of alarms # for each cause during that interval. [[outputs.http]] namepass = ["image_health_stats"] url = "${NTFY_TOPIC_URL}" method = "POST" timeout = "10s" data_format = "template" use_batch_format = false metric_batch_size = 1 template = '{{ .Tag "Cause" }} last ${STATS_PERIOD:-1h}: {{ .Field "active_true" }}' [outputs.http.headers] Content-Type = "text/plain; charset=utf-8" Title = "Image health (${STATS_PERIOD:-1h})" Tags = "camera"