diff --git a/db/zm_update-1.31.19.sql b/db/zm_update-1.31.19.sql index bd46cc968..1c1b02b38 100644 --- a/db/zm_update-1.31.19.sql +++ b/db/zm_update-1.31.19.sql @@ -18,6 +18,40 @@ end; delimiter ; +drop procedure if exists update_monitor_stats; + +delimiter // + +create procedure update_monitor_stats(IN MonitorId int(10), IN increment INT, IN space BIGINT) + +sql security invoker + +deterministic + +begin + + update Monitors set + TotalEvents = TotalEvents + increment, + TotalEventDiskSpace = TotalEventDiskSpace + increment * space, + HourEvents = HourEvents + increment, + HourEventDiskSpace = HourEventDiskSpace + increment * space, + DayEvents = DayEvents + increment, + DayEventDiskSpace = DayEventDiskSpace + increment * space, + WeekEvents = WeekEvents + increment, + WeekEventDiskSpace = WeekEventDiskSpace + increment * space, + MonthEvents = MonthEvents + increment, + MonthEventDiskSpace = MonthEventDiskSpace + increment * space, + ArchivedEvents = ArchivedEvents + increment, + ArchivedEventDiskSpace = ArchivedEventDiskSpace + increment * space + where Id = MonitorId; + +end; + +// + +delimiter ; + + drop trigger if exists event_update_trigger; delimiter // @@ -42,6 +76,8 @@ begin call update_storage_stats(OLD.StorageId, -OLD.DiskSpace); END IF; + call update_monitor_stats( OLD.MonitorId, -1, OLD.DiskSpace ); + call update_monitor_stats( NEW.MonitorId, 1, NEW.DiskSpace ); end; @@ -64,6 +100,7 @@ for each row begin call update_storage_stats(NEW.StorageId, NEW.DiskSpace); + call update_monitor_stats( NEW.MonitorId, 1, NEW.DiskSpace ); end; @@ -87,6 +124,7 @@ for each row begin call update_storage_stats(OLD.StorageId, -OLD.DiskSpace); + call update_monitor_stats( OLD.MonitorId, -1, OLD.DiskSpace ); end; diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index e1c97e448..a05d631be 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -593,6 +593,26 @@ Debug("Event $db_event is not in fs."); } # end if ZM_LOG_DATABASE_LIMIT $loop = $continuous; + my $eventcounts_sql = q` +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) ), + HourEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 hour) AND DiskSpace IS NOT NULL), + DayEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 day)), + DayEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 day) AND DiskSpace IS NOT NULL), + WeekEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 week)), + WeekEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 week) AND DiskSpace IS NOT NULL), + MonthEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB( NOW(), INTERVAL 1 month)), + 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(); + sleep( $Config{ZM_AUDIT_CHECK_INTERVAL} ) if $continuous; }; diff --git a/scripts/zmwatch.pl.in b/scripts/zmwatch.pl.in index afd9acc11..e2f8197a3 100644 --- a/scripts/zmwatch.pl.in +++ b/scripts/zmwatch.pl.in @@ -79,31 +79,11 @@ 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 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) ), - HourEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 hour) AND DiskSpace IS NOT NULL), - DayEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 day)), - DayEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 day) AND DiskSpace IS NOT NULL), - WeekEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 week)), - WeekEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 week) AND DiskSpace IS NOT NULL), - MonthEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB( NOW(), INTERVAL 1 month)), - 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) - WHERE Id=?`; - -my $eventcounts_sth = $dbh->prepare_cached( $eventcounts_sql ); - while( 1 ) { my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () ) or Fatal( "Can't execute: ".$sth->errstr() ); while( my $monitor = $sth->fetchrow_hashref() ) { - $eventcounts_sth->execute( $$monitor{Id} ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); - my $now = time(); next if $monitor->{Function} eq 'None'; my $restart = 0; @@ -199,7 +179,6 @@ while( 1 ) { } # end foreach monitor - $eventcounts_sth->finish(); sleep( $Config{ZM_WATCH_CHECK_INTERVAL} ); } # end while (1)