List of usage examples for com.amazonaws.services.cloudwatch AmazonCloudWatchClient AmazonCloudWatchClient
AmazonCloudWatchClient(AwsSyncClientParams clientParams, boolean endpointDiscoveryEnabled)
From source file:be.dataminded.nifi.plugins.PutCloudWatchCountMetricAndAlarm.java
License:Apache License
/** * Create client using aws credentials provider. This is the preferred way for creating clients */// w w w .ja v a2 s. co m @Override protected AmazonCloudWatchClient createClient(ProcessContext processContext, AWSCredentialsProvider awsCredentialsProvider, ClientConfiguration clientConfiguration) { getLogger().info("Creating client using aws credentials provider"); return new AmazonCloudWatchClient(awsCredentialsProvider, clientConfiguration); }
From source file:be.dataminded.nifi.plugins.PutCloudWatchCountMetricAndAlarm.java
License:Apache License
/** * Create client using AWSCredentials//from w w w . j a va 2 s. co m * * @deprecated use {@link #createClient(ProcessContext, AWSCredentialsProvider, ClientConfiguration)} instead */ @Override protected AmazonCloudWatchClient createClient(ProcessContext processContext, AWSCredentials awsCredentials, ClientConfiguration clientConfiguration) { getLogger().debug("Creating client with aws credentials"); return new AmazonCloudWatchClient(awsCredentials, clientConfiguration); }
From source file:com.amazon.kinesis.streaming.agent.AgentContext.java
License:Open Source License
public AmazonCloudWatch getCloudWatchClient() { if (cloudwatchClient == null) { cloudwatchClient = new AmazonCloudWatchClient(getAwsCredentialsProvider(), getAwsClientConfiguration()); if (!Strings.isNullOrEmpty(cloudwatchEndpoint())) cloudwatchClient.setEndpoint(cloudwatchEndpoint()); }// w w w. j ava2s . c om return cloudwatchClient; }
From source file:com.amazon.kinesis.streaming.agent.metrics.CWMetricsFactory.java
License:Open Source License
/** * Constructor.// w ww . jav a 2 s. com * * @param credentialsProvider client credentials for CloudWatch * @param clientConfig Configuration to use with the AmazonCloudWatchClient * @param namespace the namespace under which the metrics will appear in the CloudWatch console * @param bufferTimeMillis time to buffer metrics before publishing to CloudWatch * @param maxQueueSize maximum number of metrics that we can have in a queue */ public CWMetricsFactory(AWSCredentialsProvider credentialsProvider, ClientConfiguration clientConfig, String namespace, long bufferTimeMillis, int maxQueueSize) { this(new AmazonCloudWatchClient(credentialsProvider, clientConfig), namespace, bufferTimeMillis, maxQueueSize); }
From source file:com.capitalone.dashboard.collector.AWSCloudCollectorTask.java
License:Apache License
/** * The collection action. This is the task which will run on a schedule to * gather data from the feature content source system and update the * repository with retrieved data.//from w w w .ja v a 2 s . co m */ public void collect(AWSCloudCollector collector) { logger.info("Starting Cloud collection..."); ClientConfiguration clientConfig = new ClientConfiguration().withProxyHost("proxy.kdc.capitalone.com") .withProxyPort(8099).withPreemptiveBasicProxyAuth(true).withProxyUsername("xxx") .withProxyPassword("xxxxxxx"); String accessKey = "xxx"; String secretKey = "xxx"; AWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey); AmazonEC2Client ec2Client = new AmazonEC2Client(creds, clientConfig); AmazonCloudWatchClient cwClient = new AmazonCloudWatchClient(creds, clientConfig); DescribeInstancesResult result = ec2Client.describeInstances(); //create list of instances List<Instance> instanceList = new ArrayList<Instance>(); List<Reservation> reservations = result.getReservations(); for (Reservation currRes : reservations) { List<Instance> currInstanceList = currRes.getInstances(); instanceList.addAll(currInstanceList); } //purge the repo of old instance data if (awsRawDataRepository.count() > 0) awsRawDataRepository.deleteAll(); //for every instance determine all metrics logger.info("Collecting Raw Data..."); for (Instance currInstance : instanceList) { CloudRawData object = cloudClient.getMetrics(currInstance, cwClient, accessKey); awsRawDataRepository.save(object); } //purge the repo of old account data if (cloudAccountRepository.count() > 0) cloudAccountRepository.deleteAll(); logger.info("Agregating Data..."); CloudAggregatedData aggregatedData = new CloudAggregatedData(); aggregatedData.setAgeWarning(awsRawDataRepository.runAgeWarning("cof-sandbox-dev").size()); aggregatedData.setAgeExpired(awsRawDataRepository.runAgeExpired("cof-sandbox-dev").size()); aggregatedData.setAgeGood(awsRawDataRepository.runAgeGood("cof-sandbox-dev").size()); aggregatedData.setCpuHigh(awsRawDataRepository.runCpuUtilizationHigh("cof-sandbox-dev").size()); aggregatedData.setCpuMid(awsRawDataRepository.runCpuUtilizationMid("cof-sandbox-dev").size()); aggregatedData.setCpuLow(awsRawDataRepository.runCpuUtilizationLow("cof-sandbox-dev").size()); aggregatedData.setNonEncryptedCount(awsRawDataRepository.runNonEncrypted("cof-sandbox-dev").size()); aggregatedData.setNonTaggedCount(awsRawDataRepository.runNonTagged("cof-sandbox-dev").size()); aggregatedData.setStoppedCount(awsRawDataRepository.runStopped("cof-sandbox-dev").size()); aggregatedData.setAccountName("cof-sandbox-dev"); aggregatedData.setTotalInstanceCount(awsRawDataRepository.runAllInstanceCount("cof-sandbox-dev").size()); cloudAccountRepository.save(aggregatedData); logger.info("Finished Cloud collection."); }
From source file:com.ec2box.manage.action.SystemAction.java
License:Apache License
@Action(value = "/admin/viewSystems", results = { @Result(name = "success", location = "/admin/view_systems.jsp") }) public String viewSystems() { Long userId = AuthUtil.getUserId(servletRequest.getSession()); String userType = AuthUtil.getUserType(servletRequest.getSession()); List<String> ec2RegionList = EC2KeyDB.getEC2Regions(); List<String> instanceIdList = new ArrayList<String>(); //default instance state if (sortedSet.getFilterMap().get(FILTER_BY_INSTANCE_STATE) == null) { sortedSet.getFilterMap().put(FILTER_BY_INSTANCE_STATE, AppConfig.getProperty("defaultInstanceState")); }//from w w w . ja va 2 s. com try { Map<String, HostSystem> hostSystemList = new HashMap<String, HostSystem>(); //if user profile has been set or user is a manager List<Profile> profileList = UserProfileDB.getProfilesByUser(userId); if (profileList.size() > 0 || Auth.MANAGER.equals(userType)) { //set tags for profile List<String> profileTags = new ArrayList<>(); for (Profile profile : profileList) { profileTags.add(profile.getTag()); } Map<String, List<String>> profileTagMap = parseTags(profileTags); //set tags from input filters Map<String, List<String>> filterTags = fetchInputFilterTags(userType, profileTagMap); //parse out security group list in format group[,group] List<String> securityGroupList = new ArrayList<>(); if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_SECURITY_GROUP))) { securityGroupList = Arrays .asList(sortedSet.getFilterMap().get(FILTER_BY_SECURITY_GROUP).split(",")); } //get AWS credentials from DB for (AWSCred awsCred : AWSCredDB.getAWSCredList()) { if (awsCred != null) { //set AWS credentials for service BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(), awsCred.getSecretKey()); for (String ec2Region : ec2RegionList) { //create service AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig()); service.setEndpoint(ec2Region); //only return systems that have keys set List<String> keyValueList = new ArrayList<String>(); for (EC2Key ec2Key : EC2KeyDB.getEC2KeyByRegion(ec2Region, awsCred.getId())) { keyValueList.add(ec2Key.getKeyNm()); } DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest(); Filter keyNmFilter = new Filter("key-name", keyValueList); describeInstancesRequest.withFilters(keyNmFilter); //instance state filter if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_INSTANCE_STATE))) { List<String> instanceStateList = new ArrayList<String>(); instanceStateList.add(sortedSet.getFilterMap().get(FILTER_BY_INSTANCE_STATE)); Filter instanceStateFilter = new Filter("instance-state-name", instanceStateList); describeInstancesRequest.withFilters(instanceStateFilter); } if (securityGroupList.size() > 0) { Filter groupFilter = new Filter("group-name", securityGroupList); describeInstancesRequest.withFilters(groupFilter); } //set name value pair for tag filter List<String> tagList = new ArrayList<String>(); //always add all profile tags to filter list addTagsToDescribeInstanceRequest(profileTagMap, describeInstancesRequest, tagList); //add all additional filter tags provided by the user addTagsToDescribeInstanceRequest(filterTags, describeInstancesRequest, tagList); if (tagList.size() > 0) { Filter tagFilter = new Filter("tag-key", tagList); describeInstancesRequest.withFilters(tagFilter); } DescribeInstancesResult describeInstancesResult = service .describeInstances(describeInstancesRequest); for (Reservation res : describeInstancesResult.getReservations()) { for (Instance instance : res.getInstances()) { HostSystem hostSystem = new HostSystem(); hostSystem.setInstance(instance.getInstanceId()); //check for public dns if doesn't exist set to ip or pvt dns if (!"true".equals(AppConfig.getProperty("useEC2PvtDNS")) && StringUtils.isNotEmpty(instance.getPublicDnsName())) { hostSystem.setHost(instance.getPublicDnsName()); } else if (!"true".equals(AppConfig.getProperty("useEC2PvtDNS")) && StringUtils.isNotEmpty(instance.getPublicIpAddress())) { hostSystem.setHost(instance.getPublicIpAddress()); } else if (StringUtils.isNotEmpty(instance.getPrivateDnsName())) { hostSystem.setHost(instance.getPrivateDnsName()); } else { hostSystem.setHost(instance.getPrivateIpAddress()); } hostSystem.setKeyId(EC2KeyDB .getEC2KeyByNmRegion(instance.getKeyName(), ec2Region, awsCred.getId()) .getId()); hostSystem.setEc2Region(ec2Region); hostSystem.setState(instance.getState().getName()); for (Tag tag : instance.getTags()) { if ("Name".equals(tag.getKey())) { hostSystem.setDisplayNm(tag.getValue()); } } instanceIdList.add(hostSystem.getInstance()); hostSystemList.put(hostSystem.getInstance(), hostSystem); } } if (instanceIdList.size() > 0) { //set instance id list to check permissions when creating sessions servletRequest.getSession().setAttribute("instanceIdList", new ArrayList<String>(instanceIdList)); if (showStatus) { //make service call 100 instances at a time b/c of AWS limitation int i = 0; List<String> idCallList = new ArrayList<String>(); while (!instanceIdList.isEmpty()) { idCallList.add(instanceIdList.remove(0)); i++; //when i eq 100 make call if (i >= 100 || instanceIdList.isEmpty()) { //get status for host systems DescribeInstanceStatusRequest describeInstanceStatusRequest = new DescribeInstanceStatusRequest(); describeInstanceStatusRequest.withInstanceIds(idCallList); DescribeInstanceStatusResult describeInstanceStatusResult = service .describeInstanceStatus(describeInstanceStatusRequest); for (InstanceStatus instanceStatus : describeInstanceStatusResult .getInstanceStatuses()) { HostSystem hostSystem = hostSystemList .remove(instanceStatus.getInstanceId()); hostSystem.setSystemStatus( instanceStatus.getSystemStatus().getStatus()); hostSystem.setInstanceStatus( instanceStatus.getInstanceStatus().getStatus()); //check and filter by instance or system status if ((StringUtils.isEmpty( sortedSet.getFilterMap().get(FILTER_BY_INSTANCE_STATUS)) && StringUtils.isEmpty(sortedSet.getFilterMap() .get(FILTER_BY_SYSTEM_STATUS))) || (hostSystem.getInstanceStatus() .equals(sortedSet.getFilterMap() .get(FILTER_BY_INSTANCE_STATUS)) && StringUtils.isEmpty(sortedSet.getFilterMap() .get(FILTER_BY_SYSTEM_STATUS))) || (hostSystem.getInstanceStatus() .equals(sortedSet.getFilterMap() .get(FILTER_BY_SYSTEM_STATUS)) && StringUtils.isEmpty(sortedSet.getFilterMap() .get(FILTER_BY_INSTANCE_STATUS))) || (hostSystem.getInstanceStatus() .equals(sortedSet.getFilterMap() .get(FILTER_BY_SYSTEM_STATUS)) && hostSystem.getInstanceStatus() .equals(sortedSet.getFilterMap() .get(FILTER_BY_INSTANCE_STATUS)))) { hostSystemList.put(hostSystem.getInstance(), hostSystem); } } //start over i = 0; //clear list idCallList.clear(); } } //check alarms for ec2 instances AmazonCloudWatchClient cloudWatchClient = new AmazonCloudWatchClient( awsCredentials, AWSClientConfig.getClientConfig()); cloudWatchClient.setEndpoint(ec2Region.replace("ec2", "monitoring")); DescribeAlarmsResult describeAlarmsResult = cloudWatchClient.describeAlarms(); for (MetricAlarm metricAlarm : describeAlarmsResult.getMetricAlarms()) { for (Dimension dim : metricAlarm.getDimensions()) { if (dim.getName().equals("InstanceId")) { HostSystem hostSystem = hostSystemList.remove(dim.getValue()); if (hostSystem != null) { if ("ALARM".equals(metricAlarm.getStateValue())) { hostSystem .setMonitorAlarm(hostSystem.getMonitorAlarm() + 1); } else if ("INSUFFICIENT_DATA" .equals(metricAlarm.getStateValue())) { hostSystem.setMonitorInsufficientData( hostSystem.getMonitorInsufficientData() + 1); } else { hostSystem.setMonitorOk(hostSystem.getMonitorOk() + 1); } //check and filter by alarm state if (StringUtils.isEmpty( sortedSet.getFilterMap().get(FILTER_BY_ALARM_STATE))) { hostSystemList.put(hostSystem.getInstance(), hostSystem); } else if ("ALARM".equals( sortedSet.getFilterMap().get(FILTER_BY_ALARM_STATE)) && hostSystem.getMonitorAlarm() > 0) { hostSystemList.put(hostSystem.getInstance(), hostSystem); } else if ("INSUFFICIENT_DATA".equals( sortedSet.getFilterMap().get(FILTER_BY_ALARM_STATE)) && hostSystem.getMonitorInsufficientData() > 0) { hostSystemList.put(hostSystem.getInstance(), hostSystem); } else if ("OK".equals( sortedSet.getFilterMap().get(FILTER_BY_ALARM_STATE)) && hostSystem.getMonitorOk() > 0 && hostSystem.getMonitorInsufficientData() <= 0 && hostSystem.getMonitorAlarm() <= 0) { hostSystemList.put(hostSystem.getInstance(), hostSystem); } } } } } } } } } } //set ec2 systems SystemDB.setSystems(hostSystemList.values()); sortedSet = SystemDB.getSystemSet(sortedSet, new ArrayList<String>(hostSystemList.keySet())); } } catch (AmazonServiceException ex) { log.error(ex.toString(), ex); } if (script != null && script.getId() != null) { script = ScriptDB.getScript(script.getId(), userId); } return SUCCESS; }
From source file:com.swap.aws.elb.client.AWSHelper.java
License:Apache License
public AWSHelper(String awsAccessKey, String awsSecretKey, String availabilityZone, String region) { this.awsAccessKey = awsAccessKey; this.awsSecretKey = awsSecretKey; this.availabilityZone = availabilityZone; this.region = region; awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey); clientConfiguration = new ClientConfiguration(); cloudWatchClient = new AmazonCloudWatchClient(awsCredentials, clientConfiguration); }
From source file:lumbermill.aws.kcl.internal.KinesisConsumerBootstrap.java
License:Apache License
public void start() { int mb = 1024 * 1024; LOG.info("Max memory: {} mb", Runtime.getRuntime().maxMemory() / mb); LOG.info("Starting up Kinesis Consumer... (may take a few seconds)"); AmazonKinesisClient kinesisClient = new AmazonKinesisClient(kinesisCfg.getKinesisCredentialsProvider(), kinesisCfg.getKinesisClientConfiguration()); AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(kinesisCfg.getDynamoDBCredentialsProvider(), kinesisCfg.getDynamoDBClientConfiguration()); AmazonCloudWatch cloudWatchClient = new AmazonCloudWatchClient( kinesisCfg.getCloudWatchCredentialsProvider(), kinesisCfg.getCloudWatchClientConfiguration()); Worker worker = new Worker.Builder() .recordProcessorFactory(// w w w. j a v a 2s.c o m () -> new RecordProcessor(unitOfWorkListener, exceptionStrategy, metricsCallback, dry)) .config(kinesisCfg).kinesisClient(kinesisClient).dynamoDBClient(dynamoDBClient) .cloudWatchClient(cloudWatchClient).build(); worker.run(); }
From source file:org.apache.stratos.aws.extension.AWSHelper.java
License:Apache License
public AWSHelper() throws LoadBalancerExtensionException { // Read values for awsAccessKey, awsSecretKey etc. from config file String awsPropertiesFile = System.getProperty(Constants.AWS_PROPERTIES_FILE); Properties properties = new Properties(); InputStream inputStream = null; try {/*from w w w. j a va2 s . c o m*/ inputStream = new FileInputStream(awsPropertiesFile); properties.load(inputStream); this.awsAccessKey = properties.getProperty(Constants.AWS_ACCESS_KEY); this.awsSecretKey = properties.getProperty(Constants.AWS_SECRET_KEY); if (this.awsAccessKey.isEmpty() || this.awsSecretKey.isEmpty()) { throw new LoadBalancerExtensionException("Invalid AWS credentials."); } this.lbPrefix = properties.getProperty(Constants.LB_PREFIX); if (this.lbPrefix.isEmpty() || this.lbPrefix.length() > Constants.LOAD_BALANCER_PREFIX_MAX_LENGTH) { throw new LoadBalancerExtensionException("Invalid load balancer prefix."); } lbSequence = new AtomicInteger(1); this.lbSecurityGroupName = properties.getProperty(Constants.LOAD_BALANCER_SECURITY_GROUP_NAME); lbSecurityGroupId = properties.getProperty(Constants.LOAD_BALANCER_SECURITY_GROUP_ID); if ((lbSecurityGroupId == null || lbSecurityGroupId.isEmpty()) && (this.lbSecurityGroupName.isEmpty() || this.lbSecurityGroupName.length() > Constants.SECURITY_GROUP_NAME_MAX_LENGTH)) { throw new LoadBalancerExtensionException( "Either security group name or security " + "group id is required"); } // if (this.lbSecurityGroupName.isEmpty() || this.lbSecurityGroupName.length() > // Constants.SECURITY_GROUP_NAME_MAX_LENGTH) { // throw new LoadBalancerExtensionException("Invalid load balancer security group name."); // } // Read the SSL certificate Id. This is mandatory if only we are using HTTPS as the front end protocol. // http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/using-elb-listenerconfig-quickref.html this.sslCertificateId = properties.getProperty(Constants.LOAD_BALANCER_SSL_CERTIFICATE_ID).trim(); // Cookie name for application level stickiness this.appStickySessionCookie = properties.getProperty(Constants.APP_STICKY_SESSION_COOKIE_NAME).trim(); this.allowedCidrIpForLBSecurityGroup = properties.getProperty(Constants.ALLOWED_CIDR_IP_KEY); if (this.allowedCidrIpForLBSecurityGroup.isEmpty()) { throw new LoadBalancerExtensionException("Invalid allowed CIDR IP."); } String allowedProtocols = properties.getProperty(Constants.ALLOWED_PROTOCOLS); if (allowedProtocols.isEmpty()) { throw new LoadBalancerExtensionException("Please specify at least one Internet protocol."); } String[] protocols = allowedProtocols.split(","); this.allowedProtocolsForLBSecurityGroup = new ArrayList<String>(); for (String protocol : protocols) { this.allowedProtocolsForLBSecurityGroup.add(protocol); } String interval = properties.getProperty(Constants.STATISTICS_INTERVAL); if (interval == null || interval.isEmpty()) { this.statisticsInterval = Constants.STATISTICS_INTERVAL_MULTIPLE_OF; } else { try { this.statisticsInterval = Integer.parseInt(interval); if (this.statisticsInterval % Constants.STATISTICS_INTERVAL_MULTIPLE_OF != 0) { this.statisticsInterval = Constants.STATISTICS_INTERVAL_MULTIPLE_OF; } } catch (NumberFormatException e) { log.warn("Invalid statistics interval. Setting it to 15."); this.statisticsInterval = 15; } } this.lbSecurityGroupDescription = Constants.LOAD_BALANCER_SECURITY_GROUP_DESCRIPTION; String commaSeparatedInitialZones = properties.getProperty(Constants.INITIAL_AVAILABILITY_ZONES); if (commaSeparatedInitialZones != null && !commaSeparatedInitialZones.isEmpty()) { initialZones.addAll(Arrays.asList(commaSeparatedInitialZones.trim().split("\\s*," + "\\s*"))); } String commaSeparatedSubnetIds = properties.getProperty(Constants.SUBNET_IDS); if (commaSeparatedSubnetIds != null && !commaSeparatedSubnetIds.isEmpty()) { subnetIds.addAll(Arrays.asList(commaSeparatedSubnetIds.trim().split("\\s*," + "\\s*"))); } String commaSeparatedVPCIds = properties.getProperty(Constants.VPC_IDS); if (commaSeparatedVPCIds != null && !commaSeparatedVPCIds.isEmpty()) { vpcIds.addAll(Arrays.asList(commaSeparatedVPCIds.trim().split("\\s*," + "\\s*"))); } lbScheme = properties.getProperty(Constants.LB_SCHEME); regionToSecurityGroupIdMap = new ConcurrentHashMap<String, String>(); awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey); clientConfiguration = new ClientConfiguration(); elbClient = new AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration); ec2Client = new AmazonEC2Client(awsCredentials, clientConfiguration); cloudWatchClient = new AmazonCloudWatchClient(awsCredentials, clientConfiguration); } catch (IOException e) { log.error("Error reading aws configuration file."); throw new LoadBalancerExtensionException("Error reading aws configuration file.", e); } finally { try { inputStream.close(); } catch (Exception e) { log.warn("Failed to close input stream to aws configuration file."); } } }