# Prometheus 普羅米俢斯 & Grafana ## 角色說明 1. [參考文件](https://blog.techbridge.cc/2019/08/26/how-to-use-prometheus-grafana-in-flask-app/) 2. Grafana 是 dashboard 軟體 3. Prometheus 是資料庫及資料接收器。 4. exporter 負責提供主機資訊,安裝在需觀察的主機上。 ```sequence user->grafana:view dashboard grafana->prometheus:query data prometheus->exporter:pull(定時取得主機資訊) ``` ## Prometheus #### 限制資料量 1. 系統預設資料只保存 15 天(待驗證)。 #### 動態載入設定檔 1. 啟動時啟動權限 for yaml ```yaml= command: - '在這裡要加入原本預設的項目' - '--web.enable-lifecycle' ``` > <small>因為 YAML 中使用 command 會取代原本image 的預設指令。使用 dokcer 指令查出 prometheus 的預設指令</small> 2. 呼叫 api ```bash= curl -XPOST http://ip:port/-/reload ``` ## Grafana #### 安裝 * [官方文件](https://grafana.com/docs/grafana/latest/installation/docker/) * [設定值](https://grafana.com/docs/grafana/latest/installation/docker/) 預設帳密在 ```ini= $WORKING_DIR/conf/defaults.ini [security] admin_user = ***** admin_password = ***** ``` >預設帳密通常是 admin / admin 必須綁定目錄,帳號和 dashboard 的資料都在這裡。 必須綁定 grafana.ini 檔,才可在 container 外修改設定檔。 grafana.ini 預設值[下載位置](https://gist.githubusercontent.com/xlson/56b5c0d49265aa0bf1d41faaa4383ec5/raw/8e4fdc2323bf669fea0ba82cd183cb5735331203/grafana.ini) ```yaml= volumes: - ./env/grafana:/var/lib/grafana - ./env/grafana/grafana.ini:/etc/grafana/grafana.ini ``` >上述目錄須開放相對權限供 container 存取 ### Garfana dashboard - 直接找前人作好的 dashboard 匯入。 * [Grafana Lab](https://grafana.com/grafana/dashboards) * 須注意 exporter 版本,專案使用是0.13 windows wmi exporter 和 0.8 就不相容。 ## 安裝 exporter ### windows 目前使用 0.13 版,若無必要就不要變動。 [安裝頁面](https://github.com/prometheus-community/windows_exporter/releases) ### ubuntu exporter 預設使用 9100 port,記得要開啟 ```shell= sudo apt update sudo apt install prometheus-node-exporter ``` ### Centos 7 取得 exporter 版本 ```base= node_exporter --version ``` ### docker #### cAdvisor (exporter) [安裝說明-及使用說明](https://yunlzheng.gitbook.io/prometheus-book/part-ii-prometheus-jin-jie/exporter/commonly-eporter-usage/use-prometheus-monitor-container) ## 查詢語法 ### windows exporter V0.13 1. **cpu 使用率** ```shell= 100 - (avg by (instance) (irate(windows_cpu_time_total{instance="{instance_名稱}",mode="idle"}[5m])) * 100) ``` 2. **記憶體使用率**