Skip to content

Instantly share code, notes, and snippets.

@weldpua2008
Created August 15, 2018 06:16
Show Gist options
  • Save weldpua2008/6a6608915182e8b38601fb3eb0439440 to your computer and use it in GitHub Desktop.
Save weldpua2008/6a6608915182e8b38601fb3eb0439440 to your computer and use it in GitHub Desktop.
Ansible template from prometheus
## {{ ansible_managed }}
# Ansible vars
# prometheus_jobs:
# - default
# add_to_prometheus: yes
# prometheus_pattern_regex: "backend-.*"
# prometheus_group: "BackEnd"
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first.rules"
# - "second.rules"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['127.0.0.1:9090']
{% set my_hosts = groups["all"][:] %}
{%for job in prometheus_jobs %}
- job_name: {{job}}_instances
{% if job == 'default' %}
metrics_path: '/metrics'
{% else %}
metrics_path: '/metrics_{{job}}'
{% endif %}
static_configs:
{%for host in my_hosts %}
{% if 'add_to_prometheus' in hostvars[host] and hostvars[host]['add_to_prometheus'] and 'openresty_structure' in hostvars[host] and hostvars[host]['openresty_structure'] == 'v2' and 'openresty_instances' in hostvars[host] and hostvars[host]['openresty_instances'] %}
{% for job_target in hostvars[host]['openresty_instances'] %}
{% if job == job_target %}
- targets: ['{{host}}:{{prometheus_port|default('60000')}}']
{% endif %}
{%endfor%}
{% elif ('add_to_prometheus' in hostvars[host] and hostvars[host]['add_to_prometheus'] and 'openresty_structure' not in hostvars[host] and job == 'default') or ('add_to_prometheus' in hostvars[host] and hostvars[host]['add_to_prometheus'] and 'openresty_structure' in hostvars[host] and hostvars[host]['openresty_structure'] == 'v1' and job == 'default') %}
- targets: ['{{host}}:{{prometheus_port|default('60000')}}']
{% endif %}
{%endfor%}
{% if job == 'default' %}
{% for host_connection in openresty_stage_instances_v1|default([]) %}
- targets: ['{{host_connection}}']
{%endfor%}
{% endif %}
{% for host_connection in openresty_stage_instances_v2[job]|default([]) %}
- targets: ['{{host_connection}}']
{%endfor%}
metric_relabel_configs:
{%for group in groups %}
{%for host in groups[group] %}
{% if loop.first and group != 'all' and 'prometheus_pattern_regex' in hostvars[host] and hostvars[host]['prometheus_pattern_regex'] and 'prometheus_group' in hostvars[host] and hostvars[host]['prometheus_group'] %}
# {{ group }}
- source_labels: [instance]
regex: '{{ hostvars[host]['prometheus_pattern_regex'] }}'
target_label: group
replacement: '{{ hostvars[host]['prometheus_group'] }}'
{% endif %}
{%endfor%}
{%endfor%}
{%endfor%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment