喂數據給graphite

   初學graphite,我想把統計的連接數以圖的形式展現出來,由於連接數的種類的不止一種,所以不能用簡單的nc發送,需要發送給carbon。

參考官網http://graphite.readthedocs.org/en/latest/feeding-carbon.html

改了下example-client.py

import sys
import time
import os
import subprocess
from socket import socket
CARBON_SERVER = '127.0.0.1'
CARBON_PORT = 2003
delay = 60
if len(sys.argv) > 1:
  delay = int( sys.argv[1] )
def netstat():
    command = "netstat -a|awk '/tcp/{++s[$NF]}END{for (i in s)print i,s[i]}'"
    process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
    os.waitpid(process.pid, 0)
    output = process.stdout.read().replace(',', ' ').strip().split()
    return output
sock = socket()
try:
  sock.connect( (CARBON_SERVER,CARBON_PORT) )
except:
  print "Couldn't connect to %(server)s on port %(port)d, is carbon-agent.py running?" % { 'server':CARBON_SERVER, 'port':CARBON_PORT }
  sys.exit(1)
while True:
  now = int( time.time() )
  lines = []
  #We're gonna report all three loadavg values
  net = netstat()
  lines.append("netstat.%s %s %d" % (net[0],net[1],now))
  lines.append("netstat.%s %s %d" % (net[2],net[3],now))
  lines.append("netstat.%s %s %d" % (net[4],net[5],now))
  message = '\n'.join(lines) + '\n' #all lines must end in a newline
  print "sending message\n"
  print '-' * 80
  print message
  print
  sock.sendall(message)
  time.sleep(delay)

代碼中"netstat.%s %s %d" 爲schema的命名方案。

再編輯storage-schemas.conf,加入以下內容

[netstat]
pattern = ^netstat\.
retentions = 3s:1d

pattern 是一個正則表達式,它將匹配以netstat開頭的數據。

retentions 我的理解是表示每個數據點保存3s,這樣的數據點一共保存1天

參考http://graphite.readthedocs.org/en/latest/config-carbon.html

最後啓動carbon-cache.py

執行example-client.py 就實現了我要的效果


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