def __init__(self): self._log = htm_it_logging.getExtendedLogger(self.__class__.__name__) self._profiling = ( htm.it.app.config.getboolean("debugging", "profiling") or self._log.isEnabledFor(logging.DEBUG)) self._pollInterval = htm.it.app.config.getfloat( "metric_collector", "poll_interval") self._metricErrorGracePeriod = htm.it.app.config.getfloat( "metric_collector", "metric_error_grace_period") # Interval for periodic garbage collection of our caches (e.g., # self._metricInfoCache and self._resourceInfoCache) self._cacheGarbageCollectionIntervalSec = self._metricErrorGracePeriod * 2 # Time (unix epoch) when to run the next garbage collection of # self._metricInfoCache and self._resourceInfoCache; 0 triggers it ASAP as a # quick test of the logic. See self._cacheGarbageCollectionIntervalSec. self._nextCacheGarbageCollectionTime = 0 # We use this to cache info about metrics that helps us avoid unnecessary # queries to the datasource. The keys are metric uid's and corresponding # values are _MetricInfoCacheItem objects. self._metricInfoCache = defaultdict(_MetricInfoCacheItem) # We use this to cache info about resources referenced by metrics to help # us avoid unnecessary resource-status queries to the datasource. The keys # are resource cananonical identifiers (from Metric.server) and # corresponding values are _ResourceInfoCacheItem objects. self._resourceInfoCache = defaultdict(_ResourceInfoCacheItem) self.metricStreamer = MetricStreamer()
def __init__(self): # Make sure we have the latest version of configuration htm.it.app.config.loadConfig() self._log = getExtendedLogger(self.__class__.__name__) self._modelResultsExchange = (htm.it.app.config.get( "metric_streamer", "results_exchange_name"))
def __init__(self): # Make sure we have the latest version of configuration htm.it.app.config.loadConfig() self._log = getExtendedLogger(self.__class__.__name__) self._modelResultsExchange = ( htm.it.app.config.get("metric_streamer", "results_exchange_name"))
def _collect(task): """ Executed via multiprocessing Pool: Collect metric data and corresponding resource status. :param task: a _DataCollectionTask instance """ log = htm_it_logging.getExtendedLogger(MetricCollector.__name__) startTime = time.time() result = _DataCollectionResult(metricID=task.metricID) dsAdapter = None try: dsAdapter = createDatasourceAdapter(task.datasource) result.data, result.nextCallStart = dsAdapter.getMetricData( metricSpec=task.metricSpec, start=task.rangeStart, end=None) except Exception as e: # pylint: disable=W0703 log.exception("getMetricData failed in task=%s", task) result.data = e try: if task.updateResourceStatus: result.resourceStatus = dsAdapter.getMetricResourceStatus( metricSpec=task.metricSpec) except Exception as e: # pylint: disable=W0703 log.exception("getMetricResourceStatus failed in task=%s", task) result.resourceStatus = e result.duration = time.time() - startTime task.resultsQueue.put(result) return True
metrics_api, models_api, notifications_api, settings_api, support_api, update_api, wufoo_api, UnauthorizedResponse) from htm.it.app.webservices.utils import encodeJson from htmengine.utils import jsonDecode logging_support.LoggingSupport.initService() log = getExtendedLogger("webapp-logger") urls = ( # Web UI "", "DefaultHandler", "/", "DefaultHandler", "/htmit", "HTMITHandler", r"/htmit/([-\/\w]*)", "HTMITHandler", # i18n strings "/_msgs", "MessagesHandler", # REST API Endpoints "/_annotations", annotations_api.app,
def _getLogger(): return getExtendedLogger(_MODULE_NAME)
# http://numenta.org/licenses/ # ---------------------------------------------------------------------- import json import web from htm.it.app.adapters.datasource import createDatasourceAdapter from htm.it.app import exceptions as app_exceptions, repository from htm.it.app.webservices import (AuthenticatedBaseHandler, ManagedConnectionWebapp) from htm.it.app.webservices.utils import (getMetricDisplayFields, convertMetricRowToMetricDict) from htm.it import htm_it_logging LOGGER = htm_it_logging.getExtendedLogger(__name__) urls = ( # /_metrics/custom '', "CustomDefaultHandler", # /_metrics/custom/ '/', "CustomDefaultHandler", # /_metrics/custom/<metricName> '/([\w\.\-]+)', "CustomDefaultHandler", ) class CustomDefaultHandler(AuthenticatedBaseHandler):
import htm.it.app import htm.it.app.adapters.datasource as datasource_adapter_factory from htm.it.app.adapters.datasource.cloudwatch import aws_base import htm.it.app.exceptions as app_exceptions from htm.it.app import repository from htm.it.app.repository import schema from htmengine.repository.queries import MetricStatus import htmengine.utils from htm.it.test_utils.app.test_case_base import TestCaseBase, unittest import htm.it.test_utils.aws_utils _LOG = getExtendedLogger("cw_datasource_adapter_base_test") class CloudwatchDatasourceAdapterBaseTest(TestCaseBase): # EC2 Region name of the test ec2 instance _testRegion = None _testId = None _modelSpecNoMinMax = None _modelSpecWithMinMax = None # What we expect to be supported so far _expectedResourceTypes = [ aws_base.ResourceTypeNames.EC2_INSTANCE,
import htm.it.app from htm.it.app.aws import instance_utils from htm.it.app.aws.ec2_utils import checkEC2Authorization from htm.it.app.exceptions import AuthFailure, AWSPermissionsError from htm.it.app.webservices import ( annotations_api, anomalies_api, autostacks_api, instances_api, getWebLogPrefix, logging_api, messagemanager, metrics_api, models_api, notifications_api, settings_api, support_api, update_api, wufoo_api, UnauthorizedResponse) from htm.it.app.webservices.utils import encodeJson from htmengine.utils import jsonDecode logging_support.LoggingSupport.initService() log = getExtendedLogger("webapp-logger") urls = ( # Web UI "", "DefaultHandler", "/", "DefaultHandler", "/htmit", "HTMITHandler", r"/htmit/([-\/\w]*)", "HTMITHandler", # i18n strings "/_msgs", "MessagesHandler",
import yaml from htm.it import logging_support from htm.it.htm_it_logging import getExtendedLogger import htm.it.app import htm.it.app.adapters.datasource as datasource_adapter_factory from htm.it.app.adapters.datasource.cloudwatch import aws_base from htm.it.app import repository from htm.it.app.repository.queries import MetricStatus from htmengine.utils import jsonDecode from htm.it.test_utils.app.test_case_base import TestCaseBase, unittest import htm.it.test_utils.aws_utils _LOG = getExtendedLogger("cw_datasource_adapter_metrics_test") def setUpModule(): logging_support.LoggingSupport.initTestApp() class CloudwatchDatasourceAdapterMetricsTest(TestCaseBase): _supportedResourceTypes = [ aws_base.ResourceTypeNames.EC2_INSTANCE, aws_base.ResourceTypeNames.AUTOSCALING_GROUP, #aws_base.ResourceTypeNames.DYNAMODB_TABLE, (NO DATA) aws_base.ResourceTypeNames.ELB_LOAD_BALANCER, #aws_base.ResourceTypeNames.EBS_VOLUME, (NO DATA) aws_base.ResourceTypeNames.OPSWORKS_STACK,
def setUpModule(): logging_support.LoggingSupport.initTestApp() global g_log # pylint: disable=W0603 g_log = getExtendedLogger("autostack_datasource_adapter_test")
def _getLogger(): return htm_it_logging.getExtendedLogger("htm.it.app.quota")
from htm.it import logging_support from htm.it.htm_it_logging import getExtendedLogger import htm.it.app import htm.it.app.adapters.datasource as datasource_adapter_factory from htm.it.app.adapters.datasource.cloudwatch import aws_base import htm.it.app.exceptions as app_exceptions from htm.it.app import repository from htm.it.app.repository import schema from htmengine.repository.queries import MetricStatus import htmengine.utils from htm.it.test_utils.app.test_case_base import TestCaseBase, unittest import htm.it.test_utils.aws_utils _LOG = getExtendedLogger("cw_datasource_adapter_base_test") class CloudwatchDatasourceAdapterBaseTest(TestCaseBase): # EC2 Region name of the test ec2 instance _testRegion = None _testId = None _modelSpecNoMinMax = None _modelSpecWithMinMax = None # What we expect to be supported so far _expectedResourceTypes = [ aws_base.ResourceTypeNames.EC2_INSTANCE, aws_base.ResourceTypeNames.AUTOSCALING_GROUP,
import htmengine.model_swapper.utils as model_swapper_utils from htm.it.app.adapters.datasource import (createAutostackDatasourceAdapter, createCloudwatchDatasourceAdapter) from htm.it.app.exceptions import DuplicateRecordError, ObjectNotFoundError from htm.it.app.webservices import (AuthenticatedBaseHandler, ManagedConnectionWebapp) from htm.it.app.webservices.responses import (InvalidRequestResponse, quotaErrorResponseWrapper) from htm.it.app.webservices.utils import (getMetricDisplayFields, convertMetricRowToMetricDict) from htm.it.app.exceptions import QuotaError from htm.it import htm_it_logging from htm.it.app import repository from htm.it.app.quota import checkQuotaForInstanceAndRaise log = htm_it_logging.getExtendedLogger("webservices") urls = ( r"/preview_instances/?", "AutostackInstancesHandler", r"/([-\w]+)/?", "AutostackHandler", r"/([-\w]+)/metrics/?([-\w]*)", "AutostackMetricsHandler", r"/([-\w]+)/instances/?", "AutostackInstancesHandler", r"/?", "AutostacksHandler", )
def _collectMetrics(task): """ Executed via multiprocessing Pool: Collect metric data :param task: a _MetricCollectionTask object :returns: a _MetricCollectionTaskResult object """ log = getExtendedLogger(_MODULE_NAME + ".COLLECTOR") taskResult = _MetricCollectionTaskResult( refID=task.refID, metricID=task.metricID, instanceID=task.instanceID) try: if task.metricName == "InstanceCount": timestamp = datetime.utcnow().replace(second=0, microsecond=0) taskResult.data = (MetricRecord(timestamp=timestamp, value=1.0),) else: backoffSec = 0.75 backoffGrowthFactor = 1.5 maxBackoffSec = 5 rawdata = None while rawdata is None: try: cw = cloudwatch.connect_to_region(region_name=task.region, **getAWSCredentials()) rawdata = cw.get_metric_statistics( period=task.period, start_time=task.timeRange.start, end_time=task.timeRange.end, metric_name=task.metricName, namespace="AWS/EC2", statistics=task.stats, dimensions=dict(InstanceId=task.instanceID), unit=task.unit) except BotoServerError as e: if e.status == 400 and e.error_code == "Throttling": # TODO: unit-test log.info("Throttling: %r", e) if backoffSec <= maxBackoffSec: time.sleep(backoffSec) backoffSec *= backoffGrowthFactor else: raise app_exceptions.MetricThrottleError(repr(e)) else: raise # Sort the data by timestamp in ascending order rawdata.sort(key=lambda row: row["Timestamp"]) # Convert raw data to MetricRecord objects def getValue(rawDataItem, stats): if isinstance(stats, basestring): return rawDataItem[stats] else: return dict((field, rawDataItem[field]) for field in stats) taskResult.data = tuple( MetricRecord(timestamp=rawItem["Timestamp"], value=getValue(rawItem, task.stats)) for rawItem in rawdata) except Exception as e: # pylint: disable=W0703 log.exception("Error in task=%r", task) taskResult.exception = e finally: taskResult.duration = time.time() - taskResult.creationTime return taskResult
from htm.it import logging_support from htm.it.htm_it_logging import getExtendedLogger import htm.it.app import htm.it.app.adapters.datasource as datasource_adapter_factory from htm.it.app.adapters.datasource.cloudwatch import aws_base from htm.it.app import repository from htm.it.app.repository.queries import MetricStatus from htmengine.utils import jsonDecode from htm.it.test_utils.app.test_case_base import TestCaseBase, unittest import htm.it.test_utils.aws_utils _LOG = getExtendedLogger("cw_datasource_adapter_metrics_test") def setUpModule(): logging_support.LoggingSupport.initTestApp() class CloudwatchDatasourceAdapterMetricsTest(TestCaseBase): _supportedResourceTypes = [ aws_base.ResourceTypeNames.EC2_INSTANCE, aws_base.ResourceTypeNames.AUTOSCALING_GROUP, #aws_base.ResourceTypeNames.DYNAMODB_TABLE, (NO DATA)
self._log.exception("RabbitMQ connection failed") raise except amqp.exceptions.AmqpChannelError: self._log.exception("RabbitMQ channel failed") raise except Exception as ex: self._log.exception("Error Streaming data: %s", ex) raise except KeyboardInterrupt: self._log.info("Stopping htm-it Notification Service: KeyboardInterrupt") finally: self._log.info("HTM-IT Notification Service is exiting") if __name__ == "__main__": g_log = getExtendedLogger("NotificationService") logging_support.LoggingSupport.initService() try: NotificationService().run() except KeyboardInterrupt: pass except Exception as e: outp = StringIO.StringIO() ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb, file=outp) outp.seek(0) g_log.info(outp.read()) g_log.exception("Unexpected Error") sys.exit(1)
def _getCollectWorkerLogger(): return getExtendedLogger(_MODULE_NAME + ".COLLECTOR")
def _getLogger(): return htm_it_logging.getExtendedLogger(_MODULE_NAME)
def _getInstanceWorkerLogger(): return getExtendedLogger(_MODULE_NAME + ".INSTANCE")
self._log.exception("RabbitMQ connection failed") raise except amqp.exceptions.AmqpChannelError: self._log.exception("RabbitMQ channel failed") raise except Exception as ex: self._log.exception("Error Streaming data: %s", ex) raise except KeyboardInterrupt: self._log.info( "Stopping htm-it Notification Service: KeyboardInterrupt") finally: self._log.info("HTM-IT Notification Service is exiting") if __name__ == "__main__": g_log = getExtendedLogger("NotificationService") logging_support.LoggingSupport.initService() try: NotificationService().run() except KeyboardInterrupt: pass except Exception as e: outp = StringIO.StringIO() ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb, file=outp) outp.seek(0) g_log.info(outp.read()) g_log.exception("Unexpected Error") sys.exit(1)
import json from pkg_resources import resource_filename import requests from boto.exception import BotoServerError import web import htm.it.app from htm.it.app import config from htmengine import utils from htm.it.app.aws import instance_utils from htm.it.app.aws import ses_utils from htm.it import htm_it_logging log = htm_it_logging.getExtendedLogger("webservices") urls = ( '', "WufooHandler" ) class WufooHandler(object): # save to wufoo form def POST(self): url = config.get("usertrack", "wufoo_url") user = config.get("usertrack", "wufoo_user")