Puppet Function: simp_rsyslog::format_options

Defined in:
lib/puppet/functions/simp_rsyslog/format_options.rb
Function type:
Ruby 4.x API

Overview

simp_rsyslog::format_options(Hash $opts)String

Formats a passed log options hash into a form that is appropriate for an Expression Filter-based Rsyslog 7 rule.

Parameters:

  • opts (Hash)

    The options hash.

    • All entries will be combined with a logical OR

    • NOTE Only the documented Hash keys will be respected

Returns:

  • (String)

    A formatted entry suitable for injecting into an if statement in Rsyslog 7

See Also:

Author:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/puppet/functions/simp_rsyslog/format_options.rb', line 8

Puppet::Functions.create_function(:simp_rsyslog::format_options') do
  # @param opts
  #   The options hash.
  #
  #   * All entries will be combined with a logical ``OR``
  #   * **NOTE** Only the documented Hash keys will be respected
  #
  # @option options [Array[String]] 'programs' logged daemon names
  # @option options [Array[String]] 'facilities' syslog facilities
  # @option options [Array[String]] 'priorities' syslog priorities
  # @option options [Array[String]] 'msg_starts' strings the message starts with
  # @option options [Array[String]] 'msg_regex' regular exprssion match on the message
  #
  # @return [String]
  #   A formatted entry suitable for injecting into an ``if`` statement in
  #   Rsyslog 7
  #
  dispatch :format_options do
    param 'Hash', :opts
  end

  def format_options(opts)
    valid_options = {
      'programs'   => {
        :start => '($programname == ',
        :end   => ')'
      },
      'facilities' => {
        :start => 'prifilt(',
        :end   => ')'
      },
      'msg_starts' => {
        :start => '($msg startswith ',
        :end   => ')'
      },
      'msg_regex'  => {
        :start => 're_match($msg, ',
        :end   => ')'
      }
    }

    return_str = []

    Array(opts['facilities']).each do |facility|
      unless facility.include?('.')
        fail('All facility entries must be of the form "facility.priority"')
      end
    end

    valid_options.keys.each do |opt|
      Array(opts[opt]).each do |value|
        return_str << valid_options[opt][:start] + "'" + value + "'" + valid_options[opt][:end]
      end
    end

    if return_str.empty?
      fail('Did not find any valid content in the passed Options')
    end

    return return_str.join(' or ')
  end
end