Martin Peřina has uploaded a new change for review. Change subject: core: Allow multiple alerts of the same type for host ......................................................................
core: Allow multiple alerts of the same type for host Add repeatable attribute to AuditLog and AuditLogableBase class which allows saving multiple alerts of the same type for the same host. Change-Id: I0deda59438b61c88d45f45af59e85b003f218e1a Bug-Url: https://bugzilla.redhat.com/1142904 Signed-off-by: Martin Perina <[email protected]> --- M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogableBase.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/AuditLogDAODbFacadeImpl.java M backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/AuditLogDAOTest.java M packaging/dbscripts/audit_log_sp.sql 6 files changed, 122 insertions(+), 5 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/76/34776/1 diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java index 07686e3..3ccfb42 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java @@ -47,6 +47,15 @@ private String quotaEnforcementType; private String callStack; + /** + * If set to {@code true}, it allows storing to db multiple alerts of the same type for the same host. + * By default it's set to {@code false}, to ignore (not save) them (backward compatibility). It affects + * only alerts as other severities are logged always. + * + * It's not persisted in db, because it's used only to determine if alert should be stored or not. + */ + boolean repeatable; + public AuditLog() { this(AuditLogType.UNASSIGNED, AuditLogSeverity.NORMAL); } @@ -59,6 +68,7 @@ this.eventFloodInSec = 30; this.customData = ""; this.logTime = new Date(); + this.repeatable = false; } public AuditLog(AuditLogType type, @@ -417,6 +427,14 @@ this.callStack = callStack; } + public boolean isRepeatable() { + return repeatable; + } + + public void setRepeatable(boolean repeatable) { + this.repeatable = repeatable; + } + @Override public int hashCode() { final int prime = 31; diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java index 7301fac..bfe700f 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java @@ -160,6 +160,7 @@ auditLog.setQuotaId(auditLogable.getQuotaIdForLog()); auditLog.setQuotaName(auditLogable.getQuotaNameForLog()); auditLog.setCallStack(auditLogable.getCallStack()); + auditLog.setRepeatable(auditLogable.isRepeatable()); getDbFacadeInstance().getAuditLogDao().save(auditLog); String logMessage; if (!"".equals(loggerString)) { diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogableBase.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogableBase.java index c5f4534..dd340d2 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogableBase.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogableBase.java @@ -97,10 +97,17 @@ private String quotaNameForLog; private String callStack; + /** + * @see org.ovirt.engine.core.common.businessentities.AuditLog#repeatable + */ + private boolean repeatable; + public AuditLogableBase() { + repeatable = false; } public AuditLogableBase(final Guid vdsId) { + this(); mVdsId = vdsId; } @@ -110,6 +117,7 @@ } public AuditLogableBase(final AuditLog auditLog) { + this(auditLog.getVdsId(), auditLog.getVmId()); this._storageDomainId = auditLog.getStorageDomainId(); this._storagePoolId = auditLog.getStoragePoolId(); this.correlationId = auditLog.getCorrelationId(); @@ -122,9 +130,7 @@ this.mUserId = auditLog.getUserId(); this.mUserName = auditLog.getUserName(); this.mVdsGroupId = auditLog.getVdsGroupId(); - this.mVdsId = auditLog.getVdsId(); this.mVdsName = auditLog.getVdsName(); - this.mVmId = auditLog.getVmId(); this.mVmName = auditLog.getVmName(); this.mVmTemplateId = auditLog.getVmTemplateId(); this.mVmTemplateName = auditLog.getVmTemplateName(); @@ -822,4 +828,12 @@ setCallStack(ExceptionUtils.getStackTrace(throwable)); } } + + public boolean isRepeatable() { + return repeatable; + } + + public void setRepeatable(boolean repeatable) { + this.repeatable = repeatable; + } } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/AuditLogDAODbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/AuditLogDAODbFacadeImpl.java index be78500..c610a23 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/AuditLogDAODbFacadeImpl.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/AuditLogDAODbFacadeImpl.java @@ -137,7 +137,8 @@ .addValue("quota_name", event.getQuotaName()) .addValue("gluster_volume_id", event.getGlusterVolumeId()) .addValue("gluster_volume_name", event.getGlusterVolumeName()) - .addValue("call_stack", event.getCallStack()); + .addValue("call_stack", event.getCallStack()) + .addValue("repeatable", event.isRepeatable()); } private MapSqlParameterSource getExternalEventSqlMapper(AuditLog event) { diff --git a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/AuditLogDAOTest.java b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/AuditLogDAOTest.java index 1fd2536..66426c2 100644 --- a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/AuditLogDAOTest.java +++ b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/AuditLogDAOTest.java @@ -18,15 +18,19 @@ import org.ovirt.engine.core.common.config.Config; import org.ovirt.engine.core.common.config.ConfigValues; import org.ovirt.engine.core.compat.Guid; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * <code>AuditLogDAOTest</code> performs tests against the {@link AuditLogDAO} type. * */ public class AuditLogDAOTest extends BaseDAOTestCase { + private static final Logger log = LoggerFactory.getLogger(AuditLogDAOTest.class); private static final String VM_NAME = "rhel5-pool-50"; private static final String VM_TEMPLATE_NAME = "1"; private static final Guid VDS_ID = new Guid("afce7a39-8e8c-4819-ba9c-796d316592e6"); + private static final String VDS_NAME = "magenta-vdsc"; private static final Guid VM_ID = new Guid("77296e00-0cad-4e5a-9299-008a7b6f4354"); private static final Guid VM_TEMPLATE_ID = new Guid("1b85420c-b84c-4f29-997e-0eb674b40b79"); private static final long EXISTING_ENTRY_ID = 44291; @@ -325,4 +329,82 @@ assertTrue(result.isDeleted()); } + + + private int getAlertCount(AuditLog entry, List<AuditLog> results) { + int count = 0; + log.info("Founding alerts"); + if (results != null) { + for (AuditLog al : results) { + log.info("Testing: {id='{}', severity='{}', type='{}', hostId='{}', hostName='{}', date='{}'}", + al.getAuditLogId(), + al.getSeverity(), + al.getLogType(), + al.getVdsId(), + al.getVdsName(), + al.getLogTime()); + if (al.getSeverity() == entry.getSeverity() + && al.getVdsId().equals(entry.getVdsId()) + && al.getLogType() == entry.getLogType()) { + count++; + } + log.info("Count: {}", count); + } + } + log.info("Found alerts: {}", count); + return count; + } + + /** + * Checks if multiple alerts of the same type for the same host are ignored if repeatable is set to {@code false} + */ + @Test + public void testMultipleAlertsWithSameTypeAndHostAreIgnored() { + AuditLog entry = new AuditLog(AuditLogType.VDS_ALERT_FENCE_DISABLED_BY_CLUSTER_POLICY, AuditLogSeverity.ALERT); + entry.setVdsId(VDS_ID); + entry.setVdsName(VDS_NAME); + entry.setMessage("Testing alert"); + + // test if no alert of the same type for the same host exists + assertEquals(0, getAlertCount(entry, dao.getAll(null, false))); + + dao.save(entry); + + // test if 1st alert was stored in db + assertEquals(1, getAlertCount(entry, dao.getAll(null, false))); + + // try to store 2nd alert in db + entry.setLogTime(new Date()); + dao.save(entry); + + // test if 2nd alert was ignored + assertEquals(1, getAlertCount(entry, dao.getAll(null, false))); + } + + /** + * Checks if multiple alerts of the same type for the same host are saved if repeatable is set to {@code true} + */ + @Test + public void testMultipleAlertsWithSameTypeAndHostAreSavedIfRepeatableTrue() { + AuditLog entry = new AuditLog(AuditLogType.VDS_ALERT_FENCE_DISABLED_BY_CLUSTER_POLICY, AuditLogSeverity.ALERT); + entry.setVdsId(VDS_ID); + entry.setVdsName(VDS_NAME); + entry.setMessage("Testing alert"); + entry.setRepeatable(true); + + // test if no alert of the same type for the same host exists + assertEquals(0, getAlertCount(entry, dao.getAll(null, false))); + + dao.save(entry); + + // test if 1st alert was stored in db + assertEquals(1, getAlertCount(entry, dao.getAll(null, false))); + + // try to save 2nd alert + entry.setLogTime(new Date()); + dao.save(entry); + + // test if 2nd alert was also stored in db + assertEquals(2, getAlertCount(entry, dao.getAll(null, false))); + } } diff --git a/packaging/dbscripts/audit_log_sp.sql b/packaging/dbscripts/audit_log_sp.sql index ff139e5..bd7e7a4 100644 --- a/packaging/dbscripts/audit_log_sp.sql +++ b/packaging/dbscripts/audit_log_sp.sql @@ -28,7 +28,8 @@ v_job_id UUID, v_gluster_volume_id UUID, v_gluster_volume_name VARCHAR(1000), - v_call_stack text) + v_call_stack text, + v_repeatable BOOLEAN) AS $procedure$ DECLARE v_min_alret_severity INTEGER; @@ -42,7 +43,7 @@ v_audit_log_id := CURRVAL('audit_log_seq'); else - if (not exists(select audit_log_id from audit_log where vds_name = v_vds_name and log_type = v_log_type and not deleted)) then + if (v_repeatable OR not exists(select audit_log_id from audit_log where vds_name = v_vds_name and log_type = v_log_type and not deleted)) then INSERT INTO audit_log(LOG_TIME, log_type, log_type_name, severity,message, user_id, USER_NAME, vds_id, VDS_NAME, vm_id, VM_NAME,vm_template_id,VM_TEMPLATE_NAME,storage_pool_id,STORAGE_POOL_NAME,storage_domain_id,STORAGE_DOMAIN_NAME,vds_group_id,vds_group_name, correlation_id, job_id, quota_id, quota_name, gluster_volume_id, gluster_volume_name, call_stack) VALUES(v_log_time, v_log_type, v_log_type_name, v_severity, v_message, v_user_id, v_user_name, v_vds_id, v_vds_name, v_vm_id, v_vm_name,v_vm_template_id,v_vm_template_name,v_storage_pool_id,v_storage_pool_name,v_storage_domain_id,v_storage_domain_name,v_vds_group_id,v_vds_group_name, v_correlation_id, v_job_id, v_quota_id, v_quota_name, v_gluster_volume_id, v_gluster_volume_name, v_call_stack); -- To view, visit http://gerrit.ovirt.org/34776 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0deda59438b61c88d45f45af59e85b003f218e1a Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Martin Peřina <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
