Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #REQUIRES THE FOLLOWING ENVIRONMENT VARIABLES: APPD_USER, APPD_PASS
- require "rest-client"
- require "json"
- require "date"
- require "active_support/all"
- class AppD
- def get_time(datapoint)
- time = datapoint[:startTimeInMillis] || ""
- return time
- end
- def get_value(datapoint)
- value = datapoint[:value] || 0
- return value.round(2)
- end
- def query(metric_path, since=nil)
- @APPD_METRIC_URL = "<your AppDynamics url>"
- since ||= 3
- start_millis = (Time.now - (since*60*60)).to_f * 1000.0
- query_params = {
- "metric-path": metric_path,
- "time-range-type": "BETWEEN_TIMES",
- "start-time": start_millis.floor,
- "end-time": (Time.now.to_f * 1000.0).floor,
- "rollup": "false",
- "output": "json"
- }
- url = "#{@APPD_METRIC_URL}?#{query_params.to_param}"
- response = RestClient::Request.execute method: :get, url: url, user: "#{ENV["APPD_USER"]}@<your account>", password: ENV["APPD_PASS"]
- result = JSON.parse(response.body, :symbolize_names => true)
- return result.first
- end
- def total_since(name, since=nil)
- stats = query name, since
- total = stats[:metricValues].select{|el| not (el.nil? or el[:value].nil?)}.map{|e| get_value(e)}.sum
- return total
- end
- def points(name, since=nil)
- stats = query name, since
- points = []
- if stats.key?(:metricValues)
- datapoints = stats[:metricValues]
- (datapoints.select { |el| not el[0].nil? }).each do|item|
- points << { x: get_time(item), y: get_value(item)}
- end
- end
- return points
- end
- def value(name, since=nil)
- stats = query name, since
- last = (stats[:metricValues].select { |el| not (el.nil? or el[:value].nil?) }).last
- return get_value(last)
- end
- end
Add Comment
Please, Sign In to add comment