Module: TelemetryDaemon

Defined in:
lib/telemetry/daemon_config_parser.rb

Constant Summary collapse

@@flows_expire_in =
nil
@@begin_interval =

Code blocks users can call at the beginning and end of each interval

nil
@@end_interval =
nil

Instance Method Summary collapse

Instance Method Details

#api_host(api_host) ⇒ Object



5
6
7
# File 'lib/telemetry/daemon_config_parser.rb', line 5

def api_host(api_host)
	Telemetry.api_host = api_host
end

#api_token(token) ⇒ Object



9
10
11
# File 'lib/telemetry/daemon_config_parser.rb', line 9

def api_token(token)
	Telemetry.token = token
end

#barchart(tag, frequency = 0, offset = nil, &block) ⇒ Object



66
67
68
69
# File 'lib/telemetry/daemon_config_parser.rb', line 66

def barchart(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :barchart, tag, frequency, offset, block ]
end

#begin_interval(&block) ⇒ Object



50
51
52
# File 'lib/telemetry/daemon_config_parser.rb', line 50

def begin_interval(&block)
	@@begin_interval = block
end

#box(tag, frequency = 0, offset = nil, &block) ⇒ Object



76
77
78
79
# File 'lib/telemetry/daemon_config_parser.rb', line 76

def box(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :countdown, tag, frequency, offset, block ]
end

#bulletchart(tag, frequency = 0, offset = nil, &block) ⇒ Object



71
72
73
74
# File 'lib/telemetry/daemon_config_parser.rb', line 71

def bulletchart(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :value, tag, frequency, offset, block ]
end

#clock(tag, frequency = 0, offset = nil, &block) ⇒ Object



81
82
83
84
# File 'lib/telemetry/daemon_config_parser.rb', line 81

def clock(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :countdown, tag, frequency, offset, block ]
end

#compass(tag, frequency = 0, offset = nil, &block) ⇒ Object



91
92
93
94
# File 'lib/telemetry/daemon_config_parser.rb', line 91

def compass(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :countdown, tag, frequency, offset, block ]
end

#countdown(tag, frequency = 0, offset = nil, &block) ⇒ Object



86
87
88
89
# File 'lib/telemetry/daemon_config_parser.rb', line 86

def countdown(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :countdown, tag, frequency, offset, block ]
end

#custom(tag, frequency = 0, offset = nil, &block) ⇒ Object



96
97
98
99
# File 'lib/telemetry/daemon_config_parser.rb', line 96

def custom(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :custom, tag, frequency, offset, block ]
end

#end_interval(&block) ⇒ Object



58
59
60
# File 'lib/telemetry/daemon_config_parser.rb', line 58

def end_interval(&block)
	@@end_interval = block
end

#flows_expire_in(i) ⇒ Object



26
27
28
# File 'lib/telemetry/daemon_config_parser.rb', line 26

def flows_expire_in(i)
	@@flows_expire_in = i
end

#funnelchart(tag, frequency = 0, offset = nil, &block) ⇒ Object



101
102
103
104
# File 'lib/telemetry/daemon_config_parser.rb', line 101

def funnelchart(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :funnelchart, tag, frequency, offset, block ]
end

#gauge(tag, frequency = 0, offset = nil, &block) ⇒ Object



106
107
108
109
# File 'lib/telemetry/daemon_config_parser.rb', line 106

def gauge(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :gauge, tag, frequency, offset, block ]
end

#graph(tag, frequency = 0, offset = nil, &block) ⇒ Object



111
112
113
114
# File 'lib/telemetry/daemon_config_parser.rb', line 111

def graph(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :graph, tag, frequency, offset, block ]
end

#grid(tag, frequency = 0, offset = nil, &block) ⇒ Object



116
117
118
119
# File 'lib/telemetry/daemon_config_parser.rb', line 116

def grid(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :gid, tag, frequency, offset, block ]
end

#histogram(tag, frequency = 0, offset = nil, &block) ⇒ Object



121
122
123
124
# File 'lib/telemetry/daemon_config_parser.rb', line 121

def histogram(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :histogram, tag, frequency, offset, block ]
end

#icon(tag, frequency = 0, offset = nil, &block) ⇒ Object



126
127
128
129
# File 'lib/telemetry/daemon_config_parser.rb', line 126

def icon(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :icon, tag, frequency, offset, block ]
end

#iframe(tag, frequency = 0, offset = nil, &block) ⇒ Object



136
137
138
139
# File 'lib/telemetry/daemon_config_parser.rb', line 136

