diff --git a/playbooks/statistica.yml b/playbooks/statistica.yml index f3afbc7..8c4eb8d 100644 --- a/playbooks/statistica.yml +++ b/playbooks/statistica.yml @@ -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 gather_facts: yes vars: telegram_token: "8820292312:AAECFO1AEWOJTh30zNuhyjC5C81o_qJHQcM" telegram_chat_id: "407968638" + # Прокси можно переопределить в инвентаре или оставить пустым proxy: "socks5h://administrator:60NthRegT@192.168.0.218:1080" tasks: @@ -122,7 +123,7 @@ set_fact: cpu_freq_text: "{{ cpu_freq.stdout if cpu_freq.stdout != '' else '' }}" - # --- Топ-5 процессов (сохраняем как список) --- + # --- Топ-5 процессов --- - name: Get top 5 processes by memory shell: | 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 %}" disk_health: "{% if (disk_used_percent | float) < 70 %}🟢{% elif (disk_used_percent | float) < 85 %}🟡{% else %}🔴{% endif %}" - # --- Формирование сообщения (HTML) с пустой строкой перед прогресс-баром CPU --- + # --- Формирование сообщения (HTML) --- - name: Build message (HTML) set_fact: message_html: | @@ -243,19 +244,33 @@ 🔥 Топ-5 процессов по памяти:
{{ top_procs_joined }}
- # --- Отправка в Telegram с проверкой ответа ---
+ # --- Отправка в Telegram с проверкой HTTP-кода и поддержкой опционального прокси ---
- name: Send to Telegram via proxy
shell: |
- response=$(curl -s -X POST \
- --socks5-hostname "{{ proxy }}" \
+ set -e
+ 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" \
-d '{"chat_id": "{{ telegram_chat_id }}", "text": {{ message_html | to_json }}, "parse_mode": "HTML"}' \
- "https://api.telegram.org/bot{{ telegram_token }}/sendMessage")
- echo "Telegram API response: $response"
- if echo "$response" | grep -q '"ok":true'; then
- exit 0
+ "https://api.telegram.org/bot{{ telegram_token }}/sendMessage" 2>&1)
+ http_code=$(echo "$response" | tail -n1)
+ body=$(echo "$response" | sed '$d')
+ 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
- echo "ERROR: Telegram API returned: $response" >&2
+ echo "ERROR: HTTP error $http_code: $body" >&2
exit 1
fi
changed_when: false