Skip to main content

Setting up Prometheus Alertmanager

I have a pretty standard Prometheus, bunch of exporters and Grafana setup at home. This is mostly used to monitor different aspects of my house, like the exporter I have for power usage. However, while trying to figure out the cause of a node exporter crash I found myself in need of an alerting system, so that it could tell me when the node exporter crashed instead of me just checking on a daily basis to see if it had.
Thankfully there’s the Prometheus Alertmanager which neatly integrates into the whole ecosystem I already have. However, configuring it turned out to be a bit more of a pain in the ass, mainly b/c the configuration is split up between Prometheus and Alertmanager itself. And the docs are rather sparse.

Getting Alertmanager up and running

I run most of these components through Docker, so I created a unit file like this:
[Unit]
Description=Prometheus Alert Manager
After=docker.service
Requires=docker.service

[Service]
ExecStartPre=-/usr/bin/docker rm alertmanager
ExecStart=-/usr/bin/docker run \
                           --name alertmanager \
                           --read-only \
                           -p 9093:9093 \
                           --expose 9093 \
                           -v alertmanager_data:/alertmanager:rw \
                           -v /etc/docker/config/alertmanager:/etc/alertmanager/:ro \
                           prom/alertmanager:v0.14.0 \
                           --config.file=/etc/alertmanager/alertmanager.yml \
                           --storage.path=/alertmanager \
                           --mesh.listen-address="" \
                           --web.external-url=https://your.domain.tld/alertmanager
ExecStop=-/usr/bin/docker stop alertmanager

[Install]
WantedBy=multi-user.target
Don’t forget to also create the volume with a docker volume create alertmanager_data, and adjust the path to where you want to store the configuration file on the host too. I’ve put Alertmanager behind a reverse proxy, hence the --web.external-url parameter. I’m also disabling mesh by setting --mesh.listen-address="" b/c I don’t have more than a single Alertmanager running at home.
Now, lets start with a very basic Alertmanager config, enough for it to start but not push alerts to anywhere just yet. Put this in alertmanager.yml:
route:
  receiver: dummy

receivers:
  - name: dummy
And now systemctl daemon-reload; sytemctl start alertmanager. That’s all you need. Take a look at journalctl -u alertmanager and ensure it starts up. At this point you can reach it on http://localhost:9093 or whatever you set --web.external-url to, but don’t forget to configure the reverse proxy first.

Hooking up Prometheus and Alertmanager

Next, we need to actually tell Prometheus that there’s an alertmanager it can use when one of the alerting rules trigger. To that end, update your prometheus.yml with this:
rule_files:
  - 'alerts.yml'

alerting:
  alertmanagers:
    - scheme: http
      path_prefix: /alertmanager
      static_configs:
        - targets: ['ip-of-alertmanager:9093']

I’m using a static_configs here to manually specify the target, but you can use all the usual discovery mechanisms available to you in Prometheus too. The rule_files is an array of files where we can find our alerting definitions. When a relative path is given, it’s relative to the location of prometheus.yml. Also note that you don’t need path_prefix if you haven’t specified --web.external-url or if --web.external-url does not include a path.
With that done, you can create an empty alerts.yml and restart Prometheus.

With their powers combined

Alright, so the reason I went down this road in the first place is because I wanted to be informed when a node exporter stopped reporting in. This rarely happens at home and either means the network is severely broken, or the exporter is dead.
To that end I added an alert in alerts.yml like this:
groups:
  - name: default
    rules:
    - alert: InstanceDown
      expr: up == 0
      for: 5m
      labels:
        severity: critical
      annotations:
        summary: "Instance {{ $labels.instance }} down"
        description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
        
Alerts, like recording rules, are grouped in groups. You need at least 1 group and you can set the name to whatever floats your boat. Then you’ll have to add 1 or more rules which we do by providing an array of alerting rules.
I configure my alerts in the v2/YAML format, but there’s also the older v1 format. You’ll likely run into it if you find blog posts with examples from before mid-2017. v1 looks more like SQL statements, but maps really easily onto the v2 format.
In this case, the alert is named InstanceDown and in order to figure out if that’s the case it executes the up == 0 PromQL expr. It’ll fire once this is true for more than 5 minutes and attaches a severity label of critical to the alert. Finally we specify the summary and description of the alert, which are passed on to Alertmanager.
This works, but… we haven’t configured Alertmanager to poke us yet, so right now all you’ll see is the alert in Alertmanager’s web UI. In my case I wanted to use Pushover, so I created a new application in my account, put the token and user key in my alertmanager.yml and restarted it:
route:
  receiver: pushover

