Search in sources :

Example 1 with ComponentLocator

use of com.cloud.utils.component.ComponentLocator 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 ComponentLocator

use of com.cloud.utils.component.ComponentLocator in project CloudStack-archive by CloudStack-extras.

the class CheckPointManagerTest method testCompleteCase.

public void testCompleteCase() throws Exception {
    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    CheckPointManagerImpl taskMgr = ComponentLocator.inject(CheckPointManagerImpl.class);
    assertTrue(taskMgr.configure("TaskManager", new HashMap<String, Object>()));
    assertTrue(taskMgr.start());
    MockMaid delegate = new MockMaid();
    delegate.setValue("first");
    long taskId = taskMgr.pushCheckPoint(delegate);
    StackMaidDao maidDao = locator.getDao(StackMaidDao.class);
    CheckPointVO task = maidDao.findById(taskId);
    assertEquals(task.getDelegate(), MockMaid.class.getName());
    MockMaid retrieved = (MockMaid) SerializerHelper.fromSerializedString(task.getContext());
    assertEquals(retrieved.getValue(), delegate.getValue());
    delegate.setValue("second");
    taskMgr.updateCheckPointState(taskId, delegate);
    task = maidDao.findById(taskId);
    assertEquals(task.getDelegate(), MockMaid.class.getName());
    retrieved = (MockMaid) SerializerHelper.fromSerializedString(task.getContext());
    assertEquals(retrieved.getValue(), delegate.getValue());
    taskMgr.popCheckPoint(taskId);
    assertNull(maidDao.findById(taskId));
}
Also used : ComponentLocator(com.cloud.utils.component.ComponentLocator) MockComponentLocator(com.cloud.utils.component.MockComponentLocator) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StackMaidDao(com.cloud.cluster.dao.StackMaidDao)

Example 3 with ComponentLocator

use of com.cloud.utils.component.ComponentLocator 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)

Example 4 with ComponentLocator

use of com.cloud.utils.component.ComponentLocator in project CloudStack-archive by CloudStack-extras.

the class UsageServer method main.

/**
     * @param args
     */
public static void main(String[] args) {
    // TODO: do we need to communicate with mgmt server?
    final ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
    UsageManager mgr = _locator.getManager(UsageManager.class);
    if (mgr != null) {
        if (s_logger.isInfoEnabled()) {
            s_logger.info("UsageServer ready...");
        }
    }
}
Also used : ComponentLocator(com.cloud.utils.component.ComponentLocator)

Example 5 with ComponentLocator

use of com.cloud.utils.component.ComponentLocator in project CloudStack-archive by CloudStack-extras.

the class AgentShell method init.

private void init(String[] args) throws ConfigurationException {
    final ComponentLocator locator = ComponentLocator.getLocator("agent");
    final Class<?> c = this.getClass();
    _version = c.getPackage().getImplementationVersion();
    if (_version == null) {
        throw new CloudRuntimeException("Unable to find the implementation version of this agent");
    }
    s_logger.info("Implementation Version is " + _version);
    parseCommand(args);
    _storage = locator.getManager(StorageComponent.class);
    if (_storage == null) {
        s_logger.info("Defaulting to using properties file for storage");
        _storage = new PropertiesStorage();
        _storage.configure("Storage", new HashMap<String, Object>());
    }
    // command line parameters
    for (Map.Entry<String, Object> cmdLineProp : getCmdLineProperties().entrySet()) {
        _properties.put(cmdLineProp.getKey(), cmdLineProp.getValue());
    }
    final Adapters adapters = locator.getAdapters(BackoffAlgorithm.class);
    final Enumeration en = adapters.enumeration();
    while (en.hasMoreElements()) {
        _backoff = (BackoffAlgorithm) en.nextElement();
        break;
    }
    if (en.hasMoreElements()) {
        s_logger.info("More than one backoff algorithm specified.  Using the first one ");
    }
    if (_backoff == null) {
        s_logger.info("Defaulting to the constant time backoff algorithm");
        _backoff = new ConstantTimeBackoff();
        _backoff.configure("ConstantTimeBackoff", new HashMap<String, Object>());
    }
}
Also used : Enumeration(java.util.Enumeration) Adapters(com.cloud.utils.component.Adapters) StorageComponent(com.cloud.agent.dao.StorageComponent) ComponentLocator(com.cloud.utils.component.ComponentLocator) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PropertiesStorage(com.cloud.agent.dao.impl.PropertiesStorage) HashMap(java.util.HashMap) Map(java.util.Map) ConstantTimeBackoff(com.cloud.utils.backoff.impl.ConstantTimeBackoff)

Aggregations

ComponentLocator (com.cloud.utils.component.ComponentLocator)9 HashMap (java.util.HashMap)4 StackMaidDao (com.cloud.cluster.dao.StackMaidDao)3 MockComponentLocator (com.cloud.utils.component.MockComponentLocator)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConfigurationDao (com.cloud.configuration.dao.ConfigurationDao)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 StorageComponent (com.cloud.agent.dao.StorageComponent)1 PropertiesStorage (com.cloud.agent.dao.impl.PropertiesStorage)1 SimulatorManager (com.cloud.agent.manager.SimulatorManager)1 AlertDao (com.cloud.alert.dao.AlertDao)1 ServerApiException (com.cloud.api.ServerApiException)1 SuccessResponse (com.cloud.api.response.SuccessResponse)1 ConfigurationService (com.cloud.configuration.ConfigurationService)1 ConsoleProxyService (com.cloud.consoleproxy.ConsoleProxyService)1 EntityManager (com.cloud.dao.EntityManager)1 NetworkService (com.cloud.network.NetworkService)1 StorageNetworkService (com.cloud.network.StorageNetworkService)1 VirtualNetworkApplianceService (com.cloud.network.VirtualNetworkApplianceService)1 FirewallService (com.cloud.network.firewall.FirewallService)1