使用Prometheus监控服务器
2017-02-12
Prometheus是一套开源监控系统,使用Go语言开发,是Google BorgMon监控系统的类似实现。
Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控,是比较适合Docker,Kubernetes等环境的监控系统之一。输出监控信息的HTTP接口被称作exporter。
安装Prometheus #
下载二进制包:
1wget https://github.com/prometheus/prometheus/releases/download/v1.5.2/prometheus-1.5.2.linux-amd64.tar.gz
2tar -zxvf prometheus-1.5.2.linux-amd64.tar.gz
3mv prometheus-1.5.2.linux-amd64 /usr/local/prometheus
4cd /usr/local/prometheus
5
6./prometheus -version
7prometheus, version 1.5.2 (branch: master, revision: bd1182d29f462c39544f94cc822830e1c64cf55b)
8 build user: root@a8af9200f95d
9 build date: 20170210-14:41:22
10 go version: go1.7.5
创建prometheus用户:
1groupadd prometheus
2useradd -g prometheus -d /var/lib/prometheus -s /sbin/nologin prometheus
/usr/lib/systemd/system/prometheus.service
1[Unit]
2Description=prometheus
3After=network.target
4[Service]
5Type=simple
6User=prometheus
7ExecStart=/usr/local/prometheus/prometheus -config.file=/usr/local/prometheus/prometheus.yml -storage.local.path=/var/lib/prometheus
8Restart=on-failure
9[Install]
10WantedBy=multi-user.target
启动:
1systemctl enable prometheus
2systemctl start prometheus
安装node_exporter #
为监控服务器CPU,内存,磁盘,I/O等信息,需要安装node_exporter。
下载node_exporter:
1wget https://github.com/prometheus/node_exporter/releases/download/v0.13.0/node_exporter-0.13.0.linux-amd64.tar.gz
2tar -zxvf node_exporter-0.13.0.linux-amd64.tar.gz
3mv node_exporter-0.13.0.linux-amd64 /usr/local/prometheus/node_exporter
/usr/lib/systemd/system/node_exporter.service
1[Unit]
2Description=node_exporter
3After=network.target
4[Service]
5Type=simple
6User=prometheus
7ExecStart=/usr/local/prometheus/node_exporter/node_exporter
8Restart=on-failure
9[Install]
10WantedBy=multi-user.target
1systemctl enable node_exporter
2systemctl start node_exporter
修改/usr/local/prometheus/prometheus.yml,加入下面的监控目标:
1scrape_configs:
2 - job_name: 'linux'
3 static_configs:
4 - targets: ['localhost:9100']
5 labels:
6 instance: node1
重启Prometheus。
安装Grafana #
/etc/yum.repos.d/grafana.repo
1[grafana]
2name=grafana
3baseurl=https://packagecloud.io/grafana/stable/el/6/$basearch
4repo_gpgcheck=1
5enabled=1
6gpgcheck=1
7gpgkey=https://packagecloud.io/gpg.key https://grafanarel.s3.amazonaws.com/RPM-GPG-KEY-grafana
8sslverify=1
9sslcacert=/etc/pki/tls/certs/ca-bundle.crt
1yum install grafana
2
3systemctl daemon-reload
4systemctl enable grafana-server
5systemctl start grafana-server
6systemctl status grafana-server
浏览器中打开http://192.168.61.10:3000/, 默认登录用户名和密码:admin/admin
接下来在Grafana中添加Prometheus数据源:
- Name填上
Prometheus
- Type选
Prometheus
- Url填上
http://localhost:9090/
- Access选
proxy
从这里下载Node Exporter Server Metrics这个Dashboard的json文件。
在Grafana的Dashboard中导入这个文件,选择Prometheus
数据源,就可以服务器的CPU, 内存, 磁盘等统计信息展现在图表中了。