├── Gemfile ├── Rakefile ├── .travis.yml ├── spec ├── spec_helper.rb └── dsl_spec.rb ├── .gitignore ├── collectd-dsl.gemspec ├── README.md ├── LICENSE.txt └── lib └── collectd-dsl.rb /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in collectd-dsl.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require 'rspec/core/rake_task' 3 | 4 | RSpec::Core::RakeTask.new('spec') 5 | 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: ruby 3 | rvm: 4 | - 1.9.3 5 | - 1.9.2 6 | - jruby-19mode 7 | - 1.8.7 8 | script: "rake spec" 9 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler/setup' 3 | 4 | require 'collectd-dsl' # and any other gems you need 5 | 6 | RSpec.configure do |config| 7 | 8 | end 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /collectd-dsl.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'collectd-dsl' 5 | 6 | Gem::Specification.new do |gem| 7 | gem.name = "collectd-dsl" 8 | gem.version = Collectd::DSL::VERSION 9 | gem.authors = ["Pierre-Yves Ritschard"] 10 | gem.email = ["pyr@spootnik.org"] 11 | gem.description = %q{Produce Valid Collectd configurations from ruby} 12 | gem.summary = %q{collectd configuration dsl} 13 | gem.homepage = "https://github.com/pyr/collectd-dsl" 14 | 15 | gem.files = `git ls-files`.split($/) 16 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 17 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 18 | gem.require_paths = ["lib"] 19 | gem.add_development_dependency "rake" 20 | gem.add_development_dependency "rspec", ">= 2.0.0" 21 | end 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Collectd::DSL 2 | 3 | Write Collectd configurations in Ruby 4 | 5 | ## Installation 6 | 7 | Add this line to your application's Gemfile: 8 | 9 | gem 'collectd-dsl' 10 | 11 | And then execute: 12 | 13 | $ bundle 14 | 15 | Or install it yourself as: 16 | 17 | $ gem install collectd-dsl 18 | 19 | ## Usage 20 | 21 | Note that the collectd-dsl converts snake-case to CamelCase 22 | 23 | ```ruby 24 | config = Collectd::DSL.parse do 25 | load_plugin :redis 26 | load_plugin :java 27 | load_plugin :disk 28 | 29 | plugin :disk do 30 | disk "/dev/sda" 31 | disk "/dev/sdb" 32 | ignore_selected "false" 33 | end 34 | end 35 | 36 | ``` 37 | 38 | The following will be output 39 | ``` 40 | LoadPlugin redis 41 | LoadPlugin java 42 | LoadPlugin disk 43 | 44 | Disk "/dev/sda" 45 | Disk "/dev/sdb" 46 | IgnoreSelected "false" 47 | 48 | 49 | ``` 50 | 51 | 52 | ## Contributing 53 | 54 | 1. Fork it 55 | 2. Create your feature branch (`git checkout -b my-new-feature`) 56 | 3. Commit your changes (`git commit -am 'Add some feature'`) 57 | 4. Push to the branch (`git push origin my-new-feature`) 58 | 5. Create new Pull Request 59 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Pierre-Yves Ritschard 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /spec/dsl_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Collectd::DSL do 4 | context "simple options" do 5 | it "no args given" do 6 | config = Collectd::DSL.parse do 7 | load_plugin :disk 8 | end 9 | config.should == "LoadPlugin disk\n" 10 | end 11 | it "one arg given" do 12 | config = Collectd::DSL.parse do 13 | load_plugin :disk, '/dev/sda' 14 | end 15 | config.should == "LoadPlugin disk \"/dev/sda\"\n" 16 | end 17 | it "section" do 18 | config = Collectd::DSL.parse do 19 | plugin :disk do 20 | disk '/dev/sda' 21 | end 22 | end 23 | config.should == "\n\tDisk \"/dev/sda\"\n\n" 24 | end 25 | it "section with no args" do 26 | config = Collectd::DSL.parse do 27 | plugin do 28 | disk '/dev/sda' 29 | end 30 | end 31 | config.should == "\n\tDisk \"/dev/sda\"\n\n" 32 | end 33 | it "empty section" do 34 | config = Collectd::DSL.parse do 35 | plugin do 36 | end 37 | end 38 | config.should == "\n\n" 39 | 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/collectd-dsl.rb: -------------------------------------------------------------------------------- 1 | module Collectd 2 | class DSL 3 | VERSION = "0.3.5" 4 | 5 | def initialize(&block) 6 | @directives = [] 7 | @indent = 0 8 | instance_eval(&block) if block_given? 9 | end 10 | 11 | def dump 12 | @directives.join("\n") + "\n" 13 | end 14 | 15 | def self.parse(&block) 16 | DSL.new(&block).dump if block_given? 17 | end 18 | 19 | def indent str 20 | ("\t" * @indent) + str 21 | end 22 | 23 | def snake_to_camel method_name 24 | method_name.split("_").map{|w| w.capitalize }.join("") 25 | end 26 | 27 | def method_missing method_name, *args 28 | mapped_args = args.map do |a| 29 | if a.class == String 30 | "\"" + a.to_s + "\"" 31 | else 32 | a.to_s 33 | end 34 | end.join(" ") 35 | 36 | mapped_args = (" " + mapped_args) unless mapped_args.empty? 37 | 38 | camel_method_name = snake_to_camel method_name.to_s 39 | if block_given? 40 | @directives << indent("<#{camel_method_name}#{mapped_args}>") 41 | @indent += 1 42 | yield # rely on the outer scope 43 | @indent -= 1 44 | @directives << indent("") 45 | else 46 | @directives << indent("#{camel_method_name}#{mapped_args}") 47 | end 48 | end 49 | end 50 | end 51 | --------------------------------------------------------------------------------