Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to skip loading old RRD files #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions app/models/collectd_provider.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
require 'rrd'

class CollectdProvider < Provider
attr_custom :rrd_path, :collectd_sock, :rrdcached_sock
attr_custom :rrd_path, :collectd_sock, :rrdcached_sock, :ignore_not_updated_since

def load_metrics(ignore_unknown_metrics = false)
def load_metrics
data = {}
Dir.glob("#{rrd_path}/**/*.rrd").each do |filename|
next if ignore_not_updated_since and get_last_update(filename) < (Time.now.to_i - ignore_not_updated_since)

parts = filename.match(/#{rrd_path}\/(?<host>.*?)\/(?<rrd>.*)/)
dss = get_ds_list(filename)
dss.each do |ds|
data[parts[:host]] ||= []
data[parts[:host]] << {:name => "#{parts[:rrd]}:#{ds}", :rrd => parts[:rrd], :ds => ds}
end
end
super(data, ignore_unknown_metrics)
super(data)
end

def get_values(options = {})
Expand Down Expand Up @@ -48,8 +50,11 @@ def get_values(options = {})
private

def get_ds_list(rrd_file)
rrd = RRD::Base.new(rrd_file)
rrd.info.keys.select {|v| v =~ /^ds/ }.map {|v| v[/^ds\[(\S+)\]\..*$/, 1] }.uniq
RRD::Base.new(rrd_file).info.keys.select {|v| v =~ /^ds/ }.map {|v| v[/^ds\[(\S+)\]\..*$/, 1] }.uniq
end

def get_last_update(rrd_file)
RRD::Base.new(rrd_file).info['last_update']
end

def flush(path, socket)
Expand Down
8 changes: 4 additions & 4 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Provider < ActiveRecord::Base

has_many :instances, :inverse_of => :provider

def load_metrics(data, ignore_unknown_metrics = false)
def load_metrics(data)
new_entities = []
new_metrics = []

Expand All @@ -34,7 +34,7 @@ def load_metrics(data, ignore_unknown_metrics = false)
m = metric_type.new(metric_h)

# skip creation if metric not defined in DSL and ignore_unknown_metrics option set on provider
next if !m.defined and ignore_unknown_metrics
next if !m.defined and @ignore_unknown_metrics

# retrieve DB row if any, or create one
if metric = metric_type.find_by_name(m.name)
Expand All @@ -55,8 +55,8 @@ def load_metrics(data, ignore_unknown_metrics = false)
end
end

Entity.where("id NOT IN (?)", new_entities).destroy_all
Metric.where("id NOT IN (?)", new_metrics).destroy_all
new_entities.empty? ? Entity.destroy_all : Entity.where("id NOT IN (?)", new_entities).destroy_all
new_metrics.empty? ? Metric.destroy_all : Metric.where("id NOT IN (?)", new_metrics).destroy_all
end
end

Expand Down
3 changes: 2 additions & 1 deletion config/providers.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
MySupaCollectd:
type: collectd
rrd_path: /home/you/collectd/rrd
ignore_unknown_metrics: true
ignore_unknown_metrics: true # ignore metrics that aren't described in definition files
ignore_not_updated_since: 86400 # ignore RRDs not updated since N seconds
5 changes: 3 additions & 2 deletions lib/tasks/vizir.rake
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ namespace :vizir do
exit
end
Provider.all.each do |provider|
ignore = @providers[provider.name]["ignore_unknown_metrics"] ? true : false
provider.load_metrics(ignore)
provider.ignore_unknown_metrics = @providers[provider.name]["ignore_unknown_metrics"] ? true : false
provider.ignore_not_updated_since = @providers[provider.name]["ignore_not_updated_since"] if @providers[provider.name]["ignore_not_updated_since"]
provider.load_metrics
end
Graph.load_defs
Dashboard.load_defs
Expand Down