diff --git a/db/zm_create.sql.in b/db/zm_create.sql.in index f9bb3866d..ecb3fe0dc 100644 --- a/db/zm_create.sql.in +++ b/db/zm_create.sql.in @@ -224,10 +224,41 @@ CREATE TABLE `Events_Hour` ( `StartTime` datetime default NULL, `DiskSpace` bigint unsigned default NULL, PRIMARY KEY (`EventId`), - KEY `Events_Hour_MonitorId_idx` (`MonitorId`) + KEY `Events_Hour_MonitorId_idx` (`MonitorId`), KEY `Events_Hour_StartTime_idx` (`StartTime`) ) ENGINE=@ZM_MYSQL_ENGINE@; +delimiter // +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; +// + +DROP TRIGGER IF EXISTS Events_Hour_update_trigger// + +CREATE TRIGGER Events_Hour_update_trigger AFTER UPDATE ON Events_Hour +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + IF ( NEW.MonitorID != OLD.MonitorID ) THEN + UPDATE Monitors SET HourEventDiskSpace=COALESCE(DayEventDiskSpace,0)-OLD.DiskSpace WHERE Monitors.Id=OLD.MonitorId; + UPDATE Monitors SET HourEventDiskSpace=COALESCE(DayEventDiskSpace,0)+NEW.DiskSpace WHERE Monitors.Id=NEW.MonitorId; + ELSE + UPDATE Monitors SET HourEventDiskSpace=COALESCE(DayEventDiskSpace,0)+diff WHERE Monitors.Id=NEW.MonitorId; + END IF; + END IF; + END; + +DELIMITER ; + DROP TABLE IF EXISTS `Events_Day`; CREATE TABLE `Events_Day` ( `EventId` int(10) unsigned NOT NULL, @@ -235,11 +266,41 @@ CREATE TABLE `Events_Day` ( `StartTime` datetime default NULL, `DiskSpace` bigint unsigned default NULL, PRIMARY KEY (`EventId`), - KEY `Events_Day_MonitorId_idx` (`MonitorId`) + KEY `Events_Day_MonitorId_idx` (`MonitorId`), KEY `Events_Day_StartTime_idx` (`StartTime`) ) ENGINE=@ZM_MYSQL_ENGINE@; +delimiter // +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// +DROP TRIGGER IF EXISTS Events_Day_update_trigger; +CREATE TRIGGER Events_Day_update_trigger AFTER UPDATE ON Events_Day +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + IF ( NEW.MonitorID != OLD.MonitorID ) THEN + UPDATE Monitors SET DayEventDiskSpace=COALESCE(DayEventDiskSpace,0)-OLD.DiskSpace WHERE Monitors.Id=OLD.MonitorId; + UPDATE Monitors SET DayEventDiskSpace=COALESCE(DayEventDiskSpace,0)+NEW.DiskSpace WHERE Monitors.Id=NEW.MonitorId; + ELSE + UPDATE Monitors SET DayEventDiskSpace=COALESCE(DayEventDiskSpace,0)+diff WHERE Monitors.Id=NEW.MonitorId; + END IF; + END IF; + END; + // + + +DELIMITER ; DROP TABLE IF EXISTS `Events_Week`; CREATE TABLE `Events_Week` ( `EventId` int(10) unsigned NOT NULL, @@ -247,10 +308,41 @@ CREATE TABLE `Events_Week` ( `StartTime` datetime default NULL, `DiskSpace` bigint unsigned default NULL, PRIMARY KEY (`EventId`), - KEY `Events_Week_MonitorId_idx` (`MonitorId`) + KEY `Events_Week_MonitorId_idx` (`MonitorId`), KEY `Events_Week_StartTime_idx` (`StartTime`) ) ENGINE=@ZM_MYSQL_ENGINE@; +delimiter // +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS Events_Week_update_trigger; +CREATE TRIGGER Events_Week_update_trigger AFTER UPDATE ON Events_Week +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + IF ( NEW.MonitorID != OLD.MonitorID ) THEN + UPDATE Monitors SET WeekEventDiskSpace=COALESCE(WeekEventDiskSpace,0)-OLD.DiskSpace WHERE Monitors.Id=OLD.MonitorId; + UPDATE Monitors SET WeekEventDiskSpace=COALESCE(WeekEventDiskSpace,0)+NEW.DiskSpace WHERE Monitors.Id=NEW.MonitorId; + ELSE + UPDATE Monitors SET WeekEventDiskSpace=COALESCE(WeekEventDiskSpace,0)+diff WHERE Monitors.Id=NEW.MonitorId; + END IF; + END IF; + END; + // + +DELIMITER ; + DROP TABLE IF EXISTS `Events_Month`; CREATE TABLE `Events_Month` ( `EventId` int(10) unsigned NOT NULL, @@ -258,10 +350,42 @@ CREATE TABLE `Events_Month` ( `StartTime` datetime default NULL, `DiskSpace` bigint unsigned default NULL, PRIMARY KEY (`EventId`), - KEY `Events_Month_MonitorId_idx` (`MonitorId`) + KEY `Events_Month_MonitorId_idx` (`MonitorId`), KEY `Events_Month_StartTime_idx` (`StartTime`) ) ENGINE=@ZM_MYSQL_ENGINE@; +delimiter // +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + + +DROP TRIGGER IF EXISTS Events_Month_update_trigger; +CREATE TRIGGER Events_Month_update_trigger AFTER UPDATE ON Events_Month +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + IF ( NEW.MonitorID != OLD.MonitorID ) THEN + UPDATE Monitors SET MonthEventDiskSpace=COALESCE(MonthEventDiskSpace,0)-OLD.DiskSpace WHERE Monitors.Id=OLD.MonitorId; + UPDATE Monitors SET MonthEventDiskSpace=COALESCE(MonthEventDiskSpace,0)+NEW.DiskSpace WHERE Monitors.Id=NEW.MonitorId; + ELSE + UPDATE Monitors SET MonthEventDiskSpace=COALESCE(MonthEventDiskSpace,0)+diff WHERE Monitors.Id=NEW.MonitorId; + END IF; + END IF; + END; + // + + +DELIMITER ; DROP TABLE IF EXISTS `Events_Archived`; CREATE TABLE `Events_Archived` ( @@ -346,27 +470,30 @@ delimiter ; DROP TRIGGER IF EXISTS event_insert_trigger; delimiter // -create trigger event_insert_trigger after insert on Events -for each row - begin +/* 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 INSERT INTO Events_Hour (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0); 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; -end; + 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; // -delimiter ; - - -drop trigger if exists event_delete_trigger; - -delimiter // - -CREATE TRIGGER event_delete_trigger BEFORE DELETe on Events +DROP TRIGGER IF EXISTS event_delete_trigger// +CREATE TRIGGER event_delete_trigger BEFORE DELETE ON Events FOR EACH ROW BEGIN call update_storage_stats(OLD.StorageId, -OLD.DiskSpace); @@ -378,14 +505,14 @@ BEGIN DELETE FROM Events_Archived WHERE EventId=OLD.Id; UPDATE Monitors SET ArchivedEvents = ArchivedEvents - 1, - ArchivedEventDiskSpace = ArchivedEventDiskSpace - OLD.DiskSpace, + ArchivedEventDiskSpace = COALESCE(ArchivedEventDiskSpace,0) - COALESCE(OLD.DiskSpace,0), TotalEvents = TotalEvents - 1, - TotalEventDiskSpace = TotalEventDiskSpace - OLD.DiskSpace + TotalEventDiskSpace = COALESCE(TotalEventDiskSpace,0) - COALESCE(OLD.DiskSpace,0) WHERE Id=OLD.MonitorId; ELSE UPDATE Monitors SET TotalEvents = TotalEvents-1, - TotalEventDiskSpace=TotalEventDiskSpace-OLD.DiskSpace + TotalEventDiskSpace=COALESCE(TotalEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0) WHERE Id=OLD.MonitorId; END IF; END; diff --git a/db/zm_update-1.31.33.sql b/db/zm_update-1.31.33.sql new file mode 100644 index 000000000..92ed1275d --- /dev/null +++ b/db/zm_update-1.31.33.sql @@ -0,0 +1,31 @@ +drop trigger if exists event_delete_trigger; + +delimiter // + +CREATE TRIGGER event_delete_trigger BEFORE DELETe on Events + +FOR EACH ROW + BEGIN + call update_storage_stats(OLD.StorageId, -OLD.DiskSpace); + DELETE FROM Events_Hour WHERE EventId=OLD.Id; + DELETE FROM Events_Day WHERE EventId=OLD.Id; + DELETE FROM Events_Week WHERE EventId=OLD.Id; + DELETE FROM Events_Month WHERE EventId=OLD.Id; + IF ( OLD.Archived ) THEN + DELETE FROM Events_Archived WHERE EventId=OLD.Id; + UPDATE Monitors SET + ArchivedEvents = ArchivedEvents - 1, + ArchivedEventDiskSpace = COALESCE(ArchivedEventDiskSpace,0) - COALESCE(OLD.DiskSpace,0), + TotalEvents = TotalEvents - 1, + TotalEventDiskSpace = COALESCE(TotalEventDiskSpace,0) - COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; + ELSE + UPDATE Monitors SET + TotalEvents = TotalEvents-1, + TotalEventDiskSpace=COALESCE(TotalEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; + END IF; +END; + +// + diff --git a/db/zm_update-1.31.34.sql b/db/zm_update-1.31.34.sql new file mode 100644 index 000000000..24c0ee844 --- /dev/null +++ b/db/zm_update-1.31.34.sql @@ -0,0 +1,62 @@ +delimiter // + +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS event_insert_trigger// + +CREATE TRIGGER event_insert_trigger AFTER INSERT ON Events +FOR EACH ROW + BEGIN + INSERT INTO Events_Hour (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0); + 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 + HourEvents = COALESCE(HourEvents,0)+1, + DayEvents = COALESCE(DayEvents,0)+1, + WeekEvents = COALESCE(WeekEvents,0)+1, + MonthEvents = COALESCE(MonthEvents,0)+1, + TotalEvents = COALESCE(TotalEvents,0)+1 + WHERE Id=NEW.MonitorId; + END; + // + + diff --git a/db/zm_update-1.31.35.sql b/db/zm_update-1.31.35.sql new file mode 100644 index 000000000..a347d9159 --- /dev/null +++ b/db/zm_update-1.31.35.sql @@ -0,0 +1,140 @@ +delimiter // + +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS Events_Hour_update_trigger; +CREATE TRIGGER Events_Hour_update_trigger AFTER UPDATE ON Events_Hour +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + UPDATE Monitors SET HourEventDiskSpace=COALESCE(HourEventDiskSpace,0)+diff WHERE Monitors.Id=MonitorId; + END IF; +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS Events_Day_update_trigger; +CREATE TRIGGER Events_Day_update_trigger AFTER UPDATE ON Events_Day +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + UPDATE Monitors SET DayEventDiskSpace=COALESCE(DayEventDiskSpace,0)+diff WHERE Monitors.Id=MonitorId; + END IF; +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS Events_Week_update_trigger; +CREATE TRIGGER Events_Week_update_trigger AFTER UPDATE ON Events_Week +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + UPDATE Monitors SET WeekEventDiskSpace=COALESCE(WeekEventDiskSpace,0)+diff WHERE Monitors.Id=MonitorId; + END IF; +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS Events_Month_update_trigger; +CREATE TRIGGER Events_Month_update_trigger AFTER UPDATE ON Events_Month +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + UPDATE Monitors SET MonthEventDiskSpace=COALESCE(MonthEventDiskSpace,0)+diff WHERE Monitors.Id=MonitorId; + END IF; +END; +// + +DROP TRIGGER IF EXISTS event_insert_trigger// + +CREATE TRIGGER event_insert_trigger AFTER INSERT ON Events +FOR EACH ROW + BEGIN + INSERT INTO Events_Hour (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0); + 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 + HourEvents = COALESCE(HourEvents,0)+1, + DayEvents = COALESCE(DayEvents,0)+1, + WeekEvents = COALESCE(WeekEvents,0)+1, + MonthEvents = COALESCE(MonthEvents,0)+1, + TotalEvents = COALESCE(TotalEvents,0)+1 + WHERE Id=NEW.MonitorId; + END; + // + +delimiter ; +SET @s = (SELECT IF( + (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE() + AND table_name = 'Events' + AND column_name = 'Scheme' + ) > 0, + "SELECT 'Column Scheme already exists in Events'", + "ALTER TABLE Events ADD `Scheme` enum('Deep','Medium','Shallow') NOT NULL default 'Deep' AFTER `DiskSpace`" + )); + + PREPARE stmt FROM @s; + EXECUTE stmt; + + +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) diff --git a/db/zm_update-1.31.36.sql b/db/zm_update-1.31.36.sql new file mode 100644 index 000000000..a22507f81 --- /dev/null +++ b/db/zm_update-1.31.36.sql @@ -0,0 +1,148 @@ +delimiter // + +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS Events_Hour_update_trigger; +CREATE TRIGGER Events_Hour_update_trigger AFTER UPDATE ON Events_Hour +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + IF ( NEW.MonitorID != OLD.MonitorID ) THEN + UPDATE Monitors SET HourEventDiskSpace=COALESCE(DayEventDiskSpace,0)-OLD.DiskSpace WHERE Monitors.Id=OLD.MonitorId; + UPDATE Monitors SET HourEventDiskSpace=COALESCE(DayEventDiskSpace,0)+NEW.DiskSpace WHERE Monitors.Id=NEW.MonitorId; + ELSE + UPDATE Monitors SET HourEventDiskSpace=COALESCE(DayEventDiskSpace,0)+diff WHERE Monitors.Id=NEW.MonitorId; + END IF; + END IF; +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS Events_Day_update_trigger; +CREATE TRIGGER Events_Day_update_trigger AFTER UPDATE ON Events_Day +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + IF ( NEW.MonitorID != OLD.MonitorID ) THEN + UPDATE Monitors SET DayEventDiskSpace=COALESCE(DayEventDiskSpace,0)-OLD.DiskSpace WHERE Monitors.Id=OLD.MonitorId; + UPDATE Monitors SET DayEventDiskSpace=COALESCE(DayEventDiskSpace,0)+NEW.DiskSpace WHERE Monitors.Id=NEW.MonitorId; + ELSE + UPDATE Monitors SET DayEventDiskSpace=COALESCE(DayEventDiskSpace,0)+diff WHERE Monitors.Id=NEW.MonitorId; + END IF; + END IF; +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS Events_Week_update_trigger; +CREATE TRIGGER Events_Week_update_trigger AFTER UPDATE ON Events_Week +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + IF ( NEW.MonitorID != OLD.MonitorID ) THEN + UPDATE Monitors SET WeekEventDiskSpace=COALESCE(WeekEventDiskSpace,0)-OLD.DiskSpace WHERE Monitors.Id=OLD.MonitorId; + UPDATE Monitors SET WeekEventDiskSpace=COALESCE(WeekEventDiskSpace,0)+NEW.DiskSpace WHERE Monitors.Id=NEW.MonitorId; + ELSE + UPDATE Monitors SET WeekEventDiskSpace=COALESCE(WeekEventDiskSpace,0)+diff WHERE Monitors.Id=NEW.MonitorId; + END IF; + END IF; +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,0)-COALESCE(OLD.DiskSpace,0) + WHERE Id=OLD.MonitorId; +END; +// + +DROP TRIGGER IF EXISTS Events_Month_update_trigger; +CREATE TRIGGER Events_Month_update_trigger AFTER UPDATE ON Events_Month +FOR EACH ROW + BEGIN + declare diff BIGINT default 0; + + set diff = COALESCE(NEW.DiskSpace,0) - COALESCE(OLD.DiskSpace,0); + IF ( diff ) THEN + IF ( NEW.MonitorID != OLD.MonitorID ) THEN + UPDATE Monitors SET MonthEventDiskSpace=COALESCE(MonthEventDiskSpace,0)-OLD.DiskSpace WHERE Monitors.Id=OLD.MonitorId; + UPDATE Monitors SET MonthEventDiskSpace=COALESCE(MonthEventDiskSpace,0)+NEW.DiskSpace WHERE Monitors.Id=NEW.MonitorId; + ELSE + UPDATE Monitors SET MonthEventDiskSpace=COALESCE(MonthEventDiskSpace,0)+diff WHERE Monitors.Id=NEW.MonitorId; + END IF; + END IF; +END; +// + +DROP TRIGGER IF EXISTS event_insert_trigger// + +CREATE TRIGGER event_insert_trigger AFTER INSERT ON Events +FOR EACH ROW + BEGIN + INSERT INTO Events_Hour (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0); + 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 + HourEvents = COALESCE(HourEvents,0)+1, + DayEvents = COALESCE(DayEvents,0)+1, + WeekEvents = COALESCE(WeekEvents,0)+1, + MonthEvents = COALESCE(MonthEvents,0)+1, + TotalEvents = COALESCE(TotalEvents,0)+1 + WHERE Id=NEW.MonitorId; + END; + // + +delimiter ; + +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) 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) diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index b42caa39e..67c0b8ab6 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -107,7 +107,6 @@ FfmpegCamera::FfmpegCamera( int p_id, const std::string &p_path, const std::stri mRawFrame = NULL; mFrame = NULL; frameCount = 0; - mIsOpening = false; mCanCapture = false; mOpenStart = 0; @@ -122,6 +121,7 @@ FfmpegCamera::~FfmpegCamera() { int FfmpegCamera::PrimeCapture() { if ( mCanCapture ) { + Info( "Priming capture from %s", mPath.c_str() ); CloseFfmpeg(); } mVideoStreamId = -1; @@ -185,7 +185,6 @@ int FfmpegCamera::OpenFfmpeg() { int ret; mOpenStart = time(NULL); - mIsOpening = true; // Open the input, not necessarily a file #if !LIBAVFORMAT_VERSION_CHECK(53, 2, 0, 4, 0) @@ -224,7 +223,6 @@ int FfmpegCamera::OpenFfmpeg() { if ( avformat_open_input( &mFormatContext, mPath.c_str(), NULL, &opts ) != 0 ) #endif { - mIsOpening = false; Error("Unable to open input %s due to: %s", mPath.c_str(), strerror(errno)); return -1; } @@ -236,8 +234,7 @@ int FfmpegCamera::OpenFfmpeg() { monitor->GetLastEventId() ; - mIsOpening = false; - Debug ( 1, "Opened input" ); + Debug(1, "Opened input"); Info( "Stream open %s, parsing streams...", mPath.c_str() ); @@ -246,7 +243,10 @@ int FfmpegCamera::OpenFfmpeg() { #else if ( avformat_find_stream_info( mFormatContext, 0 ) < 0 ) #endif - Fatal("Unable to find stream info from %s due to: %s", mPath.c_str(), strerror(errno)); + { + Error("Unable to find stream info from %s due to: %s", mPath.c_str(), strerror(errno)); + return -1; + } Debug(4, "Got stream info"); diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index b237cf13a..fb6f5b76d 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -1226,12 +1226,16 @@ void Monitor::UpdateAnalysisFPS() { struct timeval now; gettimeofday( &now, NULL ); if ( analysis_image_count && fps_report_interval && !(analysis_image_count%fps_report_interval) ) { - analysis_fps = double(fps_report_interval)/(now.tv_sec - last_analysis_fps_time); - Info( "%s: %d - Analysing at %.2f fps", name, image_count, analysis_fps ); - static char sql[ZM_SQL_SML_BUFSIZ]; - snprintf( sql, sizeof(sql), "INSERT INTO Monitor_Status (MonitorId,AnalysisFPS) VALUES (%d, %.2lf) ON DUPLICATE KEY UPDATE AnalysisFPS = %.2lf", id, analysis_fps, analysis_fps ); - if ( mysql_query( &dbconn, sql ) ) { - Error( "Can't run query: %s", mysql_error( &dbconn ) ); + double new_analysis_fps = double(fps_report_interval)/(now.tv_sec - last_analysis_fps_time); + Info("%s: %d - Analysing at %.2f fps", name, image_count, new_analysis_fps); + if ( new_analysis_fps != analysis_fps ) { + analysis_fps = new_analysis_fps; + + static char sql[ZM_SQL_SML_BUFSIZ]; + snprintf( sql, sizeof(sql), "INSERT INTO Monitor_Status (MonitorId,AnalysisFPS) VALUES (%d, %.2lf) ON DUPLICATE KEY UPDATE AnalysisFPS = %.2lf", id, analysis_fps, analysis_fps ); + if ( mysql_query( &dbconn, sql ) ) { + Error("Can't run query: %s", mysql_error(&dbconn)); + } } last_analysis_fps_time = now.tv_sec; } @@ -2935,16 +2939,18 @@ int Monitor::Capture() { // If we are too fast, we get div by zero. This seems to happen in the case of audio packets. if ( now != last_fps_time ) { // # of images per interval / the amount of time it took - capture_fps = double(fps_report_interval)/(now-last_fps_time); + double new_capture_fps = double(fps_report_interval)/(now-last_fps_time); //Info( "%d -> %d -> %d", fps_report_interval, now, last_fps_time ); //Info( "%d -> %d -> %lf -> %lf", now-last_fps_time, fps_report_interval/(now-last_fps_time), double(fps_report_interval)/(now-last_fps_time), fps ); - Info( "%s: %d - Capturing at %.2lf fps", name, image_count, capture_fps ); - last_fps_time = now; - static char sql[ZM_SQL_SML_BUFSIZ]; - snprintf( sql, sizeof(sql), "INSERT INTO Monitor_Status (MonitorId,CaptureFPS) VALUES (%d, %.2lf) ON DUPLICATE KEY UPDATE CaptureFPS = %.2lf", id, capture_fps, capture_fps ); - if ( mysql_query( &dbconn, sql ) ) { - Error( "Can't run query: %s", mysql_error( &dbconn ) ); - } + Info("%s: %d - Capturing at %.2lf fps", name, image_count, capture_fps); + if ( new_capture_fps != capture_fps ) { + last_fps_time = now; + static char sql[ZM_SQL_SML_BUFSIZ]; + snprintf(sql, sizeof(sql), "INSERT INTO Monitor_Status (MonitorId,CaptureFPS) VALUES (%d, %.2lf) ON DUPLICATE KEY UPDATE CaptureFPS = %.2lf", id, capture_fps, capture_fps); + if ( mysql_query(&dbconn, sql) ) { + Error("Can't run query: %s", mysql_error(&dbconn)); + } + } // end if fps has changed } } // end if report fps } else { // result == 0 diff --git a/src/zm_remote_camera_http.cpp b/src/zm_remote_camera_http.cpp index 5aac4999a..8e305ee40 100644 --- a/src/zm_remote_camera_http.cpp +++ b/src/zm_remote_camera_http.cpp @@ -144,12 +144,12 @@ int RemoteCameraHttp::Connect() { if ( p == NULL ) { Error("Unable to connect to the remote camera, aborting"); - return( -1 ); + return -1; } - Debug( 3, "Connected to host, socket = %d", sd ); - return( sd ); -} + Debug(3, "Connected to host, socket = %d", sd); + return sd; +} // end int RemoteCameraHttp::Connect() int RemoteCameraHttp::Disconnect() { close( sd ); @@ -1006,8 +1006,8 @@ int RemoteCameraHttp::PreCapture() { if ( sd < 0 ) { Connect(); if ( sd < 0 ) { - Error( "Unable to connect to camera" ); - return( -1 ); + Error("Unable to connect to camera"); + return -1; } mode = SINGLE_IMAGE; buffer.clear(); diff --git a/src/zmc.cpp b/src/zmc.cpp index 518ec3db6..4caebb7c0 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -241,6 +241,7 @@ int main(int argc, char *argv[]) { int result = 0; while( ! zm_terminate ) { + result = 0; for ( int i = 0; i < n_monitors; i ++ ) { time_t now = (time_t)time(NULL); monitors[i]->setStartupTime(now); @@ -371,6 +372,7 @@ int main(int argc, char *argv[]) { } } // end foreach monitor delete [] analysis_threads; + sleep(10); } // end while ! zm_terminate outer connection loop delete [] monitors; diff --git a/version b/version index 71736e954..c2db3b3d2 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.31.32 +1.31.36 diff --git a/web/skins/classic/views/js/events.js b/web/skins/classic/views/js/events.js index 3fe4af690..b8946ec4d 100644 --- a/web/skins/classic/views/js/events.js +++ b/web/skins/classic/views/js/events.js @@ -57,7 +57,7 @@ function deleteEvents( element, name ) { } if ( count > 0 ) { if ( confirm( confirmDeleteEventsString ) ) { - form.action.value = 'delete'; + form.elements['action'].value = 'delete'; form.submit(); } } @@ -120,13 +120,13 @@ function viewEvents( element, name ) { function archiveEvents( element, name ) { var form = element.form; - form.action.value = 'archive'; + form.elements['action'].value = 'archive'; form.submit(); } -function unarchiveEvents( element, name ) { +function unarchiveEvents(element, name) { var form = element.form; - form.action.value = 'unarchive'; + form.elements['action'].value = 'unarchive'; form.submit(); }