告警配置

通常我们可以使用运行参数 -alertmanager.xxx 来配置 Alertmanager, 但是这样不够灵活,没有办法做到动态更新加载,以及动态定义告警属性。

所以 alerting 配置主要用来解决这个问题,它能够更好的管理 Alertmanager, 主要包含 2 个参数:

  • alert_relabel_configs: 动态修改 alert 属性的规则配置。
  • alertmanagers: 用于动态发现 Alertmanager 的配置。

其代码结构体定义为:

  1. // AlertingConfig configures alerting and alertmanager related configs.
  2. type AlertingConfig struct {
  3. AlertRelabelConfigs []*RelabelConfig `yaml:"alert_relabel_configs,omitempty"`
  4. AlertmanagerConfigs []*AlertmanagerConfig `yaml:"alertmanagers,omitempty"`
  5. // Catches all undefined fields and must be empty after parsing.
  6. XXX map[string]interface{} `yaml:",inline"`
  7. }

配置文件结构大概为:

  1. # Alerting specifies settings related to the Alertmanager.
  2. alerting:
  3. alert_relabel_configs:
  4. [ - <relabel_config> ... ]
  5. alertmanagers:
  6. [ - <alertmanager_config> ... ]

其中 alertmanagers 为 alertmanager_config 数组,而 alertmanager_config 的代码结构体为,

  1. // AlertmanagerConfig configures how Alertmanagers can be discovered and communicated with.
  2. type AlertmanagerConfig struct {
  3. // We cannot do proper Go type embedding below as the parser will then parse
  4. // values arbitrarily into the overflow maps of further-down types.
  5. ServiceDiscoveryConfig ServiceDiscoveryConfig `yaml:",inline"`
  6. HTTPClientConfig HTTPClientConfig `yaml:",inline"`
  7. // The URL scheme to use when talking to Alertmanagers.
  8. Scheme string `yaml:"scheme,omitempty"`
  9. // Path prefix to add in front of the push endpoint path.
  10. PathPrefix string `yaml:"path_prefix,omitempty"`
  11. // The timeout used when sending alerts.
  12. Timeout time.Duration `yaml:"timeout,omitempty"`
  13. // List of Alertmanager relabel configurations.
  14. RelabelConfigs []*RelabelConfig `yaml:"relabel_configs,omitempty"`
  15. // Catches all undefined fields and must be empty after parsing.
  16. XXX map[string]interface{} `yaml:",inline"`
  17. }

配置文件结构大概为:

  1. # Per-target Alertmanager timeout when pushing alerts.
  2. [ timeout: <duration> | default = 10s ]
  3. # Prefix for the HTTP path alerts are pushed to.
  4. [ path_prefix: <path> | default = / ]
  5. # Configures the protocol scheme used for requests.
  6. [ scheme: <scheme> | default = http ]
  7. # Sets the `Authorization` header on every request with the
  8. # configured username and password.
  9. basic_auth:
  10. [ username: <string> ]
  11. [ password: <string> ]
  12. # Sets the `Authorization` header on every request with
  13. # the configured bearer token. It is mutually exclusive with `bearer_token_file`.
  14. [ bearer_token: <string> ]
  15. # Sets the `Authorization` header on every request with the bearer token
  16. # read from the configured file. It is mutually exclusive with `bearer_token`.
  17. [ bearer_token_file: /path/to/bearer/token/file ]
  18. # Configures the scrape request's TLS settings.
  19. tls_config:
  20. [ <tls_config> ]
  21. # Optional proxy URL.
  22. [ proxy_url: <string> ]
  23. # List of Azure service discovery configurations.
  24. azure_sd_configs:
  25. [ - <azure_sd_config> ... ]
  26. # List of Consul service discovery configurations.
  27. consul_sd_configs:
  28. [ - <consul_sd_config> ... ]
  29. # List of DNS service discovery configurations.
  30. dns_sd_configs:
  31. [ - <dns_sd_config> ... ]
  32. # List of EC2 service discovery configurations.
  33. ec2_sd_configs:
  34. [ - <ec2_sd_config> ... ]
  35. # List of file service discovery configurations.
  36. file_sd_configs:
  37. [ - <file_sd_config> ... ]
  38. # List of GCE service discovery configurations.
  39. gce_sd_configs:
  40. [ - <gce_sd_config> ... ]
  41. # List of Kubernetes service discovery configurations.
  42. kubernetes_sd_configs:
  43. [ - <kubernetes_sd_config> ... ]
  44. # List of Marathon service discovery configurations.
  45. marathon_sd_configs:
  46. [ - <marathon_sd_config> ... ]
  47. # List of AirBnB's Nerve service discovery configurations.
  48. nerve_sd_configs:
  49. [ - <nerve_sd_config> ... ]
  50. # List of Zookeeper Serverset service discovery configurations.
  51. serverset_sd_configs:
  52. [ - <serverset_sd_config> ... ]
  53. # List of Triton service discovery configurations.
  54. triton_sd_configs:
  55. [ - <triton_sd_config> ... ]
  56. # List of labeled statically configured Alertmanagers.
  57. static_configs:
  58. [ - <static_config> ... ]
  59. # List of Alertmanager relabel configurations.
  60. relabel_configs:
  61. [ - <relabel_config> ... ]