├── LICENSE ├── README.md ├── ansible.cfg ├── inventory ├── playbook.yml └── roles ├── prometheus ├── handlers │ └── main.yml ├── tasks │ └── main.yml ├── templates │ ├── init.service.j2 │ └── prometheus.conf.j2 └── vars │ └── main.yml ├── prometheus_blackbox_exporter ├── handlers │ └── main.yml ├── tasks │ └── main.yml ├── templates │ ├── blackbox.yml.j2 │ └── init.service.j2 └── vars │ └── main.yml └── prometheus_node_exporter ├── tasks └── main.yml ├── templates └── init.service.j2 └── vars └── main.yml /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Mitesh Sharma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Prometheus Setup using Ansible 2 | 3 | In this project, we are configurating prometheus with blackbox_exporter on one instance and node_exporter on another instance using ansible. 4 | 5 | ## Getting Started 6 | 7 | Step 1: Update ip address of both instances in inventory file. 8 | 9 | Step 2: Run ansible command to setup prometheus server with node exporter 10 | 11 | Ansible command: ansible-playbook playbook.yml 12 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | inventory = ./inventory 3 | remote_user = ec2-user -------------------------------------------------------------------------------- /inventory: -------------------------------------------------------------------------------- 1 | [prometheus] 2 | 52.15.222.96 3 | 4 | [node_exporter] 5 | 18.222.182.23 -------------------------------------------------------------------------------- /playbook.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: node_exporter 3 | become: yes 4 | become_user: root 5 | become_method: sudo 6 | roles: 7 | - prometheus_node_exporter 8 | 9 | - hosts: prometheus 10 | become: yes 11 | become_user: root 12 | become_method: sudo 13 | roles: 14 | - prometheus_blackbox_exporter 15 | - prometheus -------------------------------------------------------------------------------- /roles/prometheus/handlers/main.yml: -------------------------------------------------------------------------------- 1 | - name: Restart the Prometheus service 2 | service: 3 | name: prometheus 4 | state: restarted 5 | listen: event_restart_prometheus 6 | 7 | - name: Reload systemd 8 | command: systemctl daemon-reload 9 | listen: systemd_reload -------------------------------------------------------------------------------- /roles/prometheus/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Creating prometheus user group 2 | group: name="{{groupId}}" 3 | become: true 4 | 5 | - name: Creating prometheus user 6 | user: 7 | name: "{{userId}}" 8 | group: "{{groupId}}" 9 | system: yes 10 | shell: "/sbin/nologin" 11 | comment: "{{userId}} nologin User" 12 | createhome: "no" 13 | state: present 14 | 15 | - name: Install prometheus 16 | unarchive: 17 | src: "https://github.com/prometheus/prometheus/releases/download/v{{ version }}/prometheus-{{ version }}.linux-amd64.tar.gz" 18 | dest: /tmp/ 19 | remote_src: yes 20 | 21 | - name: Copy prometheus file to bin 22 | copy: 23 | src: "/tmp/prometheus-{{ version }}.linux-amd64/prometheus" 24 | dest: "/usr/local/bin/prometheus" 25 | owner: "{{userId}}" 26 | group: "{{groupId}}" 27 | remote_src: yes 28 | mode: 0755 29 | 30 | - name: Delete prometheus tmp folder 31 | file: 32 | path: '/tmp/prometheus-{{ version }}.linux-amd64' 33 | state: absent 34 | 35 | - name: Creates directory 36 | file: 37 | path: "/data/prometheus/" 38 | state: directory 39 | owner: "{{userId}}" 40 | group: "{{groupId}}" 41 | mode: 0755 42 | 43 | - name: Creates directory 44 | file: 45 | path: "/etc/prometheus/" 46 | state: directory 47 | owner: "{{userId}}" 48 | group: "{{groupId}}" 49 | mode: 0755 50 | 51 | - name: config file 52 | template: 53 | src: prometheus.conf.j2 54 | dest: /etc/prometheus/prometheus.conf 55 | 56 | - name: Copy systemd init file 57 | template: 58 | src: init.service.j2 59 | dest: /etc/systemd/system/prometheus.service 60 | notify: systemd_reload 61 | 62 | - name: Start prometheus service 63 | service: 64 | name: prometheus 65 | state: started 66 | enabled: yes 67 | 68 | - name: Check if prometheus is accessible 69 | uri: 70 | url: http://localhost:9090 71 | method: GET 72 | status_code: 200 73 | -------------------------------------------------------------------------------- /roles/prometheus/templates/init.service.j2: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description={{serviceName}} 3 | Wants=network-online.target 4 | After=network-online.target 5 | 6 | [Service] 7 | User={{ userId }} 8 | Group={{ groupId }} 9 | Restart=always 10 | RestartSec=2 11 | StartLimitInterval=0 12 | Type=simple 13 | ExecStart={{ exec_command }} 14 | 15 | [Install] 16 | WantedBy=multi-user.target -------------------------------------------------------------------------------- /roles/prometheus/templates/prometheus.conf.j2: -------------------------------------------------------------------------------- 1 | global: 2 | scrape_interval: 15s 3 | 4 | scrape_configs: 5 | - job_name: 'prometheus' 6 | scrape_interval: 5s 7 | static_configs: 8 | - targets: ['localhost:9090'] 9 | - job_name: 'node_exporter' 10 | scrape_interval: 5s 11 | static_configs: 12 | - targets: 13 | {% for host in groups['all'] %} 14 | {% if inventory_hostname != host %} 15 | - '{{ host }}:9100' 16 | {% endif %} 17 | {% endfor %} 18 | 19 | - job_name: 'blackbox' 20 | metrics_path: /probe 21 | params: 22 | module: [http_2xx] 23 | static_configs: 24 | - targets: 25 | - http://www.google.com 26 | relabel_configs: 27 | - source_labels: [__address__] 28 | target_label: __param_target 29 | - source_labels: [__param_target] 30 | target_label: instance 31 | - target_label: __address__ 32 | replacement: localhost:9115 -------------------------------------------------------------------------------- /roles/prometheus/vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | serviceName: "prometheus" 3 | userId: "prometheus" 4 | groupId: "prometheus" 5 | exec_command: "/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.conf --storage.tsdb.path=/data/prometheus --storage.tsdb.retention=2d" 6 | version: "2.3.2" -------------------------------------------------------------------------------- /roles/prometheus_blackbox_exporter/handlers/main.yml: -------------------------------------------------------------------------------- 1 | - name: Restart the blackbox_exporter service 2 | service: 3 | name: blackbox_exporter 4 | state: restarted 5 | listen: event_restart_blackbox_exporter -------------------------------------------------------------------------------- /roles/prometheus_blackbox_exporter/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Creating blackbox_exporter user group 2 | group: name="{{groupId}}" 3 | become: true 4 | 5 | - name: Creating blackbox_exporter user 6 | user: 7 | name: "{{userId}}" 8 | group: "{{groupId}}" 9 | system: yes 10 | shell: "/sbin/nologin" 11 | comment: "{{userId}} nologin User" 12 | createhome: "no" 13 | state: present 14 | 15 | - name: Install prometheus blackbox exporter 16 | unarchive: 17 | src: "https://github.com/prometheus/blackbox_exporter/releases/download/v{{ version }}/blackbox_exporter-{{ version }}.linux-amd64.tar.gz" 18 | dest: /tmp/ 19 | remote_src: yes 20 | 21 | - name: Copy prometheus blackbox exporter file to bin 22 | copy: 23 | src: "/tmp/blackbox_exporter-{{ version }}.linux-amd64/blackbox_exporter" 24 | dest: "/usr/local/bin/blackbox_exporter" 25 | owner: "{{userId}}" 26 | group: "{{groupId}}" 27 | remote_src: yes 28 | mode: 0755 29 | 30 | - name: Delete blackbox exporter tmp folder 31 | file: 32 | path: '/tmp/blackbox_exporter-{{ version }}.linux-amd64' 33 | state: absent 34 | 35 | - name: Creates directory 36 | file: 37 | path: "/data/blackbox_exporter/" 38 | state: directory 39 | owner: "{{userId}}" 40 | group: "{{groupId}}" 41 | mode: 0755 42 | 43 | - name: Copy blackbox exporter config file 44 | template: 45 | src: blackbox.yml.j2 46 | dest: /data/blackbox_exporter/blackbox.yml 47 | owner: "{{userId}}" 48 | group: "{{groupId}}" 49 | 50 | - name: Copy systemd init file 51 | template: 52 | src: init.service.j2 53 | dest: /etc/systemd/system/blackbox_exporter.service 54 | notify: event_restart_blackbox_exporter 55 | 56 | - name: Start blackbox_exporter service 57 | service: 58 | name: blackbox_exporter 59 | state: started 60 | enabled: yes 61 | 62 | - name: Check if blackbox_exporter is accessible 63 | uri: 64 | url: http://localhost:9115 65 | method: GET 66 | status_code: 200 -------------------------------------------------------------------------------- /roles/prometheus_blackbox_exporter/templates/blackbox.yml.j2: -------------------------------------------------------------------------------- 1 | modules: 2 | http_2xx: 3 | prober: http 4 | timeout: 5s 5 | http: 6 | preferred_ip_protocol: "ipv4" 7 | valid_http_versions: ["HTTP/1.1", "HTTP/2"] 8 | valid_status_codes: [] 9 | method: GET -------------------------------------------------------------------------------- /roles/prometheus_blackbox_exporter/templates/init.service.j2: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description={{serviceName}} 3 | Wants=network-online.target 4 | After=network-online.target 5 | 6 | [Service] 7 | User={{ userId }} 8 | Group={{ groupId }} 9 | Restart=always 10 | RestartSec=2 11 | StartLimitInterval=0 12 | Type=simple 13 | ExecStart={{ exec_command }} 14 | 15 | [Install] 16 | WantedBy=multi-user.target -------------------------------------------------------------------------------- /roles/prometheus_blackbox_exporter/vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | serviceName: "blackbox_exporter" 3 | userId: "blackbox_exporter" 4 | groupId: "blackbox_exporter" 5 | exec_command: /usr/local/bin/blackbox_exporter --config.file /data/blackbox_exporter/blackbox.yml 6 | version: 0.12.0 -------------------------------------------------------------------------------- /roles/prometheus_node_exporter/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Creating node_exporter user group 2 | group: name="{{groupId}}" 3 | become: true 4 | 5 | - name: Creating node_exporter user 6 | user: 7 | name: "{{userId}}" 8 | group: "{{groupId}}" 9 | system: yes 10 | shell: "/sbin/nologin" 11 | comment: "{{userId}} nologin User" 12 | createhome: "no" 13 | state: present 14 | 15 | - name: Install prometheus node exporter 16 | unarchive: 17 | src: "https://github.com/prometheus/node_exporter/releases/download/v{{ version }}/node_exporter-{{ version }}.linux-amd64.tar.gz" 18 | dest: /tmp/ 19 | remote_src: yes 20 | 21 | - name: Copy prometheus node exporter file to bin 22 | copy: 23 | src: "/tmp/node_exporter-{{ version }}.linux-amd64/node_exporter" 24 | dest: "/usr/local/bin/node_exporter" 25 | owner: "{{userId}}" 26 | group: "{{groupId}}" 27 | remote_src: yes 28 | mode: 0755 29 | 30 | - name: Delete node exporter tmp folder 31 | file: 32 | path: '/tmp/node_exporter-{{ version }}.linux-amd64' 33 | state: absent 34 | 35 | - name: Copy systemd init file 36 | template: 37 | src: init.service.j2 38 | dest: /etc/systemd/system/node_exporter.service 39 | 40 | - name: Start node_exporter service 41 | service: 42 | name: node_exporter 43 | state: started 44 | enabled: yes 45 | 46 | - name: Check if node exporter emits metrices 47 | uri: 48 | url: http://127.0.0.1:9100/metrics 49 | method: GET 50 | status_code: 200 -------------------------------------------------------------------------------- /roles/prometheus_node_exporter/templates/init.service.j2: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description={{serviceName}} 3 | Wants=network-online.target 4 | After=network-online.target 5 | 6 | [Service] 7 | User={{ userId }} 8 | Group={{ groupId }} 9 | Restart=on-failure 10 | Type=simple 11 | ExecStart={{ exec_command }} 12 | 13 | [Install] 14 | WantedBy=multi-user.target -------------------------------------------------------------------------------- /roles/prometheus_node_exporter/vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | serviceName: "node_exporter" 3 | userId: "node_exporter" 4 | groupId: "node_exporter" 5 | exec_command: /usr/local/bin/node_exporter 6 | version: 0.16.0 --------------------------------------------------------------------------------