Role service-prometheus: simplify and move all files to role directory

This commit is contained in:
Julian Labus 2019-02-15 15:45:38 +01:00
parent 603f6af64d
commit 7a91bbaa2f
No known key found for this signature in database
GPG Key ID: 8AF209F2C6B3572A
9 changed files with 79 additions and 377 deletions

View File

@ -14,13 +14,6 @@ http_lookingglass_prefix: lg
http_prometheus_prefix: prom
http_openlayers_prefix: ol
prometheus_conf_main: prometheus/prometheus.yml.j2
prometheus_rule_files:
alert_rules:
src: prometheus/alert.rules
dest: alert.rules
prometheus_components:
- prometheus
- alertmanager

View File

@ -1,92 +0,0 @@
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # By default, scrape targets every 15 seconds.
# scrape_timeout is set to the global default (10s).
alerting:
alertmanagers:
- scheme: https
path_prefix: /alertmanager/
static_configs:
- targets: ['{{ http_prometheus_prefix }}.{{ http_domain_external }}']
{% if prometheus_rule_files is defined %}
# Rule files specifies a list of files from which rules are read.
rule_files:
{% for (key, value) in prometheus_rule_files.items() %}
- {{ prometheus_rule_path }}/{{ value.dest }}
{% endfor %}
{% endif %}
# A list of scrape configurations.
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 10s
scrape_timeout: 10s
static_configs:
- targets: ['localhost:9090']
- job_name: "node"
scheme: "https"
static_configs:
{%for group in prometheus_groups %}
- targets:
{% for host in groups[group] %}
- '{{ host }}:9100'
{% endfor %}
labels:
group: '{{ group }}'
{% endfor %}
relabel_configs:
- source_labels: [__address__]
regex: '([a-z]+)\..*'
replacement: '$1'
target_label: hostname
- job_name: "fastd"
scheme: "https"
static_configs:
- targets:
{% for host in groups['ffmwu-gateways'] %}
- '{{ host }}:9281'
{% endfor %}
relabel_configs:
- source_labels: [__address__]
regex: '([a-z]+)\..*'
replacement: '$1'
target_label: hostname
{% for job in ['icmp4','icmp6'] %}
- job_name: "{{ job }}"
metrics_path: /probe
params:
module: ["{{ job }}"]
static_configs:
- targets:
{% for group in prometheus_groups %}
{% for host in groups[group] %}
{% if host != inventory_hostname %}
- "{{ host.rsplit('.')[0] }}.ffwi.org"
- "{{ host.rsplit('.')[0] }}.ffmz.org"
{% endif %}
{% endfor %}
{% endfor %}
{% for host, _ in bgp_mwu_servers.items() %}
{% if host not in ['extrasahne'] %}
- "{{ host }}.ffwi.org"
- "{{ host }}.ffmz.org"
{% endif %}
{% endfor %}
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115
- source_labels: [__param_target]
regex: '([a-z]+)\..*'
replacement: '$1'
target_label: hostname
{% endfor %}

View File

