carbios 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in carbios.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ carbios (0.0.1)
5
+ graphite
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ eventmachine (1.0.0)
11
+ graphite (0.2.0)
12
+ eventmachine
13
+ rufus-scheduler
14
+ rufus-scheduler (2.0.17)
15
+ tzinfo (>= 0.3.23)
16
+ tzinfo (0.3.33)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ carbios!
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ carbios
2
+ =======
3
+
4
+ Nagios perfdata to Graphite/Carbon connector
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/carbios ADDED
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'carbios'
4
+ require 'optparse'
5
+ require 'logger'
6
+ require 'yaml'
7
+ require 'pp'
8
+ require 'graphite/logger'
9
+
10
+
11
+ logger = Logger.new($stdout)
12
+ logger.level = Logger::WARN
13
+
14
+ config = { :reverse_hostname => true,
15
+ :prefix => 'perfdata',
16
+ :graphite_host => 'localhost',
17
+ :graphite_port => 2003,
18
+ :archive => true, }
19
+
20
+ options = {}
21
+ opts = OptionParser.new
22
+ opts.banner = "Usage #{$0} --config FILE FILES"
23
+ opts.on("-c", "--config FILE", String, "Config file" ) do |v|
24
+ options[:configfile] = v
25
+ end
26
+ opts.on("-h", "--help", "help" ) do |v|
27
+ puts opts
28
+ exit 1
29
+ end
30
+ opts.on("-d", "--debug", "Turn on debug logging" ) do |v|
31
+ logger.level = Logger::DEBUG
32
+ end
33
+ opts.parse!
34
+
35
+ unless options[:configfile] && File.file?(options[:configfile])
36
+ logger.fatal "Must specify --config FILE"
37
+ puts opts
38
+ exit 1
39
+ end
40
+
41
+ config.merge! YAML.load_file(options[:configfile])
42
+
43
+ g = Graphite::Logger.new("#{config[:graphite_host]}:#{config[:graphite_port]}",
44
+ logger)
45
+ files = []
46
+ ARGF.each do |str|
47
+ perfdata = Carbios::PerfData.new(str, :base_hostname => config[:base_hostname],
48
+ :prefix => config[:prefix],
49
+ :reverse_hostname => config[:reverse_hostname] )
50
+ logger.debug str
51
+ logger.debug "time => #{perfdata.time.to_i}"
52
+ logger.debug "perfdata => #{perfdata.to_h.inspect}"
53
+ g.log(perfdata.time.to_i, perfdata.to_h) if perfdata.has_perfdata?
54
+
55
+ files << ARGF.filename unless files.include? ARGF.filename
56
+ end
57
+
58
+ # clean up
59
+ files.each do |filename|
60
+ next if filename == '-'
61
+ begin
62
+ if config[:archive]
63
+ newfn = "#{filename}.#{Time.new.strftime('%Y%m%d%H%M%S')}"
64
+ logger.info "Renaming #{filename} to #{newfn}"
65
+ File.rename(filename, newfn)
66
+ else
67
+ logger.info "Removing #{filename}"
68
+ File.unlink(filename) if File.file? filename
69
+ end
70
+ rescue
71
+ logger.error "Unable to remove or rename #{filename}"
72
+ end
73
+ end
data/carbios.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "carbios/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "carbios"
7
+ s.version = Carbios::VERSION
8
+ s.authors = ["Aaron Brown"]
9
+ s.email = ["abrown@ideeli.com"]
10
+ s.homepage = "https://github.com/ideeli/carbios"
11
+ s.summary = %q{Nagios perfdata to Graphite/Carbon connector}
12
+ s.description = %q{Nagios perfdata to Graphite/Carbon connector}
13
+
14
+ s.rubyforge_project = "carbios"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "graphite"
24
+ end
@@ -0,0 +1,34 @@
1
+ ---
2
+ # base_hostname is a regular expression that is removed
3
+ # from the hostname field in the graphite metric.
4
+ # Example:
5
+ # :base_hostname: !ruby/regexp /\.ideeli\.(net|com)$/
6
+ # hostname = "foo.bar.ideeli.net"
7
+ # metric = "loadavg"
8
+ # => "bar.foo.loadavg"
9
+ :base_hostname: !ruby/regexp /\.ideeli\.(net|com)$/
10
+
11
+ # reverse_hostname will make the hostname appear in
12
+ # reverse domain order in the graphite metric name
13
+ # Example:
14
+ # hostname = "foo.bar.ideeli.net"
15
+ # metric = "loadavg"
16
+ # => "net.ideeli.bar.foo.loadavg"
17
+ :reverse_hostname: true
18
+
19
+ :graphite_host: tv.borg.lan
20
+ :graphite_port: 2003
21
+
22
+ # prefix adds a prefix to the graphite metric name
23
+ # Example:
24
+ # hostname = "foo.bar.ideeli.net"
25
+ # metric = "loadavg"
26
+ # prefix = "perfdata"
27
+ # => "perfdata.net.ideeli.bar.foo.loadavg"
28
+ :prefix: perfdata
29
+
30
+ # if archive is true, the perfdata files will be renamed
31
+ # with a timestamp extension after being processed
32
+ #
33
+ # if archive is false, the perfdata files WILL BE REMOVED
34
+ :archive: true
@@ -0,0 +1,9 @@
1
+ module Carbios
2
+
3
+ module Helpers
4
+ def normalize_key (str)
5
+ str.gsub(/\W+/,'_')
6
+ end
7
+ end # module Helpers
8
+
9
+ end # module
@@ -0,0 +1,22 @@
1
+ module Carbios
2
+
3
+ class Hostname
4
+ attr_reader :hostname
5
+
6
+ def initialize ( hostname, options = {} )
7
+ @hostname = hostname
8
+ if options[:base_hostname]
9
+ @hostname.gsub!(options[:base_hostname], '')
10
+ end
11
+ end
12
+
13
+ def reverse
14
+ @hostname.split('.').reverse.join('.')
15
+ end
16
+
17
+ def to_s
18
+ @hostname
19
+ end
20
+ end # class Hostname
21
+
22
+ end #module Carbios
@@ -0,0 +1,53 @@
1
+ module Carbios
2
+
3
+ class PerfData
4
+ include Helpers
5
+
6
+ attr_reader :time, :hostname
7
+
8
+ SERVICEKEY = 'SERVICEPERFDATA'
9
+ HOSTKEY = 'HOSTPERFDATA'
10
+
11
+ def initialize ( checkstr, options = {} )
12
+ @prefix = options[:prefix]
13
+ parse_checkdata(checkstr)
14
+ @time = Time.at(@checkdata['TIMET'].to_i)
15
+ @hostname = Hostname.new( @checkdata['HOSTNAME'],
16
+ :base_hostname => options[:base_hostname] )
17
+ @desc = normalize_key(@checkdata['SERVICEDESC']) if @checkdata['SERVICEDESC']
18
+ @reverse_hostname = options[:reverse_hostname]
19
+ parse_perfdata
20
+ end
21
+
22
+ def parse_checkdata( checkstr )
23
+ @checkdata = Hash[checkstr.scan(/(.*?)::(?:(.*?)(?:\t+|$))/)]
24
+ end
25
+
26
+ def parse_perfdata
27
+ if @checkdata.has_key? SERVICEKEY
28
+ @key = SERVICEKEY
29
+ elsif @checkdata.has_key? HOSTKEY
30
+ @key = HOSTKEY
31
+ else
32
+ raise "Cannot find #{SERVICEKEY} or #{HOSTKEY} keys"
33
+ end
34
+ @perfdata = Hash[@checkdata[@key].scan(/(\S+)=(\d+(?:\.\d+)?)\S*\s?/)]
35
+ end
36
+
37
+ def normalize_perfdata
38
+ Hash[@perfdata.map { |k,v| [normalize_key(k), v] }]
39
+ end
40
+
41
+ def has_perfdata?
42
+ !@perfdata.empty?
43
+ end
44
+
45
+ def to_h
46
+ hostname = @reverse_hostname ? @hostname.reverse : @hostname
47
+ prefix = [@prefix, hostname, @desc].compact.join('.')
48
+ normalize_perfdata.prefix_keys("#{prefix}.")
49
+ end
50
+
51
+ end # class PerfData
52
+
53
+ end # module Carbios
@@ -0,0 +1,3 @@
1
+ module Carbios
2
+ VERSION = "0.0.1"
3
+ end
data/lib/carbios.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "carbios/version"
2
+ require 'carbios/helpers'
3
+ require 'carbios/perfdata'
4
+ require 'carbios/hostname'
5
+
6
+ require 'graphite'
7
+ require 'hash'
8
+
9
+ module Carbios
10
+ # Your code goes here...
11
+ end
data/lib/hash.rb ADDED
@@ -0,0 +1,11 @@
1
+ class Hash
2
+ def prefix_keys ( prefix )
3
+ if block_given?
4
+ Hash[self.map do |k,v|
5
+ yield(k,v) ? ["#{prefix}#{k}", v] : [k,v]
6
+ end]
7
+ else
8
+ Hash[self.map { |k,v| [ "#{prefix}#{k}", v] }]
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carbios
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Aaron Brown
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-09 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: graphite
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Nagios perfdata to Graphite/Carbon connector
35
+ email:
36
+ - abrown@ideeli.com
37
+ executables:
38
+ - carbios
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Gemfile.lock
47
+ - README.md
48
+ - Rakefile
49
+ - bin/carbios
50
+ - carbios.gemspec
51
+ - config/carbios.sample.yaml
52
+ - lib/carbios.rb
53
+ - lib/carbios/helpers.rb
54
+ - lib/carbios/hostname.rb
55
+ - lib/carbios/perfdata.rb
56
+ - lib/carbios/version.rb
57
+ - lib/hash.rb
58
+ homepage: https://github.com/ideeli/carbios
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: carbios
87
+ rubygems_version: 1.8.10
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Nagios perfdata to Graphite/Carbon connector
91
+ test_files: []
92
+