利用Metrics+influxdb+grafana構建監控平臺

上一篇利用Metrics搭建了一個簡易的JVM實時監控工具。git

這裏再配合InfluxdbGrafana能夠構建一個很是漂亮的實時監控界面。github

Grafana監控界面web

採集數據(Metrics)-> 存儲數據(InfluxDB) -> 顯示數據(Grafana)。數據庫

資源有限,我把這一套服務搭建在了一臺CentOS 7的服務器上。瀏覽器

安裝influxdb

InfluxDB是Go語言寫的一個時序型數據庫,能夠經過rpm的方式來安裝它,這裏最好能安裝InfluxDB的最新版本,不然跟Grafana的交互會有一些不兼容。服務器

wget http://dl.influxdata.com/influxdb/releases/influxdb-0.12.2-1.x86_64.rpm
yum localinstall influxdb-0.12.2-1.x86_64.rpm

安裝完成以後,啓動並查看這個服務是否在正常運行:併發

systemctl start influxdb.service
[root@metrics ~]# systemctl status influxdb.service
● influxdb.service - InfluxDB is an open-source, distributed, time series database
   Loaded: loaded (/usr/lib/systemd/system/influxdb.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2016-04-25 17:50:04 CST; 1 day 1h ago
     Docs: https://docs.influxdata.com/influxdb/
 Main PID: 17871 (sh)
   CGroup: /system.slice/influxdb.service
           ├─17871 /bin/sh -c /usr/bin/influxd -config /etc/influxdb/influxdb.conf  >>/dev/nul...
           └─17872 /usr/bin/influxd -config /etc/influxdb/influxdb.conf

4月 25 17:50:04 metrics systemd[1]: Started InfluxDB is an open-source, distributed...se.
4月 25 17:50:04 metrics systemd[1]: Starting InfluxDB is an open-source, distribute......
Hint: Some lines were ellipsized, use -l to show in full.

啓動後打開 web 管理界面 http://192.168.2.183:8083/ 默認用戶名和密碼是 root 和 root. InfluxDB 的 Web 管理界面端口是 8083,HTTP API 監聽端口是 8086,若是須要更改這些默認設定,修改 InfluxDB 的配置文件(/etc/influxdb/influxdb.conf)並重啓就能夠了。工具

安裝 Grafana

Grafana是一個很是好看的監控界面,它的安裝方式也很是簡單,經過yum,一條命令就能夠在server上完成安裝。測試

yum install https://grafanarel.s3.amazonaws.com/builds/grafana-3.0.0-beta51460725904.x86_64.rpm

完成安裝以後,啓動並檢查它的狀態:ui

[root@metrics ~]# systemctl start grafana-server.service
[root@metrics ~]# systemctl status grafana-server.service
● grafana-server.service - Starts and stops a single grafana instance on this system
   Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2016-04-25 09:37:07 CST; 1 day 10h ago
     Docs: http://docs.grafana.org
 Main PID: 10309 (grafana-server)
   CGroup: /system.slice/grafana-server.service
           └─10309 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile= cfg:d...

用瀏覽器訪問 Grafana,默認端口是3000,默認的賬號密碼都是 admin(能夠在配置文件中找到),登陸以後配置數據庫

 

設置數據源

Metrics Reporter

以前提到Metrics只須要配置它的Reporter就能夠輸出到對應的地方,對於Influxdb,在github上找到了一個InfluxdbReporter(https://github.com/davidB/metrics-influxdb)。

Maven中引入:

<dependency>
    <groupId>com.github.davidb</groupId>
    <artifactId>metrics-influxdb</artifactId>
    <version>0.8.2</version>
</dependency>

並在代碼中配置Influxdb的數據源:

@Bean(name = "influxdbReporter")
public ScheduledReporter influxdbReporter(MetricRegistry metrics) throws Exception {

    return InfluxdbReporter.forRegistry(metrics)
            .protocol(InfluxdbProtocols.http("host_ip_address", port, "username", "password", "database"))
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .filter(MetricFilter.ALL)
            .skipIdleMetrics(false)
            .build();
}

啓動項目以後,咱們開始配置咱們須要看到的圖表信息:

 

配置圖標信息

最後咱們利用wrk測試工具併發訪問鏈接:

➜  ~ wrk -t 10 -c 50 -d 5s http://127.0.0.1:8888/hello
Running 5s test @ http://127.0.0.1:8888/hello
  10 threads and 50 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     6.59ms   12.17ms 210.05ms   92.16%
    Req/Sec     1.40k   427.51     2.79k    71.80%
  69902 requests in 5.04s, 10.28MB read
Requests/sec:  13873.71
Transfer/sec:      2.04MB

於此同時Grafana界面上系統實時的數據信息也展示出來了:

 

實時監控畫面

做者:whthomas 連接:http://www.jianshu.com/p/fadcf4d92b0e 來源:簡書 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。

相關文章
相關標籤/搜索