日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用graphite和grafana进行应用程序监控

發布時間:2024/9/21 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用graphite和grafana进行应用程序监控 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

graphite+grafana 介紹

grafana,按照官方的說法是 Beautiful metric & analytic dashboards。grafana 負責數據的展示,可以配置各種不同的數據源,其中包括 graphite。

graphite 包含多個模塊,這里我們使用的模塊包括:

  • Whisper:固定大小的數據庫,存儲方式類似RRD (round-robin-database),用來存儲收集到的 metrics
  • Carbon:metrics 接收服務,接收到 metrics 以后調用 Whisper 進行存儲
  • graphite-api:WSGI webapp 接口服務,grafana 在需要展現數據的時候使用其提供的 REST API 進行數據的獲取

本文的搭建的監控系統結構如下:

在本文檔中,我們會盡量將相關文件安裝在/opt/graphite目錄

準備Python 2.7 環境

對于某些默認Python環境不是2.7的系統,需要安裝Python2.7。

從源碼編譯Python2.7

configure make make install

創建Python2.7的virtualenv環境

virtualenv /opt/graphite --python=/usr/local/bin/python

加載virtualenv環境

source /opt/graphite/bin/activate

安裝Carbon+Whisper

pip install carbon --install-option="--prefix=/opt/graphite" --install-option="--install-lib=/opt/graphite/lib" pip install whisper

使用默認的配置文件:

cp /opt/graphite/conf/storage-schemas.conf.example /opt/graphite/conf/storage-schemas.conf cp /opt/graphite/conf/carbon.conf.example /opt/graphite/conf/carbon.conf

啟動 Carbon

/opt/graphite/bin/carbon-cache.py start

carbon的文件目錄在配置文件 /opt/graphite/conf/carbon.conf 中進行定義,下面是默認的配置:

LOCAL_DATA_DIR = /opt/graphite/storage/whisper/

安裝graphite-api

yum install libffi-devel pip install graphite-api --install-option="--prefix=/opt/graphite"

使用nginx+uwsgi的方式部署graphite-api

首先安裝uwsgi

pip install uwsgi

創建graphite-api的配置文件:/opt/graphite/etc/graphite-api.yml

search_index: /opt/graphite/index finders:- graphite_api.finders.whisper.WhisperFinder functions:- graphite_api.functions.SeriesFunctions- graphite_api.functions.PieFunctions whisper:directories:- /opt/graphite/storage/whisper carbon:hosts:- 127.0.0.1:7002timeout: 1retry_delay: 15carbon_prefix: carbonreplication_factor: 1

在這個配置文件中,whisper的數據路徑配置為/opt/graphite/storage/whisper,這個是在carbon配置文件 /opt/graphite/conf/carbon.conf 中使用配置項LOCAL_DATA_DIR進行定義的。

centos中沒有uwsgi的package,需要自行下載相關的啟動腳本,這里使用 https://github.com/jgoldschrafe/rpm-uwsgi

wget -O /etc/init.d/uwsgi https://raw.githubusercontent.com/jgoldschrafe/rpm-uwsgi/master/SOURCES/uwsgi.init chmod +x /etc/init.d/uwsgi

編輯文件 /etc/init.d/uwsgi 進行目錄配置

uwsgi="/opt/graphite/bin/uwsgi" prog=$(basename "$uwsgi") UWSGI_CONF_DIR="/etc/uwsgi" UWSGI_LOG_DIR="/var/log/uwsgi" PIDFILE_DIR="/var/run/uwsgi"

創建文件/etc/uwsgi/graphite-api.ini

[uwsgi] processes = 2 socket = 0.0.0.0:5000 module = graphite_api.app:app home = /opt/graphite env = GRAPHITE_API_CONFIG=/opt/graphite/conf/graphite-api.yml

啟動uwsgi

service uwsgi start

啟動以后會監聽5000端口,注意這里5000端口是uwsgi協議,需要后面配置nginx進行代理。

server {listen 81;location / {include uwsgi_params;uwsgi_pass localhost:5000;} }

nginx啟動后可以訪問洗面的鏈接檢查數據返回是否正常

http://127.0.0.1:81/render?target=test.metric

向監控系統中寫入數據

可以使用多種個方式向監控系統中寫入數據,例如 dropwizard metrics。這里為了方便使用Python進行數據上報:

import socket import time CARBON_SERVER = 'xxx.xxx.xxx.xxx' CARBON_PORT = 2003for k in xrange(100000):sock = socket.socket()sock.connect((CARBON_SERVER, CARBON_PORT))message = "test.meter.qps %d %d\n" % (k % 10, int(time.time()))print messagesock.sendall(message)sock.close()time.sleep(5)

程序運行以后可以在carbon的數據目錄中會發現如下的文件結構:

test|-- metric.wsp|-- meter| |-- qps.wsp

配置grafana展現metric

首先配置grafana使用graphite作為數據源

配置數據顯示:

歷史數據處理

對于監控系統,長期運行以后必然會積攢大量的歷史數據,whisper 通過配置數據保存的時間段以及在時間短內每間隔多長時間保存一條數據來解決歷史數據問題。

配置文件 /opt/graphite/conf/storage-schemas.conf 中描述了上述信息:

[default] pattern = .* retentions = 1m:30d,1h:1y

這個配置中,30天內的數據每間隔1分鐘保存一條數據,30天-1年之間的數據,每個小時保存一條數據。

由于 whisper 是一個固定大小的數據庫,所以當 storage-schemas.conf 設定以后,一個metrics所占的磁盤空間就已經確定了。在系統運行的周期中只要根據 metrics 的增加進行適當擴容即可。

注意:storage-schemas.conf修改以后對于已經在磁盤上進行記錄的Metrics不會生效,需要刪除數據重新寫入或者進行數據遷移才行。

轉載于:https://my.oschina.net/u/575122/blog/791745

總結

以上是生活随笔為你收集整理的使用graphite和grafana进行应用程序监控的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。