Skip to content

Commit

Permalink
add systemd-timers module
Browse files Browse the repository at this point in the history
Co-authored-by: Quentin Manfroi <[email protected]>
  • Loading branch information
Nicolas Senaud and xp-1000 committed Nov 16, 2021
1 parent 739dbfb commit e7c6c83
Show file tree
Hide file tree
Showing 15 changed files with 490 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/severity.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
- [supervisor](#supervisor)
- [system-common](#system-common)
- [systemd-services](#systemd-services)
- [systemd-timers](#systemd-timers)
- [tomcat](#tomcat)
- [varnish](#varnish)
- [zookeeper](#zookeeper)
Expand Down Expand Up @@ -1032,6 +1033,15 @@
|Systemd-services aliveness|X|-|-|-|-|


## systemd-timers

|Detector|Critical|Major|Minor|Warning|Info|
|---|---|---|---|---|---|
|Systemd-timers heartbeat|-|-|X|-|-|
|Systemd-timers execution delay|-|X|-|-|-|
|Systemd-timers last execution state|-|X|-|-|-|


## tomcat

|Detector|Critical|Major|Minor|Warning|Info|
Expand Down
149 changes: 149 additions & 0 deletions modules/smart-agent_systemd-timers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# SYSTEMD-TIMERS SignalFx detectors

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
:link: **Contents**

- [How to use this module?](#how-to-use-this-module)
- [What are the available detectors in this module?](#what-are-the-available-detectors-in-this-module)
- [How to collect required metrics?](#how-to-collect-required-metrics)
- [Examples](#examples)
- [Metrics](#metrics)
- [Related documentation](#related-documentation)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## How to use this module?

This directory defines a [Terraform](https://www.terraform.io/)
[module](https://www.terraform.io/docs/modules/usage.html) you can use in your
existing [stack](https://github.com/claranet/terraform-signalfx-detectors/wiki/Getting-started#stack) by adding a
`module` configuration and setting its `source` parameter to URL of this folder:

```hcl
module "signalfx-detectors-smart-agent-systemd-timers" {
source = "github.com/claranet/terraform-signalfx-detectors.git//modules/smart-agent_systemd-timers?ref={revision}"
environment = var.environment
notifications = local.notifications
}
```

Note the following parameters:

* `source`: Use this parameter to specify the URL of the module. The double slash (`//`) is intentional and required.
Terraform uses it to specify subfolders within a Git repo (see [module
sources](https://www.terraform.io/docs/modules/sources.html)). The `ref` parameter specifies a specific Git tag in
this repository. It is recommended to use the latest "pinned" version in place of `{revision}`. Avoid using a branch
like `master` except for testing purpose. Note that every modules in this repository are available on the Terraform
[registry](https://registry.terraform.io/modules/claranet/detectors/signalfx) and we recommend using it as source
instead of `git` which is more flexible but less future-proof.

* `environment`: Use this parameter to specify the
[environment](https://github.com/claranet/terraform-signalfx-detectors/wiki/Getting-started#environment) used by this
instance of the module.
Its value will be added to the `prefixes` list at the start of the [detector
name](https://github.com/claranet/terraform-signalfx-detectors/wiki/Templating#example).
In general, it will also be used in the `filtering` internal sub-module to [apply
filters](https://github.com/claranet/terraform-signalfx-detectors/wiki/Guidance#filtering) based on our default
[tagging convention](https://github.com/claranet/terraform-signalfx-detectors/wiki/Tagging-convention) by default.

* `notifications`: Use this parameter to define where alerts should be sent depending on their severity. It consists
of a Terraform [object](https://www.terraform.io/docs/configuration/types.html#object-) where each key represents an
available [detector rule severity](https://docs.signalfx.com/en/latest/detect-alert/set-up-detectors.html#severity)
and its value is a list of recipients. Every recipients must respect the [detector notification
format](https://registry.terraform.io/providers/splunk-terraform/signalfx/latest/docs/resources/detector#notification-format).
Check the [notification binding](https://github.com/claranet/terraform-signalfx-detectors/wiki/Notifications-binding)
documentation to understand the recommended role of each severity.

These 3 parameters alongs with all variables defined in [common-variables.tf](common-variables.tf) are common to all
[modules](../) in this repository. Other variables, specific to this module, are available in
[variables-gen.tf](variables-gen.tf).
In general, the default configuration "works" but all of these Terraform
[variables](https://www.terraform.io/docs/configuration/variables.html) make it possible to
customize the detectors behavior to better fit your needs.

Most of them represent usual tips and rules detailled in the
[guidance](https://github.com/claranet/terraform-signalfx-detectors/wiki/Guidance) documentation and listed in the
common [variables](https://github.com/claranet/terraform-signalfx-detectors/wiki/Variables) dedicated documentation.

Feel free to explore the [wiki](https://github.com/claranet/terraform-signalfx-detectors/wiki) for more information about
general usage of this repository.

## What are the available detectors in this module?

This module creates the following SignalFx detectors which could contain one or multiple alerting rules:

|Detector|Critical|Major|Minor|Warning|Info|
|---|---|---|---|---|---|
|Systemd-timers heartbeat|-|-|X|-|-|
|Systemd-timers execution delay|-|X|-|-|-|
|Systemd-timers last execution state|-|X|-|-|-|

## How to collect required metrics?

This module uses metrics available from
[monitors](https://docs.signalfx.com/en/latest/integrations/agent/monitors/_monitor-config.html)
available in the [SignalFx Smart
Agent](https://github.com/signalfx/signalfx-agent). Check the [Related documentation](#related-documentation) section for more
information including the official documentation of this monitor.


This module checks Systemd services launched by Systemd timers. The difference with [smart-agent_systemd-services](https://github.com/claranet/terraform-signalfx-detectors/tree/master/modules/smart-agent_systemd-services) is that services launched by timers are not supposed to be always running, and instead can be stopped most of the time.

Detectors are designed to check that the job did not failed (enabled by default), the service have not been removed (disabled by default), and the job have been launched during the past day (disabled by default, you can override the default delay with the `execution_delay_lasting_duration_major` variable).

For instance, to enable all the optional detectors and be alerted if the service haven't been launched during the past week, you can use the following options:
```hcl
execution_delay_disabled = false
heartbeat_disabled = false
execution_delay_lasting_duration_major = "1w"
```

### Examples

You can configure the systemd collector in the agent the following way to report only the state of the `my_daily_job` and `my_weekly_job` services and the metrics needed by the detector:
```yaml
- type: collectd/systemd
services:
- my_daily_job
- my_weekly_job
sendActiveState: true
datapointsToExclude:
- metricNames:
- '*'
- '!gauge.active_state.activating'
- '!gauge.active_state.active'
- '!gauge.substate.failed'
extraMetrics:
- gauge.substate.failed
- gauge.active_state.active
- gauge.active_state.activating
```
### Metrics
To filter only required metrics for the detectors of this module, add the
[datapointsToExclude](https://docs.signalfx.com/en/latest/integrations/agent/filtering.html) parameter to
the corresponding monitor configuration:
```yaml
datapointsToExclude:
- metricNames:
- '*'
- '!gauge.active_state.activating'
- '!gauge.active_state.active'
- '!gauge.substate.failed'

```



## Related documentation

* [Terraform SignalFx provider](https://registry.terraform.io/providers/splunk-terraform/signalfx/latest/docs)
* [Terraform SignalFx detector](https://registry.terraform.io/providers/splunk-terraform/signalfx/latest/docs/resources/detector)
* [Smart Agent monitor collectd/systemd](https://docs.signalfx.com/en/latest/integrations/agent/monitors/collectd-systemd.html)
1 change: 1 addition & 0 deletions modules/smart-agent_systemd-timers/common-filters.tf
1 change: 1 addition & 0 deletions modules/smart-agent_systemd-timers/common-locals.tf
1 change: 1 addition & 0 deletions modules/smart-agent_systemd-timers/common-modules.tf
1 change: 1 addition & 0 deletions modules/smart-agent_systemd-timers/common-variables.tf
1 change: 1 addition & 0 deletions modules/smart-agent_systemd-timers/common-versions.tf
12 changes: 12 additions & 0 deletions modules/smart-agent_systemd-timers/conf/00-heartbeat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module: systemd-timers
name: heartbeat

transformation: false
aggregation: true
disabled: true

signals:
signal:
metric: "gauge.substate.failed"
rules:
minor:
18 changes: 18 additions & 0 deletions modules/smart-agent_systemd-timers/conf/01-execution-delay.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module: systemd-timers
name: "execution delay"
aggregation: true
transformation: true
disabled: true
signals:
A:
metric: gauge.active_state.active
B:
metric: gauge.active_state.activating
signal:
formula: "(A+B)"
rules:
major:
threshold: 1
comparator: "<"
description: "is too long"
lasting_duration: 1d
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module: systemd-timers
name: "last execution state"

transformation: true
aggregation: true

signals:
signal:
metric: "gauge.substate.failed"
rules:
major:
threshold: 0
comparator: ">"
37 changes: 37 additions & 0 deletions modules/smart-agent_systemd-timers/conf/readme.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
documentations:
- name: Smart Agent monitor collectd/systemd
url: https://docs.signalfx.com/en/latest/integrations/agent/monitors/collectd-systemd.html

source_doc: |
This module checks Systemd services launched by Systemd timers. The difference with [smart-agent_systemd-services](https://github.com/claranet/terraform-signalfx-detectors/tree/master/modules/smart-agent_systemd-services) is that services launched by timers are not supposed to be always running, and instead can be stopped most of the time.
Detectors are designed to check that the job did not failed (enabled by default), the service have not been removed (disabled by default), and the job have been launched during the past day (disabled by default, you can override the default delay with the `execution_delay_lasting_duration_major` variable).
For instance, to enable all the optional detectors and be alerted if the service haven't been launched during the past week, you can use the following options:
```hcl
execution_delay_disabled = false
heartbeat_disabled = false
execution_delay_lasting_duration_major = "1w"
```
### Examples
You can configure the systemd collector in the agent the following way to report only the state of the `my_daily_job` and `my_weekly_job` services and the metrics needed by the detector:
```yaml
- type: collectd/systemd
services:
- my_daily_job
- my_weekly_job
sendActiveState: true
datapointsToExclude:
- metricNames:
- '*'
- '!gauge.active_state.activating'
- '!gauge.active_state.active'
- '!gauge.substate.failed'
extraMetrics:
- gauge.substate.failed
- gauge.active_state.active
- gauge.active_state.activating
```
80 changes: 80 additions & 0 deletions modules/smart-agent_systemd-timers/detectors-gen.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
resource "signalfx_detector" "heartbeat" {
name = format("%s %s", local.detector_name_prefix, "Systemd-timers heartbeat")

authorized_writer_teams = var.authorized_writer_teams
teams = try(coalescelist(var.teams, var.authorized_writer_teams), null)
tags = compact(concat(local.common_tags, local.tags, var.extra_tags))

max_delay = 900

program_text = <<-EOF
from signalfx.detectors.not_reporting import not_reporting
signal = data('gauge.substate.failed', filter=${module.filtering.signalflow})${var.heartbeat_aggregation_function}.publish('signal')
not_reporting.detector(stream=signal, resource_identifier=None, duration='${var.heartbeat_timeframe}', auto_resolve_after='${local.heartbeat_auto_resolve_after}').publish('MINOR')
EOF

rule {
description = "has not reported in ${var.heartbeat_timeframe}"
severity = "Minor"
detect_label = "MINOR"
disabled = coalesce(var.heartbeat_disabled, var.detectors_disabled)
notifications = coalescelist(lookup(var.heartbeat_notifications, "minor", []), var.notifications.minor)
runbook_url = try(coalesce(var.heartbeat_runbook_url, var.runbook_url), "")
tip = var.heartbeat_tip
parameterized_subject = var.message_subject == "" ? local.rule_subject_novalue : var.message_subject
parameterized_body = var.message_body == "" ? local.rule_body : var.message_body
}
}

resource "signalfx_detector" "execution_delay" {
name = format("%s %s", local.detector_name_prefix, "Systemd-timers execution delay")

authorized_writer_teams = var.authorized_writer_teams
teams = try(coalescelist(var.teams, var.authorized_writer_teams), null)
tags = compact(concat(local.common_tags, local.tags, var.extra_tags))

program_text = <<-EOF
A = data('gauge.active_state.active', filter=${module.filtering.signalflow})${var.execution_delay_aggregation_function}${var.execution_delay_transformation_function}
B = data('gauge.active_state.activating', filter=${module.filtering.signalflow})${var.execution_delay_aggregation_function}${var.execution_delay_transformation_function}
signal = (A+B).publish('signal')
detect(when(signal < ${var.execution_delay_threshold_major}, lasting=%{if var.execution_delay_lasting_duration_major == null}None%{else}'${var.execution_delay_lasting_duration_major}'%{endif}, at_least=${var.execution_delay_at_least_percentage_major})).publish('MAJOR')
EOF

rule {
description = "is too long < ${var.execution_delay_threshold_major}"
severity = "Major"
detect_label = "MAJOR"
disabled = coalesce(var.execution_delay_disabled, var.detectors_disabled)
notifications = coalescelist(lookup(var.execution_delay_notifications, "major", []), var.notifications.major)
runbook_url = try(coalesce(var.execution_delay_runbook_url, var.runbook_url), "")
tip = var.execution_delay_tip
parameterized_subject = var.message_subject == "" ? local.rule_subject : var.message_subject
parameterized_body = var.message_body == "" ? local.rule_body : var.message_body
}
}

resource "signalfx_detector" "last_execution_state" {
name = format("%s %s", local.detector_name_prefix, "Systemd-timers last execution state")

authorized_writer_teams = var.authorized_writer_teams
teams = try(coalescelist(var.teams, var.authorized_writer_teams), null)
tags = compact(concat(local.common_tags, local.tags, var.extra_tags))

program_text = <<-EOF
signal = data('gauge.substate.failed', filter=${module.filtering.signalflow})${var.last_execution_state_aggregation_function}${var.last_execution_state_transformation_function}.publish('signal')
detect(when(signal > ${var.last_execution_state_threshold_major}, lasting=%{if var.last_execution_state_lasting_duration_major == null}None%{else}'${var.last_execution_state_lasting_duration_major}'%{endif}, at_least=${var.last_execution_state_at_least_percentage_major})).publish('MAJOR')
EOF

rule {
description = "is too high > ${var.last_execution_state_threshold_major}"
severity = "Major"
detect_label = "MAJOR"
disabled = coalesce(var.last_execution_state_disabled, var.detectors_disabled)
notifications = coalescelist(lookup(var.last_execution_state_notifications, "major", []), var.notifications.major)
runbook_url = try(coalesce(var.last_execution_state_runbook_url, var.runbook_url), "")
tip = var.last_execution_state_tip
parameterized_subject = var.message_subject == "" ? local.rule_subject : var.message_subject
parameterized_body = var.message_body == "" ? local.rule_body : var.message_body
}
}

15 changes: 15 additions & 0 deletions modules/smart-agent_systemd-timers/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
output "execution_delay" {
description = "Detector resource for execution_delay"
value = signalfx_detector.execution_delay
}

output "heartbeat" {
description = "Detector resource for heartbeat"
value = signalfx_detector.heartbeat
}

output "last_execution_state" {
description = "Detector resource for last_execution_state"
value = signalfx_detector.last_execution_state
}

3 changes: 3 additions & 0 deletions modules/smart-agent_systemd-timers/tags.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
tags = ["smart-agent", "systemd-timers"]
}
Loading

0 comments on commit e7c6c83

Please sign in to comment.