@ -1,236 +1,14 @@
FFMWU prometheus
============
## Summary
# Ansible role service-prometheus
Prometheus ansible role based on **[williamyeh.prometheus](https://galaxy.ansible.com/williamyeh/prometheus/)**
This Ansible role has the following features for [Prometheus](http://prometheus.io/):
- Install specific versions of [Prometheus server](https://github.com/prometheus/prometheus), [Node exporter](https://github.com/prometheus/node_exporter), [Alertmanager](https://github.com/prometheus/alertmanager).
- Install specific versions of [Prometheus server](https://github.com/prometheus/prometheus), [Node exporter](https://github.com/prometheus/node_exporter), [Blackbox exporter](https://github.com/prometheus/blackbox_exporter), [Alertmanager](https://github.com/prometheus/alertmanager).
- Handlers for restart/reload/stop events;
- Bare bone configuration (*real* configuration should be left to user's template files; see **Usage** section below).
## Role Variables
### Mandatory variables
The components to be installed:
```yaml
# Supported components:
#
# [Server components]
# - "prometheus"
# - "alertmanager"
#
# [Exporter components]
# - "node_exporter"
#
prometheus_components
```
### Optional variables: general settings
User-configurable defaults:
```yaml
# user and group
prometheus_user: prometheus
prometheus_group: prometheus
# directory for executable files
prometheus_install_path: /opt/prometheus
# directory for configuration files
prometheus_config_path: /etc/prometheus
# directory for temporary files
prometheus_download_path: /tmp
# version of helper utility "gosu"
gosu_version: "1.10"
```
### Optional variables: Prometheus server
User-configurable defaults:
```yaml
# which version?
prometheus_version: 2.0.0
# directory for rule files
prometheus_rule_path: {{ prometheus_config_path }}/rules
# directory for file_sd files
prometheus_file_sd_config_path: {{ prometheus_config_path }}/tgroups
# directory for runtime database
prometheus_db_path: /var/lib/prometheus
```
User-installable configuration file (see [doc](http://prometheus.io/docs/operating/configuration/) for details):
```yaml
# main conf template relative to `playbook_dir`;
# to be installed to "{{ prometheus_config_path }}/prometheus.yml"
prometheus_conf_main
```
User-installable rule files (see [doc](http://prometheus.io/docs/alerting/rules/) for details):
```yaml
# rule files to be installed to "{{ prometheus_rule_path }}" directory;
# dict fields:
# - key: memo for this rule
# - value:
# - src: file relative to `playbook_dir`
# - dest: target file relative to `{{ prometheus_rule_path }}`
prometheus_rule_files
```
Additional command-line arguments, if any (use `prometheus --help` to see the full list of arguments):
```yaml
prometheus_opts
```
### Optional variables: Node exporter
User-configurable defaults:
```yaml
# which version?
node_exporter_version: 0.15.1
```
Additional command-line arguments, if any (use `node_exporter --help` to see the full list of arguments):
```yaml
node_exporter_opts
```
### Optional variables: Alertmanager
User-configurable defaults:
```yaml
# which version?
alertmanager_version: 0.10.0
# directory for runtime database (currently for `silences.json`)
alertmanager_db_path: /var/lib/alertmanager
```
User-installable alertmanager conf file (see [doc](http://prometheus.io/docs/alerting/alertmanager/) for details):
See files directory alertmanager.yml
Additional command-line arguments, if any (use `alertmanager --help` to see the full list of arguments):
```yaml
prometheus_alertmanager_opts
```
## Handlers
Prometheus server:
- `reload prometheus`
Alertmanager:
- `reload alertmanager`
## Usage
### Step 1: add role
Add role name `service-prometheus` to your playbook file.
### Step 2: add variables
Set vars in your playbook file, if necessary.
Simple example:
```yaml
---
# file: simple-playbook.yml
- hosts: all
become: True
roles:
- service-prometheus
vars:
prometheus_components: [ "prometheus", "alertmanager" ]
```
### Step 3: copy user's config files, if necessary
More practical example:
```yaml
---
# file: complex-playbook.yml
- hosts: all
become: True
roles:
- service-prometheus
vars:
prometheus_components:
- prometheus
- node_exporter
- alertmanager
prometheus_rule_files:
this_is_rule_1_InstanceDown:
src: some/path/basic.rules
dest: basic.rules
```
### Step 4: browse the default Prometheus pages
Open the page in your browser:
- Prometheus - `http://HOST:9090` or `http://HOST:9090/consoles/node.html`
- Alertmanager - `http://HOST:9093`
## License
MIT License. See the [LICENSE file](LICENSE) for details.

View File

@ -14,14 +14,13 @@ node_exporter_version: 0.17.0
blackbox_exporter_version: 0.13.0
alertmanager_version: 0.16.0
gosu_version: "1.10"
prometheus_url_external: "{{ http_prometheus_prefix }}.{{ http_domain_external }}"
prometheus_url_internal: "{{ http_prometheus_prefix }}.{{ http_domain_internal }}"
prometheus_install_path: /opt/prometheus
prometheus_config_path: /etc/prometheus
prometheus_rule_path: "{{ prometheus_config_path }}/rules"
prometheus_rule_files: [ "alert.rules" ]
prometheus_file_sd_config_path: "{{ prometheus_config_path }}/tgroups"
prometheus_db_path: /var/lib/prometheus
alertmanager_db_path: /var/lib/alertmanager

View File

@ -1,19 +0,0 @@
# Install "gosu" utility.
#
# @see https://github.com/tianon/gosu
#
- name: set internal variables for convenience
set_fact:
gosu_bin_url: "https://github.com/tianon/gosu/releases/download/{{ gosu_version }}/gosu-amd64"
- name: download gosu executable
get_url:
url: "{{ gosu_bin_url }}"
dest: "/usr/local/bin/gosu"
- name: add executable permission
file:
path: "/usr/local/bin/gosu"
state: file
mode: "a+x"

View File

@ -25,9 +25,6 @@
- "{{ prometheus_install_path }}"
- "{{ prometheus_config_path }}"
- name: install helper utility "gosu"
include_tasks: install-gosu.yml
- name: install and configure prometheus service
include_tasks: prometheus.yml
when: '"prometheus" in prometheus_components'

View File

@ -53,30 +53,20 @@
- reload systemd
- restart prometheus
- name: copy rule files from playbook's, if any
- name: copy rule files
copy:
src: "{{ playbook_dir }}/{{ item.value.src }}"
dest: "{{ prometheus_rule_path }}/{{ item.value.dest }}"
src: "{{ item }}"
dest: "{{ prometheus_rule_path }}/{{ item }}"
validate: "{{ prometheus_daemon_dir }}/promtool check rules %s"
loop: '{{ prometheus_rule_files | default({}) | dict2items }}'
loop: "{{ prometheus_rule_files | default([]) }}"
notify:
- restart prometheus
- name: copy prometheus main config file from role's default, if necessary
- name: copy prometheus main config
template:
src: "prometheus.yml.j2"
dest: "{{ prometheus_config_path }}/prometheus.yml"
validate: "{{ prometheus_daemon_dir }}/promtool check config %s"
when: prometheus_conf_main is not defined
notify:
- restart prometheus
- name: copy prometheus main config file from playbook's, if any
template:
src: "{{ playbook_dir }}/{{ prometheus_conf_main }}"
dest: "{{ prometheus_config_path }}/prometheus.yml"
validate: "{{ prometheus_daemon_dir }}/promtool check config %s"
when: prometheus_conf_main is defined
notify:
- restart prometheus

View File

@ -3,22 +3,23 @@ global:
evaluation_interval: 15s # By default, scrape targets every 15 seconds.
# scrape_timeout is set to the global default (10s).
# The labels to add to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'master'
alerting:
alertmanagers:
- scheme: https
path_prefix: /alertmanager/
static_configs:
- targets: ['{{ http_prometheus_prefix }}.{{ http_domain_external }}']
{% if prometheus_rule_files is defined %}
# Rule files specifies a list of files from which rules are read.
rule_files:
{% for (key, value) in prometheus_rule_files.items() %}
- {{ prometheus_rule_path }}/{{ value.dest }}
{% for item in prometheus_rule_files %}
- {{ prometheus_rule_path }}/{{ item }}
{% endfor %}
{% endif %}
# A list of scrape configurations.
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 10s
scrape_timeout: 10s
@ -26,11 +27,66 @@ scrape_configs:
- targets: ['localhost:9090']
- job_name: "node"
file_sd_configs:
- files:
- '{{ prometheus_file_sd_config_path }}/*.json'
- '{{ prometheus_file_sd_config_path }}/*.yml'
- '{{ prometheus_file_sd_config_path }}/*.yaml'
#static_configs:
#- targets:
# - "localhost:9100"
scheme: "https"
static_configs:
{%for group in prometheus_groups %}
- targets:
{% for host in groups[group] %}
- '{{ host }}:9100'
{% endfor %}
labels:
group: '{{ group }}'
{% endfor %}
relabel_configs:
- source_labels: [__address__]
regex: '([a-z]+)\..*'
replacement: '$1'
target_label: hostname
- job_name: "fastd"
scheme: "https"
static_configs:
- targets:
{% for host in groups['ffmwu-gateways'] %}
- '{{ host }}:9281'
{% endfor %}
relabel_configs:
- source_labels: [__address__]
regex: '([a-z]+)\..*'
replacement: '$1'
target_label: hostname
{% for job in ['icmp4','icmp6'] %}
- job_name: "{{ job }}"
metrics_path: /probe
params:
module: ["{{ job }}"]
static_configs:
- targets:
{% for group in prometheus_groups %}
{% for host in groups[group] %}
{% if host != inventory_hostname %}
- "{{ host.rsplit('.')[0] }}.ffwi.org"
- "{{ host.rsplit('.')[0] }}.ffmz.org"
{% endif %}
{% endfor %}
{% endfor %}
{% for host, _ in bgp_mwu_servers.items() %}
{% if host not in ['extrasahne'] %}
- "{{ host }}.ffwi.org"
- "{{ host }}.ffmz.org"
{% endif %}
{% endfor %}
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115
- source_labels: [__param_target]
regex: '([a-z]+)\..*'
replacement: '$1'
target_label: hostname
{% endfor %}