Prometheus入門+grafana集成

開始使用Prometheus

$ systemctl start prometheus
$ netstat -lntp
tcp6       0      0 :::9090                 :::*                    LISTEN      19824/./prometheus

在瀏覽器訪問:http://ip:9090/graph 。Prometheus會把自身做爲一個項目進行自監控,查看收集到監控項:http://172.16.180.129:9090/metrics (若是是首次啓動,須要等待30s左右的時間)node

使用內置表達式查看數據

地址:http://ip:9090/graphlinux

Prometheus內置監控項 prometheus_target_interval_length_seconds ,將該監控項直接輸入console查詢,可獲取數據:git

![image-20190322132500188](/Users/adai/Library/Application Support/typora-user-images/image-20190322132500188.png)github

使用prometheus監控服務器

上面用Prometheus自己的數據簡單演示了監控數據的查詢,這裏咱們用一個監控服務器狀態的例子來更加直觀說明。json

爲監控服務器CPU、內存、磁盤、I/O等信息,首先須要安裝node_exporter。node_exporter的做用是用於機器系統數據收集。vim

安裝node_exporter

node_exporter也是用Golang實現,直接使用預編譯的二進制文件部署,開箱即用。瀏覽器

$ cd /home/prometheus && wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
$ tar zxvf node_exporter-0.17.0.linux-amd64.tar.gz 
$ mv node_exporter-0.17.0.linux-amd64 /usr/local/prometheus/node_exporter
  • 建立systemd服務bash

    $ vim /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
  • 啓動node_exporter服務器

    $ systemctl start node_exporter
    $netstat -lntp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name          
    tcp6       0      0 :::9100                 :::*                    LISTEN      24126/node_exporter
  • 修改prometheus.yml,加入下面的監控目標(node_exporter默認的抓取地址爲http://ip:9100)tcp

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

說明:prometheus.yml中一共定義了兩個監控,一個是監控prometheus自身服務,另外一個是監控Linux服務器。

這裏給一個完整示例:

scrape_configs:

  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'linux'
    static_configs:
      - targets: ['localhost:9100']
        labels:
          instance: node1
  • 重啓prometheus服務

    $ systemctl restart prometheus
  • 在Prometheus Web查看監控的目標:訪問Prometheus Web,在Status->Targets頁面下,咱們能夠看到咱們配置的兩個Target(linux和prometheus),它們的State爲UP。

  • 查看memory使用狀況 ![image-20190322134529082](/Users/adai/Library/Application Support/typora-user-images/image-20190322134529082.png)

Prometheus Web界面自帶的圖表是很是基礎的,比較適合用來作測試。若是要構建強大的Dashboard,仍是須要更加專業的工具才行。接下來咱們將使用Grafana來對Prometheus採集到的數據進行可視化展現。

prometheus集成grafana

在Grafana中添加Prometheus數據源:Configuration——DataSource——"add new DataSource"——Prometheus

  • http

  • 上述配置完成後須要導入node-exporter-server-metrics 的數據模板到grafana,兩種導入方法:

    • 經過url導入:grafana——菜單欄加號➕——import—— 輸入URL https://grafana.com/dashboards/405 —— 導入——Options (Name、Folder、Prometheus) —— import
    • 下載後導入:grafana——菜單欄加號➕——import—— 輸入 下載 好的json文件 ——import

    完成上述操做後便可看到node-exporter採集的數據。

  • 在Dashboards上選Node Exporter Server Metrics模板,就能夠看到被監控服務器的CPU, 內存, 磁盤等統計信息。

  • 在Dashboards上選Prometheus Status模板,查看Prometheus各項指標數據。

相關文章
相關標籤/搜索