快捷搜索:  汽车  科技

prometheus 监控磁盘容量:深入浅出监控神器Prometheus

prometheus 监控磁盘容量:深入浅出监控神器Prometheusfunc (h *Handler) reload(w http.ResponseWriter r *http.Request) { rc := make(chan error) h.reloadCh <- rc // 发送一个信号到channe了中 if err := <-rc; err != nil { http.Error(w fmt.Sprintf("failed to reload config: %s" err) http.StatusInternalServerError) } }在main函数中会去监听这个channel,只要有监听到信号,就会做配置的reload,重新将新配置加载到内存中if o.EnableLifecycle { router.Post("/-/quit"

目前,Prometheus支持多达二十多种服务发现协议:

<azure_sd_config> <consul_sd_config> <digitalocean_sd_config> <docker_sd_config> <dockerswarm_sd_config> <dns_sd_config> <ec2_sd_config> <openstack_sd_config> <file_sd_config> <gce_sd_config> <hetzner_sd_config> <http_sd_config> <kubernetes_sd_config> <kuma_sd_config> <lightsail_sd_config> <linode_sd_config> <marathon_sd_config> <nerve_sd_config> <serverset_sd_config> <triton_sd_config> <eureka_sd_config> <scaleway_sd_config> <static_config>(二)配置更新

在更新完Prometheus的配置文件后,我们需要更新我们的配置到程序内存里,这里的更新方式有两种,第一种简单粗暴,就是重启Prometheus,第二种是动态更新的方式。如何实现动态的更新Prometheus配置。

第一步:首先要保证启动Prometheus的时候带上启动参数:--web.enable-lifecycle

prometheus --config.file=/usr/local/etc/prometheus.yml --web.enable-lifecycle

第二步:去更新我们的Prometheus配置:

curl -v --request POST 'http://localhost:9090/-/reload'

第三步:更新完配置后,我们可以通过Post请求的方式,动态更新配置:

原理:

Prometheus在web模块中,注册了一个handler:

if o.EnableLifecycle { router.Post("/-/quit" h.quit) router.Put("/-/quit" h.quit) router.Post("/-/reload" h.reload) // reload配置 router.Put("/-/reload" h.reload) }

通过h.reload这个handler方法实现:这个handler就是往一个channle中发送一个信号:

func (h *Handler) reload(w http.ResponseWriter r *http.Request) { rc := make(chan error) h.reloadCh <- rc // 发送一个信号到channe了中 if err := <-rc; err != nil { http.Error(w fmt.Sprintf("failed to reload config: %s" err) http.StatusInternalServerError) } }

在main函数中会去监听这个channel,只要有监听到信号,就会做配置的reload,重新将新配置加载到内存中

case rc := <-webHandler.Reload(): if err := reloadConfig(cfg.configFile cfg.enableExpandExternalLabels cfg.tsdb.EnableExemplarStorage logger noStepSubqueryInterval reloaders...); err != nil { level.Error(logger).Log("msg" "Error reloading config" "err" err) rc <- err } else { rc <- nil }(三)指标抓取和存储

Prometheus对指标的抓取采取主动Pull的方式,即周期性的请求被监控服务暴露的metrics接口或者是PushGateway,从而获取到Metrics指标,默认时间是15s抓取一次,配置项如下:

global: scrape_interval: 15s

抓取到的指标会被以时间序列的形式保存在内存中,并且定时刷到磁盘上,默认是两个小时回刷一次。并且为了防止Prometheus 发生崩溃或重启时能够恢复数据,Prometheus也提供了类似MySQL中binlog一样的预写日志,当Prometheus崩溃重启时,会读这个预写日志来恢复数据。

prometheus 监控磁盘容量:深入浅出监控神器Prometheus(1)

四、Metric指标

prometheus 监控磁盘容量:深入浅出监控神器Prometheus(2)

(一)数据模型

prometheus 监控磁盘容量:深入浅出监控神器Prometheus(3)

Prometheus采集的所有指标都是以时间序列的形式进行存储,每一个时间序列有三部分组成:

  • 指标名和指标标签集合:metric_name{<label1=v1> <label2=v2>....},指标名:表示这个指标是监控哪一方面的状态,比如http_request_total表示:请求数量;指标标签,描述这个指标有哪些维度,比如http_request_total这个指标,有请求状态码code= 200/400/500,请求方式:method=get/post等,实际上指标名称实际上是以标签的形式保存,这个标签是name,即:name=。
  • 时间戳:描述当前时间序列的时间,单位:毫秒。
  • 样本值:当前监控指标的具体数值,比如http_request_total的值就是请求数是多少。

可以通过查看Prometheus的metrics接口查看所有上报的指标:

prometheus 监控磁盘容量:深入浅出监控神器Prometheus(4)

所有的指标也都是通过如下所示的格式来标识的:

# HELP // HELP:这里描述的指标的信息,表示这个是一个什么指标,统计什么的 # TYPE // TYPE:这个指标是什么类型的 <metric name>{<label name>=<label value> ...} value // 指标的具体格式,<指标名>{标签集合} 指标值(二)指标类型

Prometheus底层存储上其实并没有对指标做类型的区分,都是以时间序列的形式存储,但是为了方便用户的使用和理解不同监控指标之间的差异,Prometheus定义了4种不同的指标类型:计数器counter,仪表盘gauge,直方图histogram,摘要summary。

prometheus 监控磁盘容量:深入浅出监控神器Prometheus(5)

猜您喜欢: