Prometheus和Grafana组合基本上是监控系统的标配。Prometheus作存储后端,Grafana作分析及可视化界面。 前端
普罗米修斯是开源的系统监控/报警工具库,功能很是全,且拥有活跃的开发者和用户社区。Prometheus经过HTTP按期主动拉取(Pull)的方式得到指标(直接获取或经过gateway推送),在本地存储全部抓取的样本,并对这些数据运行规则,从现有数据聚合和记录新的时间序列,或生成警报。 Prometheus原生的可视化界面作得比较原始(主要用于调试),因此社区(官方推荐)使用Grafana来作数据展现。 Grafana专一于数据展现,有着丰富用成熟的展现方式和插件,数据源支持Elasticsearch, Prometheus, Graphite, InfluxDB等等。可让你经过界面点击(无需写前端代码)快速搭建一个很是专业漂亮的展现界面。即使对于前端零基础的开发者也很是友好!node
在官网下载须要的版本(uname -rv
查看linux内核版本及发行号)。好比x86的就下载linux-386系列。linux
Prometheus会主动经过HTTP请求来收集受监控目标的指标。 好比它也经过HTTP Rest API导出了本身的健康状态数据,因此也能够用来监控本身。解压下载源文件内包含一个基本的prometheus.yml
配置能够参照。配置很是简单。git
global: # 全局配置 scrape_interval: 15s #主动拉取指标的间隔 evaluation_interval: 15s #计算间隔 scrape_configs: #监控的目标配置 - job_name: prometheus #名称 static_configs: # 静态配置 - targets: ['127.0.0.1:9090'] #监控目标暴露的HTTP API端口,是个列表
前台启动Prometheus,若是是在生产环境,须要后台启动时,最好自行配置Systemd。github
# Start Prometheus. # By default, Prometheus stores its database in ./data (flag --storage.tsdb.path). ./prometheus --config.file=prometheus.yml
用浏览器打开http://IP:9090/metrics查询全部指标列表。shell
用浏览器打开http://IP:9090/graph,原生的简易的展现界面(太简陋了,基本没人会用)。 PS:由于Promethues本身导出的指标和展现界面都是同一个9090端口。但实践中metrics接口指向的是目标机器的指标列表,用于Promethues主动拉取。数据库
丰富的Exporter能够下载使用,开箱即用。下面能够用NodeExporter来作个范例。json
NodeExporter暴露不少和硬件/软件相关的指标(metrics)。后端
$ tar xvfz node_exporter-* $ cd node_exporter-*
启动NodeExporter。浏览器
$ ./node_exporter INFO[0000] Starting node_exporter (version=0.18.1, branch=HEAD, revision=3db77732e925c08f675d7404a8c46466b2ece83e) source="node_exporter.go:156" INFO[0000] Build context (go=go1.12.5, user=root@b50852a1acba, date=20190604-16:41:43) source="node_exporter.go:157" INFO[0000] Enabled collectors: source="node_exporter.go:97" INFO[0000] - arp source="node_exporter.go:104" ... INFO[0000] Listening on :9100 source="node_exporter.go:170"
能够使用./node_exporter -h
查看具体的启动参数。从上面能够看它使用的端口是9100,全部的指标列表均可以和上面示例中的prometheus的接口同样:
$ curl http://localhost:9100/metrics # HELP go_gc_duration_seconds A summary of the GC invocation durations. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 2.8138e-05 go_gc_duration_seconds{quantile="0.25"} 4.1588e-05 go_gc_duration_seconds{quantile="0.5"} 0.000102923 go_gc_duration_seconds{quantile="0.75"} 0.000162106 go_gc_duration_seconds{quantile="1"} 0.000495923 go_gc_duration_seconds_sum 0.060153937 go_gc_duration_seconds_count 537 # HELP go_goroutines Number of goroutines that currently exist. ...
能够看到全部关于node_exporter的指标列表。接下来就是把让prometheus来取这些指标。
配置prometheus拉node exporter的指标。即把targets增长9100端口。
scrape_configs: - job_name: 'node' static_configs: - targets: ['127.0.0.1:9100']
重启prometheus。
./prometheus --config.file=./prometheus.yml
再次查看浏览器打开graph查看:http://127.0.0.1:9090/graph。勾选Enable query history
后直接输入以node就能够看到大量关于node为前缀的指标了。好比:node_filesystem_avail_bytes
查看文件系统可用空间大小状况。
这个界面仍是太原始了,但能够用来体验一下PromQL。接下来演示一下接入grafana来展现这些数据。
按官方指引下载安装:好比Centos安装是
$ wget https://dl.grafana.com/oss/release/grafana-6.3.3-1.x86_64.rpm $ sudo yum localinstall grafana-6.3.3-1.x86_64.rpm
你能够在/etc/grafana/grafana.ini
中配置端口及其它的,具体全部的配置项在: https://grafana.com/docs/installation/configuration/,咱们这里都保持默认值,端口默认为3000.用户名/密码默认为admin。 你能够在这里找到对应的启动方式,好比在CentOS上就是
sudo service grafana-server start
启动成功后,你能够使用浏览器打开http://IP:3000使用admin/admin登陆。
Grafana.com上有不少别人分享的优化的dashboards,咱们能够直接从上面找到node exporter对应的dashboard来使用。下载对应的json文件,而后导入。
在Grafana上如何为选择合适的图表来展现Prometheus对应的数据类型(单调递增的Counter,可降可升的Gauge,用于柱状图展现的Histogram),提供滑动窗口求和的Summary。