60 lines
No EOL
1.9 KiB
Django/Jinja
60 lines
No EOL
1.9 KiB
Django/Jinja
#!/bin/bash
|
||
|
||
# Конфигурация
|
||
METRIC_FILE="/var/lib/node_exporter/textfile_collector/cron_magos.prom"
|
||
TIMEOUT_SECONDS=120 # 2 минуты максимум на выполнение
|
||
mkdir -p "$(dirname ${METRIC_FILE})"
|
||
|
||
# Закрываем stdin чтобы скрипты не ждали ввода
|
||
exec </dev/null
|
||
|
||
START=$(date +%s)
|
||
STATUS=0
|
||
|
||
# Функция для безопасного выполнения команды с timeout
|
||
safe_run() {
|
||
local cmd="$1"
|
||
local description="$2"
|
||
|
||
# Запускаем с timeout и закрытым stdin
|
||
timeout ${TIMEOUT_SECONDS} bash -c "${cmd}" </dev/null
|
||
local exit_code=$?
|
||
|
||
if [ ${exit_code} -eq 124 ]; then
|
||
echo "[$(date)] ERROR: ${description} timed out after ${TIMEOUT_SECONDS}s" >> /var/log/cron_monitor_errors.log
|
||
return 1
|
||
elif [ ${exit_code} -ne 0 ]; then
|
||
echo "[$(date)] ERROR: ${description} failed with exit code ${exit_code}" >> /var/log/cron_monitor_errors.log
|
||
return 1
|
||
fi
|
||
|
||
return 0
|
||
}
|
||
|
||
# Выполняем команды последовательно
|
||
if safe_run "{{ magos_bin }} > {{ magos_log_file }} 2>&1" "magos"; then
|
||
if safe_run "{{ magos_upload_logs_script_path }}" "upload_logs.sh"; then
|
||
if safe_run "{{ magos_upload_configs_script_path }}" "upload_configs.sh"; then
|
||
STATUS=1
|
||
fi
|
||
fi
|
||
fi
|
||
|
||
DURATION=$(($(date +%s) - START))
|
||
|
||
# Записываем метрики
|
||
cat > "${METRIC_FILE}" << EOF
|
||
# HELP cron_magos_status Cron job execution status (1=success, 0=failure)
|
||
# TYPE cron_magos_status gauge
|
||
cron_magos_status ${STATUS}
|
||
|
||
# HELP cron_magos_last_run Last execution timestamp
|
||
# TYPE cron_magos_last_run gauge
|
||
cron_magos_last_run $(date +%s)
|
||
|
||
# HELP cron_magos_duration_seconds Execution duration
|
||
# TYPE cron_magos_duration_seconds gauge
|
||
cron_magos_duration_seconds ${DURATION}
|
||
EOF
|
||
|
||
exit $((1 - STATUS)) |