Search in sources :

Example 1 with ConfigurationDao

use of com.cloud.configuration.dao.ConfigurationDao in project CloudStack-archive by CloudStack-extras.

the class UsageManagerImpl method configure.

public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    final String run = "usage.vmops.pid";
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Checking to see if " + run + " exists.");
    }
    final Class<?> c = UsageServer.class;
    m_version = c.getPackage().getImplementationVersion();
    if (m_version == null) {
        throw new CloudRuntimeException("Unable to find the implementation version of this usage server");
    }
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Implementation Version is " + m_version);
    }
    m_name = name;
    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    if (configDao == null) {
        s_logger.error("Unable to get the configuration dao.");
        return false;
    }
    Map<String, String> configs = configDao.getConfiguration(params);
    if (params != null) {
        mergeConfigs(configs, params);
    }
    String execTime = configs.get("usage.stats.job.exec.time");
    String aggregationRange = configs.get("usage.stats.job.aggregation.range");
    String execTimeZone = configs.get("usage.execution.timezone");
    String aggreagationTimeZone = configs.get("usage.aggregation.timezone");
    String sanityCheckInterval = configs.get("usage.sanity.check.interval");
    if (sanityCheckInterval != null) {
        m_sanityCheckInterval = Integer.parseInt(sanityCheckInterval);
    }
    if (aggreagationTimeZone != null && !aggreagationTimeZone.isEmpty()) {
        m_usageTimezone = TimeZone.getTimeZone(aggreagationTimeZone);
    }
    s_logger.debug("Usage stats aggregation time zone: " + aggreagationTimeZone);
    try {
        if ((execTime == null) || (aggregationRange == null)) {
            s_logger.error("missing configuration values for usage job, usage.stats.job.exec.time = " + execTime + ", usage.stats.job.aggregation.range = " + aggregationRange);
            throw new ConfigurationException("Missing configuration values for usage job, usage.stats.job.exec.time = " + execTime + ", usage.stats.job.aggregation.range = " + aggregationRange);
        }
        String[] execTimeSegments = execTime.split(":");
        if (execTimeSegments.length != 2) {
            s_logger.error("Unable to parse usage.stats.job.exec.time");
            throw new ConfigurationException("Unable to parse usage.stats.job.exec.time '" + execTime + "'");
        }
        int hourOfDay = Integer.parseInt(execTimeSegments[0]);
        int minutes = Integer.parseInt(execTimeSegments[1]);
        m_jobExecTime.setTime(new Date());
        m_jobExecTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
        m_jobExecTime.set(Calendar.MINUTE, minutes);
        m_jobExecTime.set(Calendar.SECOND, 0);
        m_jobExecTime.set(Calendar.MILLISECOND, 0);
        if (execTimeZone != null && !execTimeZone.isEmpty()) {
            m_jobExecTime.setTimeZone(TimeZone.getTimeZone(execTimeZone));
        }
        // if the hour to execute the job has already passed, roll the day forward to the next day
        Date execDate = m_jobExecTime.getTime();
        if (execDate.before(new Date())) {
            m_jobExecTime.roll(Calendar.DAY_OF_YEAR, true);
        }
        s_logger.debug("Execution Time: " + execDate.toString());
        Date currentDate = new Date(System.currentTimeMillis());
        s_logger.debug("Current Time: " + currentDate.toString());
        m_aggregationDuration = Integer.parseInt(aggregationRange);
        if (m_aggregationDuration < USAGE_AGGREGATION_RANGE_MIN) {
            s_logger.warn("Usage stats job aggregation range is to small, using the minimum value of " + USAGE_AGGREGATION_RANGE_MIN);
            m_aggregationDuration = USAGE_AGGREGATION_RANGE_MIN;
        }
        m_hostname = InetAddress.getLocalHost().getHostName() + "/" + InetAddress.getLocalHost().getHostAddress();
    } catch (NumberFormatException ex) {
        throw new ConfigurationException("Unable to parse usage.stats.job.exec.time '" + execTime + "' or usage.stats.job.aggregation.range '" + aggregationRange + "', please check configuration values");
    } catch (Exception e) {
        s_logger.error("Unhandled exception configuring UsageManger", e);
        throw new ConfigurationException("Unhandled exception configuring UsageManager " + e.toString());
    }
    m_pid = Integer.parseInt(System.getProperty("pid"));
    return true;
}
Also used : ConfigurationDao(com.cloud.configuration.dao.ConfigurationDao) Date(java.util.Date) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SQLException(java.sql.SQLException) ComponentLocator(com.cloud.utils.component.ComponentLocator) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 2 with ConfigurationDao

use of com.cloud.configuration.dao.ConfigurationDao in project CloudStack-archive by CloudStack-extras.

the class UsageAlertManagerImpl method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    _name = name;
    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    if (configDao == null) {
        s_logger.error("Unable to get the configuration dao.");
        return false;
    }
    Map<String, String> configs = configDao.getConfiguration("management-server", params);
    // set up the email system for alerts
    String emailAddressList = configs.get("alert.email.addresses");
    String[] emailAddresses = null;
    if (emailAddressList != null) {
        emailAddresses = emailAddressList.split(",");
    }
    String smtpHost = configs.get("alert.smtp.host");
    int smtpPort = NumbersUtil.parseInt(configs.get("alert.smtp.port"), 25);
    String useAuthStr = configs.get("alert.smtp.useAuth");
    boolean useAuth = ((useAuthStr == null) ? false : Boolean.parseBoolean(useAuthStr));
    String smtpUsername = configs.get("alert.smtp.username");
    String smtpPassword = configs.get("alert.smtp.password");
    String emailSender = configs.get("alert.email.sender");
    String smtpDebugStr = configs.get("alert.smtp.debug");
    boolean smtpDebug = false;
    if (smtpDebugStr != null) {
        smtpDebug = Boolean.parseBoolean(smtpDebugStr);
    }
    _emailAlert = new EmailAlert(emailAddresses, smtpHost, smtpPort, useAuth, smtpUsername, smtpPassword, emailSender, smtpDebug);
    _alertDao = locator.getDao(AlertDao.class);
    if (_alertDao == null) {
        s_logger.error("Unable to get the alert dao.");
        return false;
    }
    return true;
}
Also used : ConfigurationDao(com.cloud.configuration.dao.ConfigurationDao) ComponentLocator(com.cloud.utils.component.ComponentLocator) AlertDao(com.cloud.alert.dao.AlertDao)

Aggregations

ConfigurationDao (com.cloud.configuration.dao.ConfigurationDao)2 ComponentLocator (com.cloud.utils.component.ComponentLocator)2 AlertDao (com.cloud.alert.dao.AlertDao)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 ConfigurationException (javax.naming.ConfigurationException)1