利用Metrics+influxdb+grafana构建监控平台

上一篇利用Metrics搭建了一个简易的JVM实时监控工具。

这里再配合InfluxdbGrafana可以构建一个非常漂亮的实时监控界面。


Grafana监控界面

采集数据(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

425 17:50:04 metrics systemd[1]: Started InfluxDB is an open-source, distributed...se.
425 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

完成安装之后,启动并检查它的状态:

[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
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值