class Object
Public Instance Methods
download(url)
click to toggle source
@param [String] url @return [String] Data read from the URL
# File bin/ffmapquery, line 373 def download(url) data = nil Open3.popen2('wget', '-q', '-O', '-', '--connect-timeout=5', url) {| _, o, _ | o.binmode data = o.read } data end
hash_flatten(hash, sub = '')
click to toggle source
# File bin/ffmapquery, line 74 def hash_flatten(hash, sub = '') new_hash = Hash.new sub = sub + '.' if sub.length > 0 case hash when Array hash.each_index { |key| data = hash[key] key = sub + key.to_s case data when Hash new_hash.merge!(hash_flatten(data, key)) when Array new_hash.merge!(hash_flatten(data, key)) else new_hash[key] = data end } when Hash hash.each_pair { |key, data| key = sub + key.to_s case data when Hash new_hash.merge!(hash_flatten(data, key)) when Array new_hash.merge!(hash_flatten(data, key)) else new_hash[key] = data end } else raise Exception.new end new_hash end
main_loop()
click to toggle source
@return [nil]
# File bin/ffmapquery, line 393 def main_loop if($C[:interval_forced]) delay = ($next_update - Time.now).to_i sleep(delay) sleep(0.1) until Time.now > $next_update else sleep(Random.rand(10)) while Time.now < $next_update end begin # Timeout at 95% of the interval Timeout::timeout($C[:interval]*(0.95)) { begin alfred_json = time('download alfred_json') { download($C[:alfred_json]) } alfred = time('parsing alfred.json') { Alfred.new(alfred_json).hash } rescue alfred = Hash.new end graph_json = time('download graph_json') { download($C[:graph_json]) } links = time('calculating links') { Links.new(graph_json) } nodes_json = time('download nodes_json') { download($C[:nodes_json]) } time('calculating nodes_json') { NodesJson.new(nodes_json, links, alfred) } } rescue Timeout::Error $stderr.puts 'WARNING: Timeout triggered.' ensure begin $next_update = Time.now + $C[:interval] if $next_update <= Time.now rescue $next_update = Time.now + $C[:interval] end end puts "Next update: #{$next_update.to_s} (in #{($next_update - Time.now).to_i} seconds)" end
read_config(config_file)
click to toggle source
@param [String] config_file @return [nil]
# File bin/ffmapquery, line 338 def read_config(config_file) conf = File.readlines(config_file) comment = /^( |\t)*#/ options = 'alfred_json|graph_json|nodes_json|interval|interval_forced|influx_url|carbon_host|carbon_port|region|groups_dir|quiet'.split('|') options_required = 'graph_json|nodes_json|interval|influx_url|region'.split('|') conf.each { | line | next if line.match(comment) a, b = line.split('=', 2) a = a.strip.chomp.to_sym b = b.strip.chomp # Prüfen ob der Wert ein Integer ist und ggfs. konvertieren b = b.to_i if(b.to_i.to_s == b) b = true if(b.to_s == "true") b = true if(b.to_s == "yes") b = false if(b.to_s == "false") b = false if(b.to_s == "no") if options.include?(a.to_s) $C[a] = b else $stderr.puts("ERROR: Unbekannte Option: #{a}") exit end } $C.each_key { | option | options_required = options_required - [option.to_s] } if options_required.length > 0 puts "ERROR: Fehlende Optionen. Die folgenden Optionen fehlen in der Konfigurationsdatei: #{options_required.join(', ')}" Kernel.exit 1 end nil end
remove_unsafe_chars(text, r = '')
click to toggle source
Sollte alles entfernen, was potentiell gefährlich im Dateinamen ist… @param [String] text @param [String] r @return [String]
# File bin/ffmapquery, line 325 def remove_unsafe_chars(text, r = '') $remove_unsafe_chars_cache = Hash.new unless $remove_unsafe_chars_cache text = text.to_s orig = String.new(text) cache = $remove_unsafe_chars_cache[text] return cache if cache unsafe_chars = /[^-_0-9a-zA-Z]/ text[unsafe_chars] = r while text[unsafe_chars] String.new($remove_unsafe_chars_cache[orig] = text) end
time(description) { || ... }
click to toggle source
@param [String] description
# File bin/ffmapquery, line 383 def time(description) start = Time.now r = yield finish = Time.now diff = '%.3f' % (finish - start).to_f puts "#{description} took #{diff} seconds" r end