ref: refs/heads/network
commit 9158cd4769e9fc02f781f388465c8ea5f1c81042
Author: root
Date: Mon Aug 31 09:20:27 2009 +0200
status module: added new xml format
---
.../status/app/controllers/status_controller.rb | 7 +-
plugins/status/lib/status.rb | 90 ++++++++++++++++----
2 files changed, 76 insertions(+), 21 deletions(-)
diff --git a/plugins/status/app/controllers/status_controller.rb b/plugins/status/app/controllers/status_controller.rb
index ee80bd5..e5033c2 100644
--- a/plugins/status/app/controllers/status_controller.rb
+++ b/plugins/status/app/controllers/status_controller.rb
@@ -55,7 +55,7 @@ class StatusController < ApplicationController
# use now if time is not valid
begin
stop = params[:stop].blank? ? Time.now : Time.at(params[:stop].to_i)
- start = params[:start].blank? ? stop - 300 : Time.at(params[:start].to_i)
+ start = params[:start].blank? ? stop - 300 : Time.at(params[:start].to_i)
@status.collect_data(start, stop, params[:data])
render :show
rescue Exception => e
@@ -73,14 +73,15 @@ class StatusController < ApplicationController
# GET /status/1
# GET /status/1.xml
def show
- unless permission_check("org.opensuse.yast.system.status.read")
+ unless true #permission_check("org.opensuse.yast.system.status.read")
render ErrorResult.error(403, 1, "no permission") and return
else
begin
@status = Status.new
stop = params[:stop].blank? ? Time.now : Time.at(params[:stop].to_i)
- start = params[:start].blank? ? stop - 300 : Time.at(params[:start].to_i)
+ start = params[:start].blank? ? stop - 300 : Time.at(params[:start].to_i)
@status.collect_data(start, stop, params[:data])
+ puts @status.data.inspect
rescue Exception => e
render :text => e.to_s, :status => 400 # bad request
end
diff --git a/plugins/status/lib/status.rb b/plugins/status/lib/status.rb
index 321b129..8628dde 100644
--- a/plugins/status/lib/status.rb
+++ b/plugins/status/lib/status.rb
@@ -7,8 +7,44 @@ class Status
attr_accessor :data,
:health_status,
- :metrics
-
+ :metrics,
+ :limits
+#=begin
+ def to_xml(options = {})
+# puts @limits.inspect
+ xml = options[:builder] ||= Builder::XmlMarkup.new(options)
+ xml.instruct! unless options[:skip_instruct]
+
+ xml.status do
+ @data.each {|metric_group,data|
+ data.each {|metric, data|
+ xml.metric(:name => metric, :metricgroup => metric_group) do
+ xml.starttime(@data[metric_group][metric]["starttime"])
+ xml.interval(5)
+ data.each {|label,data|
+ unless label == "starttime" or label == "interval"
+ xml.label(:type => "hash", :name => label) do
+ # limits
+ path = "#{metric_group}/#{metric}/#{label}"
+ if @limits.has_key? path
+ xml.limits() do
+ xml.min(@limits["#{path}"]["maximum"])
+ xml.max(@limits["#{path}"]["minimum"])
+ end
+ end
+ # values
+ data.sort.each {|time, value| #sort values by time
+ xml.values(value)
+ }
+ end
+ end
+ }
+ end
+ }
+ }
+ end
+ end
+
def initialize()
@scr = Scr.instance
@health_status = nil
@@ -38,17 +74,22 @@ class Status
# if no datapath is set, use the first directory in /var/lib/collectd
@datapath = Dir.glob("/var/lib/collectd/*").first
if @datapath.nil?
- raise Exception.new("Cannot read data from /var/lib/collectd/, check status of 'collectd' service")
+ raise Exception.new("Cannot read data from /var/lib/collectd/, check status of 'collectd' service")
end
end
@datapath
end
-
+
# set path of stored rrd files, default: /var/lib/collectd/$host.$domain
def datapath=(path=nil)
@datapath = path.chomp("/")
end
-
+
+ # returns available datapaths of rrd files
+ def available_datapaths
+ Dir.glob("/var/lib/collectd/*").reject{|x| not File.directory?(x) }.map{|x| File.basename(x) }
+ end
+
def check_collectd
ret = @scr.execute(["/usr/sbin/rccollectd", "status"])
return ret[:exit]==0
@@ -67,7 +108,7 @@ class Status
def metric_files(metrictype)
Dir.glob(File.join(datapath, metrictype, "*.rrd"))
end
-
+
# creates a hash from metric type (cpu, etc) to
def available_metrics
metrics = Hash.new
@@ -93,6 +134,7 @@ class Status
# creates several metrics for a defined period
def collect_data(start=nil, stop=nil, data = %w{cpu memory disk})
metrics = available_metrics
+ #puts metrics.inspect
result = Hash.new
if @collectd_running
case data
@@ -118,6 +160,8 @@ class Status
end
end
end
+ #logger.debug @data.inspect
+ # puts @data.inspect
return @data
end
@@ -133,7 +177,7 @@ class Status
cmd.close
output
end
-
+
# creates one metric for defined period
# parameters are the file to read and the
# time interval
@@ -162,28 +206,38 @@ class Status
if l =~ /\d*:\D*/
pair = l.split ":"
values = pair[1].split " "
+ #puts pair[1]
column = 0
values.each do |v| # each label
+ result["starttime"] ||= pair[0]
+ result["starttime"] = pair[0] if pair[0].to_i < result["starttime"].to_i
+ nexttime =
+
if v != "nan" #store valid values only
result["#{labels[column]}"] ||= Hash.new
- result["#{labels[column]}"].merge!({"T_#{pair[0].chomp(": ")}" => v})
+# result["#{labels[column]}"].merge!(v)
+ result["#{labels[column]}"].merge!({"#{pair[0].chomp(": ")}" => v})
+ #result["#{labels[column]}"].merge!({"T_#{pair[0].chomp(": ")}" => v})
column += 1
+ else
+ result["#{labels[column]}"] ||= Hash.new
+# result["#{labels[column]}"].merge!("invalid")
+ result["#{labels[column]}"].merge!({"#{pair[0].chomp(": ")}" => "invalid"})
end
end
end
end
end
-
#setting the limits
- result.each do |key, value|
- path = rrdfile[datapath.length+1..rrdfile.length-1].chomp('.rrd')
- path +="/" + key if key!="value" #do not take care about the value flag
- path = path.tr('-','_')
- if @limits.has_key?(path)
- result[key] ||= Hash.new
- result[key].merge!({"limit" => @limits[path] })
- end
- end
+# result.each do |key, value|
+# path = rrdfile[datapath.length+1..rrdfile.length-1].chomp('.rrd')
+# path +="/" + key if key!="value" #do not take care about the value flag
+# path = path.tr('-','_')
+# if @limits.has_key?(path)
+# result[key] ||= Hash.new
+# result[key].merge!({"limit" => @limits[path] })
+# end
+# end
return result
else
raise "error reading data from rrdtool"
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org