collectd2graphite 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ # Collectd2Graphite
2
+
3
+ Yet another collectd to graphite converter.
4
+
5
+ What began as the first attempt at a rack app is proving most useful as a library.
6
+
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+
3
+ task :default => 'gembuild'
4
+
5
+ desc "build the gem"
6
+ task :gembuild do
7
+ %x(gem build ./collectd2graphite.gemspec)
8
+ end
@@ -0,0 +1,91 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'sinatra'
4
+ #require 'pp'
5
+ require 'json'
6
+ require 'json2graphite'
7
+ require 'yaml'
8
+
9
+ config = YAML::load(File.read('etc/c2g.yaml'))
10
+
11
+ set :graphiteserver, config[:server]
12
+ set :graphiteport, config[:port]
13
+ set :port, 47654
14
+
15
+ post '/post-collectd' do
16
+ request.body.rewind # in case someone already read it
17
+ received = JSON.parse request.body.read
18
+ received.each do |r|
19
+ #pp r
20
+
21
+ # Values retrieved from the raw json
22
+ time = r["time"].to_i
23
+ values = r["values"]
24
+ host = r["host"].gsub('.', '_')
25
+ type = r["type"]
26
+ type_instance = r["type_instance"]
27
+ plugin = r["plugin"]
28
+ plugin_instance = r["plugin_instance"]
29
+ pluginstring = [r["plugin"], ["plugin_instance"]].join('-')
30
+
31
+ # Set the pluginstring for better target specification
32
+ #
33
+ # If the plugin_instance contains something, ues it in the plugin string
34
+ # if not, use the type_instance
35
+ if plugin_instance.empty?
36
+ if type_instance.empty?
37
+ # Neither plguin_instance nor type_instance exist
38
+ typestring = r["type"]
39
+ pluginstring = r["plugin"]
40
+ else
41
+ # Plugin_instance set while type_instance is not
42
+ typestring = r["type"]
43
+ pluginstring = [r["plugin"],r["type_instance"]].join('-')
44
+ end
45
+ else
46
+ if type_instance.empty?
47
+ # type_instance not set, while plugin_instance is set
48
+ typestring = r["type"]
49
+ pluginstring = [r["plugin"],r["plugin_instance"]].join('-')
50
+ else
51
+ # Both instance for plugin and type exist
52
+ typestring = [r["type"],r["type_instance"]].join('-')
53
+ pluginstring = [r["plugin"],r["plugin_instance"]].join('-')
54
+ end
55
+ end
56
+ superstring = [pluginstring,typestring].join('.')
57
+
58
+ # Set the typestring for better target specification
59
+ #if type_instance.empty?
60
+ #else
61
+ #end
62
+
63
+
64
+ # Create some empty hashes to work with
65
+ data = Hash.new
66
+ data[:agents] = Hash.new
67
+ data[:agents][host] = Hash.new
68
+ if values.count > 1
69
+ data[:agents][host][superstring] = Hash.new
70
+ end
71
+
72
+ # Fill in the hash
73
+ values.each_index do |i|
74
+ if values.count > 1
75
+ data[:agents][host][superstring][r["dsnames"][i]] = r["values"][i]
76
+ else
77
+ data[:agents][host][superstring] = r["values"][i]
78
+ end
79
+ end
80
+
81
+ # Convert the hash to graphite formatted data
82
+ processed = Json2Graphite.get_graphite(data, time)
83
+ #puts processed
84
+ s = TCPSocket.open(settings.graphiteserver, settings.graphiteport)
85
+ processed.each do |line|
86
+ s.puts(line)
87
+ end
88
+ s.close
89
+ end
90
+ end
91
+
@@ -0,0 +1,143 @@
1
+ require 'sinatra'
2
+ require 'json'
3
+ require 'json2graphite'
4
+ require 'yaml'
5
+ #require 'pp'
6
+
7
+ # A module for converting collectd data into graphite data.
8
+ #
9
+
10
+ module Collectd2Graphite
11
+ module_function
12
+
13
+ # Accepts an array of hashes formatted by the collectd write_http plugin
14
+ # Returns an array of hashes formatted to your liking, but in a structure
15
+ # that can be used with the jsonn2graphite library
16
+ #
17
+ # The format of the reeived data:
18
+ #
19
+ # [{
20
+ # "values": [1.16497e+08,3.91247e+08],
21
+ # "time":1288638055,
22
+ # "interval":10,
23
+ # "host":"collectd_restmq_server",
24
+ # "plugin":"df",
25
+ # "plugin_instance":"",
26
+ # "type":"df",
27
+ # "type_instance":"boot"}
28
+ # }]
29
+ #
30
+
31
+ def raw_convert (array)
32
+
33
+ # If we've not received an Array, something is broken.
34
+ exit 127 unless array.is_a? Array
35
+
36
+ # Initialize the object that we will return
37
+ data_array = Array.new
38
+
39
+ # Process each hash in the array
40
+ array.each do |r|
41
+
42
+ # initialize that data object that will return
43
+ data = Hash.new
44
+
45
+ # Pull out the useful bits from the raw collectd hash that we have received
46
+ time = r["time"].to_i
47
+ values = r["values"]
48
+ host = r["host"].gsub('.', '_')
49
+ type = r["type"]
50
+ type_instance = r["type_instance"]
51
+ plugin = r["plugin"]
52
+ plugin_instance = r["plugin_instance"]
53
+ pluginstring = [r["plugin"], ["plugin_instance"]].join('-')
54
+
55
+ # Set the time the data was created
56
+ data[:time] = time
57
+
58
+ # Set the pluginstring for better target specification
59
+ #
60
+ # Collectd formats the data object in a manner that takes some munging to
61
+ # make sense of.
62
+ #
63
+ # This process doesn't actually build a nested hash, which I think I
64
+ # might prefer, but for now, it just formatts the string, so the result
65
+ # when used with graphite should be identical.
66
+ #
67
+
68
+ #plugindata = Hash.new
69
+ #puts 'r is:'
70
+ #pp r
71
+ #plugindata = ["type","type_instance","plugin", "plugin_instance"].inject({}) {|hash, element|
72
+ # #next if r[element].empty?
73
+ # #puts "what is in element: #{element}"
74
+ # #puts "is r[element] empty?"
75
+ # #puts r[element].empty?
76
+ # #puts "element is: #{element}"
77
+ # #puts "r[element] is #{r[element]}"
78
+ # #hash[r[element]] = {} unless r[element].empty?
79
+ # #puts "hash is now: "
80
+ # #pp hash
81
+ # #puts hash[r[element]]
82
+ # #hash[r[element]] = r["#{element}"] unless r[element].empty?
83
+ # hash = hash[r[element]] unless r[element].empty?
84
+ # hash
85
+ #}
86
+
87
+ #pp plugindata
88
+
89
+
90
+ if plugin_instance.empty?
91
+ if type_instance.empty?
92
+ # Neither plguin_instance nor type_instance exist
93
+ typestring = r["type"]
94
+ pluginstring = r["plugin"]
95
+ else
96
+ # Plugin_instance set while type_instance is not
97
+ typestring = r["type"]
98
+ pluginstring = [r["plugin"],r["type_instance"]].join('-')
99
+ end
100
+ else
101
+ if type_instance.empty?
102
+ # type_instance not set, while plugin_instance is set
103
+ typestring = r["type"]
104
+ pluginstring = [r["plugin"],r["plugin_instance"]].join('-')
105
+ else
106
+ # Both instance for plugin and type exist
107
+ typestring = [r["type"],r["type_instance"]].join('-')
108
+ pluginstring = [r["plugin"],r["plugin_instance"]].join('-')
109
+ end
110
+ end
111
+
112
+ # Here is the string that will actually be used in the output
113
+ superstring = [pluginstring,typestring].join('.')
114
+
115
+ # Create some empty hashes to work with
116
+ data[:collectd] = Hash.new
117
+ data[:collectd][host] = Hash.new
118
+
119
+ # if we are working with multiple values, we should handle specially
120
+ if values.count > 1
121
+ data[:collectd][host][superstring] = Hash.new
122
+ end
123
+
124
+ # Load the hash with actual data
125
+ values.each_index do |i|
126
+ if values.count > 1
127
+ data[:collectd][host][superstring][r["dsnames"][i]] = r["values"][i]
128
+ else
129
+ data[:collectd][host][superstring] = r["values"][i]
130
+ end
131
+ end
132
+
133
+ # Add our hash to the method return object
134
+ data_array << data
135
+
136
+ end
137
+
138
+ # Return the array of hashes we promised
139
+ data_array
140
+ end
141
+
142
+ end
143
+
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: collectd2graphite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zach Leslie
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json2graphite
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Convert json blob received from collectd's write_http plugin into graphite
47
+ formatted data'
48
+ email: xaque208@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - Rakefile
54
+ - bin/collectd2graphite.rb
55
+ - lib/collectd2graphite.rb
56
+ - README.md
57
+ homepage: https://github.com/xaque208/collectd2graphite
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.23
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Convert json blob from collectd to graphite
81
+ test_files: []