diff --git a/db/zm_create.sql.in b/db/zm_create.sql.in index b97c9a282..f94d3755b 100644 --- a/db/zm_create.sql.in +++ b/db/zm_create.sql.in @@ -228,6 +228,24 @@ CREATE TABLE `Events_Hour` ( KEY `Events_Hour_StartTime_idx` (`StartTime`) ) ENGINE=@ZM_MYSQL_ENGINE@; +delimiter // +DROP TRIGGER IF EXISTS Events_Hour_insert_trigger// + +CREATE TRIGGER Events_Hour_insert_trigger AFTER INSERT ON Events_Hour +FOR EACH ROW BEGIN + UPDATE Monitors SET HourEvents = COALESCE(HourEvents,0)+1 WHERE Id=NEW.MonitorId; +END; +// +DROP TRIGGER IF EXISTS Events_Hour_delete_trigger// +CREATE TRIGGER Events_Hour_delete_trigger BEFORE DELETE ON Events_Hour +FOR EACH ROW BEGIN + UPDATE Monitors SET HourEvents = COALESCE(HourEvents,1)-1, + HourEventDiskSpace=COALESCE(HourEventDiskSpace)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// +DELIMITER ; + DROP TABLE IF EXISTS `Events_Day`; CREATE TABLE `Events_Day` ( `EventId` int(10) unsigned NOT NULL, @@ -239,7 +257,26 @@ CREATE TABLE `Events_Day` ( KEY `Events_Day_StartTime_idx` (`StartTime`) ) ENGINE=@ZM_MYSQL_ENGINE@; +delimiter // +DROP TRIGGER IF EXISTS Events_Day_insert_trigger// +CREATE TRIGGER Events_Day_insert_trigger AFTER INSERT ON Events_Day +FOR EACH ROW BEGIN + UPDATE Monitors SET + DayEvents = COALESCE(DayEvents,0)+1, + DayEventDiskSpace=COALESCE(DayEventDiskSpace)+COALESCE(NEW.DiskSpace,0) + WHERE Id=NEW.MonitorId; +END; +// +DROP TRIGGER IF EXISTS Events_Day_delete_trigger// +CREATE TRIGGER Events_Day_delete_trigger BEFORE DELETE ON Events_Day +FOR EACH ROW BEGIN + UPDATE Monitors SET DayEvents = COALESCE(DayEvents,1)-1, + DayEventDiskSpace=COALESCE(DayEventDiskSpace)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// +DELIMITER ; DROP TABLE IF EXISTS `Events_Week`; CREATE TABLE `Events_Week` ( `EventId` int(10) unsigned NOT NULL, @@ -251,6 +288,28 @@ CREATE TABLE `Events_Week` ( KEY `Events_Week_StartTime_idx` (`StartTime`) ) ENGINE=@ZM_MYSQL_ENGINE@; +delimiter // +DROP TRIGGER IF EXISTS Events_Week_insert_trigger// +CREATE TRIGGER Events_Week_insert_trigger AFTER INSERT ON Events_Week +FOR EACH ROW BEGIN + UPDATE Monitors SET + WeekEvents = COALESCE(WeekEvents,0)+1, + WeekEventDiskSpace=COALESCE(WeekEventDiskSpace)+COALESCE(NEW.DiskSpace,0) + WHERE Id=NEW.MonitorId; +END; +// +DROP TRIGGER IF EXISTS Events_Week_delete_trigger// +CREATE TRIGGER Events_Week_delete_trigger BEFORE DELETE ON Events_Week +FOR EACH ROW BEGIN + UPDATE Monitors SET + WeekEvents = COALESCE(WeekEvents,1)-1 + WeekEventDiskSpace=COALESCE(WeekEventDiskSpace)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DELIMITER ; + DROP TABLE IF EXISTS `Events_Month`; CREATE TABLE `Events_Month` ( `EventId` int(10) unsigned NOT NULL, @@ -262,6 +321,27 @@ CREATE TABLE `Events_Month` ( KEY `Events_Month_StartTime_idx` (`StartTime`) ) ENGINE=@ZM_MYSQL_ENGINE@; +delimiter // +DROP TRIGGER IF EXISTS Events_Month_insert_trigger// +create trigger Events_Month_insert_trigger after insert on Events_Month +FOR EACH ROW BEGIN + UPDATE Monitors SET + MonthEvents = COALESCE(MonthEvents,0)+1, + MonthEventDiskSpace=COALESCE(MonthEventDiskSpace)+COALESCE(NEW.DiskSpace,0) + WHERE Id=NEW.MonitorId; +END; +// +DROP TRIGGER IF EXISTS Events_Month_delete_trigger// +CREATE TRIGGER Events_Month_delete_trigger BEFORE DELETE ON Events_Month +FOR EACH ROW BEGIN + UPDATE Monitors SET + MonthEvents = COALESCE(MonthEvents,1)-1 + MonthEventDiskSpace=COALESCE(MonthEventDiskSpace)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DELIMITER ; DROP TABLE IF EXISTS `Events_Archived`; CREATE TABLE `Events_Archived` ( @@ -346,6 +426,9 @@ delimiter ; DROP TRIGGER IF EXISTS event_insert_trigger; delimiter // +/* The assumption is that when an Event is inserted, it has no size yet, so don't bother updating the DiskSpace, just the count. + * The DiskSpace will get update in the Event Update Trigger + */ create trigger event_insert_trigger after insert on Events for each row begin @@ -354,7 +437,13 @@ for each row INSERT INTO Events_Day (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0); INSERT INTO Events_Week (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0); INSERT INTO Events_Month (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0); - UPDATE Monitors SET TotalEvents = COALESCE(TotalEvents,0)+1 WHERE Id=NEW.MonitorId; + UPDATE Monitors SET + HourEvents = COALESCE(DayEvents,0)+1, + DayEvents = COALESCE(DayEvents,0)+1, + WeekEvents = COALESCE(DayEvents,0)+1, + MonthEvents = COALESCE(DayEvents,0)+1 + TotalEvents = COALESCE(TotalEvents,0)+1 + WHERE Id=NEW.MonitorId; end; // @@ -365,7 +454,7 @@ drop trigger if exists event_delete_trigger; delimiter // -CREATE TRIGGER event_delete_trigger BEFORE DELETe on Events +CREATE TRIGGER event_delete_trigger BEFORE DELETE ON Events FOR EACH ROW BEGIN diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index 7be542391..b0fd7697b 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -647,17 +647,17 @@ MAIN: while( $loop ) { # Number of rows my $selectLogRowCountSql = 'SELECT count(*) AS Rows FROM Logs'; my $selectLogRowCountSth = $dbh->prepare_cached( $selectLogRowCountSql ) - or Fatal( "Can't prepare '$selectLogRowCountSql': ".$dbh->errstr() ); + or Fatal( "Can't prepare '$selectLogRowCountSql': ".$dbh->errstr() ); $res = $selectLogRowCountSth->execute() - or Fatal( "Can't execute: ".$selectLogRowCountSth->errstr() ); + or Fatal( "Can't execute: ".$selectLogRowCountSth->errstr() ); my $row = $selectLogRowCountSth->fetchrow_hashref(); my $logRows = $row->{Rows}; if ( $logRows > $Config{ZM_LOG_DATABASE_LIMIT} ) { my $deleteLogByRowsSql = 'DELETE low_priority FROM Logs ORDER BY TimeKey ASC LIMIT ?'; my $deleteLogByRowsSth = $dbh->prepare_cached( $deleteLogByRowsSql ) - or Fatal( "Can't prepare '$deleteLogByRowsSql': ".$dbh->errstr() ); + or Fatal( "Can't prepare '$deleteLogByRowsSql': ".$dbh->errstr() ); $res = $deleteLogByRowsSth->execute( $logRows - $Config{ZM_LOG_DATABASE_LIMIT} ) - or Fatal( "Can't execute: ".$deleteLogByRowsSth->errstr() ); + or Fatal( "Can't execute: ".$deleteLogByRowsSth->errstr() ); if ( $deleteLogByRowsSth->rows() ) { aud_print( 'Deleted '.$deleteLogByRowsSth->rows() ." log table entries by count\n" ); } @@ -668,9 +668,9 @@ MAIN: while( $loop ) { 'DELETE low_priority FROM Logs WHERE TimeKey < unix_timestamp(now() - interval '.$Config{ZM_LOG_DATABASE_LIMIT}.')'; my $deleteLogByTimeSth = $dbh->prepare_cached( $deleteLogByTimeSql ) - or Fatal( "Can't prepare '$deleteLogByTimeSql': ".$dbh->errstr() ); + or Fatal( "Can't prepare '$deleteLogByTimeSql': ".$dbh->errstr() ); $res = $deleteLogByTimeSth->execute() - or Fatal( "Can't execute: ".$deleteLogByTimeSth->errstr() ); + or Fatal( "Can't execute: ".$deleteLogByTimeSth->errstr() ); if ( $deleteLogByTimeSth->rows() ){ aud_print( 'Deleted '.$deleteLogByTimeSth->rows() ." log table entries by time\n" ); @@ -680,7 +680,7 @@ MAIN: while( $loop ) { $loop = $continuous; my $eventcounts_sql = q` -UPDATE Monitors SET + UPDATE Monitors SET TotalEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id), TotalEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND DiskSpace IS NOT NULL), HourEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB( NOW(), INTERVAL 1 hour) ), @@ -693,12 +693,56 @@ UPDATE Monitors SET MonthEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 month) AND DiskSpace IS NOT NULL), ArchivedEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND Archived=1), ArchivedEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND Archived=1 AND DiskSpace IS NOT NULL) -`; + `; -my $eventcounts_sth = $dbh->prepare_cached( $eventcounts_sql ); -$eventcounts_sth->execute(); -$eventcounts_sth->finish(); + my $eventcounts_sth = $dbh->prepare_cached( $eventcounts_sql ); + $eventcounts_sth->execute(); + $eventcounts_sth->finish(); + my $eventcounts_hour_sql = q` + UPDATE Monitors INNER JOIN ( + SELECT MonitorId, COUNT(*) AS HourEvents, SUM(COALESCE(DiskSpace,0)) AS HourEventDiskSpace + FROM Events_Hour GROUP BY MonitorId + ) AS E ON E.MonitorId=Monitors.Id SET + Monitors.HourEvents = E.HourEvents, + Monitors.HourEventDiskSpace = E.HourEventDiskSpace + `; + + + my $eventcounts_day_sql = q` + UPDATE Monitors INNER JOIN ( + SELECT MonitorId, COUNT(*) AS DayEvents, SUM(COALESCE(DiskSpace,0)) AS DayEventDiskSpace + FROM Events_Day GROUP BY MonitorId + ) AS E ON E.MonitorId=Monitors.Id SET + Monitors.DayEvents = E.DayEvents, + Monitors.DayEventDiskSpace = E.DayEventDiskSpace + `; + + my $eventcounts_week_sql = q` + UPDATE Monitors INNER JOIN ( + SELECT MonitorId, COUNT(*) AS WeekEvents, SUM(COALESCE(DiskSpace,0)) AS WeekEventDiskSpace + FROM Events_Week GROUP BY MonitorId + ) AS E ON E.MonitorId=Monitors.Id SET + Monitors.WeekEvents = E.WeekEvents, + Monitors.WeekEventDiskSpace = E.WeekEventDiskSpace + `; + + my $eventcounts_month_sql = q` + UPDATE Monitors INNER JOIN ( + SELECT MonitorId, COUNT(*) AS MonthEvents, SUM(COALESCE(DiskSpace,0)) AS MonthEventDiskSpace + FROM Events_Month GROUP BY MonitorId + ) AS E ON E.MonitorId=Monitors.Id SET + Monitors.MonthEvents = E.MonthEvents, + Monitors.MonthEventDiskSpace = E.MonthEventDiskSpace + `; + my $eventcounts_hour_sth = $dbh->prepare_cached( $eventcounts_hour_sql ); + my $eventcounts_day_sth = $dbh->prepare_cached( $eventcounts_day_sql ); + my $eventcounts_week_sth = $dbh->prepare_cached( $eventcounts_week_sql ); + my $eventcounts_month_sth = $dbh->prepare_cached( $eventcounts_month_sql ); + $eventcounts_hour_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); + $eventcounts_day_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); + $eventcounts_week_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); + $eventcounts_month_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); sleep( $Config{ZM_AUDIT_CHECK_INTERVAL} ) if $continuous; }; diff --git a/scripts/zmstats.pl.in b/scripts/zmstats.pl.in index 3ea850ef5..3c955020c 100644 --- a/scripts/zmstats.pl.in +++ b/scripts/zmstats.pl.in @@ -76,81 +76,6 @@ my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=?' : 'S my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); -my $eventcounts_sql = q` -UPDATE Monitors INNER JOIN ( - SELECT MonitorId, - COUNT(Id) AS TotalEvents, - SUM(DiskSpace) AS TotalEventDiskSpace, - SUM(IF(Archived,1,0)) AS ArchivedEvents, - SUM(IF(Archived,DiskSpace,0)) AS ArchivedEventDiskSpace, - SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 hour),1,0)) AS HourEvents, - SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 hour),DiskSpace,0)) AS HourEventDiskSpace, - SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 day),1,0)) AS DayEvents, - SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 day),DiskSpace,0)) AS DayEventDiskSpace, - SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 week),1,0)) AS WeekEvents, - SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 week),DiskSpace,0)) AS WeekEventDiskSpace, - SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 month),1,0)) AS MonthEvents, - SUM(IF(StartTime > DATE_SUB(NOW(), INTERVAL 1 month),DiskSpace,0)) AS MonthEventDiskSpace - FROM Events GROUP BY MonitorId - ) AS E ON E.MonitorId=Monitors.Id SET - Monitors.TotalEvents = E.TotalEvents, - Monitors.TotalEventDiskSpace = E.TotalEventDiskSpace, - Monitors.ArchivedEvents = E.ArchivedEvents, - Monitors.ArchivedEventDiskSpace = E.ArchivedEventDiskSpace, - Monitors.HourEvents = E.HourEvents, - Monitors.HourEventDiskSpace = E.HourEventDiskSpace, - Monitors.DayEvents = E.DayEvents, - Monitors.DayEventDiskSpace = E.DayEventDiskSpace, - Monitors.WeekEvents = E.WeekEvents, - Monitors.WeekEventDiskSpace = E.WeekEventDiskSpace, - Monitors.MonthEvents = E.MonthEvents, - Monitors.MonthEventDiskSpace = E.MonthEventDiskSpace - `; - - - my $eventcounts_hour_sql = q` -UPDATE Monitors INNER JOIN ( - SELECT MonitorId, COUNT(*) AS HourEvents, SUM(COALESCE(DiskSpace,0)) AS HourEventDiskSpace - FROM Events_Hour GROUP BY MonitorId - ) AS E ON E.MonitorId=Monitors.Id SET - Monitors.HourEvents = E.HourEvents, - Monitors.HourEventDiskSpace = E.HourEventDiskSpace - `; - - - my $eventcounts_day_sql = q` -UPDATE Monitors INNER JOIN ( - SELECT MonitorId, COUNT(*) AS DayEvents, SUM(COALESCE(DiskSpace,0)) AS DayEventDiskSpace - FROM Events_Day GROUP BY MonitorId - ) AS E ON E.MonitorId=Monitors.Id SET - Monitors.DayEvents = E.DayEvents, - Monitors.DayEventDiskSpace = E.DayEventDiskSpace - `; - - my $eventcounts_week_sql = q` -UPDATE Monitors INNER JOIN ( - SELECT MonitorId, COUNT(*) AS WeekEvents, SUM(COALESCE(DiskSpace,0)) AS WeekEventDiskSpace - FROM Events_Week GROUP BY MonitorId - ) AS E ON E.MonitorId=Monitors.Id SET - Monitors.WeekEvents = E.WeekEvents, - Monitors.WeekEventDiskSpace = E.WeekEventDiskSpace - `; - - my $eventcounts_month_sql = q` -UPDATE Monitors INNER JOIN ( - SELECT MonitorId, COUNT(*) AS MonthEvents, SUM(COALESCE(DiskSpace,0)) AS MonthEventDiskSpace - FROM Events_Month GROUP BY MonitorId - ) AS E ON E.MonitorId=Monitors.Id SET - Monitors.MonthEvents = E.MonthEvents, - Monitors.MonthEventDiskSpace = E.MonthEventDiskSpace - `; - - -my $eventcounts_sth = $dbh->prepare_cached( $eventcounts_sql ); -my $eventcounts_hour_sth = $dbh->prepare_cached( $eventcounts_hour_sql ); -my $eventcounts_day_sth = $dbh->prepare_cached( $eventcounts_day_sql ); -my $eventcounts_week_sth = $dbh->prepare_cached( $eventcounts_week_sql ); -my $eventcounts_month_sth = $dbh->prepare_cached( $eventcounts_month_sql ); while( 1 ) { while ( ! ( $dbh and $dbh->ping() ) ) { @@ -162,10 +87,6 @@ while( 1 ) { $dbh->do('DELETE FROM Events_Day WHERE StartTime < DATE_SUB(NOW(), INTERVAL 1 day)'); $dbh->do('DELETE FROM Events_Week WHERE StartTime < DATE_SUB(NOW(), INTERVAL 1 week)'); $dbh->do('DELETE FROM Events_Month WHERE StartTime < DATE_SUB(NOW(), INTERVAL 1 month)'); - $eventcounts_hour_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); - $eventcounts_day_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); - $eventcounts_week_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); - $eventcounts_month_sth->execute( ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); sleep( $Config{ZM_STATS_UPDATE_INTERVAL} ); } # end while (1)