Class: Circonus::Values
- Inherits:
-
Object
- Object
- Circonus::Values
- Defined in:
- lib/circonus/values.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#raise_errors ⇒ Object
Returns the value of attribute raise_errors.
Instance Method Summary collapse
-
#_first_valid_datapoint(data) ⇒ Object
Find the first valid data point in our set.
- #eval_composite(composite, values) ⇒ Object
- #eval_composites(uuid) ⇒ Object
- #eval_formula(formula, values = []) ⇒ Object
-
#graph_data(uuid, t_start = nil, t_end = nil) ⇒ Object
Get the range of data values from start to end time (t_start and t_end should be Time class vars).
-
#initialize(username, password, account = '') ⇒ Values
constructor
A new instance of Values.
-
#last_graph_data(uuid) ⇒ Object
Convenience function …
-
#login ⇒ Object
You need to call login before doing anything …
-
#metric_value(checkid, metric_name) ⇒ Object
Get the value of a particular metric name.
-
#total_last_graph_data(uuid) ⇒ Object
Get the sum of the last valid graph point.
Constructor Details
#initialize(username, password, account = '') ⇒ Values
Returns a new instance of Values.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/circonus/values.rb', line 22 def initialize(username,password,account='') @username = username @password = password @cookie = '' @debug = true @host = 'circonus.com' @login_host = "login.circonus.com" @account = account @data_host = "#{@account}.circonus.com" @raise_errors = false @data_prefix = "/json/graph/data/" @metrics_prefix = "/account/#{@account}/json/metrics/value" @headers = { "X-Circonus-Auth-Token" => @apitoken, "X-Circonus-App-Name" => @appname, "Accept" => 'application/json' } @url_v1_prefix = "https://circonus.com/api/json/" @url_prefix = "https://api.circonus.com/v2/" end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
20 21 22 |
# File 'lib/circonus/values.rb', line 20 def debug @debug end |
#raise_errors ⇒ Object
Returns the value of attribute raise_errors.
19 20 21 |
# File 'lib/circonus/values.rb', line 19 def raise_errors @raise_errors end |
Instance Method Details
#_first_valid_datapoint(data) ⇒ Object
Find the first valid data point in our set
104 105 106 107 108 109 110 111 |
# File 'lib/circonus/values.rb', line 104 def _first_valid_datapoint(data) return nil if data.nil? or not data.any? begin data.reverse.select { |s| not s[1].nil? }.first[1] rescue Exception => e return nil end end |
#eval_composite(composite, values) ⇒ Object
137 138 139 |
# File 'lib/circonus/values.rb', line 137 def eval_composite(composite,values) return eval_formula(composite['reconnoiter_source_expression'],values) end |
#eval_composites(uuid) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/circonus/values.rb', line 141 def eval_composites(uuid) data = last_graph_data(uuid) values = [] composites = [] data['data'].each do |d| if d['metric_type'] == 'numeric' values << _first_valid_datapoint(d['data']).to_f elsif d['metric_type'] == 'composite' composites << d end end composites.each do |composite| composite['result'] = eval_composite(composite,values) end return composites end |
#eval_formula(formula, values = []) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/circonus/values.rb', line 124 def eval_formula(formula,values=[]) formula = formula.clone formula.tr!('^A-Za-z0-9/+.*_)(-','' ) # prevent injection formula.tr!('A-Z','a-z') formula.gsub!(/[a-z]+/) { |n| "var_#{n}" } # prevent clobbering of ruby keywords evalstr = "" ('a'..'zzzz').each_with_index do |x,i| break if i == values.length evalstr += "var_#{x}=#{values[i].to_f.to_s}\n" # force an s->i->s conversion to prevent injection in the values end results = eval "#{evalstr}\n#{formula}\n" end |
#graph_data(uuid, t_start = nil, t_end = nil) ⇒ Object
Get the range of data values from start to end time (t_start and t_end should be Time class vars)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/circonus/values.rb', line 75 def graph_data(uuid,t_start=nil,t_end=nil) http = Net::HTTP.new(@data_host, 443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE # set sane defaults t_end = Time.new if t_end.nil? t_start = (t_end - 300) if t_start.nil? # convert to strings s_t_mid = (t_start + ((t_end - t_start) / 2)).strftime('%s') s_t_start = t_start.strftime('%s') s_t_end = t_end.strftime('%s') uuid = uuid.split('/').last # make sure we don't have /graph in the id.... path = "#{@data_prefix}#{uuid}?start=#{s_t_start}000&end=#{s_t_end}000cnt=&type=×=epoch_ms&_=#{s_t_mid}000" headers = { 'Cookie' => @cookie, 'Accept' => 'application/application/json, text/javascript, */*; q=0.01' } resp = http.get(path, headers) return Yajl::Parser.parse(resp.body) end |
#last_graph_data(uuid) ⇒ Object
Convenience function … get the last graph data points (We use 300 seconds to make sure we at least have something.…)
98 99 100 101 |
# File 'lib/circonus/values.rb', line 98 def last_graph_data(uuid) t = Time.new return graph_data(uuid,t - 300, t) end |
#login ⇒ Object
You need to call login before doing anything … This gives us our session id
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/circonus/values.rb', line 45 def login() http = Net::HTTP.new(@login_host, 443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE path = '/login' headers = { 'Content-Type' => 'application/x-www-form-urlencoded' } data="login_username=#{CGI.escape @username}&login_password=#{CGI.escape @password}&login_remember=1&whereTo=https://#{@account}.circonus.com%2Flogin&&login_submit=Sign+In+%BB&welcome_submit=Sign+In+%BB" resp = http.post(path, data, headers) @cookie = resp.response['set-cookie'].split('; ')[0] return true end |
#metric_value(checkid, metric_name) ⇒ Object
Get the value of a particular metric name
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/circonus/values.rb', line 61 def metric_value(checkid,metric_name) http = Net::HTTP.new(@data_host, 443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE path = "#{@metrics_prefix}?check_id=#{CGI.escape checkid.to_s}&metric_name=#{CGI.escape metric_name}" headers = { 'Cookie' => @cookie, 'Accept' => 'application/application/json, text/javascript, */*; q=0.01' } resp = http.get(path, headers) return Yajl::Parser.parse(resp.body) end |
#total_last_graph_data(uuid) ⇒ Object
Get the sum of the last valid graph point
114 115 116 117 118 119 120 121 122 |
# File 'lib/circonus/values.rb', line 114 def total_last_graph_data(uuid) data = last_graph_data(uuid) sum = 0 data['data'].each do |d| next unless d['metric_type'] == 'numeric' sum += _first_valid_datapoint(d['data']).to_f end return sum end |