跳到主要內容

Prometheus + Grafana + Exporters = Server Monitor

Grafana
功能強大且美觀的Dashboard
Prometheus
用來作為數據收集及儲存,透過設定至各 exporter 端"拉"資料回來
exporter
Prometheus exporter

環境建置


使用 docker-compose 時,會依所在目錄建立 ${path}-default docker network
純粹為了賞心悅目 建立一個 monitor-net 集中堆放 Grafana , Prometheus , xxxx exporter

建立 docker network



docker network create monitor-net


Grafana + Prometheus docker-compose.yml


version: '3' 
services: 
  grafana:
    image: grafana/grafana
    ports:
     - 9000:9000
     - 9010:3000
    environment:
     - GF_SERVER_ROOT_URL= 
     - GF_SECURITY_ADMIN_PASSWORD=
    volumes:  # copy grafana.ini to /etc/grafana
     - /etc/grafana/grafana.ini:/etc/grafana/grafana.ini
     - /var/lib/grafana:/var/lib/grafana
     - /var/log/grafana:/var/log/grafana
    container_name: my-grafana
    restart: always
    networks:
      - myonitornet
  prometheus:
    image: prom/prometheus
    ports:
     - 9090:9090
    volumes:   # copy prometheus.yml to /etc/prometheus
     - /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    command: --config.file=/etc/prometheus/prometheus.yml
    container_name: my-prometheus
    restart: always
    networks:
      - myonitornet
networks:
  myonitornet:
    external:
      name: monitor-net


Others exporter docker-compose.yml



version: '3' # 目前使用的版本,可以參考官網
services: 
  nodeexporter:
    image: prom/node-exporter
    ports: 
     - 9100:9100
    container_name: my-nodeexporter
    restart: always
    networks:
        - myonitornet
  cadivor:
    image: google/cadvisor
    ports:
      - 9111:8080
    container_name: my-cadivor
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
      - /dev/disk/:/dev/disk:ro
    restart: always
    networks:
        - myonitornet
  redisexporter:
    image: oliver006/redis_exporter
    ports:
     - 9121:9121
    container_name: my-redisexporter
    environment:
      - REDIS_ADDR=# ${redis domain}:${redis port}
      - REDIS_PASSWORD=# ${redis password}
    restart: always
    networks:
        - myonitornet
  mysqlexporter:
    image: prom/mysqld-exporter
    ports:
     - 9131:9104
    environment:
      # ${user name}@${password}@)${domain}:${port}}/${database}
      - DATA_SOURCE_NAME=
    container_name: my-mysqlexporter
    restart: always
    networks:
        - myonitornet
  esexporter:
    image: justwatch/elasticsearch_exporter:1.1.0
    ports:
      - 9141:9114
    container_name: my-esexporter
    command:
      - '--es.uri=# ${es address}:${es port}
    restart: always
    networks:
        - myonitornet
networks:
  myonitornet:
    external:
      name: monitor-net
  


環境設定


Prometheus Config

Examples



global:
    scrape_interval: 15s
    external_labels:
      monitor: 'my-monitor'
  scrape_configs:
    - job_name: 'prometheus'
      scrape_interval: 5s
      static_configs:
        - targets: ['${prometheus server}:9090']
    - job_name: 'node resources'
      scrape_interval: 10s
      tls_config:
        insecure_skip_verify: true
      static_configs:
        - targets: ['${server 1}:9100']
        - targets: ['${server 2}:9100']
    - job_name: 'cadvisor'
      scrape_interval: 10s
      static_configs:
        - targets: ['${server 1}:9111']
        - targets: ['${server 2}:9111']
    - job_name: 'redis resources'
      scrape_interval: 10s
      static_configs:
        - targets: ['${server 1}:9121']
        - targets: ['${server 2}:9121']
    - job_name: 'mysql resources'
      scrape_interval: 10s
      static_configs:
        - targets: ['${server 1}:9131']
    - job_name: 'elasticsearch resources'
      scrape_interval: 10s
      static_configs:
        - targets: ['${server 1}:9141']
    


Grafana Config

留言

這個網誌中的熱門文章

Grafana Dashboard 建立

建立自己的 Dashboard # 由於 intelligent sense 相當不錯,輸入關鍵字他會帶出 metric label # 另外可參考 https://prometheus.io/docs/prometheus/latest/querying/basics/ Prometheus Query # 或是直接拿其他已建立的Dashboard 可複製到新的 Dashboard ex: node_memory_MemTotal_bytes # 取伺服器記憶體容量資料 # 過濾條件在{}加入 ex: node_memory_MemTotal_bytes{instance="${server 1}:9100"} # 要取特定伺服器資料 # Setting 中設定 Variables ex: node_memory_MemTotal_bytes{instance=~"$node"} # 變數名稱 node 建立 Alert .Visualization 必須是Graph

FluentD 實作 Nginx Access Log

實作 Nginx Access Log 透過 FluentD 收集 /var/log/nginx/nginx_web.access.log 日誌 過濾不必要的紀錄 輸出到MongoDB & Elasticsearch 延伸閱讀 FluentD 參數說明 FluentD 實作 Nginx Access Log 補充 fluent.conf # workers parameter for specifying the number of workers <system> workers 1 </system> <worker 0> <source> @type tail path /var/log/nginx/nginx_web.access.log pos_file /var/log/td-agent/nginx_web.access.log.pos tag nginx.web.access format /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<logtime>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")(?: "(?<custom_field1>[^\"]*)" "(?< custom_field2>[^\"]*)" "(?< custom_field3>[^\"]*)")?$/ time_format %d/%b/%Y:%H:%M:%S %z </source> # 濾掉不必要的存取紀錄 <fil...

FluentD 實作 Error Log

FluentD 實作 Error Log 本篇將介紹使用 DotNet 專案 log4net 套件,紀錄的 log 針對 Error Level 的訊息透過FluentD 提取出來 在紀錄中 增加 trace ID 設入 MongoDB , 及加入 Slack 通知 延伸閱讀 FluentD 參數說明 FluentD 實作 Nginx Access Log FluentD 實作 Nginx Access Log 補充 log4net <?xml version="1.0" encoding="utf-8" ?> <configuration> <log4net> <appender name="All" type="log4net.Appender.RollingFileAppender"> <file value="/var/log/my.log" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <datePattern value="yyyy-MM-dd" /> <maximumFileSize value="5MB" /> <maxSizeRollBackups value="10" /> <staticLogFileName value="true" /> <PreserveLogFileNameExtension value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%date] [...