Example #1
0
    def testUnMonitorMetric(self, requestsMock):

        requestsMock.delete.return_value = Mock(status_code=200)

        metric_utils.unmonitorMetric("localhost", "taurus", "foo")

        requestsMock.delete.assert_called_once_with(
            "https://localhost/_models/foo", verify=ANY, auth=("taurus", ""))
  def testUnMonitorMetric(self, requestsMock):

    requestsMock.delete.return_value = Mock(status_code=200)

    metric_utils.unmonitorMetric("localhost", "taurus", "foo")

    requestsMock.delete.assert_called_once_with(
      "https://localhost/_models/foo",
      verify=ANY, auth=("taurus", ""))
def main():
  logging_support.LoggingSupport.initTool()

  try:
    options = _parseArgs()
    g_log.info("Running %s with options=%r", sys.argv[0], options)

    if options["unmonitorAll"]:
      models = metric_utils.getAllModels(
        host=options["htmServer"],
        apiKey=options["apiKey"])
    else:
      models = tuple(
        metric_utils.getOneModel(
          host=options["htmServer"],
          apiKey=options["apiKey"],
          modelId=modelId)
        for modelId in options["modelIds"]
      )

    # Save model objects to file for use by monitor_metrics
    with open(options["modelsFilePath"], "w") as outFile:
      json.dump(models, outFile, indent=4)

    if not models:
      g_log.info("No models to unmonitor")
      return

    g_log.info("Unmonitoring %d models", len(models))

    for i, model in enumerate(models, 1):
      modelId = model["uid"]
      metric_utils.unmonitorMetric(
        host=options["htmServer"],
        apiKey=options["apiKey"],
        modelId=modelId)
      g_log.info("Unmonitored metric=%s (%d of %d)",
                 modelId, i, len(models))

    g_log.info("Unmonitored %d models", len(models))
  except SystemExit as e:
    if e.code != 0:
      g_log.exception("unmonitor_metrics failed")
    raise
  except Exception:
    g_log.exception("unmonitor_metrics failed")
    raise
def main():
    logging_support.LoggingSupport.initTool()

    try:
        options = _parseArgs()
        g_log.info("Running %s with options=%r", sys.argv[0], options)

        if options["unmonitorAll"]:
            models = metric_utils.getAllModels(host=options["htmServer"],
                                               apiKey=options["apiKey"])
        else:
            models = tuple(
                metric_utils.getOneModel(host=options["htmServer"],
                                         apiKey=options["apiKey"],
                                         modelId=modelId)
                for modelId in options["modelIds"])

        # Save model objects to file for use by monitor_metrics
        with open(options["modelsFilePath"], "w") as outFile:
            json.dump(models, outFile, indent=4)

        if not models:
            g_log.info("No models to unmonitor")
            return

        g_log.info("Unmonitoring %d models", len(models))

        for i, model in enumerate(models, 1):
            modelId = model["uid"]
            metric_utils.unmonitorMetric(host=options["htmServer"],
                                         apiKey=options["apiKey"],
                                         modelId=modelId)
            g_log.info("Unmonitored metric=%s (%d of %d)", modelId, i,
                       len(models))

        g_log.info("Unmonitored %d models", len(models))
    except SystemExit as e:
        if e.code != 0:
            g_log.exception("unmonitor_metrics failed")
        raise
    except Exception:
        g_log.exception("unmonitor_metrics failed")
        raise
