prometheus簡單監控Linux,mysql,nginx


prometheus安裝

下載安裝

#官網下載 解壓即可使用
https://prometheus.io/download/
#docker 方式安裝
sudo docker run -n prometheus -d -p 9090:9090 prom/prometheus

配置文件

 /etc/prometheus/prometheus.yml 或 可執行文件當前目錄下/prometheus.yml

完整配置文件

  scheme: http
  static_configs:
  - targets:
    - localhost:9090
- job_name: node1_self
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  static_configs:
  - targets:
    - 192.168.3.103:9100
- job_name: mysql
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  static_configs:
  - targets:
    - 192.168.3.103:9104
    labels:
      instance: db1
- job_name: nginx
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /status/format/prometheus
  scheme: http
  static_configs:
  - targets:
    - 192.168.3.139:80
    labels:
      instance: web1
  basic_auth:
    username: UserName
    password: PassWord

重啟服務

重啟服務或發信號重新加載配置
killall -HUP prometheus

官方exports 大全

https://prometheus.io/docs/instrumenting/exporters/

Linux服務器配置

下載安裝node_exporter (下載解壓即可使用)

https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
./node_exporter
測試node_exporter
curl http://localhost:9100/metrics

mysql的exporter下載和配置

可以在mysql機器上安裝也可以在別的機器上安裝
`. 老樣子下載解壓
https://github.com/prometheus/mysqld_exporter/releases
2. 要配置一下被監控的mysql賬戶信息
最好單獨配置權限
為 mysqld_exporter 創建一個單獨的用戶
並賦予它受限的權限(PROCESS、REPLICATION CLIENT、SELECT)
最好還限制它的最大連接數(MAX_USER_CONNECTIONS)

CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'password' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

$ cat .my.cnf

[client]
host=localhost
port=3306
user=root
password=123456
  1. 運行mysqld_exporter
    `./mysqld_exporter --config.my-cnf=".my.cnf"

  2. 在 prometheus服務端的配置文件prometheus.yml中找到 scrape_config子項 添加一個 job
    如:

  - job_name: mysql
    static_configs:
      - targets: ['192.168.1.7:9104']
        labels:
          instance: db1
  1. 發送 重載配置文件信號
    killall -SIGHUP prometheus
  2. 到prometheus 網頁中 導航欄->status->target 查看剛才添加的是否成功!

nginx exporter 安裝和配置

好多方式都可以.lua腳本,openresty 等
我們選擇 編譯nginx的nginx-module-vts 這就意味着我們要自己手動編譯了.

  1. 下載nginx源碼后解壓.
    wget https://github.com/nginx/nginx/releases/tag/release-1.17.1
    tar -xvf nginx-release-1.17.1.tar.gz
    cd nginx-release-1.17.1
  2. 下載或克隆nginx-module-vts 模塊 https://github.com/vozlt/nginx-module-vts
  3. 編譯安裝nginx
    安裝依賴(centos)
  • openssl-devel
  • pcre-devel
  • gcc

./auto/configure --add-module=/home/pi/nginx-module-vts --with-http_ssl_module --with-debug
干掉nginx
make -j4 使用4個線程編譯.樹莓派有四個線程
make install 安裝nginx默認
4.配置nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf

user root;
#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}



http {
    include       mime.types;
    default_type  application/octet-stream;
    vhost_traffic_status_zone;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
	charset utf-8;
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
	    auth_basic "needAuth";
	    auth_basic_user_file /usr/local/nginx/conf/passwd.db;
        }
	location /status {
		vhost_traffic_status_display;
		vhost_traffic_status_display_format html;
 	}
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

   
    }

}

配置http密碼
apt install apache2-utils -y
htpasswd -c /usr/local/nginx/conf/passwd.db UserName
啟動nginx
先看下模塊有沒編譯進來

cd /usr/local/nginx/
/usr/local/nginx/sbin/nginx -V |grep nginx-module-vts

能看到信息就代表模塊編譯成功.
4. 運行nginx
/usr/local/nginx/sbin/nginx
4.1 查看nginx機器的ip
ip a
5. 在prometheus中增加一個監控nginx的任務
添加配置內容在配置文件 prometheus.yml 注意因為我們nginx配置了驗證,所以在prometheus中也要添加驗證.要不沒有辦法支持訪問

- job_name: nginx
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /status/format/prometheus
  scheme: http
  static_configs:
  - targets:
    - 192.168.3.139:80
    labels:
      instance: web1
  basic_auth:
    username: UserName
    password: PassWord
  1. 發送 重載配置文件信號
    killall -SIGHUP prometheus
  2. 到prometheus 網頁中 導航欄->status->target 查看剛才添加的是否成功!

之后可以使用配合Grafana可以愉快玩耍了.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM