def run(server, apiKey, metricName, resource, numRecords):
  grok = GrokSession(server=server, apikey=apiKey)

  inc = 300
  currentTimestamp = int(time.time()) - (numRecords * inc)
  with grok.connect() as sock:

    for i in xrange(numRecords):
      value = random.random()

      sock.sendall("%s %f %d\n" % (metricName, value, currentTimestamp))

      currentTimestamp += inc

      if i % 100 == 99:
        print ".",
        sys.stdout.flush()

      if i == RECORDS_BEFORE_MONITOR:
        print
        print "Creating model...",
        sys.stdout.flush()
        # Monitor the metric
        modelSpec = {"metric": metricName, "datasource": "custom"}
        if resource is not None:
          modelSpec["resource"] = resource
        model = grok.createModel(modelSpec)
        print "done"
Esempio n. 2
0
def run(server, apiKey, metricName, resource, numRecords):
    grok = GrokSession(server=server, apikey=apiKey)

    inc = 300
    currentTimestamp = int(time.time()) - (numRecords * inc)
    with grok.connect() as sock:

        for i in xrange(numRecords):
            value = random.random()

            sock.sendall("%s %f %d\n" % (metricName, value, currentTimestamp))

            currentTimestamp += inc

            if i % 100 == 99:
                print ".",
                sys.stdout.flush()

            if i == RECORDS_BEFORE_MONITOR:
                print
                print "Creating model...",
                sys.stdout.flush()
                # Monitor the metric
                modelSpec = {"metric": metricName, "datasource": "custom"}
                if resource is not None:
                    modelSpec["resource"] = resource
                model = grok.createModel(modelSpec)
                print "done"
Esempio n. 3
0
    #                                "uid": uid,
    #                                "resource": "Open File Descriptors",
    #                                "unit": "Files Open",
    #                              },
    #                              "modelParams": {
    #                                "min": 0,
    #                                "max": 20000,
    #                              }
    #                             })
    #
    models = grok.createModel({"datasource": "custom",
                               "metricSpec": {
                                 "uid": uid,
                                 "resource": "My Resource Name",
                                 "unit": "Files Open",
                               },
                               "modelParams": {
                                 "min": 0,
                                 "max": 20000,
                               }
                              })

    model = models[0]
    assert model["uid"] == uid
    assert model["name"] == METRIC_NAME

    # Get model status
    for _ in xrange(30):
      modelResponse = grok.get(grok.server + "/_models/" + uid, auth=grok.auth)
      models = modelResponse.json()
      model = models[0]
Esempio n. 4
0
    for metric in grok.listMetrics("custom"):
        if metric["name"] == METRIC_NAME:
            uid = metric["uid"]
            print 'Metric "%s" has uid: %s' % (METRIC_NAME, uid)
            break
    else:
        print (
            '"%s" metric does not exist (yet).  You can create the metric by'
            ' sending data to Grok.  See "sample-collect-data.py" for a'
            " simple script that you can use to periodically sample open"
            " file  descriptors, and report the results to the Grok Custom"
            " Metrics endpoint" % METRIC_NAME
        )

    # Send model creation request to create a model connected to the metric
    models = grok.createModel({"uid": uid, "datasource": "custom"})

    model = models[0]
    assert model["uid"] == uid
    assert model["name"] == METRIC_NAME

    # Get model status
    for _ in xrange(30):
        modelResponse = grok.get(grok.server + "/_models/" + uid, auth=grok.auth)
        models = modelResponse.json()
        model = models[0]
        if model["status"] == 1:
            break
        time.sleep(10)
    else:
        raise Exception('Model did not transition to "ready" status in a ' "reasonable amount of time.")
    #                                "uid": uid,
    #                                "resource": "Open File Descriptors",
    #                                "unit": "Files Open",
    #                              },
    #                              "modelParams": {
    #                                "min": 0,
    #                                "max": 20000,
    #                              }
    #                             })
    #
    models = grok.createModel({
        "datasource": "custom",
        "metricSpec": {
            "uid": uid,
            "resource": "My Resource Name",
            "unit": "Files Open",
        },
        "modelParams": {
            "min": 0,
            "max": 20000,
        }
    })

    model = models[0]
    assert model["uid"] == uid
    assert model["name"] == METRIC_NAME

    # Get model status
    for _ in xrange(30):
        modelResponse = grok.get(grok.server + "/_models/" + uid,
                                 auth=grok.auth)
        models = modelResponse.json()