#!/bin/bash

TIMEOUT=10

count_syndrome() {
    local output
    output=$(timeout "$TIMEOUT" journalctl --since "60 minutes ago" 2>/dev/null | grep mlx5_core | grep syndrome | wc -l)
    local ret=$?

    if [ $ret -eq 124 ]; then
        # timeout occurred
        return 2
    elif [ $ret -ne 0 ]; then
        # other error in pipeline
        return 1
    fi

    if ! [[ "$output" =~ ^[0-9]+$ ]]; then
        return 1
    fi

    echo "$output"
    return 0
}

syndrome_count=$(count_syndrome)
exit_code=$?

if [ $exit_code -ne 0 ]; then
    exit 1
fi

echo "[{\"tags\":\"log=syndrome\",\"value\":${syndrome_count}}]"
exit 0
