From 401423d0da733357bfc81734b022044175f2abe5 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Sat, 4 Mar 2023 15:21:56 +0900 Subject: [PATCH] Deploy `vector` config for `consul`. --- .../etc/systemd/system/vector-consul.service | 16 +++++ cookbooks/vector/files/etc/vector/consul.toml | 62 +++++++++++++++++++ cookbooks/vector/setup.rb | 20 ++++++ 3 files changed, 98 insertions(+) create mode 100644 cookbooks/vector/files/etc/systemd/system/vector-consul.service create mode 100644 cookbooks/vector/files/etc/vector/consul.toml diff --git a/cookbooks/vector/files/etc/systemd/system/vector-consul.service b/cookbooks/vector/files/etc/systemd/system/vector-consul.service new file mode 100644 index 0000000..cc12547 --- /dev/null +++ b/cookbooks/vector/files/etc/systemd/system/vector-consul.service @@ -0,0 +1,16 @@ +[Unit] +Description=Vector +Documentation=https://vector.dev +After=network-online.target +Requires=network-online.target + +[Service] +ExecStart=/usr/bin/vector --config /etc/vector/consul.toml +ExecReload=/bin/kill -HUP $MAINPID +Restart=always +StandardOutput=journal +StandardError=journal +SyslogIdentifier=vector + +[Install] +WantedBy=multi-user.target diff --git a/cookbooks/vector/files/etc/vector/consul.toml b/cookbooks/vector/files/etc/vector/consul.toml new file mode 100644 index 0000000..d59184c --- /dev/null +++ b/cookbooks/vector/files/etc/vector/consul.toml @@ -0,0 +1,62 @@ +data_dir = "/var/lib/vector/" + +[sources.consul] + type = "file" + include = [ "/var/log/consul/consul-*.log" ] + ignore_older_secs = 600 + read_from = "beginning" + +[transforms.consul_transform] + type = "remap" + inputs = ["consul"] + source = ''' + dt, err = parse_regex(.message, r'^(?P
\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}\+\d{4}) ') + . = merge(., dt) + + .timestamp = .dt + del(.dt) + + .message = replace!(.message, r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}\+\d{4} ', "") + + .hostname = .host + del(.host) + + lv, err = parse_regex(.message, r'\[(?P[^\]]+)\]') + . = merge(., lv) + + .message = replace(.message, r'\[(?P[^\]]+)\] ', "") + + .level = downcase!(.level) + ''' + +[sinks.consul_output] +type = "file" +inputs = [ "consul_transform" ] +compression = "none" +path = "/tmp/consul-%Y-%m-%d.log" + + [sinks.consul_output.encoding] + codec = "json" + + [sinks.consul_output.buffer] + max_size = 268435488 + type = "disk" + +[sinks.consul_loki] +type = "loki" +inputs = [ "consul_transform" ] +endpoint = "http://loki.service.consul:3100" +compression = "snappy" + + [sinks.consul_loki.labels] + level = "{{ level }}" + hostname = "{{ hostname }}" + job = "consul" + filename = "{{ file }}" + + [sinks.consul_loki.encoding] + codec = "json" + + [sinks.consul_loki.buffer] + max_size = 268435488 + type = "disk" diff --git a/cookbooks/vector/setup.rb b/cookbooks/vector/setup.rb index d4d084b..f3c48d3 100644 --- a/cookbooks/vector/setup.rb +++ b/cookbooks/vector/setup.rb @@ -49,6 +49,26 @@ service 'vector-auth' do action [:enable, :start] end +# Deploy config for mointoring `/var/log/consul/consul-*.log`: +remote_file '/etc/vector/consul.toml' do + owner 'root' + group 'root' + mode '0644' + + notifies :restart, 'service[vector-consul]' +end + +remote_file '/etc/systemd/system/vector-consul.service' do + owner 'root' + group 'root' + mode '0644' + + notifies :restart, 'service[vector-consul]' +end + +service 'vector-consul' do + action [:enable, :start] +end # Stop vector default service: service 'vector' do