The time data from MySQL Data source on the dashboard is different

[SYSTEM]

  • Grafana versoin: v8.1.3
  • OS: Red Hat Enterprise Linux
  • MySQL version: v8.0.34

Hello:)

I’m creating the dashboard with MySQL Data source. The time data performed the query directly in the database is different from the data performed the query in the grafana dashboard.

** The timing of data is the same. But I got the different data. Even though today is August 16th.

  • This is the query i made.
    select
    a.hostname as “Hostname”
    ,a.timedate as “Time”
    ,a.cpu_sys as “System or Privilegedtime”
    ,b.cpu_user as “User or Usertime”
    ,c.cpu_iowait as “Iowait or Processortime”
    ,d.cpu_idle as “Idle or Interrupttime”
    FROM
    (
    SELECT
    h.host as hostname
    ,STR_TO_DATE(truncate(DATE_FORMAT(FROM_UNIXTIME(hi.clock),‘%Y%m%d%H%i%S’),-3),‘%Y%m%d%H%i%S’) as timedate
    ,MAX(hi.value) as cpu_sys
    from items i , hosts h , history hi
    where 1=1
    and i.hostid=h.hostid
    and i.itemid = hi.itemid
    and hi.clock >= 1692167206 AND hi.clock <= 1692170806 ##Time range 변수
    and i.name in (
    ##평균을 내고싶은 종목 명
    ‘CPU system 10m’
    )
    and h.host in (‘kbamstarf02’) ## 시스템 명
    GROUP BY
    h.host,
    STR_TO_DATE(truncate(DATE_FORMAT(FROM_UNIXTIME(hi.clock),‘%Y%m%d%H%i%S’),-3),‘%Y%m%d%H%i%S’)
    ) a,
    (
    SELECT
    h.host as hostname
    ,STR_TO_DATE(truncate(DATE_FORMAT(FROM_UNIXTIME(hi.clock),‘%Y%m%d%H%i%S’),-3),‘%Y%m%d%H%i%S’) as timedate
    ,MAX(hi.value) as cpu_user
    from items i , hosts h , history hi
    where 1=1
    and i.hostid=h.hostid
    and i.itemid = hi.itemid
    and hi.clock >= 1692167206 AND hi.clock <= 1692170806 ##Time range 변수
    and i.name in (
    ##평균을 내고싶은 종목 명
    ‘CPU user 10m’
    )
    and h.host in (‘kbamstarf02’) ## 시스템 명
    GROUP BY
    h.host,
    STR_TO_DATE(truncate(DATE_FORMAT(FROM_UNIXTIME(hi.clock),‘%Y%m%d%H%i%S’),-3),‘%Y%m%d%H%i%S’)
    ) b ,
    (
    SELECT
    h.host as hostname
    ,STR_TO_DATE(truncate(DATE_FORMAT(FROM_UNIXTIME(hi.clock),‘%Y%m%d%H%i%S’),-3),‘%Y%m%d%H%i%S’) as timedate
    ,MAX(hi.value) as cpu_iowait
    from items i , hosts h , history hi
    where 1=1
    and i.hostid=h.hostid
    and i.itemid = hi.itemid
    and hi.clock >= 1692167206 AND hi.clock <= 1692170806 ##Time range 변수
    and i.name in (
    ##평균을 내고싶은 종목 명
    ‘CPU iowait 10m’
    )
    and h.host in (‘kbamstarf02’) ## 시스템 명
    GROUP BY
    h.host,
    STR_TO_DATE(truncate(DATE_FORMAT(FROM_UNIXTIME(hi.clock),‘%Y%m%d%H%i%S’),-3),‘%Y%m%d%H%i%S’)
    ) c,
    (
    SELECT
    h.host as hostname
    ,STR_TO_DATE(truncate(DATE_FORMAT(FROM_UNIXTIME(hi.clock),‘%Y%m%d%H%i%S’),-3),‘%Y%m%d%H%i%S’) as timedate
    ,MAX(hi.value) as cpu_idle
    from items i , hosts h , history hi
    where 1=1
    and i.hostid=h.hostid
    and i.itemid = hi.itemid
    and hi.clock >= 1692167206 AND hi.clock <= 1692170806 ##Time range 변수
    and i.name in (
    ##평균을 내고싶은 종목 명
    ‘CPU idle 10m’
    )
    and h.host in (‘kbamstarf02’) ## 시스템 명
    GROUP BY
    h.host,
    STR_TO_DATE(truncate(DATE_FORMAT(FROM_UNIXTIME(hi.clock),‘%Y%m%d%H%i%S’),-3),‘%Y%m%d%H%i%S’)
    ) d
    where 1=1
    and a.hostname = b.hostname
    and a.hostname = c.hostname
    and a.hostname = d.hostname
    and a.timedate = b.timedate
    and a.timedate = c.timedate
    and a.timedate = d.timedate

It doesn’t seem to be a query problem. Because I can get the data well when I performed a query directly from the database.

Does anyone know what’s wrong?
I’d really appreciate it if you could answer.

Hello,

I think that the timezone of those timestamps is not UTC and you store them without the timezone information. Grafana sees them as UTC and add the hours according to the timezone in your dashboard/Grafana configuration.

I’m not familiar with MySQL, look for something to specify the time zone of those timestamps (like AT TIME ZONE in PostgreSQL) in your query.
Or, in case there is no “timestamptz” type in MySQL where you can store the timezone information with the timestamp, convert the timestamp column to UTC or add a new one in UTC.

2 Likes

Or use a MYSQL function that converts it to UTC

2 Likes

Oh. got it.

Thank you for checking. I changed the query as you told me And I got the data well.

Thank you very much and Have a nice day!

2 Likes

yesss. I changed it as your comments. It helped me a lot. Thank you so much. And Have a nice day!

1 Like