Skip to content

Latest commit

 

History

History

QueryCpu

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

QueryCpu

This is an example how to query data into sequence of FluxRecord. The Telegraf sends data from CPU Input Plugin into InfluxDB 2.x.

Prerequisites:

  • Docker
  • Cloned examples:
    git clone [email protected]:influxdata/influxdb-client-swift.git
    cd Examples/QueryCpu

Sources:

How to test:

  1. Start InfluxDB:
    docker run --rm \
      --name influxdb_v2 \
      --detach \
      --publish 8086:8086 \
      influxdb:latest
  2. Configure your username, password, organization, bucket and token:
    docker run --rm \
       --link influxdb_v2 \
       curlimages/curl -s -i -X POST http://influxdb_v2:8086/api/v2/setup \
          -H 'accept: application/json' \
          -d '{"username": "my-user", "password": "my-password", "org": "my-org", "bucket": "my-bucket", "token": "my-token"}'
  3. Start Telegraf as a source of data:
    docker run --rm \
       --name telegraf \
       --link influxdb_v2 \
       --detach \
       --env HOST_ETC=/hostfs/etc \
       --env HOST_PROC=/hostfs/proc \
       --env HOST_SYS=/hostfs/sys \
       --env HOST_VAR=/hostfs/var \
       --env HOST_RUN=/hostfs/run \
       --env HOST_MOUNT_PREFIX=/hostfs \
       --volume /:/hostfs:ro \
       --volume $PWD/telegraf.conf:/etc/telegraf/telegraf.conf:ro \
       telegraf
  4. Start SwiftCLI by:
     docker run --rm \
       --link influxdb_v2 \
       --privileged \
       --interactive \
       --tty \
       --volume $PWD/../..:/client \
       --workdir /client/Examples/QueryCpu \
       swift:5.7 /bin/bash
  5. Execute Query by:
    swift run query-cpu --org my-org --bucket my-bucket --token my-token --url http://influxdb_v2:8086

Expected output

Query to execute:

from(bucket: "my-bucket")
    |> range(start: -10m)
    |> filter(fn: (r) => r["_measurement"] == "cpu")
    |> filter(fn: (r) => r["cpu"] == "cpu-total")
    |> filter(fn: (r) => r["_field"] == "usage_user" or r["_field"] == "usage_system")
    |> last()

Success response...

CPU usage:
        usage_system: 22.717622080683473
        usage_user: 61.46496815287725