A small, lightweight and extensible DynDNS server written with Ruby and Rack.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

textfile_reporter.rb 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Adapted from https://github.com/eric/metriks-graphite/blob/master/lib/metriks/reporter/graphite.rb
  2. require 'metriks'
  3. module Dyndnsd
  4. class TextfileReporter
  5. attr_reader :file
  6. def initialize(file, options = {})
  7. @file = file
  8. @prefix = options[:prefix]
  9. @registry = options[:registry] || Metriks::Registry.default
  10. @interval = options[:interval] || 60
  11. @on_error = options[:on_error] || proc { |ex| }
  12. end
  13. def start
  14. @thread ||= Thread.new do
  15. loop do
  16. sleep @interval
  17. Thread.new do
  18. begin
  19. write
  20. rescue StandardError => e
  21. @on_error[e] rescue nil
  22. end
  23. end
  24. end
  25. end
  26. end
  27. def stop
  28. @thread&.kill
  29. @thread = nil
  30. end
  31. def restart
  32. stop
  33. start
  34. end
  35. def write
  36. File.open(@file, 'w') do |f|
  37. @registry.each do |name, metric|
  38. case metric
  39. when Metriks::Meter
  40. write_metric f, name, metric, [
  41. :count, :one_minute_rate, :five_minute_rate,
  42. :fifteen_minute_rate, :mean_rate
  43. ]
  44. when Metriks::Counter
  45. write_metric f, name, metric, [
  46. :count
  47. ]
  48. when Metriks::UtilizationTimer
  49. write_metric f, name, metric, [
  50. :count, :one_minute_rate, :five_minute_rate,
  51. :fifteen_minute_rate, :mean_rate,
  52. :min, :max, :mean, :stddev,
  53. :one_minute_utilization, :five_minute_utilization,
  54. :fifteen_minute_utilization, :mean_utilization
  55. ], [
  56. :median, :get_95th_percentile
  57. ]
  58. when Metriks::Timer
  59. write_metric f, name, metric, [
  60. :count, :one_minute_rate, :five_minute_rate,
  61. :fifteen_minute_rate, :mean_rate,
  62. :min, :max, :mean, :stddev
  63. ], [
  64. :median, :get_95th_percentile
  65. ]
  66. when Metriks::Histogram
  67. write_metric f, name, metric, [
  68. :count, :min, :max, :mean, :stddev
  69. ], [
  70. :median, :get_95th_percentile
  71. ]
  72. end
  73. end
  74. end
  75. end
  76. def write_metric(file, base_name, metric, keys, snapshot_keys = [])
  77. time = Time.now.to_i
  78. base_name = base_name.to_s.gsub(/ +/, '_')
  79. base_name = "#{@prefix}.#{base_name}" if @prefix
  80. keys.flatten.each do |key|
  81. name = key.to_s.gsub(/^get_/, '')
  82. value = metric.send(key)
  83. file.write("#{base_name}.#{name} #{value} #{time}\n")
  84. end
  85. unless snapshot_keys.empty?
  86. snapshot = metric.snapshot
  87. snapshot_keys.flatten.each do |key|
  88. name = key.to_s.gsub(/^get_/, '')
  89. value = snapshot.send(key)
  90. file.write("#{base_name}.#{name} #{value} #{time}\n")
  91. end
  92. end
  93. end
  94. end
  95. end