diff --git a/core/src/com/cloud/alert/AlertVO.java b/core/src/com/cloud/alert/AlertVO.java old mode 100644 new mode 100755 index aae1d06a90e..878993dbbae --- a/core/src/com/cloud/alert/AlertVO.java +++ b/core/src/com/cloud/alert/AlertVO.java @@ -40,8 +40,11 @@ public class AlertVO implements Alert{ private long id; @Column(name="type") - private short type; - + private short type; + + @Column(name="cluster_id") + private Long clusterId = null; + @Column(name="pod_id") private Long podId = null; @@ -92,7 +95,13 @@ public class AlertVO implements Alert{ this.subject = subject; } - @Override + public Long getClusterId() { + return clusterId; + } + public void setClusterId(Long clusterId) { + this.clusterId = clusterId; + } + @Override public Long getPodId() { return podId; } diff --git a/server/src/com/cloud/alert/AlertManagerImpl.java b/server/src/com/cloud/alert/AlertManagerImpl.java index 7592469c78c..704334cad10 100755 --- a/server/src/com/cloud/alert/AlertManagerImpl.java +++ b/server/src/com/cloud/alert/AlertManagerImpl.java @@ -261,7 +261,7 @@ public class AlertManagerImpl implements AlertManager { // shouldn't we have a type/severity as part of the API so that severe errors get sent right away? try { if (_emailAlert != null) { - _emailAlert.sendAlert(alertType, dataCenterId, podId, subject, body); + _emailAlert.sendAlert(alertType, dataCenterId, podId, null, subject, body); } } catch (Exception ex) { s_logger.error("Problem sending email alert", ex); @@ -503,6 +503,8 @@ public class AlertManagerImpl implements AlertManager { String usedStr; String pctStr = formatPercent(usedCapacity/totalCapacity); short alertType = -1; + Long podId = pod == null ? null : pod.getId(); + Long clusterId = cluster == null ? null : cluster.getId(); switch (capacityType) { @@ -584,7 +586,7 @@ public class AlertManagerImpl implements AlertManager { } try { - _emailAlert.sendAlert(alertType, dc.getId(), null, msgSubject, msgContent); + _emailAlert.sendAlert(alertType, dc.getId(), podId, clusterId, msgSubject, msgContent); } catch (Exception ex) { s_logger.error("Exception in CapacityChecker", ex); } @@ -683,7 +685,7 @@ public class AlertManagerImpl implements AlertManager { } // TODO: make sure this handles SSL transport (useAuth is true) and regular - public void sendAlert(short alertType, long dataCenterId, Long podId, String subject, String content) throws MessagingException, UnsupportedEncodingException { + public void sendAlert(short alertType, long dataCenterId, Long podId, Long clusterId, String subject, String content) throws MessagingException, UnsupportedEncodingException { AlertVO alert = null; if ((alertType != AlertManager.ALERT_TYPE_HOST) && (alertType != AlertManager.ALERT_TYPE_USERVM) && @@ -691,14 +693,15 @@ public class AlertManagerImpl implements AlertManager { (alertType != AlertManager.ALERT_TYPE_CONSOLE_PROXY) && (alertType != AlertManager.ALERT_TYPE_STORAGE_MISC) && (alertType != AlertManager.ALERT_TYPE_MANAGMENT_NODE)) { - alert = _alertDao.getLastAlert(alertType, dataCenterId, podId); + alert = _alertDao.getLastAlert(alertType, dataCenterId, podId, clusterId); } if (alert == null) { // set up a new alert AlertVO newAlert = new AlertVO(); newAlert.setType(alertType); - newAlert.setSubject(subject); + newAlert.setSubject(subject); + newAlert.setClusterId(clusterId); newAlert.setPodId(podId); newAlert.setDataCenterId(dataCenterId); newAlert.setSentCount(1); // initialize sent count to 1 since we are now sending an alert @@ -737,7 +740,7 @@ public class AlertManagerImpl implements AlertManager { public void clearAlert(short alertType, long dataCenterId, Long podId) { if (alertType != -1) { - AlertVO alert = _alertDao.getLastAlert(alertType, dataCenterId, podId); + AlertVO alert = _alertDao.getLastAlert(alertType, dataCenterId, podId, null); if (alert != null) { AlertVO updatedAlert = _alertDao.createForUpdate(); updatedAlert.setResolved(new Date()); diff --git a/server/src/com/cloud/alert/dao/AlertDao.java b/server/src/com/cloud/alert/dao/AlertDao.java old mode 100644 new mode 100755 index 37da36ff903..ec406d68f40 --- a/server/src/com/cloud/alert/dao/AlertDao.java +++ b/server/src/com/cloud/alert/dao/AlertDao.java @@ -22,5 +22,7 @@ import com.cloud.alert.AlertVO; import com.cloud.utils.db.GenericDao; public interface AlertDao extends GenericDao { + AlertVO getLastAlert(short type, long dataCenterId, Long podId, Long clusterId); + // This is for backward compatibility AlertVO getLastAlert(short type, long dataCenterId, Long podId); } diff --git a/server/src/com/cloud/alert/dao/AlertDaoImpl.java b/server/src/com/cloud/alert/dao/AlertDaoImpl.java old mode 100644 new mode 100755 index bee71b47a73..c0e080c0691 --- a/server/src/com/cloud/alert/dao/AlertDaoImpl.java +++ b/server/src/com/cloud/alert/dao/AlertDaoImpl.java @@ -30,7 +30,7 @@ import com.cloud.utils.db.SearchCriteria; @Local(value = { AlertDao.class }) public class AlertDaoImpl extends GenericDaoBase implements AlertDao { @Override - public AlertVO getLastAlert(short type, long dataCenterId, Long podId) { + public AlertVO getLastAlert(short type, long dataCenterId, Long podId, Long clusterId) { Filter searchFilter = new Filter(AlertVO.class, "createdDate", Boolean.FALSE, Long.valueOf(1), Long.valueOf(1)); SearchCriteria sc = createSearchCriteria(); @@ -38,6 +38,9 @@ public class AlertDaoImpl extends GenericDaoBase implements Alert sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, Long.valueOf(dataCenterId)); if (podId != null) { sc.addAnd("podId", SearchCriteria.Op.EQ, podId); + } + if (clusterId != null) { + sc.addAnd("clusterId", SearchCriteria.Op.EQ, clusterId); } List alerts = listBy(sc, searchFilter); @@ -45,5 +48,23 @@ public class AlertDaoImpl extends GenericDaoBase implements Alert return alerts.get(0); } return null; + } + + @Override + public AlertVO getLastAlert(short type, long dataCenterId, Long podId) { + Filter searchFilter = new Filter(AlertVO.class, "createdDate", Boolean.FALSE, Long.valueOf(1), Long.valueOf(1)); + SearchCriteria sc = createSearchCriteria(); + + sc.addAnd("type", SearchCriteria.Op.EQ, Short.valueOf(type)); + sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, Long.valueOf(dataCenterId)); + if (podId != null) { + sc.addAnd("podId", SearchCriteria.Op.EQ, podId); + } + + List alerts = listBy(sc, searchFilter); + if ((alerts != null) && !alerts.isEmpty()) { + return alerts.get(0); + } + return null; } } diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 4df1c20904d..b67e79032f6 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -181,7 +181,7 @@ import com.cloud.vm.dao.UserVmData.SecurityGroupData; public class ApiResponseHelper implements ResponseGenerator { public final Logger s_logger = Logger.getLogger(ApiResponseHelper.class); - + private static final DecimalFormat s_percentFormat = new DecimalFormat("##.##"); @Override public UserResponse createUserResponse(User user) { @@ -760,7 +760,12 @@ public class ApiResponseHelper implements ResponseGenerator { capacityResponse.setCapacityUsed(capacity.getUsedCapacity() - c.get(0).getUsedCapacity()); }else{ capacityResponse.setCapacityTotal(capacity.getTotalCapacity()); - } + } + if (capacityResponse.getCapacityTotal() != 0) { + capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f)); + } else { + capacityResponse.setPercentUsed(s_percentFormat.format(0L)); + } capacityResponses.add(capacityResponse); } // Do it for stats as well. @@ -810,6 +815,11 @@ public class ApiResponseHelper implements ResponseGenerator { }else{ capacityResponse.setCapacityTotal(capacity.getTotalCapacity()); } + if (capacityResponse.getCapacityTotal() != 0) { + capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f)); + } else { + capacityResponse.setPercentUsed(s_percentFormat.format(0L)); + } capacityResponses.add(capacityResponse); } // Do it for stats as well. @@ -840,7 +850,12 @@ public class ApiResponseHelper implements ResponseGenerator { CapacityResponse capacityResponse = new CapacityResponse(); capacityResponse.setCapacityType(capacity.getCapacityType()); capacityResponse.setCapacityUsed(capacity.getUsedCapacity()); - capacityResponse.setCapacityTotal(capacity.getTotalCapacity()); + capacityResponse.setCapacityTotal(capacity.getTotalCapacity()); + if (capacityResponse.getCapacityTotal() != 0) { + capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f)); + } else { + capacityResponse.setPercentUsed(s_percentFormat.format(0L)); + } capacityResponses.add(capacityResponse); } @@ -1033,7 +1048,11 @@ public class ApiResponseHelper implements ResponseGenerator { }else{ capacityResponse.setCapacityTotal(capacity.getTotalCapacity()); } - + if (capacityResponse.getCapacityTotal() != 0) { + capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f)); + } else { + capacityResponse.setPercentUsed(s_percentFormat.format(0L)); + } capacityResponses.add(capacityResponse); } // Do it for stats as well. diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index 0d22a14de0e..cc0f28bc930 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -1110,6 +1110,7 @@ CREATE TABLE `cloud`.`op_host_capacity` ( CREATE TABLE `cloud`.`alert` ( `id` bigint unsigned NOT NULL auto_increment, `type` int(1) unsigned NOT NULL, + `cluster_id` bigint unsigned, `pod_id` bigint unsigned, `data_center_id` bigint unsigned NOT NULL, `subject` varchar(999) COMMENT 'according to SMTP spec, max subject length is 1000 including the CRLF character, so allow enough space to fit long pod/zone/host names',