Grafana+Prometheus監控系統安裝

簡介:

grafana 是一款採用 go 語言編寫的開源應用,主要用於大規模指標數據的可視化展現,是網絡架構和應用分析中最流行的時序數據展示工具,目前已經支持絕大部分常用的時序數據庫。官網(http://docs.grafana.org/)。

用到的工具:

  • Docker,本文大量使用了Docker來啓動各個應用。
  • Prometheus,負責抓取/存儲指標信息,並提供查詢功能。
  • Grafana,負責數據可視化。
  • JMX exporter,提供JMX中和JVM相關的metrics。
  • Tomcat,用來模擬一個Java應用。

先講一下大致步驟:

  1. 利用JMX exporter,在Java進程內啓動一個小型的Http server
  2. 配置Prometheus抓取那個Http server提供的metrics。
  3. 配置Grafana連接Prometheus,配置Dashboard。

 

1. exporter介紹

廣義上講所有可以向Prometheus提供監控樣本數據的程序都可以被稱爲一個Exporter。而Exporter的一個實例稱爲target,如下所示,Prometheus通過輪詢的方式定期從這些target中獲取樣本數據:

node_exporter 服務器監控

#安裝node_exporter,需要放在該服務器中
tar -zxvf node_exporter-0.18.1.linux-amd64.tar.gz
nohup /basewin/grafana/node_exporter-0.18.1.linux-amd64/node_exporter&
9100端口

安裝node_exporter
tar -zxvf node_exporter-0.18.1.linux-amd64.tar.gz
nohup /disk2/node_exporter-0.18.1.linux-amd64/node_exporter &
9100端口
netstat -nptl 

tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
nohup /basewin/grafana/mysqld_exporter-0.12.1.linux-amd64/mysqld_exporter -config.my-cnf=".my.cnf" &

dashboard頁面 linux 系統 監控 8919

redis_exporter

#不需要放在redis所在得服務器中
wget https://github.com/oliver006/redis_exporter/releases/download/v0.15.0/redis_exporter-v0.15.0.linux-amd64.tar.gz
tar -zxvf redis_exporter-v0.15.0.linux-amd64.tar.gz
cd /basewin/grafana
#啓動 並指定 redis 地址密碼等信息
nohup ./redis_exporter  -redis.addr 120.76.236.11:6379  -redis.password test &
#nohup ./redis_exporter -web.listen-address :9122 -redis.addr 39.104.228.111:6479  -redis.password test & 

ps -aux | grep redis_exporter
#默認端口 90121
netstat -nptl 
#dashboards
wget  https://grafana.com/api/dashboards/763/revisions/1/download

配置prometheus.yml 加入

- job_name: redis_exporter
  static_configs:
  - targets: ['192.168.0.17:9121']

監控不同IP不同密碼Redis多實例

https://blog.csdn.net/qq_36937234/article/details/97669297

dashboard頁面圖標ID: 763

mysql_exporter

#需要放在mysql所在得服務器中
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
cd /basewin/grafana/mysqld_exporter-0.12.1.linux-amd64
cd /disk2/mysqld_exporter-0.12.1.linux-amd64
#*重要!設置或顯示環境變量
export DATA_SOURCE_NAME='root:test@tcp(47.75.108.111:3306)/'
#啓動
nohup /basewin/grafana/mysqld_exporter-0.12.1.linux-amd64/mysqld_exporter  --config.my-cnf="/basewin/grafana/mysqld_exporter-0.12.1.linux-amd64/.my.cnf" &
nohup ./mysqld_exporter --config.my-cnf=".my.cnf" &

#其他服務器同樣方法
export DATA_SOURCE_NAME='root:test@tcp(120.76.236.111:3306)/'
export DATA_SOURCE_NAME='root:test@tcp(39.104.228.111:3306)/'
export DATA_SOURCE_NAME='root:test@tcp(115.29.234.111:3306)/'
nohup ./mysqld_exporter --config.my-cnf=".my-mysql-exporter.cnf" &



ps -aux | grep mysqld_expo
netstat -nptl 
#默認 9104

netstat -nptl
ps -aux | grep java
ps -aux | grep tomcat
ps -aux | grep nginx
#查看端口進程號
netstat -tunlp|grep 9104
#列出該進程的啓動位置
ll /proc/23931

dashboard頁面 ranfana 導入Mysql 監控圖表推薦圖標ID:https://grafana.com/dashboards/7362

 

 

2. Prometheus介紹

Prometheus(簡稱Prom)前身是SoundCloud的告警工具包,現已演化成一個獨立的開源監控系統。屬於Kurberntes所在的Cloud Native Computing Foundation。

2.1 linux下載安裝

地址:https://prometheus.io/download/

安裝prometheus
cd /basewin/grafana
[root@prometheus src]# tar -zxvf prometheus-2.14.0.linux-amd64.tar.gz
[root@prometheus local]# mv prometheus-2.14.0.linux-amd64/ prometheus/
vi /basewin/grafana/prometheus/prometheus.yml

cd /basewin/grafana/prometheus
nohup ./prometheus &
nohup /basewin/grafana/prometheus/prometheus &
9090端口
#關閉
或者通過命令熱加載:
curl  -XPOST localhost:9090/-/reloadit
ps -aux | grep prometheus
#頁面訪問地址
http://ip:9090/targets

prometheus.yml配置

# my global config
global:
  scrape_interval: 1m # Server端抓取數據的時間間隔Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 1m #評估報警規則的時間間隔 Evaluate rules every 15 seconds. The default is every 1 minute.
  #數據抓取的超時時間 scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'mdmposlinux'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['120.76.236.111:9100']
      labels:
        instance: mdmposlinux
        
  - job_name: 'mdmposhklinux'
    static_configs:
    - targets: ['47.75.108.111:9100']
      labels:
        instance: mdmposhklinux
 
  - job_name: 'smartpeaklinux'
    static_configs:
    - targets: ['115.29.234.111:9100']
      labels:
        instance: smartpeaklinux
# testlinux
  - job_name: 'testlinux'
    static_configs:
    - targets: ['39.104.228.111:9100']
      labels:
        instance: testlinux

   # redis
  - job_name: 'redis120'
    static_configs:
    - targets: ['47.75.108.111:9121']
      labels:
        instance: redis120 
  # testredis
  - job_name: 'redis-test'
    static_configs:
    - targets: ['47.75.108.111:9122']
      labels:
        instance: redis-test   
# hkmysql
  - job_name: mysql_hk
    static_configs:
      - targets: ['47.75.108.111:9104']
        labels:
          instance: mysql_hk

# mysql
  - job_name: mysql120
    static_configs:
      - targets: ['120.76.236.111:9104']
        labels:
          instance: mysql120

# testmysql
  - job_name: mysqltest
    static_configs:
      - targets: ['39.104.228.111:9104']
        labels:
          instance: mysqltest

# mysql_smartpeak
  - job_name: mysql_smartpeak
    static_configs:
      - targets: ['115.29.234.111:9104']
        labels:
          instance: mysql_smartpeak


        

 

 

 

3. Grafana介紹

Grafana是一個跨平臺的開源的度量分析和可視化工具,可以通過將採集的數據查詢然後可視化的展示,並及時通知。它主要有以下六大特點:

1、展示方式:快速靈活的客戶端圖表,面板插件有許多不同方式的可視化指標和日誌,官方庫中具有豐富的儀表盤插件,比如熱圖、折線圖、圖表等多種展示方式;

2、數據源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;

3、通知提醒:以可視方式定義最重要指標的警報規則,Grafana將不斷計算併發送通知,在數據達到閾值時通過Slack、PagerDuty等獲得通知;

4、混合展示:在同一圖表中混合使用不同的數據源,可以基於每個查詢指定數據源,甚至自定義數據源;

5、註釋:使用來自不同數據源的豐富事件註釋圖表,將鼠標懸停在事件上會顯示完整的事件元數據和標記;

6、過濾器:Ad-hoc過濾器允許動態創建新的鍵/值過濾器,這些過濾器會自動應用於使用該數據源的所有查詢。

3.1 linux下載安裝

官網下載地址:Grafana

安裝指南:Grafana安裝指南

根據自己的系統版本和配置,下載對應的包,官方提供瞭如下說明,可直接按照說明進行下載:

啓動

下載安裝完成後,輸入命令 service grafana-server start 啓動服務,打開瀏覽器,輸入IP+端口,3000爲Grafana的默認偵聽端口。

系統默認用戶名和密碼爲admin/admin,第一次登陸系統會要求修改密碼,修改密碼後登陸,界面顯示如下:

3.2 windows下載安裝

安裝方式也是很簡單的,下載Windows版本的zip,解壓,conf目錄下面的配置文件 defaults.ini copy一份命名爲 custom.ini,然後雙擊grafana-server.exe即可。根據 custom.ini 裏面配置的端口,打開本地服務,

Grafana的默認端口爲3000,此端口在Windows上需要額外的權限。編輯custom.ini並取消註釋http_port配置選項(;ini文件中的註釋字符)並將其更改爲類似8080或類似內容。該端口不需要額外的Windows特權。

默認登錄名和密碼admin/admin

通過執行目錄grafana-server.exe中的啓動Grafana bin,最好從命令行啓動。如果要將Grafana作爲Windows服務運行,請下載NSSM。使用該工具將Grafana添加爲Windows服務非常容易。

安裝插件:

使用git bash,輸入命令行:./grafana-cli.exe plugins install grafana-piechart-panel,其中grafana-piechart-panel 爲插件名字,可以去插件庫搜索得知。

安裝好之後,本地可以在data目錄下面看到 \data\plugins\grafana-piechart-panel,實際上就是把源碼(主要是js 和css)從GitHub(https://github.com/grafana/piechart-panel)下載到本地,然後Grafana就會從這個目錄去加載插件。而插件的主要功能就是展示酷炫的效果,這個正好是js,h5的強項。

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章