Move eventcounts update from zmwatch.pl to triggers and zmaudit.pl

This commit is contained in:
Isaac Connor 2018-01-04 15:04:13 -05:00
parent 4033cf5ffc
commit db0e35bf55
3 changed files with 58 additions and 21 deletions

View File

@ -18,6 +18,40 @@ end;
delimiter ; 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; drop trigger if exists event_update_trigger;
delimiter // delimiter //
@ -42,6 +76,8 @@ begin
call update_storage_stats(OLD.StorageId, -OLD.DiskSpace); call update_storage_stats(OLD.StorageId, -OLD.DiskSpace);
END IF; END IF;
call update_monitor_stats( OLD.MonitorId, -1, OLD.DiskSpace );
call update_monitor_stats( NEW.MonitorId, 1, NEW.DiskSpace );
end; end;
@ -64,6 +100,7 @@ for each row
begin begin
call update_storage_stats(NEW.StorageId, NEW.DiskSpace); call update_storage_stats(NEW.StorageId, NEW.DiskSpace);
call update_monitor_stats( NEW.MonitorId, 1, NEW.DiskSpace );
end; end;
@ -87,6 +124,7 @@ for each row
begin begin
call update_storage_stats(OLD.StorageId, -OLD.DiskSpace); call update_storage_stats(OLD.StorageId, -OLD.DiskSpace);
call update_monitor_stats( OLD.MonitorId, -1, OLD.DiskSpace );
end; end;

View File

@ -593,6 +593,26 @@ Debug("Event $db_event is not in fs.");
} # end if ZM_LOG_DATABASE_LIMIT } # end if ZM_LOG_DATABASE_LIMIT
$loop = $continuous; $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; sleep( $Config{ZM_AUDIT_CHECK_INTERVAL} ) if $continuous;
}; };

View File

@ -79,31 +79,11 @@ my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=?' : 'S
my $sth = $dbh->prepare_cached( $sql ) my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); 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 ) { while( 1 ) {
my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () ) my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () )
or Fatal( "Can't execute: ".$sth->errstr() ); or Fatal( "Can't execute: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() ) { while( my $monitor = $sth->fetchrow_hashref() ) {
$eventcounts_sth->execute( $$monitor{Id} ) or Error( "Can't execute: ".$eventcounts_sth->errstr() );
my $now = time(); my $now = time();
next if $monitor->{Function} eq 'None'; next if $monitor->{Function} eq 'None';
my $restart = 0; my $restart = 0;
@ -199,7 +179,6 @@ while( 1 ) {
} # end foreach monitor } # end foreach monitor
$eventcounts_sth->finish();
sleep( $Config{ZM_WATCH_CHECK_INTERVAL} ); sleep( $Config{ZM_WATCH_CHECK_INTERVAL} );
} # end while (1) } # end while (1)