Skip to content

Instantly share code, notes, and snippets.

@ntk148v
Last active May 20, 2016 04:53
Show Gist options
  • Save ntk148v/0cfcee28d72dea19f1bf71dcf7d18348 to your computer and use it in GitHub Desktop.
Save ntk148v/0cfcee28d72dea19f1bf71dcf7d18348 to your computer and use it in GitHub Desktop.
#InfluxDB #Kapacitor

BÁO CÁO

Tổ chức Data Model.

Measurement Tag1 Tag2 Field1 Field2 Field3 Time
download_speed snode dnode speed mean_deviation acceleration
tcp_three_way_handshake_time snode dnode value
time_to_first_byte snode dnode value
round_trip_time snode dnode value

Chú thích:

  - Measurements:
    + download_speed: tốc độ download các file.
    + tcp_three_way_handshake_time: 
    + time_to_first_byte: 
    + round_trip_time: khoảng thời gian tính từ lúc client bắt đầu gửi request tới lúc nó nhận gói dữ liệu đầu tiên trả về không bao gồm thời gian nhận đầy đủ dữ liệu.
  - Tags:
    + snode: địa chỉ ip source node.
    + snode: địa chỉ ip destination node.
    + region: khu vực địa lý của các node.
  - Fields:
    + mean_deviation: độ biến động của tốc độ download.
    + acceleration: gia tốc của tốc độ download.
    + speed: gía trị tốc độ download.
    + value: giá trị thông số measurement.
  - Time: thời điểm ghi thông tin measuremen (nanosecond).

1. Cài đặt.

$ sudo apt-get install python3-pip
$ sudo pip install influxdb

2. Sử dụng.(Đọc docs)

3. Form result gửi đến InfluxDB - Ví dụ

results = [
  {
      "measurement": "download_speed",
      "tags": {
          "snode": "<source_node_ip>",
          "dnode": "<destination_node_ip>"
      },
      "time": "2009-11-10T23:00:00Z",
      "fields": {
          "speed": <speed_value>,
          "mean_deviation": <mean_deviation_value>,
          "acceleration": <acceleration_value>
      }
  }
]

1. Aggregations.

  • Count(): Trả về số lượng giá trị non-null của 1 field.

SELECT COUNT(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Distinct(): Trả về những giá trị duy nhất của 1 field.

SELECT DISTINCT(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Mean(): Trả về trung bình cộng của những giá trị trong 1 field.

SELECT MEAN(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Median(): Trả về giá trị ở giữa (middle value) trong các giá trị của 1 field.

SELECT MEDIAN(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Spread(): Trả về độ chênh giữa gía trị nhỏ và lớn nhất của 1 field.

SELECT SPREAD(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Sum(): Tổng các giá trị 1 field.

SELECT SUM(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

2. Selectors.

  • Bottom(): Trả về N giá trị nhỏ nhất của 1 field.

SELECT BOTTOM(<field_key>[,<tag_keys>],)[,<tag_keys>] FROM <measurement_name> [WHERE ] [GROUP BY ]

  • First(): Trả về giá trị 'cũ' nhất 1 field.

SELECT FIRST(<field_key>)[,<tag_key(s)>] FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Last(): Ngược lại với First().

SELECT LAST(<field_key>)[,<tag_key(s)>] FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Max(): Trả về giá trị lớn nhất trong 1 field.

SELECT MAX(<field_key>)[,<tag_key(s)>] FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Min(): Ngược lại với Max().

SELECT MIN(<field_key>)[,<tag_key(s)>] FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Percentile(): Trả về giá trị phần trăm thứ N cho các giá trị trong 1 field.

SELECT PERCENTILE(<field_key>, )[,<tag_key(s)>] FROM <measurement_name> [WHERE ] [GROUP BY ]

  • Top(): Trả về N giá trị lớn nhất của 1 field.

SELECT TOP(<field_key>[,<tag_keys>],)[,<tag_keys>] FROM <measurement_name> [WHERE ] [GROUP BY ]

3. Transformations.

  • Derivative(): Trả về tỉ lệ thay đổi của giá trị trong 1 field.

SELECT DERIVATIVE(<field_key>, []) FROM <measurement_name> [WHERE ]

  • Difference(): Trả về sự thay đổi giữa các giá trị thời gian liên tiếp 1 field.

SELECT DIFFERENCE(<field_key>) FROM <measurement_name> [WHERE ]

  • Moving_average():

SELECT MOVING_AVERAGE(<field_key>,) FROM <measurement_name> [WHERE ]

  • Elapsed():

SELECT ELAPSED(<field_key>, ) FROM <measurement_name> [WHERE ]

  • Non_negative_deriavite():

SELECT NON_NEGATIVE_DERIVATIVE(<field_key>, []) FROM <measurement_name> [WHERE ]

  • Stddev():

SELECT STDDEV(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

Các rule trong Kapacitor.

  • Kapacitor sử dụng file TICKScripts để định nghĩa các task, các task cho biết measurement sẽ xử lý và cách thức xử lý.

  • File TICKScripts cơ bản để xử lý(sample_alert.tick), trong đó, data sẽ được lấy ra từ sample_measurement.

    stream
    |from()
        .measurement('sample_measurement')
    |window()
        .period(1m)
        .every(1m)
    |alert()
        .id('{{ .Name }}/{{ index .Tags "snode" }}-{{ index .Tags "dnode"}}')
        .message('[{{ .Time }}] - {{ .ID }} - {{ .Level }} msg: {{ index .Tags "snode" }} to {{ index .Tags "dnode" }} - {{  index .Fields "value"}}')
        // Compare values to running mean and standard deviation
        .ok(lambda: "value" > minimum_ok_threshold and "value" < maximum_ok_threshold)
        .info(lambda: "value" > minimum_info_threshold and "value" < maximum_info_threshold)
        .warn(lambda: "value" > minimum_warn_threshold and "value" < maximum_warn_threshold)
        .crit(lambda: "value" > minimum_crit_threshold and "value" < maximum_crit_threshold)
        .log('/tmp/alerts.log')
    
        // Post data to custom endpoint
        .post('https://endpoint_to_present_to_dashboard')
    
        // Execute custom alert handler script
        .exec('/bin/custom_alert_handler.sh')
    
        // Send alerts to slack
        .slack()
        .channel('#alerts')
        
        // Send alerts to email
        .email().to('oncall@example.com')
    
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment