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

次のページを参考にしました。
[Elasticsearch(7.9.1)にデータ投入](https://qiita.com/hayapo/items/0da64f7c71b9c7e9636c)

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

##稼働の確認##

Kibana

```text
$ 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

```text
$ 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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/d2a5be6a-3d8e-a15b-1c91-7cced8e90d89.png)

Elasticsearch のバージョン確認

```text
$ 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"
}

```

##設定ファイル

```text:/etc/kibana/kibana.yml
server.port: 5601
server.host: "localhost"
```

```text:/etc/elasticsearch/jvm.options
-Xms512m
-Xmx512m
```

##認証の追加##
こちらのページを参考にしました。
[セキュリティ機能のはじめ方](https://www.elastic.co/jp/blog/getting-started-with-security)

Elasticsearch と Kibana を停止します。

設定ファイルの変更

```text:/etc/elasticsearch/elasticsearh.yml
（省略）
xpack.security.enabled: true
```

Elasticsearch を起動します。

パスワードの生成

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

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

```text:/etc/kibana/kibana.yml
（省略）
elasticsearch.username: "elastic"
elasticsearch.password: "abcdeZyhkZgU29e0r4fp"
```

Kibana を起動します。

確認

```text
$ 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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/3dce0350-7ae7-070e-1ee7-85c64b8b4f80.png)


##データの投入##

index　の作成

```bash: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 の確認

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

データの投入

```py: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)
#
# -------------------------------------------------------------------------
```

データ数のカウント

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

データの取得

```bash
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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/6347af34-82b8-28da-b596-4299b44eae37.png)


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

>http://localhost:5601
>GET temperature_humidity/_search
![kibana_aa.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/43f62753-a25e-59f9-5066-459fa32923ad.png)


Kibana の Analytics -> Discover でデータを確認
![kibana_bb.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/f307e9de-0956-611f-f565-11504249838d.png)



Index の確認
>Management -> Stack Management -> Index Management
![index_oct12.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/c83e90ba-25a4-2959-ad0f-f36ca3ad4738.png)

##グラフの作成

Dashboard でグラフの作成

![dashboard_aa.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/a6c9251c-3efd-8b1c-fa8d-2a72a2c78fc8.png)

![dashboard_bb.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/9bfb4fe0-6426-99bb-fa4b-f074f7344fab.png)