{"id":8589,"date":"2021-04-11T00:14:58","date_gmt":"2021-04-10T21:14:58","guid":{"rendered":"https:\/\/kifarunix.com\/?p=8589"},"modified":"2024-03-19T08:21:36","modified_gmt":"2024-03-19T05:21:36","slug":"configure-prometheus-email-alerting-with-alertmanager","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/configure-prometheus-email-alerting-with-alertmanager\/","title":{"rendered":"Configure Prometheus Email Alerting with AlertManager"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to configure Prometheus Email alerting with AlertManager. <a aria-label=\"AlertManager (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/prometheus.io\/docs\/alerting\/latest\/alertmanager\/\" target=\"_blank\" rel=\"noreferrer noopener\">AlertManager<\/a> is used to <em>handle alerts sent by client applications such as the Prometheus server. It takes care of deduplicating, grouping, and routing them to the correct receiver integration such as email, PagerDuty, or OpsGenie. It also takes care of silencing and inhibition of alerts<\/em>.<\/p>\n\n\n\n<p>There is more to Prometheus! Check the link below;<\/p>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.amazon.com\/gp\/product\/1492034142\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1492034142&amp;linkCode=as2&amp;tag=unixkip-20&amp;linkId=ad8e4c52d97f6364fd2fba5bc30dd789\" rel=\"noreferrer noopener\">Prometheus: Up &amp; Running: Infrastructure and Application Performance Monitoring<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring Prometheus Email Alerting with AlertManager<\/h2>\n\n\n\n<p>In our previous guide, we learnt how to <a aria-label=\"monitor SSL\/TLS certificate expiry using Prometheus and Grafana (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/monitor-ssl-tls-certificate-expiry-with-prometheus-and-grafana\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">monitor SSL\/TLS certificate expiry using Prometheus and Grafana<\/a>.<\/p>\n\n\n\n<p>We integrated Telegraf with Prometheus for SSL\/TLS certificate.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1900\" height=\"493\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/check-ssl-monitoring-telegraf.png\" alt=\"\" class=\"wp-image-8640\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/check-ssl-monitoring-telegraf.png?v=1618077611 1900w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/check-ssl-monitoring-telegraf-768x199.png?v=1618077611 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/check-ssl-monitoring-telegraf-1536x399.png?v=1618077611 1536w\" sizes=\"(max-width: 1900px) 100vw, 1900px\" \/><\/figure>\n\n\n\n<p>From the above screenshot, you can see that you have <strong><code>2591897<\/code><\/strong> seconds, which is equivalent to <strong><code>~30 days (a month)<\/code><\/strong> before the certificate expires.<\/p>\n\n\n\n<p>With that duration of time, one might end up forgetting that SSL\/TLS certificates expires and hence may forget to renew the certificate. That is why it is important to configure Prometheus Email alerting such that, when a few days are due for certificate renewal, you can be notified via email.<\/p>\n\n\n\n<p>So, assuming that you already have Prometheus up and running, how can you use AlertMnager to configure Prometheus Email alerting?<\/p>\n\n\n\n<p>Therefore, open Prometheus configuration file and set alertmanager configurations, the Promtheus alert rules files as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/prometheus\/prometheus.yml<\/code><\/pre>\n\n\n\n<p>Update the lines below accordingly;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Alertmanager configuration\nalerting:\n  alertmanagers:\n  - static_configs:\n    - targets:\n       - localhost:9093\n\n# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.\nrule_files:\n   - \"alert_rules.yml\"<\/code><\/pre>\n\n\n\n<p>Without comment lines, this is how our Prometheus configuration file looks like;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>global:\n  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.\n  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.\n<strong>alerting:\n  alertmanagers:\n  - static_configs:\n    - targets:\n       - localhost:9093\nrule_files:\n   - \"alert_rules.yml\"<\/strong>\nscrape_configs:\n  - job_name: 'prometheus'\n    static_configs:\n    - targets: ['localhost:9090']\n  - job_name: 'SSL\/TLS Certs'\n    static_configs:\n    - targets: ['192.168.59.12:9273']<\/code><\/pre>\n\n\n\n<p>Save and exit the configuration file.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create Prometheus Alert Rules<\/h4>\n\n\n\n<p>&#8220;<em>Alerting rules allow you to define alert conditions based on Prometheus expression language expressions and to send notifications about firing alerts to an external service. Whenever the alert expression results in one or more vector elements at a given point in time, the alert counts as active for these elements&#8217; label sets<\/em>&#8220;.<\/p>\n\n\n\n<p>In our configuration file, we defined the Prometheus rules file as <code><strong>alert_rules.yml<\/strong><\/code>. This file should reside within the Prometheus configurations directory, <code><strong>\/etc\/prometheus<\/strong><\/code>.<\/p>\n\n\n\n<p>Hence, create the rules file with the content similar to below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/prometheus\/alert_rules.yml<\/code><\/pre>\n\n\n\n<p>Note that in this example, we will be creating rule to alert us when the SSL\/TLS certificate is due to expire in a few days.<\/p>\n\n\n\n<p>As shown above, we have a certificate that is due for renewal in the next 30 days. To make this demo easy, we will create a rule to alert when have 30 or less days to certificate renewal.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>groups:\n- name: alert_rules\n  rules:\n  - alert: SSL_TLS_Cert_Expiry\n    expr: x509_cert_expiry{job=\"SSL\/TLS Certs\"} &lt;= 2592000 \n    for: 1m\n    labels:\n      severity: critical\n    annotations:\n      summary: SSL\/TLS Certificate Expiry<\/code><\/pre>\n\n\n\n<p>Various setting values have been explained <a aria-label=\"here (opens in a new tab)\" href=\"https:\/\/prometheus.io\/docs\/prometheus\/latest\/configuration\/recording_rules\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">here<\/a>.<\/p>\n\n\n\n<p>Save and exit the file.<\/p>\n\n\n\n<p>Check if the rule files are valid or not;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>promtool check rules \/etc\/prometheus\/alert_rules.yml<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Checking \/etc\/prometheus\/alert_rules.yml\n  SUCCESS: 1 rules found<\/code><\/pre>\n\n\n\n<p>If you check on Prometheus web interface Rules page;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1786\" height=\"492\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/prometheus-alert-rules.png\" alt=\"Configure Prometheus Email Alerting with AlertManager\" class=\"wp-image-8642\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/prometheus-alert-rules.png?v=1618086784 1786w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/prometheus-alert-rules-768x212.png?v=1618086784 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/prometheus-alert-rules-1536x423.png?v=1618086784 1536w\" sizes=\"(max-width: 1786px) 100vw, 1786px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Install and Configure AlertManager<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Install AlertManager<\/h4>\n\n\n\n<p>AlertManager can be installed using pre-compiled binaries that can be downloaded from Prometheus&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/prometheus.io\/download\/\" target=\"_blank\" class=\"rank-math-link\">downloads section<\/a>.<\/p>\n\n\n\n<p>Hence, before you can install AlertManager;<\/p>\n\n\n\n<p>Create AlertManager system user and group as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>useradd -M -r -s \/bin\/false alertmanager<\/code><\/pre>\n\n\n\n<p>Next, navigate to the downloads section and grab the latest version of AlertManager. You simply use <strong>wget<\/strong> to download it. The current release version as of this writing is 0.21.0.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VER=0.21.0<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>wget https:\/\/github.com\/prometheus\/alertmanager\/releases\/download\/v$VER\/alertmanager-$VER.linux-amd64.tar.gz<\/code><\/pre>\n\n\n\n<p>Extract the downloaded binary;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar xzf alertmanager-0.21.0.linux-amd64.tar.gz<\/code><\/pre>\n\n\n\n<p>Copy the AlertManager binary files, <code><strong>alertmanager<\/strong><\/code> and <code><strong>amtool<\/strong><\/code> to binary directory like <code><strong>\/usr\/local\/bin\/<\/strong><\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp alertmanager-0.21.0.linux-amd64\/{alertmanager,amtool} \/usr\/local\/bin\/<\/code><\/pre>\n\n\n\n<p>Next, create a configuration directory for AlertManager and copy the YAML configuration to that directory;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp alertmanager-0.21.0.linux-amd64\/alertmanager.yml \/etc\/alertmanager\/<\/code><\/pre>\n\n\n\n<p>Set the ownership of the AlertManager configuration directory and the binaries to alertmanager user created above;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>chown alertmanager: \/etc\/alertmanager\/alertmanager.yml \/usr\/local\/bin\/{alertmanager,amtool}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure AlertManager<\/h4>\n\n\n\n<p>In this setup, we will be sending the alerts via Email and we will use Gmail relay in that case.<\/p>\n\n\n\n<p>Hence, configure AlertManager as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/alertmanager\/alertmanager.yml<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>global:\n  resolve_timeout: 5m\n\nroute:\n  group_by: &#91;'alertname']\n  group_wait: 10s\n  group_interval: 10s\n  repeat_interval: 24h\n  receiver: 'email'\nreceivers:\n- name: 'email'\n  email_configs:\n  - to: 'recipient@gmail.com'\n    from: 'email_id@gmail.com'\n    smarthost: smtp.gmail.com:587\n    auth_username: 'email_id@gmail.com'\n    auth_identity: 'email_id@gmail.com'\n    auth_password: 'password'<\/code><\/pre>\n\n\n\n<p>Save and exit the configuration file. Be sure to set the email settings appropriately.<\/p>\n\n\n\n<p>More about the config on <a aria-label=\"configuration (opens in a new tab)\" href=\"https:\/\/prometheus.io\/docs\/alerting\/latest\/configuration\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">configuration<\/a> page and <a href=\"https:\/\/github.com\/prometheus\/alertmanager\" target=\"_blank\" aria-label=\"git repo (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">git repo<\/a> page.<\/p>\n\n\n\n<p>Check the Alertmanager configuration file to validate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>amtool check-config \/etc\/alertmanager\/alertmanager.yml<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Checking '\/etc\/alertmanager\/alertmanager.yml'  SUCCESS\nFound:\n - global config\n - route\n - 0 inhibit rules\n - 1 receivers\n - 0 templates<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Running AlertManager<\/h3>\n\n\n\n<p>You can run Alertmanager in standalone mode by executing the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>alertmanager --config.file \/etc\/alertmanager\/alertmanager.yml<\/code><\/pre>\n\n\n\n<p>Remember, we set Prometheus to connect to Alertmanager via <code><strong>localhost:9093<\/strong><\/code>, hence replace the <code><strong>x.x.x.x<\/strong><\/code> with the correct address.<\/p>\n\n\n\n<p>To run AlertManager as a service;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt; \/etc\/systemd\/system\/alertmanager.service &lt;&lt; 'EOL'\n&#91;Unit]\nDescription=AlertManager Server Service\nWants=network-online.target\nAfter=network-online.target\n\n&#91;Service]\nUser=root\nGroup=root\nType=simple\nExecStart=\/usr\/local\/bin\/alertmanager --config.file \/etc\/alertmanager\/alertmanager.yml\n\n&#91;Install]\nWantedBy=multi-user.target\nEOL<\/code><\/pre>\n\n\n\n<p>For other options, consult <strong><code>\/usr\/local\/bin\/alertmanager --help<\/code><\/strong>.<\/p>\n\n\n\n<p>Reload systemd configurations and start Alertmanager;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl daemon-reload<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl enable --now alertmanager<\/code><\/pre>\n\n\n\n<p>Check the status;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status alertmanager<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u25cf alertmanager.service - AlertManager Server Service\n   Loaded: loaded (\/etc\/systemd\/system\/alertmanager.service; enabled; vendor preset: enabled)\n   Active: active (running) since Sat 2021-04-10 23:16:25 EAT; 1s ago\n Main PID: 3959 (alertmanager)\n    Tasks: 8 (limit: 2359)\n   Memory: 13.5M\n   CGroup: \/system.slice\/alertmanager.service\n           \u2514\u25003959 \/usr\/local\/bin\/alertmanager --config.file \/etc\/alertmanager\/alertmanager.yml --web.external-url=http:\/\/localhost:9093\n\nApr 10 23:16:25 debian systemd&#91;1]: Started AlertManager Server Service.\n...\n...\nApr 10 23:16:25 debian alertmanager&#91;3959]: level=info ts=2021-04-10T20:16:25.745Z caller=main.go:485 msg=Listening address=:9093<\/code><\/pre>\n\n\n\n<p>Check Prometheus Alerts page;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1907\" height=\"652\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/prometheus-alertmanager-alerts-firing.png\" alt=\"\" class=\"wp-image-8643\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/prometheus-alertmanager-alerts-firing.png?v=1618087422 1907w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/prometheus-alertmanager-alerts-firing-768x263.png?v=1618087422 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/prometheus-alertmanager-alerts-firing-1536x525.png?v=1618087422 1536w\" sizes=\"(max-width: 1907px) 100vw, 1907px\" \/><\/figure>\n\n\n\n<p>Also check your mail;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1022\" height=\"790\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/alertmanager-email-alert.png\" alt=\"\" class=\"wp-image-8644\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/alertmanager-email-alert.png?v=1618088069 1022w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/alertmanager-email-alert-768x594.png?v=1618088069 768w\" sizes=\"(max-width: 1022px) 100vw, 1022px\" \/><\/figure>\n\n\n\n<p>You can further customize the email template.<\/p>\n\n\n\n<p>You can also view alerts in Alertmanager, <strong>http:\/\/x.x.x.x:9093\/alerts<\/strong>;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1710\" height=\"782\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/Alertmanager-alerts.png\" alt=\"\" class=\"wp-image-8645\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/Alertmanager-alerts.png?v=1618088908 1710w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/Alertmanager-alerts-768x351.png?v=1618088908 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/04\/Alertmanager-alerts-1536x702.png?v=1618088908 1536w\" sizes=\"(max-width: 1710px) 100vw, 1710px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Further Reading<\/h3>\n\n\n\n<p><a href=\"https:\/\/prometheus.io\/docs\/alerting\/latest\/overview\/\" target=\"_blank\" aria-label=\"AlertManager overview (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">AlertManager overview<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/monitor-ssl-tls-certificates-expiry-with-nagios\/\" target=\"_blank\" rel=\"noreferrer noopener\">Monitor SSL\/TLS Certificates Expiry with Nagios<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/monitor-linux-system-metrics-with-prometheus-node-exporter\/\" target=\"_blank\">Monitor Linux System Metrics with Prometheus Node Exporter<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/monitoring-gitlab-metrics-with-prometheus-and-grafana\/\" target=\"_blank\" rel=\"noreferrer noopener\">Monitoring Gitlab Metrics with Prometheus and Grafana<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/monitor-openvpn-connections-with-prometheus-and-grafana\/\" target=\"_blank\" rel=\"noreferrer noopener\">Monitor OpenVPN Connections with Prometheus and Grafana<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-opennms-network-monitoring-tool-on-ubuntu\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install OpenNMS Network Monitoring tool on Ubuntu 20.04<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to configure Prometheus Email alerting with AlertManager. AlertManager is used to handle alerts sent by client applications such<\/p>\n","protected":false},"author":3,"featured_media":8560,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[72,301,121,294],"tags":[3423,3422,3425,3424],"class_list":["post-8589","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-monitoring","category-grafana","category-howtos","category-prometheus","tag-alertmanager-email-alerting","tag-configure-alerting-with-prometheuss","tag-configure-prometheus-email-alerting-with-alertmanager","tag-prometheus-alertmanager","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8589"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=8589"}],"version-history":[{"count":5,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8589\/revisions"}],"predecessor-version":[{"id":21849,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8589\/revisions\/21849"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/8560"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=8589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=8589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=8589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}