This commit is contained in:
2025-06-25 23:00:43 +05:00
parent 3f831b68ab
commit c2f357fae1
13 changed files with 732 additions and 72 deletions

View File

@ -10,11 +10,6 @@
После создания вы получите токен (например, 8007457609:AAGnrXOSaoxODMSh4yCzUEIp0uxfH3Hk8ws). Сохраните его, он понадобится для API.
# Настройка Vector для отправки логов в Gotify
Эта инструкция описывает, как установить Vector, настроить его для чтения JSON-логов из файла `/opt/org_ccalm_main/logs/ccalm.log`, фильтрации строк с уровнем `ERROR` и отправки уведомлений в Gotify.
@ -23,7 +18,7 @@
## 0. ✅ Подключаемся к инфраструктуре
```sh
ssh igor@ccalm.org -p 2200
ssh ubuntu@almaty.ccalm.org -p 22
```
## Предварительные требования
@ -64,11 +59,29 @@ sources:
ccalm_logs:
type: file
include:
- /opt/org_ccalm_main/logs/ccalm.log
- /opt/org_ccalm_main/logs/org-ccalm-main.log
read_from: beginning
ccalm_translation_logs:
type: file
include:
- /opt/org_ccalm_translation/logs/org-ccalm-translation.log
read_from: beginning
ccalm_dbms_logs:
type: file
include:
- /opt/org_ccalm_dbms/logs/org-ccalm-dbms.log
read_from: beginning
ccalm_jwt_logs:
type: file
include:
- /opt/org_ccalm_jwt/logs/org-ccalm-jwt.log
read_from: beginning
transforms:
parse_json:
parse_json_ccalm:
type: remap
inputs:
- ccalm_logs
@ -83,28 +96,209 @@ transforms:
}
. = merged
filter_errors:
type: filter
inputs:
- parse_json
condition: '.level == "ERROR"'
format_telegram_json:
parse_json_translation:
type: remap
inputs:
- filter_errors
- ccalm_translation_logs
source: |
structured, err = parse_json(.message)
if err != null {
abort
}
merged, err = merge(., structured)
if err != null {
abort
}
. = merged
parse_json_dbms:
type: remap
inputs:
- ccalm_dbms_logs
source: |
structured, err = parse_json(.message)
if err != null {
abort
}
merged, err = merge(., structured)
if err != null {
abort
}
. = merged
parse_json_jwt:
type: remap
inputs:
- ccalm_jwt_logs
source: |
structured, err = parse_json(.message)
if err != null {
abort
}
merged, err = merge(., structured)
if err != null {
abort
}
. = merged
filter_errors_ccalm:
type: filter
inputs:
- parse_json_ccalm
condition: '.level == "ERROR" || .level == "WARN"'
filter_errors_translation:
type: filter
inputs:
- parse_json_translation
condition: '.level == "ERROR" || .level == "WARN"'
filter_errors_dbms:
type: filter
inputs:
- parse_json_dbms
condition: '.level == "ERROR" || .level == "WARN"'
filter_errors_jwt:
type: filter
inputs:
- parse_json_jwt
condition: '.level == "ERROR" || .level == "WARN"'
format_telegram_json_ccalm:
type: remap
inputs:
- filter_errors_ccalm
source: |
msg, err = string(.message)
if err != null {
msg = "Unable to parse message"
}
.message = "{\"chat_id\":\"307675888\",\"text\":\"ERROR: " + msg + "\"}"
marker_str = ""
if exists(.marker) {
m, err = string(.marker)
if err == null && length(m) > 0 {
marker_str = "Marker: ```text\n" + m + "```"
}
}
level_str = ""
icon_str = ""
if exists(.level) {
m, err = string(.level)
if err == null {
level_str = "level: ```text\n" + m + "```"
}
if m == "ERROR" {
icon_str = "‼"
}
if m == "WARN" {
icon_str = "⚠️"
}
}
.message = "{\"chat_id\":\"-1002640082189\",\"message_thread_id\":2,\"text\":\"" + icon_str + " " + level_str + marker_str + "Message: ```text\n" + msg + "```\", \"parse_mode\":\"Markdown\"}"
format_telegram_json_translation:
type: remap
inputs:
- filter_errors_translation
source: |
msg, err = string(.message)
if err != null {
msg = "Unable to parse message"
}
marker_str = ""
if exists(.marker) {
m, err = string(.marker)
if err == null && length(m) > 0 {
marker_str = "Marker: ```text\n" + m + "```"
}
}
level_str = ""
icon_str = ""
if exists(.level) {
m, err = string(.level)
if err == null {
level_str = "level: ```text\n" + m + "```"
}
if m == "ERROR" {
icon_str = "‼"
}
if m == "WARN" {
icon_str = "⚠️"
}
}
.message = "{\"chat_id\":\"-1002640082189\",\"message_thread_id\":24,\"text\":\"" + icon_str + " " + level_str + marker_str + "Message: ```text\n" + msg + "```\", \"parse_mode\":\"Markdown\"}"
format_telegram_json_dbms:
type: remap
inputs:
- filter_errors_dbms
source: |
msg, err = string(.message)
if err != null {
msg = "Unable to parse message"
}
marker_str = ""
if exists(.marker) {
m, err = string(.marker)
if err == null && length(m) > 0 {
marker_str = "Marker: ```text\n" + m + "```"
}
}
level_str = ""
icon_str = ""
if exists(.level) {
m, err = string(.level)
if err == null {
level_str = "level: ```text\n" + m + "```"
}
if m == "ERROR" {
icon_str = "‼"
}
if m == "WARN" {
icon_str = "⚠️"
}
}
.message = "{\"chat_id\":\"-1002640082189\",\"message_thread_id\":9,\"text\":\"" + icon_str + " " + level_str + marker_str + "Message: ```text\n" + msg + "```\", \"parse_mode\":\"Markdown\"}"
format_telegram_json_jwt:
type: remap
inputs:
- filter_errors_jwt
source: |
msg, err = string(.message)
if err != null {
msg = "Unable to parse message"
}
marker_str = ""
if exists(.marker) {
m, err = string(.marker)
if err == null && length(m) > 0 {
marker_str = "Marker: ```text\n" + m + "```"
}
}
level_str = ""
icon_str = ""
if exists(.level) {
m, err = string(.level)
if err == null {
level_str = "level: ```text\n" + m + "```"
}
if m == "ERROR" {
icon_str = "‼"
}
if m == "WARN" {
icon_str = "⚠️"
}
}
.message = "{\"chat_id\":\"-1002640082189\",\"message_thread_id\":4,\"text\":\"" + icon_str + " " + level_str + marker_str + "Message: ```text\n" + msg + "```\", \"parse_mode\":\"Markdown\"}"
sinks:
telegram:
telegram_ccalm:
type: http
inputs:
- format_telegram_json
- format_telegram_json_ccalm
uri: "https://api.telegram.org/bot8007457609:AAGnrXOSaoxODMSh4yCzUEIp0uxfH3Hk8ws/sendMessage"
method: post
encoding:
@ -115,6 +309,47 @@ sinks:
batch:
max_events: 1
telegram_translation:
type: http
inputs:
- format_telegram_json_translation
uri: "https://api.telegram.org/bot8007457609:AAGnrXOSaoxODMSh4yCzUEIp0uxfH3Hk8ws/sendMessage"
method: post
encoding:
codec: text
request:
headers:
Content-Type: "application/json"
batch:
max_events: 1
telegram_dbms:
type: http
inputs:
- format_telegram_json_dbms
uri: "https://api.telegram.org/bot8007457609:AAGnrXOSaoxODMSh4yCzUEIp0uxfH3Hk8ws/sendMessage"
method: post
encoding:
codec: text
request:
headers:
Content-Type: "application/json"
batch:
max_events: 1
telegram_jwt:
type: http
inputs:
- format_telegram_json_jwt
uri: "https://api.telegram.org/bot8007457609:AAGnrXOSaoxODMSh4yCzUEIp0uxfH3Hk8ws/sendMessage"
method: post
encoding:
codec: text
request:
headers:
Content-Type: "application/json"
batch:
max_events: 1
EOF
```
@ -156,6 +391,8 @@ sudo vector --config /etc/vector/vector.yaml
1. Включите и запустите сервис:
```sh
sudo systemctl enable vector
```
```sh
sudo systemctl start vector
```
2. Проверьте статус:
@ -171,7 +408,7 @@ sudo vector --config /etc/vector/vector.yaml
1. Убедитесь, что Gotify доступен по указанному URL.
2. Добавьте тестовую строку лога в `/opt/org_ccalm_main/logs/ccalm.log`:
```sh
echo '{"level": "ERROR", "message": "Database connection failed", "timestamp": "2025-05-18T12:28:00Z"}' | sudo tee -a /opt/org_ccalm_main/logs/ccalm.log
echo '{"level": "ERROR", "marker":"12345", "message": "Database connection failed 0", "timestamp": "2025-05-18T12:28:00Z"}' | sudo tee -a /opt/org_ccalm_jwt/logs/org-ccalm-jwt.log
```
3. Проверьте Gotify (веб-интерфейс или приложение) — должно прийти уведомление с заголовком "CCALM Log Error" и сообщением "Database connection failed".