54 рядки
1.8 KiB
YAML
54 рядки
1.8 KiB
YAML
---
|
|
- name: 检测主机 CPU、内存、磁盘占用
|
|
hosts: all
|
|
gather_facts: yes
|
|
vars:
|
|
warn_threshold: 80
|
|
crit_threshold: 90
|
|
|
|
tasks:
|
|
- name: 获取 CPU 使用率
|
|
shell: |
|
|
top -bn1 | grep "Cpu(s)" | awk '{print "cpu_usage:" $2}' | cut -d'%' -f1
|
|
register: cpu_result
|
|
|
|
- name: 获取内存使用率
|
|
shell: |
|
|
free | grep Mem | awk '{printf "memory_used:%.0f\nmemory_total:%.0f\n", $3, $2}'
|
|
register: mem_result
|
|
|
|
- name: 获取磁盘使用率
|
|
shell: |
|
|
df -h | grep -E '/$|/data' | awk '{print $1 ":" $5}'
|
|
register: disk_result
|
|
|
|
- name: 格式化输出
|
|
set_fact:
|
|
cpu_usage: "{{ cpu_result.stdout_lines[0].split(':')[1] | trim }}"
|
|
memory_used_mb: "{{ (mem_result.stdout_lines[0].split(':')[1] | trim | float / 1024) | round | int }}"
|
|
memory_total_mb: "{{ (mem_result.stdout_lines[1].split(':')[1] | trim | float / 1024) | round | int }}"
|
|
disk_usage: "{{ disk_result.stdout_lines }}"
|
|
|
|
- name: 显示检测结果
|
|
debug:
|
|
msg: |
|
|
================== 主机检测报告 ==================
|
|
主机名: {{ inventory_hostname }}
|
|
CPU 使用率: {{ cpu_usage }}%
|
|
内存使用: {{ memory_used_mb }} MB / {{ memory_total_mb }} MB
|
|
磁盘使用:
|
|
{% for disk in disk_usage %}
|
|
- {{ disk }}
|
|
{% endfor %}
|
|
=================================================
|
|
|
|
- name: 告警判断
|
|
fail:
|
|
msg: "{{ inventory_hostname }} CPU 使用率超过 {{ crit_threshold }}%: {{ cpu_usage }}%"
|
|
when: cpu_usage | int >= crit_threshold
|
|
|
|
- name: 警告判断
|
|
warn:
|
|
msg: "{{ inventory_hostname }} CPU 使用率超过 {{ warn_threshold }}%: {{ cpu_usage }}%"
|
|
when: cpu_usage | int >= warn_threshold and cpu_usage | int < crit_threshold
|