|
| 1 | +module Puppet::Parser::Functions |
| 2 | + newfunction(:logstash_codec, :type => :rvalue, :doc => <<-EOS |
| 3 | + Logstash codec function |
| 4 | + EOS |
| 5 | + ) do |arguments| |
| 6 | + |
| 7 | + # We only expect maximum of 2 arguments. |
| 8 | + raise(Puppet::ParseError, "logstash_codec(): Wrong number of arguments given (#{arguments.size} given)") if arguments.size > 2 |
| 9 | + |
| 10 | + codec_name = arguments[0] |
| 11 | + codec_config = arguments[1] |
| 12 | + |
| 13 | + # First argument is the codec name, this has to be a string |
| 14 | + unless codec_name.is_a?(String) |
| 15 | + raise(Puppet::ParseError, "logstash_codec(): Codec name requires to be a String") |
| 16 | + end |
| 17 | + |
| 18 | + # Second argument is the coded config, this has to be a hash. |
| 19 | + unless codec_config.nil? and codec_config.is_a?(Hash) |
| 20 | + raise(Puppet::ParseError, "logstash_codec(): Codec config requires to be a Hash") |
| 21 | + end |
| 22 | + |
| 23 | + case codec_name |
| 24 | + |
| 25 | + when 'compress_spooler' |
| 26 | + # spool_size - number |
| 27 | + unless codec_config['spool_size'].nil? and codec_config['spool_size'].is_a?(Number) |
| 28 | + opt_spool_size = "\t spool_size => #{codec_config['spool_size']}\n" |
| 29 | + end |
| 30 | + |
| 31 | + # compress_level - number |
| 32 | + unless codec_config['compress_level'].nil? and codec_config['compress_level'].is_a?(Number) |
| 33 | + opt_spool_size = "\t compress_level => #{codec_config['compress_level']}\n" |
| 34 | + end |
| 35 | + |
| 36 | + |
| 37 | + opts_codec = "#{opt_spool_size}" |
| 38 | + |
| 39 | + when 'dots' |
| 40 | + # No config |
| 41 | + |
| 42 | + when 'edn' |
| 43 | + # No config |
| 44 | + |
| 45 | + when 'fluent' |
| 46 | + # No config |
| 47 | + |
| 48 | + when 'graphite' |
| 49 | + # charset - String |
| 50 | + unless codec_config['charset'].nil? and codec_config['charset'].is_a?(String) |
| 51 | + if [ 'UTF-8', 'bla', 'bla2', 'bla3' ].include?(codec_config['charset']) |
| 52 | + opt_charset = "\t charset => #{codec_config['charset']}\n" |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + # metrics - hash |
| 57 | + unless codec_config['metrics'].nil? and codec_config['metrics'].is_a?(Hash) |
| 58 | + opt_metrics = "\t metrics => #{codec_config['metrics']}\n" |
| 59 | + end |
| 60 | + |
| 61 | + # fields_are_metrics - bool |
| 62 | + unless codec_config['fields_are_metrics'].nil? and codec_config['fields_are_metrics'].is_a?(Bool) |
| 63 | + opt_fields_are_metrics = "\t fields_are_metrics => #{codec_config['fields_are_metrics']}\n" |
| 64 | + end |
| 65 | + |
| 66 | + # include_metrics - array |
| 67 | + unless codec_config['include_metrics'].nil? and codec_config['include_metrics'].is_a?(Array) |
| 68 | + opt_include_metrics = "\t include_metrics => #{codec_config['include_metrics']}\n" |
| 69 | + end |
| 70 | + |
| 71 | + # exclude_metrics - array |
| 72 | + unless codec_config['exclude_metrics'].nil? and codec_config['exclude_metrics'].is_a?(Array) |
| 73 | + opt_exclude_metrics = "\t exclude_metrics => #{codec_config['exclude_metrics']}\n" |
| 74 | + end |
| 75 | + |
| 76 | + # metrics_format - string |
| 77 | + unless codec_config['metrics_format'].nil? and codec_config['metrics_format'].is_a?(String) |
| 78 | + opt_metrics_format = "\t metrics_format => #{codec_config['metrics_format']}\n" |
| 79 | + end |
| 80 | + |
| 81 | + opt_codec = "#{opt_charset}#{opt_metrics}#{opt_fields_are_metrics}#{opt_include_metrics}#{opt_exclude_metrics}#{opt_metrics_format}" |
| 82 | + |
| 83 | + when 'json' |
| 84 | + unless codec_config['charset'].nil? and codec_config['charset'].is_a?(String) |
| 85 | + if [ 'UTF-8', 'bla', 'bla2', 'bla3' ].include?(codec_config['charset']) |
| 86 | + opt_charset = "\t charset => #{codec_config['charset']}\n" |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + opt_codec = "#{opt_charset}" |
| 91 | + |
| 92 | + when 'json_lines' |
| 93 | + unless codec_config['charset'].nil? and codec_config['charset'].is_a?(String) |
| 94 | + if [ 'UTF-8', 'bla', 'bla2', 'bla3' ].include?(codec_config['charset']) |
| 95 | + opt_charset = "\t charset => #{codec_config['charset']}\n" |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + opt_codec = "#{opt_charset}" |
| 100 | + |
| 101 | + when 'json_spooler' |
| 102 | + |
| 103 | + # spool_size - number |
| 104 | + unless codec_config['spool_size'].nil? and codec_config['spool_size'].is_a?(Number) |
| 105 | + opt_spool_size = "\t spool_size => #{codec_config['spool_size']}\n" |
| 106 | + end |
| 107 | + |
| 108 | + opts_codec = "#{opt_spool_size}" |
| 109 | + |
| 110 | + when 'line' |
| 111 | + |
| 112 | + unless codec_config['format'].nil? and codec_config['format'].is_a?(String) |
| 113 | + opt_format = "\t format => #{codec_config['format']}\n" |
| 114 | + end |
| 115 | + |
| 116 | + unless codec_config['charset'].nil? and codec_config['charset'].is_a?(String) |
| 117 | + if [ 'UTF-8', 'bla', 'bla2', 'bla3' ].include?(codec_config['charset']) |
| 118 | + opt_charset = "\t charset => #{codec_config['charset']}\n" |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + opt_codec = "#{opt_format}#{opt_charset}" |
| 123 | + |
| 124 | + when 'msgpack' |
| 125 | + |
| 126 | + # format - string |
| 127 | + unless codec_config['format'].nil? and codec_config['format'].is_a?(String) |
| 128 | + opt_format = "\t format => #{codec_config['format']}\n" |
| 129 | + end |
| 130 | + |
| 131 | + opts_codec = "#{opt_format}" |
| 132 | + |
| 133 | + when 'multiline' |
| 134 | + |
| 135 | + # pattern - string |
| 136 | + unless codec_config['pattern'].nil? and codec_config['pattern'].is_a?(String) |
| 137 | + opt_pattern = "\t pattern => #{codec_config['pattern']}\n" |
| 138 | + end |
| 139 | + |
| 140 | + # what - previous / next |
| 141 | + unless codec_config['what'].nil? and codec_config['what'].is_a?(String) |
| 142 | + if ['previous', 'next' ].include?(codec_config['what']) |
| 143 | + opt_what = "\t what => #{codec_config['what']}\n" |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + # negate - boolean |
| 148 | + unless codec_config['negate'].nil? and codec_config['negate'].is_a?(Bool) |
| 149 | + opt_negate = "\t negate => #{codec_config['negate']}\n" |
| 150 | + end |
| 151 | + |
| 152 | + # patterns_dir - array |
| 153 | + unless codec_config['patterns_dir'].nil? and codec_config['patterns_dir'].is_a(Array) |
| 154 | + patterns_dir = codec_config['patterns_dir'].join("', '") |
| 155 | + opt_format = "\t patterns_dir => ['"+patterns_dir+"']\n" |
| 156 | + end |
| 157 | + |
| 158 | + # charset - list |
| 159 | + unless codec_config['charset'].nil? and codec_config['charset'].is_a?(String) |
| 160 | + if [ 'UTF-8', 'bla', 'bla2', 'bla3' ].include?(codec_config['charset']) |
| 161 | + opt_charset = "\t charset => #{codec_config['charset']}\n" |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + # multiline_tag - string |
| 166 | + unless codec_config['multiline_tag'].nil? and codec_config['multiline_tag'].is_a?(String) |
| 167 | + opt_multiline_tag = "\t multiline_tag => #{codec_config['multiline_tag']}\n" |
| 168 | + end |
| 169 | + |
| 170 | + opts_codec = "#{opt_pattern}#{opt_what}#{opt_negate}#{opt_format}#{opt_charset}#{opt_multiline_tag}" |
| 171 | + |
| 172 | + when 'netflow' |
| 173 | + # cache_ttl - number |
| 174 | + unless codec_config['cache_ttl'].nil? and codec_config['cache_ttl'].is_a?(Number) |
| 175 | + opt_cache_ttl = "\t cache_ttl => #{codec_config['cache_ttl']}\n" |
| 176 | + end |
| 177 | + |
| 178 | + # target - string |
| 179 | + unless codec_config['target'].nil? and codec_config['target'].is_a?(String) |
| 180 | + opt_target = "\t target => #{codec_config['target']}\n" |
| 181 | + end |
| 182 | + |
| 183 | + # version - list |
| 184 | + unless codec_config['version'].nil? and codec_config['version'].is_a?(Number) |
| 185 | + if [ '5', '9' ].include?(codec_config['version']) |
| 186 | + opt_version = "\t version => #{codec_config['version']}\n" |
| 187 | + end |
| 188 | + end |
| 189 | + |
| 190 | + opt_codec = "#{opt_cache_ttl}#{opt_target}#{opt_version}" |
| 191 | + |
| 192 | + when 'noop' |
| 193 | + # No config |
| 194 | + |
| 195 | + when 'oldlogstashjson' |
| 196 | + # No config |
| 197 | + |
| 198 | + when 'plain' |
| 199 | + |
| 200 | + unless codec_config['format'].nil? |
| 201 | + opt_format = "\t format => #{codec_config['format']}\n" |
| 202 | + end |
| 203 | + |
| 204 | + unless codec_config['charset'].nil? |
| 205 | + if [ 'UTF-8', 'bla', 'bla2', 'bla3' ].include?(codec_config['charset']) |
| 206 | + opt_charset = "\t charset => #{codec_config['charset']}\n" |
| 207 | + end |
| 208 | + end |
| 209 | + |
| 210 | + opts_codec = "#{opt_format}#{opt_charset}" |
| 211 | + |
| 212 | + when 'rubydebug' |
| 213 | + # No config |
| 214 | + |
| 215 | + when 'spool' |
| 216 | + # spool_size - number |
| 217 | + unless codec_config['spool_size'].nil? and codec_config['spool_size'].is_a?(Number) |
| 218 | + opt_spool_size = "\t spool_size => #{codec_config['spool_size']}\n" |
| 219 | + end |
| 220 | + |
| 221 | + opts_codec = "#{opt_spool_size}" |
| 222 | + |
| 223 | + else |
| 224 | + # We have an invalid codec name |
| 225 | + raise(Puppet::ParseError, "#{codec_name} is not a valid codec name") |
| 226 | + |
| 227 | + end # case codec_name |
| 228 | + |
| 229 | + opts = "\n#{opts_codec}\t" unless opts_codec.nil? |
| 230 | + result = "#{codec_name} {#{opts}}" |
| 231 | + return result |
| 232 | + |
| 233 | + end # do |arguments| |
| 234 | + |
| 235 | +end |
0 commit comments