1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Kibana でデータ投入からグラフの作成まで

Last updated at Posted at 2021-10-12

次のページを参考にしました。
Elasticsearch(7.9.1)にデータ投入

Kibana と Elasticsearch がインストールされ、動いているものとします。

稼働の確認##

Kibana

$ sudo systemctl status kibana
● kibana.service - Kibana
     Loaded: loaded (/etc/systemd/system/kibana.service; disabled; vendor prese>
     Active: active (running) since Tue 2021-10-12 07:29:49 JST; 4h 30min ago

Elasticsearch

$ sudo systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
     Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendo>
     Active: active (running) since Tue 2021-10-12 07:29:02 JST; 4h 34min ago

http://localhost:5601 にアクセス

kibana_oct12.png

Elasticsearch のバージョン確認

$ curl localhost:9200
{
  "name" : "iwata",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "R92Wu-uxTl2gad2cs6OkIg",
  "version" : {
    "number" : "7.15.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "79d65f6e357953a5b3cbcc5e2c7c21073d89aa29",
    "build_date" : "2021-09-16T03:05:29.143308416Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

設定ファイル

/etc/kibana/kibana.yml
server.port: 5601
server.host: "localhost"
/etc/elasticsearch/jvm.options
-Xms512m
-Xmx512m

認証の追加##

こちらのページを参考にしました。
セキュリティ機能のはじめ方

Elasticsearch と Kibana を停止します。

設定ファイルの変更

/etc/elasticsearch/elasticsearh.yml
(省略)
xpack.security.enabled: true

Elasticsearch を起動します。

パスワードの生成

sudo /usr/share/elasticsearch/bin/elasticsearch-setup-passwords auto

生成されたパスワードが出てくるので、メモをします。

/etc/kibana/kibana.yml
(省略)
elasticsearch.username: "elastic"
elasticsearch.password: "abcdeZyhkZgU29e0r4fp"

Kibana を起動します。

確認

$ curl -u elastic:abcdeZyhkZgU29e0r4fp http://localhost:9200/_cat/nodes?v
ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role   master name
127.0.0.1           67          95  15    0.54    1.47     2.17 cdfhilmrstw *      iwata

http://localhost:5601 にアクセス

kibana_login.png

データの投入##

index の作成

create_index.sh
curl -H "Content-Type: application/json" -X PUT \
    -u elastic:abcdeZyhkZgU29e0r4fp \
	"http://127.0.0.1:9200/temperature_humidity?pretty" -d '
     {
       "mappings" : {
         "properties" : {
           "temperature" : {"type" : "double"},
           "humidity" : {"type" : "double"},
           "timestamp" : {"type" : "date"}
         }
       }
     }'

index の確認

get_index.sh
curl -H "Content-Type: application/json" -X GET \
    -u elastic:abcdeZyhkZgU29e0r4fp \
	"http://127.0.0.1:9200/temperature_humidity?pretty"

データの投入

insert_elastic_auth.py
# ! /usr/bin/python
#
#	insert_elastic_auth.py
#
#						Oct/14/2021
# -------------------------------------------------------------------------
import datetime
import time
import math
import sys
import json 
import requests
from requests.auth import HTTPBasicAuth
# -------------------------------------------------------------------------
url_base = "http://localhost:9200/temperature_humidity/_doc"
#
# icount = 0
# while True:
for icount in range(60):
	key = str(icount)
	url = url_base + "/" + key
	sys.stderr.write("icount = %d\n" % icount)
	now = datetime.datetime.now()
	ttx = int(now.timestamp())
	print(ttx)

	deg = (icount % 36) * 10
	rad = math.pi * deg / 180.0
	diff_temp = 5.0 * math.sin(rad)
	temperature = 25.0 + diff_temp
	diff_humi = 20.0 * math.sin(rad)
	humidity =  70.0 + diff_humi
	es_body = {
		'timestamp' : ttx,
		'temperture' : temperature,
		'humidity' : humidity,
		'icount' : icount,
		}

	json_str = json.dumps(es_body)
	print(json_str)
#
	headers = {'content-type': 'application/json'}
	rr=requests.put(url,data=json_str,headers=headers,auth=HTTPBasicAuth('elastic', 'abcdeZyhkZgU29e0r4fp'))
	print(rr)
#
	time.sleep(1)
	print(es_body)
#
# -------------------------------------------------------------------------

データ数のカウント

$ curl -u elastic:abcdeZyhkZgU29e0r4fp \
'localhost:9200/temperature_humidity/_count?q=*'
{"count":17,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0}}

データの取得

curl -X GET -u elastic:abcdeZyhkZgU29e0r4fp \
http://localhost:9200/temperature_humidity/_search | jq .hits.hits

ブラウザーでデータを確認
http://localhost:9200/temperature_humidity/_search?pretty
elastic_aa.png

Kibana の Management -> Dev Tools でデータを確認

http://localhost:5601
GET temperature_humidity/_search
kibana_aa.png

Kibana の Analytics -> Discover でデータを確認
kibana_bb.png

Index の確認

Management -> Stack Management -> Index Management
index_oct12.png

グラフの作成

Dashboard でグラフの作成

dashboard_aa.png

dashboard_bb.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?