데이터 시각화를 위한 Grafana 관련 자료

2018. 10. 22. 18:30서버 프로그래밍

IOT 데이터를 시각화하는데 적합한 오픈소스 시각화 도구인 "그라파나"를 사용하고자 검토를 하고 있다.

https://grafana.com/


47종의 데이터 소스를 연동해서 시각화를 할 수 있는데, MongoDB를 지원하지 않아서 충격을 받았다. 지금껏 IOT 데이터베이스로 적합한 데이터베이스 중 하나가 MongoDB라고 굳게 믿고 사용해왔는데, Time Series Analytics(시계열 분석)에 적합하지 않아서인지 모르겠지만 그라파나에서 지원을 하지 않는다는 것이 납득이 되지 않는다.


대신, 시계열 데이터(Time Series) 데이터베이스인 InfuxDB를 그라파나와 많이 연동하는 것 같다.

http://xcv.kr/blog/post/2018/2018-04-22-influxdb/

https://yenaworldblog.wordpress.com/2017/07/28/influxdb/

https://github.com/influxdata/influxdb-python

https://okky.kr/article/322237


InfluxDB 와 Grafana 를 이용한 빅데이터 가시화

http://hamait.tistory.com/537


막상 설치를 해서 사용해보려니 해당 블로그에 정리되어 있는 대로 설치 및 실행이 안된다. 아마도 무료로 사용할 수 있다는 0.11 버전이 아닌 최신 버전을 설치해서인듯하다. 아래 내용대로 0.11 버전을 다운받아서 설치를 하고 실행하니 그냥 실행된다.

https://github.com/influxdata/influxdb/issues/6373

For the sake of completeness. The process was ridiculously simple once I figured it out. The documentation doesn't show much or explain it very well.

wget https://s3.amazonaws.com/influxdb/influxdb_0.11.1-1_amd64.deb
dpkg -i ./influxdb_0.11.1-1_amd64.deb

Next, start influx v0.11


service influxdb start

그라파나 역시 블로그에 나온대로 하니 잘 안되어 그냥 다음과 같이 설치하고 실행했다.

http://docs.grafana.org/installation/debian/

APT Repository

Add the following line to your /etc/apt/sources.list file.

deb https://packagecloud.io/grafana/stable/debian/ stretch main

Use the above line even if you are on Ubuntu or another Debian version. There is also a testing repository if you want beta or release candidates.

deb https://packagecloud.io/grafana/testing/debian/ stretch main

Then add the Package Cloud key. This allows you to install signed packages.

curl https://packagecloud.io/gpg.key | sudo apt-key add -

Update your Apt repositories and install Grafana

sudo apt-get update
sudo apt-get install grafana

On some older versions of Ubuntu and Debian you may need to install the apt-transport-httpspackage which is needed to fetch packages over HTTPS.

sudo apt-get install -y apt-transport-https

Package details

  • Installs binary to /usr/sbin/grafana-server
  • Installs Init.d script to /etc/init.d/grafana-server
  • Creates default file (environment vars) to /etc/default/grafana-server
  • Installs configuration file to /etc/grafana/grafana.ini
  • Installs systemd service (if systemd is available) name grafana-server.service
  • The default configuration sets the log file at /var/log/grafana/grafana.log
  • The default configuration specifies an sqlite3 db at /var/lib/grafana/grafana.db
  • Installs HTML/JS/CSS and other Grafana files at /usr/share/grafana

Start the server (init.d service)

Start Grafana by running:

sudo service grafana-server start

This will start the grafana-server process as the grafana user, which was created during the package installation. The default HTTP port is 3000 and default user and group is admin.

Default login and password adminadmin

To configure the Grafana server to start at boot time:

sudo update-rc.d grafana-server defaults


그라파나를 디폴트 포트인 3000번이 아니라 80번으로 실행하려니 퍼미션 에러가 뜬다.

https://stackoverflow.com/questions/413807/is-there-a-way-for-non-root-processes-to-bind-to-privileged-ports-on-linux/414258#414258

Okay, thanks to the people who pointed out the capabilities system and CAP_NET_BIND_SERVICE capability. If you have a recent kernel, it is indeed possible to use this to start a service as non-root but bind low ports. The short answer is that you do:

setcap 'cap_net_bind_service=+ep' /path/to/program

And then anytime program is executed thereafter it will have the CAP_NET_BIND_SERVICE capability. setcap is in the debian package libcap2-bin.

Now for the caveats:


InfluxDB-Python을 이용하여 python 프로그램에서 데이터를 influxDB에 저장하려는데, 저장이 안되는 문제가 발생했다. 첫번째 이유는 fileds 키에 실제 데이터를 넣어서 저장하려고 하는데, value가 문자열, 숫자, 불린값은 문제 없이 저장되지만 배열인 경우에 저장이 안되고 Exception이 발생한다. InfluxDB는 MongoDB처럼 NoSQL보다는 RDBMS에 유사한 특징을 가지고 있는 듯하다. 배열도 저장 못하면서 schemeless 하다고는 할 수 없지 않나? 불현듯 Google BigQuery가 생각난다. 

https://github.com/influxdata/influxdb-java/issues/39


배열을 value로 가지고 있는 엘리먼트들을 모두 제거하고 저장하니 무사히 저장 완료.

https://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary

key_to_remove = "c"
d = {"a": 1, "b": 2}
try:
    del d[key_to_remove]
except KeyError as ex:
    print("No such key: '%s'" % ex.message)


두번째는 json_body에 time을 클라이언트의 현재 시간을 넣어서 보내면, 서버 시간과 다를 경우 저장이 안되는 것 같다. 그래서 time key 값를 제외하고 저장했더니 문제 없이 저장이 된다. 허허허

https://github.com/influxdata/influxdb-python

$ python

>>> from influxdb import InfluxDBClient

>>> json_body = [
    {
        "measurement": "cpu_load_short",
        "tags": {
            "host": "server01",
            "region": "us-west"
        },
        "time": "2009-11-10T23:00:00Z",
        "fields": {
            "value": 0.64
        }
    }
]

>>> client = InfluxDBClient('localhost', 8086, 'root', 'root', 'example')

>>> client.create_database('example')

>>> client.write_points(json_body)


--------------------------------------------


몽고디비와 같은 Schemeless한 NoSQL이라 벤치마킹 데이터도 있는데, 몽고디비보다 퍼포먼스가 좋다는 결과도 있다.

InfluxDB is 27x Faster vs. MongoDB for Time Series Workloads

https://www.influxdata.com/blog/influxdb-is-27x-faster-vs-mongodb-for-time-series-workloads/

올해 연초에 몽고디비를 극한까지 밀어붙여서 테스트를 해보면서 몽고디비의 성능에 다소 회의감을 가지게 되었었는데, 똑같이 사용이 쉬우면서 그보다 빠른 NoSQL이라고 하니 약간의 기대감을 가지게 된다.

물론, 몽고디비는 그 나름대로의 사용 용도가 있기 때문에 지금도 사용하고 있고 앞으로도 계속 사용하겠지만 데이터 분석과 시각화를 위해서는 대안이 필요하다고 생각한다.


같이 일하는 동료가 InfluxDB 이외도 OpenTSDB를 추천해주기도 했는데, 이것은 개인적으로 그다지 좋아하지 않은 HBase를 베이스로 만들어진 것 같다.

http://hamait.tistory.com/334


AWS CloudWatch를 이용하여, AWS의 리소스를 데이터 소스로 사용할 수 있는 방법도 있는 것 같다.

http://docs.grafana.org/features/datasources/cloudwatch/