receivers:
  - name: pushover
    pushover_configs:
      - token: app token
        user_key: your user key
As you can see pushover_configs is an array too, you can repeat that block multiple times to add more people/systems to push to. You can also have more types of receivers in a single receiver. There’s nothing stopping you from also adding a pagerduty_configs and email_configs to the same receiver, though you should rename the receiver at that point.
Since I don’t specify any route matches for any alert, Alertmanager will push every alert it gets through the default receiver, the one configured in the routesection.
Last but not least, probably want to test this. There’s no button to fire off a dummy alert, but curl will do the trick:
curl -H "Content-Type: application/json" -d '[{"status": "firing", "labels":{"alertname":"TestAlert1"}}]' localhost:9093/alertmanager/api/v1/alerts
In a couple of seconds you’ll get a push. B/c I set status: firing it’ll come in as a high priority alert through Pushover that you’ll have to explicitly acknowledge.

Next steps

This is just the tip of the iceberg of what you can do with Prometheus’ alerting rules, paired with Alertmanager’s routing capabilities. At this point you have a working setup you can now experiment with.
The full capabilities of alerting rules are documented here, and over here you can find the docs on routing rules in Alertmanager. I highly recommend using the Routing Tree Editor/Visualiser as you go along. Do be careful about pasting secrets in there (the tree is computed using JS though, so nothing should leave your system).

Comments

Popular posts from this blog

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checking a shared sec

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh , but without the use of k

标 题: 关于Daniel Guo 律师

发信人: q123452017 (水天一色), 信区: I140 标  题: 关于Daniel Guo 律师 关键字: Daniel Guo 发信站: BBS 未名空间站 (Thu Apr 26 02:11:35 2018, 美东) 这些是lz根据亲身经历在 Immigration版上发的帖以及一些关于Daniel Guo 律师的回 帖,希望大家不要被一些马甲帖广告帖所骗,慎重考虑选择律师。 WG 和Guo两家律师对比 1. fully refund的合约上的区别 wegreened家是case不过只要第二次没有file就可以fully refund。郭家是要两次case 没过才给refund,而且只要第二次pl draft好律师就可以不退任何律师费。 2. 回信速度 wegreened家一般24小时内回信。郭律师是在可以快速回复的时候才回复很快,对于需 要时间回复或者是不愿意给出确切答复的时候就回复的比较慢。 比如:lz问过郭律师他们律所在nsc区域最近eb1a的通过率,大家也知道nsc现在杀手如 云,但是郭律师过了两天只回复说让秘书update最近的case然后去网页上查,但是上面 并没有写明tsc还是nsc。 lz还问过郭律师关于准备ps (他要求的文件)的一些问题,模版上有的东西不是很清 楚,但是他一般就是把模版上的东西再copy一遍发过来。 3. 材料区别 (推荐信) 因为我只收到郭律师写的推荐信,所以可以比下两家推荐信 wegreened家推荐信写的比较长,而且每封推荐信会用不同的语气和风格,会包含lz写 的research summary里面的某个方面 郭家四封推荐信都是一个格式,一种语气,连地址,信的称呼都是一样的,怎么看四封 推荐信都是同一个人写出来的。套路基本都是第一段目的,第二段介绍推荐人,第三段 某篇或几篇文章的abstract,最后结论 4. 前期材料准备 wegreened家要按照他们的模版准备一个十几页的research summary。 郭律师在签约之前说的是只需要准备五页左右的summary,但是在lz签完约收到推荐信 ,郭律师又发来一个很长的ps要lz自己填,而且和pl的格式基本差不多。 总结下来,申请自己上心最重要。但是如果选律师,lz更倾向于wegreened,