[Ansible] ์๋ฒ ์ ๊ฒ ์คํฌ๋ฆฝํธ ์๋ํ ๊ตฌํ ์ค์ต - CPU ์ ์ ์จ
Ansible Playbook์ ์ฌ์ฉํด์ Managed Node์ ์๋ฒ ์ ๊ฒ ์๋ํ๋ฅผ ๊ตฌํํ๋ ๊ณผ์ ์ ๊ธฐ๋กํ๋ค.
Ansible ์ํคํ ์ฒ
VM์ ์ค์น๋ Rocky Linux 8.6์ Control Node๋ก ์ง์ ํ๊ณ , EC2 ์๋ฒ๋ฅผ Managed Node๋ก ์ง์ ํด Managed Node์ ์๋ฒ ์ ๊ฒํ๋ Playbook์ ๊ตฌํํ๋ค. ์๋ฒ ์ ๊ฒ ์๋ํ์๋ ๋ค์๊ณผ ๊ฐ์ ์ ๋ณด๋ฅผ ์์งํ๋ ๊ฒ์ ๋ชฉํ๋ก ํ๋ค.
- CPU ์ ์ ์จ
- Memory ์ ์ ์จ
- Disk ์ฌ์ฉ๋ฅ
- NIC ์ฌ์ฉ๋ฅ
๋ํ 1์๊ฐ์ ํ ๋ฒ์ฉ ์๋์ผ๋ก ์ค์ผ์ค๋งํ์ฌ ์ ์ ๋ณด๋ฅผ ์์งํ๊ณ , ๊ฒฐ๊ณผ๋ฅผ 'YYYY_MM_DD_HH_์์์ ๋ณด์์ง' ํ์์ txtํ์ผ๋ก ์ ์ฅํ ์ ์๋๋ก ํ๋ค.
CPU ์ ์ ์จ
top ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํด์ ์์คํ ์ ์ํ๋ฅผ ๋ชจ๋ํฐ๋งํ๋ค. top์ ๊ฒฐ๊ณผ ์ค CPU ์ฌ์ฉ๋ ์ ๋ณด๋ฅผ ํํฐ๋งํ๊ณ , awk๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ ๋น์จ์ ๊ณ์ฐํ๋ค. ์ด๋ CPU ์ฌ์ฉ๋ฅ ์ 100์์ ์ ํด ์๊ฐ์ ๋นผ์ ๊ณ์ฐํ์๋ค.
- name: CPU ์ ์ ์จ
shell: "top -bn1 | grep 'Cpu(s)' | awk -F',' '{print $4}' | awk '{print 100 - $1}'"
register: cpu_info
- name: CPU ์ ์ ์จ ์ถ๋ ฅ
debug:
msg: "CPU ์ ์ ์จ: {{ cpu_info['stdout'] }}%"
์ด ์ฝ๋์ ๊ฒฐ๊ณผ๋ cpu_info ๋ณ์์ ์ ์ฅ๋๋ฉฐ, stdout์ ํตํด ๊ฒฐ๊ณผ๊ฐ๋ง ์ถ๋ ฅํ ์ ์๋ค.
Memory ์ ์ ์จ
gather_facts๋ก ๊ตฌํ ์๋ฒ ์ ๋ณด ์ค memory_mb ๋ณ์๋ฅผ ์ฌ์ฉํด์ ๊ตฌํ ์ ์๋ค. ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ด ๋ฉ๋ชจ๋ฆฌ๋ก ๋๋ ํ 100์ ๊ณฑํด ๊ณ์ฐ ๊ฐ๋ฅํ๋ค.
- name: Memory ์ ์ ์จ
set_fact:
total_memory: "{{ ansible_facts['memory_mb']['real']['total'] }}"
used_memory: "{{ ansible_facts['memory_mb']['real']['used'] }}"
- name: Memory ์ ์ ์จ ์ถ๋ ฅ
debug:
msg: "Memory ์ ์ ์จ: {{ ((used_memory | float / total_memory | float ) * 100) | round(2) }}%"
Disk ์ฌ์ฉ๋ฅ
๋์คํฌ ์ฌ์ฉ๋ฅ ์ gather_facts๋ก ๊ตฌํ ์๋ฒ ์ ๋ณด ์ค mounts ๋ณ์๋ฅผ ์ฌ์ฉํด์ ๊ตฌํ ์ ์๋ค. ์๋ฒ์ ์ฒซ ๋ฒ์งธ ๋ง์ดํธ๋ ํํฐ์ ์ ์ฌ์ฉ๋์ ๊ธฐ์ค์ผ๋ก ๊ณ์ฐํ๊ณ , mounts ๋ณ์์ size_total๊ณผ size_available ๊ฐ์ ๊ฐ์ ธ์ ๊ณ์ฐํ๋ค.
- name: Disk ์ฌ์ฉ๋ฅ
set_fact:
size_available: "{{ ansible_facts['mounts'][0]['size_available'] }}"
size_total: "{{ ansible_facts['mounts'][0]['size_total'] }}"
- name: Disk ์ฌ์ฉ๋ฅ ์ถ๋ ฅ
debug:
msg: "Disk ์ฌ์ฉ๋ฅ : {{ ((1 - (size_available | float / size_total | float )) * 100) | round(2) }}%"
NIC ์ฌ์ฉ๋ฅ
NIC ์ฌ์ฉ๋ฅ ์ shell ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํ๋ค. ifconfig ๋ช ๋ น์ด๋ฅผ ์ธํฐํ์ด์ค๋ณ๋ก ์์ (RX) ๋ฐ ์ก์ (TX) ํจํท ์๋ฅผ ํ์ธํ๋ค.
- name: NIC ์ฌ์ฉ๋ฅ
shell: |
ifconfig | grep -E '^[a-zA-Z0-9]+|RX packets|TX packets'
register: nic_info
๊ฒฐ๊ณผ ํ์ผ ์์ฑ ๋ฐ ์ ์ฅ
์์ง๋ ์ ๋ณด๋ฅผ ํ์ผ๋ก ์ ์ฅํ๋ ๊ณผ์ ์์, ํ์ผ ๊ฒฝ๋ก๋ ํ์ฌ ๋ ์ง์ ์๊ฐ ์ ๋ณด๋ก ํ์ฑ๋๋ฉฐ, ์ ์ฅ ๊ฒฝ๋ก๋ /home/ansible/projects์ ๋ ์ง๋ณ ํด๋์ ํ์ผ์ ์์ฑํ๋๋ก ํ๋ค.
- name: ํ์ผ ์ ์ฅ ๋ณ์
set_fact:
dir_dest: "/home/ansible/projects/{{ ansible_facts.date_time.date | replace('-', '_') }}"
file_dest: "/home/ansible/projects/{{ ansible_facts.date_time.date | replace('-', '_') }}/{{ ansible_facts.date_time.date | replace('-', '_') }}_{{ ansible_facts.date_time.hour }}์_์์์ ๋ณด์์ง.txt"
ํด๋ ์์ฑ ๋ฐ ํ์ผ ๋ด์ฉ ์์ฑ
- name: ํ์ผ ์์ฑ
file:
path: "{{ dir_dest }}"
state: directory
delegate_to: localhost
- name: ๊ฒฐ๊ณผ ์ ์ฅ
copy:
content: |
-------------------------------------------------------------------------------------
Date: {{ ansible_facts.date_time.date }} {{ ansible_facts.date_time.time }}
Hostname: {{ ansible_facts.hostname }}
OS: {{ ansible_facts.distribution }} {{ ansible_facts.distribution_version }}
OS Kernel: {{ ansible_facts.kernel }}
CPU Cores: {{ ansible_facts.processor_vcpus }}
Memory: {{ ansible_facts.memory_mb.real }}
-------------------------------------------------------------------------------------
CPU ์ฌ์ฉ๋ฅ : {{ cpu_info.stdout }}%
Memory ์ฌ์ฉ๋ฅ : {{ ((used_memory | float / total_memory | float ) * 100) | round(2) }}%
Disk ์ฌ์ฉ๋ฅ : {{ ((1 - (size_available | float / size_total | float )) * 100) | round(2) }}%
NIC ์ฌ์ฉ๋ฅ :
{% for line in nic_info.stdout_lines %}
{% if ':' in line %}
{{ line.split(':')[0] }}
{% elif 'RX packets' in line or 'TX packets' in line %}
{{ line }}
{% endif %}
{% endfor %}
dest: "{{ file_dest }}"
delegate_to: localhost
playbook ์คํ ๊ฒฐ๊ณผ๋ ์ด๋ ๊ฒ ์ถ๋ ฅ๋๋ค.
์ค๋ ๋ ์ง ํด๋์ ๋ ์ง๋ณ ํ์ผ๋ ์ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.