def iframe(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :iframe, tag, frequency, offset, block ]
end

#image(tag, frequency = 0, offset = nil, &block) ⇒ Object



131
132
133
134
# File 'lib/telemetry/daemon_config_parser.rb', line 131

def image(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :image, tag, frequency, offset, block ]
end

#interval(interval) ⇒ Object



21
22
23
# File 'lib/telemetry/daemon_config_parser.rb', line 21

def interval(interval)
	@@interval = interval
end

#log(tag, frequency = 0, offset = nil, &block) ⇒ Object



141
142
143
144
# File 'lib/telemetry/daemon_config_parser.rb', line 141

def log(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :log, tag, frequency, offset, block ]
end

#log_level(log_level) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/telemetry/daemon_config_parser.rb', line 13

def log_level(log_level)
	level = log_level.to_s
	if log_level == 'debug'
		Telemetry.logger.level = Logger::DEBUG
		Telemetry.logger.debug "Starting Debug Logging" 
	end
end

#map(tag, frequency = 0, offset = nil, &block) ⇒ Object



146
147
148
149
# File 'lib/telemetry/daemon_config_parser.rb', line 146

def map(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :map, tag, frequency, offset, block ]
end

#multigauge(tag, frequency = 0, offset = nil, &block) ⇒ Object



151
152
153
154
# File 'lib/telemetry/daemon_config_parser.rb', line 151

def multigauge(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :multigauge, tag, frequency, offset, block ]
end

#multivalue(tag, frequency = 0, offset = nil, &block) ⇒ Object



156
157
158
159
# File 'lib/telemetry/daemon_config_parser.rb', line 156

def multivalue(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :multivalue, tag, frequency, offset, block ]
end

#piechart(tag, frequency = 0, offset = nil, &block) ⇒ Object



161
162
163
164
# File 'lib/telemetry/daemon_config_parser.rb', line 161

def piechart(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :piechart, tag, frequency, offset, block ]
end

#run_begin_intervalObject



54
55
56
# File 'lib/telemetry/daemon_config_parser.rb', line 54

def run_begin_interval
	@@begin_interval.yield if @@begin_interval
end

#run_end_intervalObject



62
63
64
# File 'lib/telemetry/daemon_config_parser.rb', line 62

def run_end_interval
	@@end_interval.yield if @@end_interval
end

#run_scheduled_flow_updatesObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/telemetry/daemon_config_parser.rb', line 236

def run_scheduled_flow_updates
	@@buffer = {}
	@@tasks ||= {}
	@@next_run_at ||= {}

	@@tasks.each do |task| 
		@@h = {}
		variant, tag, frequency, offset, block = task
		now = Time.now


		# Check whether we should wait an interval before running
		if frequency > 0 
			Telemetry::logger.debug "Task #{task[0]} #{task[1]} (every #{task[2]}s)"
			#Telemetry::logger.debug "Update frequency is #{frequency} now #{Time.now.to_i} next #{@@next_run_at[tag]}"
			if @@next_run_at[tag] && @@next_run_at[tag] > now.to_i
				Telemetry::logger.debug "  - Not scheduled yet (waiting #{-(now.to_i - @@next_run_at[tag])}s)"
				next
			end
			@@next_run_at[tag] = now.to_i + frequency

			Telemetry::logger.debug "  - Running intermittant task at #{now}"

	    # If an offset is defined then align runtimes to the offset
	    # How close you can get to the desired offset depends on the global interval. So set it relatively small
	    # when using this feature
			if offset and offset >= 0 and offset <= 86400
				this_morning = Time.new(now.year, now.month, now.day).to_i
				time_since_offset = now.to_i - this_morning - offset
				time_since_offset += 86400 if time_since_offset < 0

				@@next_run_at[tag] -= time_since_offset % frequency
				#Telemetry::logger.debug "#{now.to_i} #{@@next_run_at[tag]}"
			end
		else
			Telemetry::logger.debug "  - Task #{task[0]} #{task[1]}"
		end

		# Execute the flow
		Telemetry.logger.debug "  + Executing task #{task[0]} #{task[1]}"
		block.yield

		if @@h == {}
			Telemetry.logger.debug "  - Skipping empty task values #{task[0]} #{task[1]}"
			next
		end

     # Use global default to set expires_at field if necessary
		if @@flows_expire_in and not @@h[:expires_at]
       set expires_at: Time.now.to_i + @@flows_expire_in
     end

		# Append the variant
		values = @@h.merge({variant: variant})

		# Telemetry.logger.debug "  - Values for #{task[0]} #{task[1]}:\n#{values}\n#{@@last_values[tag]}"

		# Telemetry.logger.debug "LV\n\n #{@@last_values}\n\n"

		@@buffer[tag] = values
	end

	@@buffer
