推荐
Prometheus告警规则(中文告警案例)
一分钟部署完Prometheus+alertmanager+grafana
prometheus监控alertmanager邮件钉钉告警,配置模板,拿来就用
一/编写playbook
---
- name: Install node_exporter
hosts: sssaa
become: yes
vars:
service_name: "node_exporter.service"
file_path: /etc/systemd/system/node_exporter.service
file_content: |
[Unit]
Description=node_exporter
After=network.target
[Service]
User=root
Group=root
ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=:9001 --collector.systemd --collector.systemd.unit-whitelist=(sshd|nginx).service --collector.processes --collector.tcpstat --collector.supervisord
[Install]
WantedBy=multi-user.target
tasks:
- name: Create node_exporter directory
file:
path: /usr/local/node_exporter/
state: directory
- name: Download node_exporter binary
get_url:
url: https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz
dest: /usr/local/src/
- name: Extract node_exporter binary
unarchive:
src: /usr/local/src/node_exporter-1.6.0.linux-amd64.tar.gz
dest: /usr/local/node_exporter/
copy: no
extra_opts: [--strip-components=1]
- name: Create the directory if it does not exist
file:
path: "{{ file_path | dirname }}"
state: directory
- name: Create the node_exporter.service
copy:
content: "{{ file_content }}"
dest: "{{ file_path }}"
mode: '0644'
- name: Restart node_exporter.service service
systemd:
name: "{{ service_name }}"
state: restarted
说明:
• playbook的名称为“Install node_exporter”,hosts为all,表示在所有主机上运行。
• become: yes 表示使用root权限运行任务。
• 使用 vars 部分定义变量,这里定义了三个变量。 |这个表示转行的全部内容
• 创建 /usr/local/node_exporter/目录
• 使用get_url模块从 node_exporter官网下载二进制源码包到 /usr/local/src/ 目录。
• 使用unarchive模块解压二进制源码包到 /usr/local/node_exporter/目录下,并使用 --strip-components=1 选项删除默认的顶层目录名称。这样可以确保源码包直接解压到/usr/local/node_exporter/目录下,不会多一层文件夹。
• 使用 file 模块创建目录。该模块将检查目录是否存在,如果不存在,则创建它。
• 使用 copy 模块创建文件,并将 content 参数设置为 file_content 变量的值,将 dest 参数设置为 file_path 变量的值,mode给权限。
• 使用systemd模块启动 node_exporter服务。
二/运行
注:删除了过多的运行条目。
[root@DZ-192-168-1-200 playbook]# ansible-playbook /data/playbook/node_exporter.yml
PLAY [Install node_exporter] ******************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [192.168.1.6]
TASK [Create node_exporter directory] *********************************************************************************************************
changed: [192.168.1.6]
TASK [Download node_exporter binary] **********************************************************************************************************
changed: [192.168.0.6]
TASK [Extract node_exporter binary] ***********************************************************************************************************
changed: [192.168.0.6]
TASK [Create the directory if it does not exist] **********************************************************************************************
ok: [192.168.0.6]
TASK [Create the node_exporter.service] *******************************************************************************************************
changed: [192.168.1.6]
TASK [Restart node_exporter.service service] **************************************************************************************************
changed: [192.168.1.6]
PLAY RECAP ************************************************************************************************************************************
192.168.1.6 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0