Top Level Namespace

Defined Under Namespace

Modules: Kunoichi Classes: Object, ProcEntry

Instance Method Summary collapse

Instance Method Details

#generate_conf(file) ⇒ Object

Generate a configuration file, starting from defaults



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kunoichi/cli.rb', line 78

def generate_conf(file)
  # Take care not to overwrite the configuration by mistake
  if File.exist? file
    puts "File #{file} already existing."
    return
  end
  begin
    conf = File.open(file,'w+')
  rescue => e
    puts "Error creating file: #{e.message}"
    return
  end
  begin
    # Write some useful comments, and the defaults dump in yaml
    conf.write Kunoichi::CONFIGURATION_HEADER +
               Kunoichi::DEFAULTS.to_yaml
  rescue => e
    puts "Error writing to file: #{e.message}"
    return
  end
  puts "Sample configuration written to #{file}."
  conf.close
  exit
end

#helpObject

Show help



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/kunoichi/cli.rb', line 2

def help
  puts "Usage: #{$0} [options]"
  puts "Options:"
  puts "\t-h|--help\t\tThis help"
  puts "\t-d|--debug\t\tDon't run in daemon mode"
  puts "\t-n|--dry-run\t\tPerform no actions"
  puts "\t-c|--config-file\tConfiguration file path.\n\t\t\t\t(defaults: config.rb, /etc/kunoichi/config.rb)"
  puts "\t-i|--import\t\tImport configuration from ninja and writes to stdout."
  puts "\t-g|--generate-config\tGenerate sample configuration file"
  puts "\t-p|--pid-file\t\tPid file path"
  exit
end

#import_conf(file) ⇒ Object

Parse a ninja configuration file and return kunoichi yaml configuration



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kunoichi/cli.rb', line 28

def import_conf(file)

  # Try to open the file
  begin
    ninja = File.read(file)
  rescue => e
    puts 'Cannot open ninja configuration file: ' + e.message
    exit
  end

  # Initialize parsed old configuration and new configuration hashes
  oldconf = {}
  newconf = {}

  # Transform ninja configuration, comments excluded, in a ruby hash
  ninja.lines.grep(/^[^#\n].* = /).each do |x|
    key=x.split('=')[0].strip!
    value=x.split('=')[1].strip!
    oldconf[key] = value
  end

  # For each of the values that kunoichi understands
  Kunoichi::DEFAULTS.keys.each do |x|
    # If it was not declared, return the default value
    if oldconf[x].nil? then
      newconf[x] = Kunoichi::DEFAULTS[x]
    else
      # If there was the setting, turn yes/no/(null) into booleans
      case oldconf[x]
        when 'yes'
          newconf[x] = true
        when 'no', '(null)'
          newconf[x] = false
        else
          # Since all values are strings, turn to integer those that require it
          if [ 'group', 'interval', 'proc_scan_offset' ].include? x
            newconf[x] = oldconf[x].to_i
          else
            # Save the old value
            newconf[x] = oldconf[x]
          end
      end
    end
  end

  # Return the newly filled hash in yaml format, preceded by some useful comments.
  return Kunoichi::CONFIGURATION_HEADER + newconf.to_yaml
end

#include_conf(file) ⇒ Object

Try to include yaml configuration



16
17
18
19
20
21
22
23
24
25
# File 'lib/kunoichi/cli.rb', line 16

def include_conf(file)
  begin
    raise 'empty file' unless readconf = YAML.load_file(file)
  rescue => e
    puts "Cannot load configuration: #{e.message}"
    exit
  end
  @config = readconf.merge! @config
  @config[:loaded] = true
end