使用Prometheus监控服务器

2017-02-12 阅读: Prometheus

Prometheus是一套开源监控系统,使用Go语言开发,是Google BorgMon监控系统的类似实现。

Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控,是比较适合Docker,Kubernetes等环境的监控系统之一。输出监控信息的HTTP接口被称作exporter。

安装Prometheus

下载二进制包:

wget https://github.com/prometheus/prometheus/releases/download/v1.5.2/prometheus-1.5.2.linux-amd64.tar.gz
tar -zxvf prometheus-1.5.2.linux-amd64.tar.gz
mv prometheus-1.5.2.linux-amd64 /usr/local/prometheus
cd /usr/local/prometheus

./prometheus -version
prometheus, version 1.5.2 (branch: master, revision: bd1182d29f462c39544f94cc822830e1c64cf55b)
  build user:       root@a8af9200f95d
  build date:       20170210-14:41:22
  go version:       go1.7.5

创建prometheus用户:

groupadd prometheus
useradd -g prometheus -d /var/lib/prometheus -s /sbin/nologin prometheus

/usr/lib/systemd/system/prometheus.service

[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus -config.file=/usr/local/prometheus/prometheus.yml -storage.local.path=/var/lib/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动:

systemctl enable prometheus
systemctl start prometheus

安装node_exporter

为监控服务器CPU,内存,磁盘,I/O等信息,需要安装node_exporter。

下载node_exporter:

wget https://github.com/prometheus/node_exporter/releases/download/v0.13.0/node_exporter-0.13.0.linux-amd64.tar.gz
tar -zxvf node_exporter-0.13.0.linux-amd64.tar.gz
mv node_exporter-0.13.0.linux-amd64 /usr/local/prometheus/node_exporter

/usr/lib/systemd/system/node_exporter.service

[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl enable node_exporter
systemctl start node_exporter

修改/usr/local/prometheus/prometheus.yml,加入下面的监控目标:

scrape_configs:
  - job_name: 'linux'
    static_configs:
      - targets: ['localhost:9100']
        labels:
          instance: node1

重启Prometheus。

安装Grafana

/etc/yum.repos.d/grafana.repo

[grafana]
name=grafana
baseurl=https://packagecloud.io/grafana/stable/el/6/$basearch
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packagecloud.io/gpg.key https://grafanarel.s3.amazonaws.com/RPM-GPG-KEY-grafana
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
yum install grafana

systemctl daemon-reload
systemctl enable grafana-server
systemctl start grafana-server
systemctl status grafana-server

浏览器中打开http://192.168.61.10:3000/, 默认登录用户名和密码:admin/admin

grafana

接下来在Grafana中添加Prometheus数据源:

  • Name填上Prometheus
  • Type选Prometheus
  • Url填上http://localhost:9090/
  • Access选proxy

这里下载Node Exporter Server Metrics这个Dashboard的json文件。 在Grafana的Dashboard中导入这个文件,选择Prometheus数据源,就可以服务器的CPU, 内存, 磁盘等统计信息展现在图表中了。

参考

标题:使用Prometheus监控服务器
本文链接:https://blog.frognew.com/2017/02/use-prometheus-on-centos7.html
转载请注明出处。

目录