prometheus

架构:

grafana1.png

组件:

  • Prometheus:Prometheus服务端,主要负责存储及收集数据,提供相关api对外查询用。

  • Alertmanager:报警组件,主要是负责实现报警功能。

  • Pushgateway:用于网络不可直达而居于exporter与server端的中转站,主要是实现接收由Client push过来的指标数据,在指定的时间间隔,由主程序来抓取。

  • exporter:类似传统意义上的被监控端的agent,有区别的是,它不会主动推送监控数据到server端,而是等待server端定时来手机数据,即所谓的主动监控。

  • Web UI: Prometheus的web接口,可用于简单可视化,及语句执行或者服务状态监控。

参考:https://prometheus.io/docs/introduction/overview/


开始之前:

1、关闭selinux:

[root@localhost ~]# vi /etc/selinux/config
将SELINUX=enforcing修改为SELINUX=disabled

2、校准时间:

设定为上海时区

[root@localhost log]# timedatectl set-timezone Asia/Shanghai

3、同步时间

[root@localhost log]# yum install ntpdate
[root@localhost log]# /usr/sbin/ntpdate time.nist.gov
[root@localhost log]# echo '* */1 * * * ntpdate time.nist.gov' >>/etc/crontab 
[root@localhost log]# hwclock --systohc    #系统时间同步到硬件,防止系统重启后时间呗还原


安装和配置:

1、下载安装包

wget https://github.com/prometheus/prometheus/releases/download/v2.5.0/prometheus-2.5.0.linux-amd64.tar.gz

2、解压

tar xvfz prometheus-*.tar.gz -C /opt/
cd /opt/prometheus-2.5.0.linux-amd64/

3、配置Prometheus监控自已

Prometheus 通过默认 http://localhost:9090/metrics HTTP接口暴露了自己的性能指标数据。

Prometheus 采集自身性能数据就是一个十分好的例子了,打开解压目录下面的prometheus.yml文件。

# cat prometheus.yml 

# 全局配置
global:
  scrape_interval:     15s    # 默认 15秒到目标处抓取数据
  scrape_timeout: 10s        # 抓取数据超时时间
  evaluation_interval: 15s    # 评估规则的频率 
  
  #与外部系统( 联合查询、远程存储,Alertmanager)通信时添加到任何时间序列或警报的标签。  
  external_labels:    
    monitor: 'codelab-monitor'
alerting:
  alertmanagers:
  - static_configs:
    - targets:
rule_files:
  # - "first.rules"
  # - "second.rules"
  
# 这里就表示抓取对象的配置
# 设置抓取自身数据
scrape_configs:
    #  job name 这个配置是表示在这个配置内的时间序例,每一条都会自动添加上这个{job_name:"prometheus"}的标签。
  - job_name: 'prometheus'
  
      # 重写了全局抓取间隔时间,由15秒重写成5秒。    
      scrape_interval: 5s
      
    static_configs:
    - targets: ['localhost:9090']

更多配置信息:https://prometheus.io/docs/prometheus/latest/configuration/configuration/

4、启动

 cd /opt/prometheus-2.5.0.linux-amd64/ &&nohup ./prometheus --config.file=prometheus.yml &

5、使用浏览器打开该机器的 http://部署机器:9090,或者 http://localhost:9090/ 即可以看到Prometheus的graph页面了。一般等几秒抓取,就会有数据写进Prometheus里面了。

如果相验证Prometheus自己输出了什么性能数据,可以打开 http://localhost:9090/metrics 这个页面看看。

Prometheus默认是有多少个CPU内核就使用多少OS线程,主要是由GOMAXPROCS 这个环境变量控制的,开发GO的应该都清楚。一般默认就好了,太高的话可能会带来意想不到的后果。
Prometheus默认大概会占用3G左右的内存,如果想调小一点,得修改配置文件,或者添加启动参数。


使用Prometheus 自带的表达式浏览器

试试在表达式 expression 输入框:

prometheus_target_interval_length_seconds

查询Promethues目标抓取间隔时间长度。执行后会返回很多条时间序列,Console 内列出的是最后一次抓到的数值。不同的标签表示不同的时间间隔和耗时%。

如果想查询99%的抓取耗时,可以这样:

prometheus_target_interval_length_seconds{quantile="0.99"}

如果想查询返回的时间序列有多少条,可以这样:

count(prometheus_target_interval_length_seconds)


grafana部署

安装和配置:

1、下载安装:

wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.3.4-1.x86_64.rpm
sudo yum localinstall grafana-5.3.4-1.x86_64.rpm
  • 安装二进制文件 /usr/sbin/grafana-server

  • 将init.d脚本复制到 /etc/init.d/grafana-server

  • 安装默认文件(环境变量) /etc/sysconfig/grafana-server

  • 将配置文件复制到 /etc/grafana/grafana.ini

  • 安装systemd服务(如果systemd可用)名称 grafana-server.service

  • 默认配置使用日志文件 /var/log/grafana/grafana.log

  • 默认配置指定sqlite3数据库 /var/lib/grafana/grafana.db

2、启动grafana,并设置开机启动:

sudo service grafana-server start
sudo /sbin/chkconfig --add grafana-server
sudo systemctl enable grafana-server.service
//或者
systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server
sudo systemctl enable grafana-server.service

3、安装服务端图像呈现组件:

yum install -y fontconfig freetype* urw-fonts

4、访问Grafana

打开浏览器并转到 http://localhost:3000 。如果您尚未配置其他端口,则3000是Grafana侦听的默认http 端口。然后按照此处的说明操作

默认登录用户名密码:admin/admin


配置邮件报警:

首先我们需要在机器上开启smtp/25服务,确保已安装sendmail。

1、修改配置文件:

# vim /etc/grafana/grafana.ini 

#################################### SMTP / Emailing ##########################
[smtp]
enabled = true [开启邮件发送功能]
host = smtp.163.com:465[发送者的邮件协议及端口]
user = qsh_test@163.com[发送者的邮件地址]
password = Qsh123456[发送者邮箱密码]
;cert_file =
;key_file =
skip_verify = true[校验]
from_address = qsh_test@163.com[发送者的邮件地址]
from_name = Grafana Alert

2、Grafana配置Alerting

grafana2.png

之后点击Add Channel进入配置页面: 

grafana3.png