if [ "$#" -ne 3 ]; then
    echo "Uso: $0 <PON> <status_value> <host_name>"
    exit 1
fi

PON=$1
STATUS_VALUE=$2
HOST_NAME=$3

ZBX_DB="zabbix"  # Nome do banco de dados
ZBX_USER="rostelecom"  # Nome de usuário do banco de dados
ZBX_PASS="#474747@!"  # Senha do banco de dados

SQL_QUERY="
SELECT COUNT(*) AS total_status_online
FROM (
    SELECT h.value
    FROM history h
    JOIN items i ON h.itemid = i.itemid
    WHERE i.hostid = (SELECT hostid FROM hosts WHERE host = '$HOST_NAME')
      AND i.name LIKE '%Status%'
      AND i.name LIKE '%$PON%'
      AND h.clock = (
          SELECT MAX(clock)
          FROM history
          WHERE itemid = h.itemid
      )
) AS subquery
WHERE value = $STATUS_VALUE;
"
RESULT=$(mysql -u $ZBX_USER -p$ZBX_PASS $ZBX_DB -e "$SQL_QUERY" -s -N)

echo "$RESULT"

