private StatsCollector(Map<String, String> configs) { ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name); _agentMgr = locator.getManager(AgentManager.class); _userVmMgr = locator.getManager(UserVmManager.class); _hostDao = locator.getDao(HostDao.class); _userVmDao = locator.getDao(UserVmDao.class); _volsDao = locator.getDao(VolumeDao.class); _capacityDao = locator.getDao(CapacityDao.class); _storagePoolDao = locator.getDao(StoragePoolDao.class); _storageManager = locator.getManager(StorageManager.class); _storagePoolHostDao = locator.getDao(StoragePoolHostDao.class); _executor = Executors.newScheduledThreadPool(3, new NamedThreadFactory("StatsCollector")); hostStatsInterval = NumbersUtil.parseLong(configs.get("host.stats.interval"), 60000L); hostAndVmStatsInterval = NumbersUtil.parseLong(configs.get("vm.stats.interval"), 60000L); storageStatsInterval = NumbersUtil.parseLong(configs.get("storage.stats.interval"), 60000L); volumeStatsInterval = NumbersUtil.parseLong(configs.get("volume.stats.interval"), -1L); _executor.scheduleWithFixedDelay( new HostCollector(), 15000L, hostStatsInterval, TimeUnit.MILLISECONDS); _executor.scheduleWithFixedDelay( new VmStatsCollector(), 15000L, hostAndVmStatsInterval, TimeUnit.MILLISECONDS); _executor.scheduleWithFixedDelay( new StorageCollector(), 15000L, storageStatsInterval, TimeUnit.MILLISECONDS); // -1 means we don't even start this thread to pick up any data. if (volumeStatsInterval > 0) { _executor.scheduleWithFixedDelay( new VolumeCollector(), 15000L, volumeStatsInterval, TimeUnit.MILLISECONDS); } else { s_logger.info("Disabling volume stats collector"); } }
@Override public List<UserVmVO> listVirtualNetworkInstancesByAcctAndZone(long accountId, long dcId) { if (AccountDataCenterVirtualSearch == null) { ServiceOfferingDao offeringDao = ComponentLocator.getLocator("management-server").getDao(ServiceOfferingDao.class); SearchBuilder<ServiceOfferingVO> offeringSearch = offeringDao.createSearchBuilder(); offeringSearch.and( "guestIpType", offeringSearch.entity().getGuestIpType(), SearchCriteria.Op.EQ); AccountDataCenterVirtualSearch = createSearchBuilder(); AccountDataCenterVirtualSearch.and( "account", AccountDataCenterVirtualSearch.entity().getAccountId(), SearchCriteria.Op.EQ); AccountDataCenterVirtualSearch.and( "dc", AccountDataCenterVirtualSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); AccountDataCenterVirtualSearch.join( "offeringSearch", offeringSearch, AccountDataCenterVirtualSearch.entity().getServiceOfferingId(), offeringSearch.entity().getId()); AccountDataCenterVirtualSearch.done(); } SearchCriteria sc = AccountDataCenterVirtualSearch.create(); sc.setParameters("account", accountId); sc.setParameters("dc", dcId); sc.setJoinParameters("offeringSearch", "guestIpType", ServiceOffering.GuestIpType.Virtualized); return listActiveBy(sc); }
public ApiServlet() { super(); _apiServer = ApiServer.getInstance(); if (_apiServer == null) { throw new CloudRuntimeException("ApiServer not initialized"); } ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name); _accountMgr = locator.getManager(AccountService.class); }
public void init() { BaseCmd.setComponents(new ApiResponseHelper()); BaseListCmd.configure(); _systemAccount = _accountMgr.getSystemAccount(); _systemUser = _accountMgr.getSystemUser(); _dispatcher = ApiDispatcher.getInstance(); Integer apiPort = null; // api port, null by default ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name); ConfigurationDao configDao = locator.getDao(ConfigurationDao.class); SearchCriteria<ConfigurationVO> sc = configDao.createSearchCriteria(); sc.addAnd("name", SearchCriteria.Op.EQ, "integration.api.port"); List<ConfigurationVO> values = configDao.search(sc, null); if ((values != null) && (values.size() > 0)) { ConfigurationVO apiPortConfig = values.get(0); if (apiPortConfig.getValue() != null) { apiPort = Integer.parseInt(apiPortConfig.getValue()); } } Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation( APICommand.class, new String[] {"org.apache.cloudstack.api", "com.cloud.api"}); for (Class<?> cmdClass : cmdClasses) { String apiName = cmdClass.getAnnotation(APICommand.class).name(); if (_apiNameCmdClassMap.containsKey(apiName)) { s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName); continue; } _apiNameCmdClassMap.put(apiName, cmdClass); } encodeApiResponse = Boolean.valueOf(configDao.getValue(Config.EncodeApiResponse.key())); String jsonType = configDao.getValue(Config.JavaScriptDefaultContentType.key()); if (jsonType != null) { jsonContentType = jsonType; } if (apiPort != null) { ListenerThread listenerThread = new ListenerThread(this, apiPort); listenerThread.start(); } }
@Override public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException { try { ExternalNetworkDeviceManager nwDeviceMgr; ComponentLocator locator = ComponentLocator.getLocator(ManagementService.Name); nwDeviceMgr = locator.getManager(ExternalNetworkDeviceManager.class); Host device = nwDeviceMgr.addNetworkDevice(this); NetworkDeviceResponse response = nwDeviceMgr.getApiResponse(device); response.setObjectName("networkdevice"); response.setResponseName(getCommandName()); this.setResponseObject(response); } catch (InvalidParameterValueException ipve) { throw new ServerApiException(BaseCmd.PARAM_ERROR, ipve.getMessage()); } catch (CloudRuntimeException cre) { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, cre.getMessage()); } }
@Override public void init() throws ServletException { // Save Configuration Values // ComponentLocator loc = ComponentLocator.getLocator(ConfigurationServer.Name); ConfigurationServer c = (ConfigurationServer) ComponentLocator.getComponent(ConfigurationServer.Name); // ConfigurationServer c = new ConfigurationServerImpl(); try { c.persistDefaultValues(); s_locator = ComponentLocator.getLocator(ManagementServer.Name); ManagementServer ms = (ManagementServer) ComponentLocator.getComponent(ManagementServer.Name); ms.enableAdminUser("password"); ApiServer.initApiServer(ms.getApiConfig()); } catch (InvalidParameterValueException ipve) { s_logger.error("Exception starting management server ", ipve); throw new ServletException(ipve.getMessage()); } catch (Exception e) { s_logger.error("Exception starting management server ", e); throw new ServletException(e.getMessage()); } }
@Override public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException { // param checks if (snapshotReservation != null && (snapshotReservation < 0 || snapshotReservation > 100)) throw new InvalidParameterValueException("Invalid snapshot reservation"); ComponentLocator locator = ComponentLocator.getLocator(ManagementService.Name); NetappManager netappMgr = locator.getManager(NetappManager.class); StringBuilder s = new StringBuilder(getVolSize().toString()); s.append("g"); try { netappMgr.createVolumeOnFiler( ipAddress, aggrName, poolName, volName, s.toString(), snapshotPolicy, snapshotReservation, userName, password); CreateVolumeOnFilerCmdResponse response = new CreateVolumeOnFilerCmdResponse(); response.setResponseName(getCommandName()); this.setResponseObject(response); } catch (ServerException e) { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.toString()); } catch (InvalidParameterValueException e) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, e.toString()); } catch (UnknownHostException e) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, e.toString()); } }
@Override public boolean configure(final String name, final Map<String, Object> xmlParams) throws ConfigurationException { _name = name; ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name); _serverId = ((ManagementServer) ComponentLocator.getComponent(ManagementServer.Name)).getId(); _investigators = locator.getAdapters(Investigator.class); _fenceBuilders = locator.getAdapters(FenceBuilder.class); Map<String, String> params = new HashMap<String, String>(); final ConfigurationDao configDao = locator.getDao(ConfigurationDao.class); if (configDao != null) { params = configDao.getConfiguration(Long.toHexString(_serverId), xmlParams); } String value = params.get("workers"); final int count = NumbersUtil.parseInt(value, 1); _workers = new WorkerThread[count]; for (int i = 0; i < _workers.length; i++) { _workers[i] = new WorkerThread("HA-Worker-" + i); } value = params.get("force.ha"); _forceHA = Boolean.parseBoolean(value); value = params.get("time.to.sleep"); _timeToSleep = NumbersUtil.parseInt(value, 60) * 1000; value = params.get("max.retries"); _maxRetries = NumbersUtil.parseInt(value, 5); value = params.get("time.between.failures"); _timeBetweenFailures = NumbersUtil.parseLong(value, 3600) * 1000; value = params.get("time.between.cleanup"); _timeBetweenCleanups = NumbersUtil.parseLong(value, 3600 * 24); value = params.get("stop.retry.interval"); _stopRetryInterval = NumbersUtil.parseInt(value, 10 * 60); value = params.get("restart.retry.interval"); _restartRetryInterval = NumbersUtil.parseInt(value, 10 * 60); value = params.get("investigate.retry.interval"); _investigateRetryInterval = NumbersUtil.parseInt(value, 1 * 60); value = params.get("migrate.retry.interval"); _migrateRetryInterval = NumbersUtil.parseInt(value, 2 * 60); _instance = params.get("instance"); if (_instance == null) { _instance = "VMOPS"; } _haDao.releaseWorkItems(_serverId); _stopped = true; _executor = Executors.newScheduledThreadPool(count, new NamedThreadFactory("HA")); return true; }
public class IPAddressUsageParser { public static final Logger s_logger = Logger.getLogger(IPAddressUsageParser.class.getName()); private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage"); private static UsageDao m_usageDao = _locator.getDao(UsageDao.class); private static UsageIPAddressDao m_usageIPAddressDao = _locator.getDao(UsageIPAddressDao.class); public static boolean parse(AccountVO account, Date startDate, Date endDate) { if (s_logger.isDebugEnabled()) { s_logger.debug("Parsing IP Address usage for account: " + account.getId()); } if ((endDate == null) || endDate.after(new Date())) { endDate = new Date(); } // - query usage_ip_address table with the following criteria: // - look for an entry for accountId with start date in the given range // - look for an entry for accountId with end date in the given range // - look for an entry for accountId with end date null (currently running vm or owned IP) // - look for an entry for accountId with start date before given range *and* end date after // given range List<UsageIPAddressVO> usageIPAddress = m_usageIPAddressDao.getUsageRecords( account.getId(), account.getDomainId(), startDate, endDate); if (usageIPAddress.isEmpty()) { s_logger.debug("No IP Address usage for this period"); return true; } // This map has both the running time *and* the usage amount. Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>(); Map<String, IpInfo> IPMap = new HashMap<String, IpInfo>(); // loop through all the usage IPs, create a usage record for each for (UsageIPAddressVO usageIp : usageIPAddress) { long IpId = usageIp.getId(); String key = "" + IpId; // store the info in the IP map IPMap.put( key, new IpInfo(usageIp.getZoneId(), IpId, usageIp.getAddress(), usageIp.isSourceNat())); Date IpAssignDate = usageIp.getAssigned(); Date IpReleaseDeleteDate = usageIp.getReleased(); if ((IpReleaseDeleteDate == null) || IpReleaseDeleteDate.after(endDate)) { IpReleaseDeleteDate = endDate; } // clip the start date to the beginning of our aggregation range if the vm has been running // for a while if (IpAssignDate.before(startDate)) { IpAssignDate = startDate; } long currentDuration = (IpReleaseDeleteDate.getTime() - IpAssignDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to // find total number of millis to charge) updateIpUsageData(usageMap, key, usageIp.getId(), currentDuration); } for (String ipIdKey : usageMap.keySet()) { Pair<Long, Long> ipTimeInfo = usageMap.get(ipIdKey); long useTime = ipTimeInfo.second().longValue(); // Only create a usage record if we have a runningTime of bigger than zero. if (useTime > 0L) { IpInfo info = IPMap.get(ipIdKey); createUsageRecord( info.getZoneId(), useTime, startDate, endDate, account, info.getIpId(), info.getIPAddress(), info.isSourceNat()); } } return true; } private static void updateIpUsageData( Map<String, Pair<Long, Long>> usageDataMap, String key, long ipId, long duration) { Pair<Long, Long> ipUsageInfo = usageDataMap.get(key); if (ipUsageInfo == null) { ipUsageInfo = new Pair<Long, Long>(new Long(ipId), new Long(duration)); } else { Long runningTime = ipUsageInfo.second(); runningTime = new Long(runningTime.longValue() + duration); ipUsageInfo = new Pair<Long, Long>(ipUsageInfo.first(), runningTime); } usageDataMap.put(key, ipUsageInfo); } private static void createUsageRecord( long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long IpId, String IPAddress, boolean isSourceNat) { if (s_logger.isDebugEnabled()) { s_logger.debug("Total usage time " + runningTime + "ms"); } float usage = runningTime / 1000f / 60f / 60f; DecimalFormat dFormat = new DecimalFormat("#.######"); String usageDisplay = dFormat.format(usage); if (s_logger.isDebugEnabled()) { s_logger.debug( "Creating IP usage record with id: " + IpId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId()); } String usageDesc = "IPAddress: " + IPAddress; // Create the usage record UsageVO usageRecord = new UsageVO( zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", UsageTypes.IP_ADDRESS, new Double(usage), null, null, null, null, IpId, startDate, endDate, (isSourceNat ? "SourceNat" : "")); m_usageDao.persist(usageRecord); } private static class IpInfo { private long zoneId; private long IpId; private String IPAddress; private boolean isSourceNat; public IpInfo(long zoneId, long IpId, String IPAddress, boolean isSourceNat) { this.zoneId = zoneId; this.IpId = IpId; this.IPAddress = IPAddress; this.isSourceNat = isSourceNat; } public long getZoneId() { return zoneId; } public long getIpId() { return IpId; } public String getIPAddress() { return IPAddress; } public boolean isSourceNat() { return isSourceNat; } } }