Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[AMBARI-25078] Add metering metrics to AMS Metric Monitor. (#13)
- Loading branch information
Showing
22 changed files
with
295 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific | ||
|
||
curl --silent -H Metadata:true "http://169.254.169.254/metadata/instance?api-version=2017-12-01" | grep -Po '"vmSize":.*?[^\\]",' | cut -d':' -f2 | sed 's/,/ /g' | sed 's/"//g' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific | ||
|
||
curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | grep 'instanceType' | awk '{ print $3 }' | sed 's/,/ /g' | sed 's/"//g' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific | ||
|
||
curl --silent -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/machine-type" | awk -F'/' '{print $4}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env python | ||
|
||
''' | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
''' | ||
|
||
import logging | ||
import subprocess | ||
import sys | ||
|
||
logger = logging.getLogger() | ||
|
||
class HostInstanceTypeProvider: | ||
|
||
DEFAULT_INSTANCE_TYPE = "custom" | ||
KNOWN_PROVIDER_SCRIPT_PREFIX = "instance_type_provider_" | ||
KNOWN_PROVIDER_SCRIPTS = dict() | ||
|
||
def __init__(self, config): | ||
|
||
self.KNOWN_PROVIDER_SCRIPTS['google'] = config.get_config_dir() + self.KNOWN_PROVIDER_SCRIPT_PREFIX + "gce" | ||
self.KNOWN_PROVIDER_SCRIPTS['microsoft'] = config.get_config_dir() + self.KNOWN_PROVIDER_SCRIPT_PREFIX + "azure" | ||
self.KNOWN_PROVIDER_SCRIPTS['xen'] = config.get_config_dir() + self.KNOWN_PROVIDER_SCRIPT_PREFIX + "ec2" | ||
|
||
self.provider_type = config.get_provider_type() | ||
logger.info("Provider type {0}".format(self.provider_type)) | ||
|
||
script = self.get_script_for_provider(self.provider_type) | ||
logger.info("Script for provider {0}".format(script)) | ||
|
||
self.instance_type_script = config.get_instance_type_script() | ||
logger.info("Custom Instance Type Script {0}".format(self.instance_type_script)) | ||
|
||
if script: | ||
self.instance_type = self.get_instance_type_from_script(script) | ||
elif self.instance_type_script: | ||
self.instance_type = self.get_instance_type_from_script(self.instance_type_script) | ||
else: | ||
self.instance_type = self.DEFAULT_INSTANCE_TYPE | ||
logger.info("Instance type {0}".format(self.instance_type)) | ||
|
||
def get_instance_type(self): | ||
return self.instance_type | ||
|
||
def get_script_for_provider(self, provider_type): | ||
p_type = str(provider_type).lower() | ||
if provider_type and p_type in self.KNOWN_PROVIDER_SCRIPTS: | ||
return self.KNOWN_PROVIDER_SCRIPTS[p_type] | ||
return None | ||
|
||
def get_instance_type_from_script(self, script): | ||
instance_type = self.DEFAULT_INSTANCE_TYPE | ||
if script: | ||
try: | ||
osStat = subprocess.Popen([script], stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
out, err = osStat.communicate() | ||
if 0 == osStat.returncode and 0 != len(out.strip()): | ||
instance_type = out.strip() | ||
logger.info("Read instance_type '{0}' using script '{1}'".format(instance_type, script)) | ||
except: | ||
logger.warn("Unexpected error while retrieving instance_type: '{0}'".format(sys.exc_info())) | ||
return instance_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env python | ||
|
||
''' | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
''' | ||
|
||
import logging | ||
import time | ||
import json | ||
from instance_type_provider import HostInstanceTypeProvider | ||
|
||
logger = logging.getLogger() | ||
|
||
class MeteringMetricHandler: | ||
|
||
METERING_ALIVE_TIME_METRIC_SUFFIX = "lastKnownAliveTime" | ||
|
||
## At startup, | ||
def __init__(self, config): | ||
self.appId = config.get_metering_appId() | ||
self.instance_type_metric_appId = config.get_metering_appId() + "_instance_type" | ||
self.metering_enabled = config.is_metering_enabled() | ||
self.hostname = config.get_hostname_config() | ||
self.instance_id = config.get_instanceid() | ||
self.metering_metric_list = config.get_metering_metrics() | ||
self.start_ts = int(round(time.time() * 1000)) | ||
if self.metering_enabled: | ||
logger.info("Metering started with: appId = {0}, metering_metric_list = {1}, start time key = {2}" | ||
.format(self.appId, self.metering_metric_list, self.start_ts)) | ||
self.instance_type_provider = HostInstanceTypeProvider(config) | ||
self.instance_type = self.instance_type_provider.instance_type | ||
self.metering_metric_key_prefix = self.hostname + "~" + self.instance_type + "~" + str(self.start_ts) | ||
pass | ||
|
||
# Metering Metrics | ||
def get_metering_metrics(self, metrics): | ||
metering_metrics = {} | ||
curr_time = int(round(time.time() * 1000)) | ||
for metric_name, value in metrics.iteritems(): | ||
if metric_name in self.metering_metric_list: | ||
end_time_metric_key = self.metering_metric_key_prefix + "~" + metric_name + "~" + str(value) + "~" + self.METERING_ALIVE_TIME_METRIC_SUFFIX | ||
metering_metrics[end_time_metric_key] = curr_time | ||
|
||
return metering_metrics | ||
|
||
# Instance Type Metrics | ||
def get_instance_type_metrics(self): | ||
metering_metrics = {self.instance_type: 1} | ||
return metering_metrics |
Oops, something went wrong.