Module: Slurry

Defined in:
lib/slurry.rb,
lib/slurry/graphite.rb

Overview

Author:

Defined Under Namespace

Classes: Graphite

Class Method Summary collapse

Class Method Details

.cleanObject

Dump clean out everything from redis



96
97
98
99
100
101
102
103
# File 'lib/slurry.rb', line 96

def clean
  r = Redis.new

  while r.llen('slurry') > 0 do
    r.rpop('slurry')
  end

end

.funnelObject

Reads from STDIN, expects json hash of just data. Creates new hash the original hash as the value of key :hash and the current unix time as key :time. Calls method pipe() with resulting hash. {

 :hash => { <hash read from STDIN> },
 :time => <current unix time> 
}


35
36
37
38
39
40
41
# File 'lib/slurry.rb', line 35

def funnel
  body = ''
  body += STDIN.read
  jsondata = JSON.parse(body)
  raise "jsondata is not of class Hash.  Is #{jsondata.class}" unless jsondata.is_a? Hash
  pipe(timestamp(jsondata))
end

.inspectObject



86
87
88
89
90
91
92
93
# File 'lib/slurry.rb', line 86

def inspect
  r = Redis.new

  data = Hash.new
  data = r.lrange("slurry", 0, -1)

  data
end

.liaise(server, port, wait = 0.01) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/slurry.rb', line 105

def liaise (server,port,wait=0.01)
  r = Redis.new

  loop do

    # Pull something off the list
    popped = r.brpop('slurry')
    d = JSON.parse(popped)

    raise "key 'data' not found in popped hash" unless d["data"]
    raise "key 'time' not found in popped hash" unless d["time"]

    # Convert the json into graphite useable data
    graphite = Json2Graphite.get_graphite(d["data"], d["time"])
    graphite.each do |d|
      target = d[:target].to_s
      value  = d[:value].to_s
      time   = d[:time].to_s
      puts [target,value,time].join(' ')
    end
    #sleep wait
  end

end

.pipe(hash) ⇒ Object

Receives a hash formatted like so

{:time=>1345411779,
:collectd=>
   {"myhostname"=>{"ntpd"=>-0.00142014}}}

Pushes data into redis



64
65
66
67
# File 'lib/slurry.rb', line 64

def pipe (hash)
  redis = Redis.new
  redis.lpush('slurry',hash.to_json)
end

.post(postconfig) ⇒ Object

Post the json received on STDIN to a webserver



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/slurry.rb', line 44

def post(postconfig)
  body = ''
  body += STDIN.read
  jsondata = JSON.parse(body)
  begin
    raise "jsondata is not of class Hash.  Is #{jsondata.class}" unless jsondata.is_a? Hash
    RestClient.post(postconfig[:url], jsondata.to_json, :content_type => :json, :accept => :json )
  rescue => e
    puts "something broke:"
    puts e.message
    puts e.backtrace.inspect
  end
end

.push_to_redis(data, time = Time.now.to_i) ⇒ Object



69
70
71
72
73
# File 'lib/slurry.rb', line 69

def push_to_redis (data, time=Time.now.to_i)
  hash = timestamp(data, time)
  r = Redis.new
  r.lpush('slurry', hash.to_json)
end

.reportObject

Report the contents of the redis server



76
77
78
79
80
81
82
83
84
# File 'lib/slurry.rb', line 76

def report
  r = Redis.new

  data = Hash.new
  data[:slurry] = Hash.new
  data[:slurry][:waiting] = r.llen('slurry')

  data
end

.runonce(server, port, wait = 0.1) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/slurry.rb', line 131

def runonce (server,port,wait=0.1)

  r = Redis.new           # open a new conection to redis
  report = Hash.new       # initialize the report
  report[:processed] = 0  # we've not processed any items yet

  # open a socket with the graphite server
  g = Slurry::Graphite.new(server,port)


  # process every object in the list called 'slurry'
  while r.llen('slurry') > 0 do

    # pop the next object from the list
    popped = r.rpop('slurry')
    d = JSON.parse(popped)

    # make syre the data we are about to use at least exists
    raise "key 'data' not found in popped hash" unless d["data"]
    raise "key 'time' not found in popped hash" unless d["time"]


    # convert the object we popped into a graphite object
    graphite = Json2Graphite.get_graphite(d["data"], d["time"])

    # break the graphite object down into useable bits
    graphite.each do |d|
      # Make use of the values in the object
      target = d[:target].to_s
      value  = d[:value].to_s
      time   = d[:time].to_s

      # push the data to the open graphite socket
      g.send(target,value, time)
      # record the transaction
      report[:processed] += 1
    end
    #sleep wait
  end
  # close up the connection to graphite
  g.close

  # return the report in json format
  report.to_json

end

.timestamp(hash, time = Time.now.to_i) ⇒ Object

Wraps received hash in new hash with timestamp applied.

Parameters:

  • hash (Hash)


20
21
22
23
24
25
# File 'lib/slurry.rb', line 20

def timestamp (hash, time=Time.now.to_i)
  data = Hash.new
  data[:data] = hash
  data[:time] = time
  data
end