欢迎您光临本小站。希望您在这里可以找到自己想要的信息。。。

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

架构&设计模式 water 4984℃ 0评论

这里再配合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.rpmyum 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

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

[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/helloRunning 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.71Transfer/sec:      2.04MB

于此同时Grafana界面上系统实时的数据信息也展现出来了:


实时监控画面

转载请注明:学时网 » 利用Metrics+influxdb+grafana构建监控平台

喜欢 (1)or分享 (0)

您必须 登录 才能发表评论!