Mailinglist Archive: yast-commit (2114 mails)

< Previous Next >
[yast-commit] <rest-service> master : use web friendly ids for each metric.
  • From: Duncan Mac-Vicar P <dmacvicar@xxxxxxx>
  • Date: Tue, 22 Sep 2009 15:58:05 +0200
  • Message-id: <E1Mq5tz-0001ze-Ar@xxxxxxxxxxxxxxxx>
ref: refs/heads/master
commit 5de93e716db6ec16aac808ad785a734356c69a70
Author: Duncan Mac-Vicar P <dmacvicar@xxxxxxx>
Date: Tue Sep 22 15:58:05 2009 +0200

use web friendly ids for each metric.
sort the values by time in to_xml
fix start and stop intervals
---
.../status/app/controllers/metrics_controller.rb | 14 ++++++++-
plugins/status/app/models/metric.rb | 33 +++++++++++++------
plugins/status/test/unit/metric_test.rb | 6 ++--
3 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/plugins/status/app/controllers/metrics_controller.rb
b/plugins/status/app/controllers/metrics_controller.rb
index e20c94d..c699898 100644
--- a/plugins/status/app/controllers/metrics_controller.rb
+++ b/plugins/status/app/controllers/metrics_controller.rb
@@ -2,7 +2,19 @@ include ApplicationHelper

require 'scr'
require 'metric'
+require 'uri'

+#
+# Controller that exposes collectd metrics in a RESTful
+# way.
+#
+# GET /metrics returns all metrics (with no data)
+#
+# GET /metrics/id returns one metric.
+# As the id contains "/" you need to URI encode it.
+#
+# ie: /metrics/myhost.com%2Ffscache-Cookies%2Ffscache_stat-idx
+#
class MetricsController < ApplicationController
before_filter :login_required

@@ -90,7 +102,7 @@ class MetricsController < ApplicationController
def show
#permission_check("org.opensuse.yast.system.status.read")
begin
- @metric = Metric.find(CGI.unescape(params[:id]))
+ @metric = Metric.find(params[:id])
stop = params[:stop].blank? ? Time.now : Time.at(params[:stop].to_i)
start = params[:start].blank? ? stop - 300 : Time.at(params[:start].to_i)

diff --git a/plugins/status/app/models/metric.rb
b/plugins/status/app/models/metric.rb
index 41c92af..385dbf9 100644
--- a/plugins/status/app/models/metric.rb
+++ b/plugins/status/app/models/metric.rb
@@ -43,9 +43,10 @@ class Metric
attr_reader :plugin_full
attr_reader :type_full

- # alias for identifier
+ # like identifier, but to be used as a REST id
+ # so / are replaced by +
def id
- identifier
+ identifier.gsub("/", "+").gsub(".", "*")
end

def identifier
@@ -103,7 +104,7 @@ class Metric
ret[:exit].zero?
end

- #
+ # available plugins
def self.plugins
Metric.find(:all).map { |x| x.plugin }
end
@@ -149,12 +150,11 @@ class Metric
# parameter
when Hash then find_multiple(what.merge(opts))
when String
- raise "hello"
- find_multiple({:identifier => what})
+ find_multiple({:id => what}).first rescue nil
else nil
end
end
-
+
# find all values
def self.find_all
ret = []
@@ -208,12 +208,15 @@ class Metric
data_opts = {}
data_opts[:start] = opts[:start] if opts.has_key?(:start)
data_opts[:stop] = opts[:stop] if opts.has_key?(:stop)
+
+ Rails.logger.info "rendering metric #{id} from #{data_opts[:start].to_i}
to #{data_opts[:stop].to_i}"

xml = opts[:builder] ||= Builder::XmlMarkup.new(opts)
xml.instruct! unless opts[:skip_instruct]

xml.metric do
- xml.id identifier
+ xml.id id
+ xml.identifier identifier
xml.host host
xml.plugin plugin
xml.plugin_instance plugin_instance
@@ -229,7 +232,7 @@ class Metric
metric_data.each do |col, values|
next if col == "starttime"
next if col == "interval"
- xml.data(:column => col, :start => starttime.to_i, :interval =>
interval ) { values.each { |time, value| xml.value value } }
+ xml.data(:column => col, :start => starttime.to_i, :interval =>
interval ) { values.sort.each { |x| xml.comment!(x[0].to_i.to_s) if RAILS_ENV
== "development"; xml.value x[1] } }
end
end

