[SOLVED] How to SELECT LAST PING as "seconds" from a table in SQL

Issue

I’m trying to write just a simple sql query on an observability platform like Grafana that will check if a server is up and running – On my database there is a table called ping and what it does is that it usually sends ping request every 15mins to check if the server is up and then stores all the records on the ping table.

I would like to create a query on Grafana sp if I don’t get any ping from the server in 15mins, I would be able to see it on Grafana so I can set alerts.

SELECT DATE_FORMAT((
    SELECT timeint 
    from mio.nyp_ping 
    ORDER BY timeint DESC 
    LIMIT 1) , '%Y-%M-%D-%H-%') AS Result;

2022-February-3rd-10-02

Trying to achieve something like this as an SQL query so I can run it on Grafana:

Metric:

SELECT (NOW() - LAST PING ) as "seconds from last ping"

Solution

Use the MAX() function to get the last ping time.

SELECT TIME_TO_SEC(TIMEDIFF(NOW(), MAX(ping_time))) AS "seconds from last ping"
FROM ping

Answered By – Barmar

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *