您的位置:首页 > 运维架构

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

2017-12-13 00:00 1371 查看
上一篇利用
Metrics
搭建了一个简易的JVM实时监控工具。

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



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

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
上找到了一个
Influxdb
Reporter
(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
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Grafana