@@ -245,9 +248,11 @@ class Metric
# default last 5 minutes from now
def self.run_rrdtool(file, opts={})
stop = opts.has_key?(:stop) ? opts[:stop] : Time.now
- start = opts.has_key?(:stop) ? opts[:stop] : stop - 300
-
- cmd = IO.popen("rrdtool fetch #{file} AVERAGE --start
#{start.strftime("%H:%M,%m/%d/%Y")} --end #{stop.strftime("%H:%M,%m/%d/%Y")}")

+ start = opts.has_key?(:start) ? opts[:start] : stop - 300
+
+ cmdline = "rrdtool fetch #{file} AVERAGE --start #{start.to_i} --end
#{stop.to_i}"
+ Rails.logger.debug "running #{cmdline}"
+ cmd = IO.popen(cmdline)
output = cmd.read
cmd.close

@@ -271,6 +276,7 @@ class Metric
times = []
labels = []
output.each_line do |line|
+ line.chomp!
line_count += 1
next if line.blank?

@@ -282,15 +288,20 @@ class Metric
next
end

+ #Rails.logger.info "--> '#{line}'"
+
time_str, values_str = line.split(":")
time = Time.at(time_str.to_i)

# store time to get the starttime and interval
times << time
+
+ #Rails.logger.info "--> '#{values_str}'"

values = values_str.split(" ").map {|x| x == "nan" ? nil : x.to_f}

values.each_with_index do |value, index|
+# Rails.logger.info "#{value} at #{index}"
label = labels[index]
result[label] = {} if not result.has_key?(label)
result[label][time] = value
diff --git a/plugins/status/test/unit/metric_test.rb
b/plugins/status/test/unit/metric_test.rb
index 7535ed2..6f57bc4 100644
--- a/plugins/status/test/unit/metric_test.rb
+++ b/plugins/status/test/unit/metric_test.rb
@@ -76,8 +76,8 @@ class MetricTest < ActiveSupport::TestCase

assert_equal expected, packets.data(:start => start, :stop => stop)

- xml = '<?xml version="1.0"
encoding="UTF-8"?><metric><id>myhost.domain.de/interface/packets</id><host>myhost.domain.de</host><plugin>interface</plugin><plugin_instance></plugin_instance><type>packets</type><type_instance></type_instance><data
interval="10" column="tx"
start="1252075500"><value></value><value>425.76</value><value>159.22</value><value>61.66</value><value>7.79</value><value></value></data><data
interval="10" column="rx"
start="1252075500"><value></value><value>280.69</value><value>339.62</value><value>258.14</value><value>22.15</value><value></value></data></metric>'
-
+ xml = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?><metric><id>myhost*domain*de+interface+packets</id><identifier>myhost.domain.de/interface/packets</identifier><host>myhost.domain.de</host><plugin>interface</plugin><plugin_instance></plugin_instance><type>packets</type><type_instance></type_instance><data
interval=\"10\" column=\"tx\"
start=\"1252075500\"><value></value><value>425.76</value><value>159.22</value><value>61.66</value><value>7.79</value><value></value></data><data
interval=\"10\" column=\"rx\"
start=\"1252075500\"><value></value><value>280.69</value><value>339.62</value><value>258.14</value><value>22.15</value><value></value></data></metric>"
+
assert_equal xml, packets.to_xml(:start => start, :stop => stop)
end

@@ -111,7 +111,7 @@ class MetricTest < ActiveSupport::TestCase

# test attributes
assert_equal '/var/lib/collectd/myhost.domain.de/memory/memory-free.rrd',
ret.first.path
- assert_equal 'myhost.domain.de/memory/memory-free', ret.first.id
+ assert_equal 'myhost.domain.de/memory/memory-free', ret.first.identifier

ret = Metric.find(:all, :plugin_full => /meemory/)
assert_equal 0, ret.size
--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages