Updated my query
I also started to see issues with my original query. I wrote this on a dev system with very little usage, so the same issues you had only showed up when we went to production with it. I've incorporated a few more columns (this is used on an external mobile phone web interface so it might be much more then what you need). I wanted to get as close to our triggers page as possible without all the extra stuff (keeps recently cleared triggers on the page for 60 seconds). Total execution time on our system ~0.1016 secs.
Also, I opted to use MAX(eventid) instead of e.clock since that would guarantee I would see the latest event for a trigger. Using e.clock works but it didn't actually pull all the other rows across properly (could be something I was doing wrong too, I am by no means a SQL programmer).
If the API in 1.8.3 gets more robust, I might be dropping these custom queries all together
I also started to see issues with my original query. I wrote this on a dev system with very little usage, so the same issues you had only showed up when we went to production with it. I've incorporated a few more columns (this is used on an external mobile phone web interface so it might be much more then what you need). I wanted to get as close to our triggers page as possible without all the extra stuff (keeps recently cleared triggers on the page for 60 seconds). Total execution time on our system ~0.1016 secs.
Also, I opted to use MAX(eventid) instead of e.clock since that would guarantee I would see the latest event for a trigger. Using e.clock works but it didn't actually pull all the other rows across properly (could be something I was doing wrong too, I am by no means a SQL programmer).
Code:
SELECT host, t.description, f.triggerid, e.acknowledged, t.value, i.lastvalue, IF(e.acknowledged=1,a.message,"N/A"), e.eventid FROM triggers t INNER JOIN functions f ON ( f.triggerid = t.triggerid ) INNER JOIN items i ON ( i.itemid = f.itemid ) INNER JOIN hosts h ON ( i.hostid = h.hostid ) INNER JOIN events e ON ( e.objectid = t.triggerid ) LEFT JOIN acknowledges a ON ( a.eventid = e.eventid ) WHERE (e.eventid DIV 100000000000000) IN (0) AND e.object = 0 AND (t.value=1 OR (t.value =0 AND unix_timestamp(now()) - t.lastchange <60)) AND h.status = 0 AND i.status = 0 AND t.status = 0 AND e.eventid = (SELECT max(eventid) FROM events e WHERE (e.eventid DIV 100000000000000) IN (0) AND e.object = 0 AND (t.value=1 OR (t.value =0 AND unix_timestamp(now()) - t.lastchange <60)) AND h.status = 0 AND i.status = 0 AND t.status = 0 AND e.objectid = t.triggerid ) GROUP BY host, f.triggerid ORDER BY t.lastchange DESC;
and therefore need desperately your help: how to modify this query to get the latest unacknowledged alerts for a given "Application"? Is that possible?
Comment