Prometheus的安装和使用

1.Prometheus的安装:

1.下载地址:Download | Prometheus

2.解压

3.启动Prometheus

4.访问prometheus界面:通过浏览器访问http://服务器IP:9090就可以访问到prometheus的主界面

5.可以查看监控本机信息(http://11.164.3.125:9090/targets),也可以查看其他机器的信息但其他机器要安装插件node_porter,以及配置Prometheus的Prometheus.yml的文件

注意:更改Prometheus.yml文件后要重启Prometheus才能生效

[root@server ~]# tar xf prometheus-2.5.0.linuxamd64.tar.gz -C /usr/local/(解压到的路径)

[root@server ~]# mv /usr/local/prometheus-2.5.0.linuxamd64/ /usr/local/prometheus(改名字)

#直接使用默认配置文件启动

[root@server ~]# /usr/local/prometheus/prometheus(你的存放路径) --

config.file="/usr/local/prometheus/prometheus.yml" &

#确认端口(9090)

[root@server ~]# lsof -i:9090

2.node_porter的安装

1.下载地址:Download | Prometheus

2.解压

3.启动node_porter服务

4.访问界面:通过浏览器访问http://服务器IP:9100

[root@agent ~]# tar xf node_exporter-0.16.0.linuxamd64.tar.gz -C /usr/local/

[root@agent ~]# mv /usr/local/node_exporter-0.16.0.linuxamd64/ /usr/local/node_exporter

#里面就一个启动命令node_exporter,可以直接使用此命令启动

[root@agent1 ~]# ls /usr/local/node_exporter/

LICENSE node_exporter NOTICE

[root@agent1 ~]# nohup /usr/local/node_exporter/node_exporter &

#确认端口(9100)

[root@agent1 ~]# lsof -i:9100

3.alertmanage的安装

1.下载地址:Download | Prometheus

2.解压

3.更改alert manager.yml文件

4.启动alertmanager服务

5.访问界面:通过浏览器访问http://服务器IP:9093

4.grafana的安装

1.下载地址:Download Grafana | Grafana Labs

2.配置数据源

3.导入Dashboard模板(也可以自己制作)

4.访问界面(http://11.164.3.125:3000/d/9CWBz0bik/1-node-exporter-for-prometheus-dashboard-cn-v20201010?orgId=1)通过浏览器访问http://服务器IP:3000

注意:数据源是Prometheus的数据

[root@grafana ~]# rpm -ivh grafana-5.3.4-1.x86_64.rpm

#启动服务

[root@grafana ~]# systemctl start grafana-server

[root@grafana ~]# systemctl enable grafana-server

确认端口(3000)

[root@grafana ~]# lsof -i:3000

5.四者的逻辑关系

1.Prometheus如果要监控其他机器,其他机器要安装node_exporter,并且修改Prometheus. yml文件,把要监控的机器添加进去。

#在主配置文件最后加上下面三行(注意空格的间距,保证不出错可直接复制配置文件里面经行修改)
[root@server ~]# vim /usr/local/prometheus/prometheus.yml

  • job_name: 'agent' # 取一个job名称来代表被监控的机器
    static_configs:
  • targets: ['192.168.242.177:9100'] # 这里改成被监控机器的IP,后面端口接9100
  • targets: ['192.168.242.177:9100']

#改完配置文件后,重启服务
[root@server ~]# pkill prometheus
[root@server ~]# lsof -i:9090 # 确认端口没有进程占用
[root@server ~]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
[root@server ~]# lsof -i:9090

2.prometheus可以监控但想要发出警告需要安装grafana或者插件alertmanager。

3.grafana只需要在grafana上面配置就可以了,但无法使用模板变量,以及报警规则比较繁琐。

4.alertmanager告警需要先创建一个告警规则的.yml文件,并把告警规则文件和alertmanager配置文件加入到Prometheus.yml文件中,如果钉钉报警还需要下载一个Prometheus-webhook-dingtalk模板(下载地址:https://github.com/timonwong/prometheus-webhook-dingtalk)并启动。

6.简单命令

1.启动Prometheus:/usr/local/prometheus/prometheus(安装路径) --config.file="/usr/local/prometheus/prometheus.yml" &

2.启动node_porter服务:nohup /usr/local/node_exporter/node_exporter &

3.启动alertmanager服务:./alertmanager --config.file=alertmanager.yml --cluster.advertise-address=0.0.0.0:9093 &

4.钉钉告警模板启动:nohup ./prometheus-webhook-dingtalk --ding.profile="webhook1=https://oapi.dingtalk.com/robot/send?access_token=xxx" &(ding.profile是钉钉机器人的webhook)