Обновить playbooks/statistica.yml
This commit is contained in:
+26
-11
@@ -1,10 +1,11 @@
|
|||||||
---
|
---
|
||||||
- name: Collect system metrics and send to Telegram via proxy
|
- name: Collect system metrics and send to Telegram (with optional proxy)
|
||||||
hosts: all
|
hosts: all
|
||||||
gather_facts: yes
|
gather_facts: yes
|
||||||
vars:
|
vars:
|
||||||
telegram_token: "8820292312:AAECFO1AEWOJTh30zNuhyjC5C81o_qJHQcM"
|
telegram_token: "8820292312:AAECFO1AEWOJTh30zNuhyjC5C81o_qJHQcM"
|
||||||
telegram_chat_id: "407968638"
|
telegram_chat_id: "407968638"
|
||||||
|
# Прокси можно переопределить в инвентаре или оставить пустым
|
||||||
proxy: "socks5h://administrator:60NthRegT@192.168.0.218:1080"
|
proxy: "socks5h://administrator:60NthRegT@192.168.0.218:1080"
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
@@ -122,7 +123,7 @@
|
|||||||
set_fact:
|
set_fact:
|
||||||
cpu_freq_text: "{{ cpu_freq.stdout if cpu_freq.stdout != '' else '' }}"
|
cpu_freq_text: "{{ cpu_freq.stdout if cpu_freq.stdout != '' else '' }}"
|
||||||
|
|
||||||
# --- Топ-5 процессов (сохраняем как список) ---
|
# --- Топ-5 процессов ---
|
||||||
- name: Get top 5 processes by memory
|
- name: Get top 5 processes by memory
|
||||||
shell: |
|
shell: |
|
||||||
ps aux --sort=-%mem | head -6 | tail -5 | awk '{print $11 " (" $4 "%)"}'
|
ps aux --sort=-%mem | head -6 | tail -5 | awk '{print $11 " (" $4 "%)"}'
|
||||||
@@ -216,7 +217,7 @@
|
|||||||
mem_health: "{% if (mem_percent.stdout | float) < 50 %}🟢{% elif (mem_percent.stdout | float) < 80 %}🟡{% else %}🔴{% endif %}"
|
mem_health: "{% if (mem_percent.stdout | float) < 50 %}🟢{% elif (mem_percent.stdout | float) < 80 %}🟡{% else %}🔴{% endif %}"
|
||||||
disk_health: "{% if (disk_used_percent | float) < 70 %}🟢{% elif (disk_used_percent | float) < 85 %}🟡{% else %}🔴{% endif %}"
|
disk_health: "{% if (disk_used_percent | float) < 70 %}🟢{% elif (disk_used_percent | float) < 85 %}🟡{% else %}🔴{% endif %}"
|
||||||
|
|
||||||
# --- Формирование сообщения (HTML) с пустой строкой перед прогресс-баром CPU ---
|
# --- Формирование сообщения (HTML) ---
|
||||||
- name: Build message (HTML)
|
- name: Build message (HTML)
|
||||||
set_fact:
|
set_fact:
|
||||||
message_html: |
|
message_html: |
|
||||||
@@ -243,19 +244,33 @@
|
|||||||
<b>🔥 Топ-5 процессов по памяти:</b>
|
<b>🔥 Топ-5 процессов по памяти:</b>
|
||||||
<pre>{{ top_procs_joined }}</pre>
|
<pre>{{ top_procs_joined }}</pre>
|
||||||
|
|
||||||
# --- Отправка в Telegram с проверкой ответа ---
|
# --- Отправка в Telegram с проверкой HTTP-кода и поддержкой опционального прокси ---
|
||||||
- name: Send to Telegram via proxy
|
- name: Send to Telegram via proxy
|
||||||
shell: |
|
shell: |
|
||||||
response=$(curl -s -X POST \
|
set -e
|
||||||
--socks5-hostname "{{ proxy }}" \
|
if [ -n "{{ proxy }}" ]; then
|
||||||
|
proxy_opt="--socks5-hostname {{ proxy }}"
|
||||||
|
else
|
||||||
|
proxy_opt=""
|
||||||
|
fi
|
||||||
|
response=$(curl -s -w "\n%{http_code}" -X POST $proxy_opt \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{"chat_id": "{{ telegram_chat_id }}", "text": {{ message_html | to_json }}, "parse_mode": "HTML"}' \
|
-d '{"chat_id": "{{ telegram_chat_id }}", "text": {{ message_html | to_json }}, "parse_mode": "HTML"}' \
|
||||||
"https://api.telegram.org/bot{{ telegram_token }}/sendMessage")
|
"https://api.telegram.org/bot{{ telegram_token }}/sendMessage" 2>&1)
|
||||||
echo "Telegram API response: $response"
|
http_code=$(echo "$response" | tail -n1)
|
||||||
if echo "$response" | grep -q '"ok":true'; then
|
body=$(echo "$response" | sed '$d')
|
||||||
exit 0
|
echo "HTTP code: $http_code"
|
||||||
|
echo "Response body: $body"
|
||||||
|
if [ "$http_code" -eq 200 ]; then
|
||||||
|
if echo "$body" | grep -q '"ok":true'; then
|
||||||
|
echo "Message sent successfully"
|
||||||
|
exit 0
|
||||||
|
else
|
||||||
|
echo "ERROR: Telegram API returned ok=false: $body" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
echo "ERROR: Telegram API returned: $response" >&2
|
echo "ERROR: HTTP error $http_code: $body" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
changed_when: false
|
changed_when: false
|
||||||
|
|||||||
Reference in New Issue
Block a user