prometheus 监控服务

创建用户

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

安装golang

yum install golang

安装grafana

systemctl start grafana-server

安装Prometheus

cd /usr/src
wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
tar xzvf prometheus-2.3.2.linux-amd64.tar.gz
mv prometheus-2.3.2.linux-amd64 /usr/local
mv prometheus-2.3.2.linux-amd64 prometheus

检查版本

./prometheus --version
prometheus, version 1.6.3 (branch: master, revision: c580b60c67f2c5f6b638c3322161bcdf6d68d7fc)
  build user:       root@a6410e65f5c7
  build date:       20170522-09:15:06
  go version:       go1.8.1

创建用户

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

创建Systemd服务

vim /etc/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.tsdb.path=/var/lib/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl start prometheus

访问自带Web

http://ip:9090
static_configs:
      - targets: ['localhost:9090']
        labels:
         instance: node1

监控mysql

安装mysqld_exporter

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar xzvf mysqld_exporter-0.11.0.linux-amd64.tar.gz
mv mysqld_exporter-0.11.0.linux-amd64 /usr/local/prometheus/mysqld_exporterhp

创建mysql账户

GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'localhost' identified by '12345678';
GRANT SELECT ON performance_schema.* TO 'mysqld_exporter'@'localhost';
flush privileges;

创建连接MySQL的配置文件

mysqld_exporter默认会读取~/.my.cnf文件,这里是创建在mysqld_exporter的安装目录下的

vim /usr/local/prometheus/mysqld_exporter/.my.cnf

[client]
user=mysqld_exporter
password=12345678

创建Systemd服务

vim /etc/systemd/system/mysql_exporter.service

[Unit]
Description=mysql_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/mysqld_exporter/mysqld_exporter --config.my-cnf="/usr/local/prometheus/mysqld_exporter/.my.cnf"
Restart=on-failure
[Install]
WantedBy=multi-user.target

修改prometheus.yml

追加下面配置监控mysql

# 监控本机mariadb
  - job_name: mariadb
    static_configs:
      - targets: ['localhost:9104']
        labels:
          instance: db1

配置仪表盘

Grafana目前官方还没有的配置好的MySQL图表模板,这里使用Percona开源的模板

git clone https://github.com/percona/grafana-dashboards.git
导入模板

如图所示

批量导入
cp -r grafana-dashboards/dashboards /var/lib/grafana/
编辑Grafana配置文件
vim /etc/grafana/grafana.ini

# 修改以下选项
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards

systemctl restart grafana-server

监控nginx

编译nginx-module-vts模块

这个自行解决吧..

安装nginx-vts-exporter

wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
tar -zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
mv nginx-vts-exporter-0.10.3.linux-amd64  /usr/local/prometheus/nginx-vts-exporter
chmod +x /usr/local/prometheus/nginx-vts-exporter/nginx-vts-exporter

创建Systemd服务

vim /etc/systemd/system/nginx_vts_exporter.service

[Unit]
Description=nginx_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri=http://localhost/status/format/json
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl start nginx_vts_exporter.service

配置nginx server

server {
    listen  50001;
    server_name localhost;

    location /status {
            allow 127.0.0.1;
            #deny all;
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
    }
}
# 如果有数据出来...那么恭喜你..如果没有,那么你要排查问题了
curl http://localhost:50001/status/format/json

修改prometheus.yml

# 监控本机nginx
   - job_name: nginx
    static_configs:
      - targets: ['localhost:9913']
        labels:
          instance: web1

导入模板2949

监控php-fpm

大家看下面的文档就可以了,文档写的很详细

nginx server

server {
    listen  50002;
    server_name localhost;

    location /status {
         allow 127.0.0.1;
         fastcgi_pass  127.0.0.1:9000;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
     }
}

测试

curl http://127.0.0.1:50002/status\?full

创建Systemd服务

vim /etc/systemd/system/php-fpm-exporter.service

[Unit]
Description=nginx_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/php-fpm-exporter/php-fpm-exporter --addr=127.0.0.1:9914 --endpoint=http://127.0.0.1:50002/status
Restart=on-failure
[Install]
WantedBy=multi-user.target

修改prometheus.yml

# 监控本机php-fpm
- job_name: php-fpm
static_configs:
  - targets: ['localhost:9914']
    labels:
      instance: php-fpm

导入模板

文档

监控redis

sudo wget https://github.com/oliver006/redis_exporter/releases/download/v0.13/redis_exporter-v0.13.linux-amd64.tar.gz
tar -xvf  redis_exporter-v0.13.linux-amd64.tar.gz
mv redis_exporter-v0.13 /usr/local/prometheus/redis_exporter

wget  https://grafana.com/api/dashboards/763/revisions/1/download
## 无密码
./redis_exporter redis//192.168.1.120:6379
## 有密码
redis_exporter -redis.addr 192.168.1.120:6379  -redis.password 123456
vim /etc/systemd/system/redis_exporter.service

[Unit]
Description=redis_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/redis_exporter/redis_exporter redis//192.168.1.120:6379
Restart=on-failure
[Install]
WantedBy=multi-user.target
- job_name: redis
static_configs:
  - targets: ['192.168.1.120:9121']
    labels:
      instance: redis120

node


wget https://github.com/prometheus/node_exporter/releases/download/v0.18.0/node_exporter-0.18.0.linux-amd64.tar.gz
tar -zxvf node_exporter-0.18.0.linux-amd64.tar.gz 
cd node_exporter-0.18.0.linux-amd64 && mv node_exporter /usr/local/prometheus/node_exporter

sudo vim /etc/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

http://IP:9100/metrics

错误

ERRO[0000] invalid Prometheus URL: http://virtual_prometheus:9090/ source=main.go:75

因为hostname不能有 "_",修改hostname吧

参考文档

docker安装

安装grafana

创建数据目录
sudo mkdir -p /data/grafana
sudo mkdir -p /data/grafana/data
设置文件夹权限
# 新建用户
sudo groupadd -g 472 grafana
sudo useradd -u 472 -g grafana grafana

cd /data
sudo chown grafana:grafana grafana -R

bash脚本

#/bin/bash
docker rm -f grafana
docker run \
  -d \
  -p 9200:3000 \
  --name=grafana \
  -v /data/grafana:/var/lib/grafana \
  -e "GF_INSTALL_PLUGINS=grafana-piechart-panel,grafana-simple-json-datasource" \
  grafana/grafana:latest

安装prometheus

创建数据目录
sudo mkdir -p /data/prometheus
sudo mkdir -p /data/prometheus/data
赋予权限
cd /data
sudo chmod 755 prometheus  -R
sudo chown nobody:nogroup prometheus -R
先不带任何参数拉起docker,然后把prometheus.yml文件cp出来
sudo docker run -d -p 9090:9090 --name prometheus prom/prometheus
sudo docker cp prometheus:/etc/prometheus/prometheus.yml /data/prometheus/etc/.
bash脚本
#/bin/bash
docker rm -f prometheus
docker run -d -p 9090:9090 \
            -v /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
            -v /data/prometheus/data:/prometheus \
            --name prometheus \
            prom/prometheus

安装node-exporter

bash脚本
#/bin/bash
docker pull  quay.io/prometheus/node-exporter:master
docker rm -f node-exporter
docker run -d --name node-exporter \
   --restart always \
   -p 9100:9100 \
   -v "/proc:/host/proc:ro" \
   -v "/sys:/host/sys:ro" \
   -v "/:/rootfs:ro" \
   --net="host" \
   quay.io/prometheus/node-exporter:master

设置db

file

好用的看板