Example #5
0
def main():
    """
  NOTE: main also serves as entry point for "console script" generated by setup
  """
    logging_support.LoggingSupport().initTool()

    try:
        options = _parseArgs()

        g_log.info("Verifying that agents are in hot_standby mode")
        for section in config.sections():
            try:
                assert config.get(section, "opmode") == ApplicationConfig.OP_MODE_HOT_STANDBY
            except Exception, e:
                raise

        g_log.info("Verifying that the old symbol has been removed from the " "metrics configuration")
        for stockData in metric_utils.getMetricsConfiguration().itervalues():
            assert stockData["symbol"] != options.old_symbol

        if options.twitter and (not options.stocks):
            g_log.info(
                "Migrating ONLY twitter data from old-symbol=%s " "to new-symbol=%s",
                options.old_symbol,
                options.new_symbol,
            )
        elif options.stocks and (not options.twitter):
            g_log.info(
                "Migrating ONLY xignite stock data from old-symbol=%s " "to new-symbol=%s",
                options.old_symbol,
                options.new_symbol,
            )
            raise NotImplementedError
        else:
            g_log.info(
                "Migrating BOTH twitter and xignite stock data from " "old-symbol=%s to new-symbol=%s",
                options.old_symbol,
                options.new_symbol,
            )
            raise NotImplementedError

        oldSymbolTweetPrefix = "TWITTER.TWEET.HANDLE.{symbol}.".format(symbol=options.old_symbol)
        newSymbolTweetPrefix = "TWITTER.TWEET.HANDLE.{symbol}.".format(symbol=options.new_symbol)
        oldSymbolTweetMetricsList = []

        with collectorsdb.engineFactory().begin() as conn:

            g_log.info("Renaming metrics to new symbol")
            if options.twitter:
                oldSymbolTweetsQuery = sql.select([tweetSamplesSchema]).where(
                    tweetSamplesSchema.c.metric.contains(oldSymbolTweetPrefix)
                )
                oldSymbolTweets = conn.execute(oldSymbolTweetsQuery)
                for tweetSample in oldSymbolTweets:
                    newMetricName = "{newPrefix}{metric}".format(
                        newPrefix=newSymbolTweetPrefix, metric=tweetSample.metric[len(oldSymbolTweetPrefix) :]
                    )
                    if tweetSample.metric not in oldSymbolTweetMetricsList:
                        oldSymbolTweetMetricsList.append(tweetSample.metric)

                    updateSampleQuery = (
                        tweetSamplesSchema.update()
                        .where(tweetSamplesSchema.c.seq == tweetSample.seq)
                        .values(metric=newMetricName)
                    )

                    conn.execute(updateSampleQuery)

            g_log.info("Forwarding new twitter metric data to Taurus engine...")
            if options.twitter:
                oldestRecordTs = conn.execute(
                    sql.select([tweetSamplesSchema.c.agg_ts], order_by=tweetSamplesSchema.c.agg_ts.asc())
                ).first()[0]
                lastEmittedAggTime = metric_utils.establishLastEmittedSampleDatetime(
                    key=_EMITTED_TWEET_VOLUME_SAMPLE_TRACKER_KEY, aggSec=options.aggPeriod
                )
                aggOffset = (
                    math.ceil(
                        (epochFromNaiveUTCDatetime(lastEmittedAggTime) - epochFromNaiveUTCDatetime(oldestRecordTs))
                        / options.aggPeriod
                    )
                    * options.aggPeriod
                )
                aggStartDatetime = (
                    lastEmittedAggTime - timedelta(seconds=aggOffset) - timedelta(seconds=options.aggPeriod)
                )

                metric_utils.updateLastEmittedSampleDatetime(
                    key=_EMITTED_TWEET_VOLUME_SAMPLE_TRACKER_KEY, sampleDatetime=aggStartDatetime
                )

                MetricDataForwarder.runInThread(
                    metricSpecs=loadMetricSpecs(),
                    aggSec=options.aggPeriod,
                    symbolList=[options.new_symbol],
                    forwardOnlyBacklog=True,
                )

                metric_utils.updateLastEmittedSampleDatetime(
                    key=_EMITTED_TWEET_VOLUME_SAMPLE_TRACKER_KEY, sampleDatetime=lastEmittedAggTime
                )

        g_log.info("Forwarding metrics to dynamodb using new symbol...")
        if options.twitter:
            migrate_tweets_to_dynamodb.main(symbolList=[options.new_symbol])

        g_log.info("Unmonitoring and deleting existing metrics associated with " "symbol=%s", options.old_symbol)
        oldModels = metric_utils.getSymbolModels(options.htmServer, options.apikey, options.old_symbol)
        for model in oldModels:
            metric_utils.unmonitorMetric(options.htmServer, options.apikey, model.uid)
            metric_utils.deleteMetric(options.htmServer, options.apikey, model.name)