end

#scatterplot(tag, frequency = 0, offset = nil, &block) ⇒ Object



166
167
168
169
# File 'lib/telemetry/daemon_config_parser.rb', line 166

def scatterplot(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :scatterplot, tag, frequency, offset, block ]
end

#servers(tag, frequency = 0, offset = nil, &block) ⇒ Object



171
172
173
174
# File 'lib/telemetry/daemon_config_parser.rb', line 171

def servers(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :servers, tag, frequency, offset, block ]
end

#set(x) ⇒ Object



42
43
44
# File 'lib/telemetry/daemon_config_parser.rb', line 42

def set(x)
	x.each {|k,v| @@h[k.to_sym] = v}
end

#set_json(x) ⇒ Object



38
39
40
# File 'lib/telemetry/daemon_config_parser.rb', line 38

def set_json(x)
	x.each {|k,v| @@h[k.to_sym] = MultiJson.load(v)}
end

#status(tag, frequency = 0, offset = nil, &block) ⇒ Object



176
177
178
179
# File 'lib/telemetry/daemon_config_parser.rb', line 176

def status(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :servers, tag, frequency, offset, block ]
end

#table(tag, frequency = 0, offset = nil, &block) ⇒ Object



181
182
183
184
# File 'lib/telemetry/daemon_config_parser.rb', line 181

def table(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :table, tag, frequency, offset, block ]
end

#text(tag, frequency = 0, offset = nil, &block) ⇒ Object



186
187
188
189
# File 'lib/telemetry/daemon_config_parser.rb', line 186

def text(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :text, tag, frequency, offset, block ]
end

#tickertape(tag, frequency = 0, offset = nil, &block) ⇒ Object



191
192
193
194
# File 'lib/telemetry/daemon_config_parser.rb', line 191

def tickertape(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :tickertape, tag, frequency, offset, block ]
end

#timechart(tag, frequency = 0, offset = nil, &block) ⇒ Object



196
197
198
199
# File 'lib/telemetry/daemon_config_parser.rb', line 196

def timechart(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :timechart, tag, frequency, offset, block ]
end

#timeline(tag, frequency = 0, offset = nil, &block) ⇒ Object



201
202
203
204
# File 'lib/telemetry/daemon_config_parser.rb', line 201

def timeline(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :timeline, tag, frequency, offset, block ]
end

#timeseries(tag, frequency = 0, offset = nil, &block) ⇒ Object



206
207
208
209
# File 'lib/telemetry/daemon_config_parser.rb', line 206

def timeseries(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :timeseries, tag, frequency, offset, block ]
end

#upstatus(tag, frequency = 0, offset = nil, &block) ⇒ Object



211
212
213
214
# File 'lib/telemetry/daemon_config_parser.rb', line 211

def upstatus(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :upstatus, tag, frequency, offset, block ]
end

#value(tag, frequency = 0, offset = nil, &block) ⇒ Object



216
217
218
219
# File 'lib/telemetry/daemon_config_parser.rb', line 216

def value(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :value, tag, frequency, offset, block ]
end

#video(tag, frequency = 0, offset = nil, &block) ⇒ Object



221
222
223
224
# File 'lib/telemetry/daemon_config_parser.rb', line 221

def video(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :value, tag, frequency, offset, block ]
end

#wait_for_interval_from(timestamp) ⇒ Object

Ensure a minimum of 1 second between loops



31
32
33
34
35
36
# File 'lib/telemetry/daemon_config_parser.rb', line 31

def wait_for_interval_from(timestamp)
	@@interval ||= 60
	@@interval = 1.0 if @@interval < 1
	sleep_length = timestamp + @@interval.to_f - Time.now.to_f
	sleep sleep_length if sleep_length > 0
end

#waterfall(tag, frequency = 0, offset = nil, &block) ⇒ Object



226
227
228
229
# File 'lib/telemetry/daemon_config_parser.rb', line 226

def waterfall(tag, frequency = 0, offset=nil, &block)
	@@tasks ||= []
	@@tasks << [ :waterfall, tag, frequency, offset, block ]
end

#weather(tag, frequency = 0, offset = nil, &block) ⇒ Object



231
232
233
234
# File 'lib/telemetry/daemon_config_parser.rb', line 231

def weather(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :countdown, tag, frequency, offset, block ]
end