From ed501248df6f87dff007d13b1466b23cfe09522a Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Wed, 3 Apr 2024 14:26:48 +0900 Subject: [PATCH 02/10] Modify the variable name. --- cookbooks/consul/templates/etc/consul.d/server.hcl.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cookbooks/consul/templates/etc/consul.d/server.hcl.erb b/cookbooks/consul/templates/etc/consul.d/server.hcl.erb index 0713540..3af7583 100644 --- a/cookbooks/consul/templates/etc/consul.d/server.hcl.erb +++ b/cookbooks/consul/templates/etc/consul.d/server.hcl.erb @@ -11,9 +11,9 @@ acl { enable_token_persistence = true tokens { - master = "<%= @manager_hosts %>" - agent = "<%= @manager_hosts %>" - default = "<%= @manager_hosts %>" + master = "<%= @server_token %>" + agent = "<%= @server_token %>" + default = "<%= @server_token %>" } } From 65554aa1208cb4cb077481382b6715220355b2b5 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Mon, 1 Apr 2024 17:13:51 +0900 Subject: [PATCH 04/10] Delete `Promtail` dependency. --- cookbooks/loki/setup.rb | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/cookbooks/loki/setup.rb b/cookbooks/loki/setup.rb index 36eaff3..00a2134 100644 --- a/cookbooks/loki/setup.rb +++ b/cookbooks/loki/setup.rb @@ -45,31 +45,6 @@ service 'consul' do action :nothing end -# Depoy `promtail` configuration for `loki`: -HOSTNAME = run_command('uname -n').stdout.chomp - -template '/etc/promtail/loki.yaml' do - owner 'root' - group 'root' - mode '644' - - variables(HOSTNAME: HOSTNAME, LOKIENDPOINT: node['promtail']['lokiendpoint']) - - notifies :restart, 'service[promtail-loki]' -end - -# Deploy `systemd` configuration for `promtail-loki`: -remote_file '/etc/systemd/system/promtail-loki.service' do - owner 'root' - group 'root' - mode '644' -end - -# Service setting: -service 'promtail-loki' do - action [ :enable, :restart ] -end - remote_file '/etc/rsyslog.d/30-loki.conf' do owner 'root' group 'root' From 19570ea372a438661872e87a64eec4c8c6e6bd41 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Mon, 1 Apr 2024 17:15:13 +0900 Subject: [PATCH 05/10] Delete `Promtail` config. --- .../loki/templates/etc/promtail/loki.yaml | 43 ------------------- 1 file changed, 43 deletions(-) delete mode 100644 cookbooks/loki/templates/etc/promtail/loki.yaml diff --git a/cookbooks/loki/templates/etc/promtail/loki.yaml b/cookbooks/loki/templates/etc/promtail/loki.yaml deleted file mode 100644 index 6b9492b..0000000 --- a/cookbooks/loki/templates/etc/promtail/loki.yaml +++ /dev/null @@ -1,43 +0,0 @@ -server: - disable: true - -positions: - filename: /var/opt/promtail/promtail_loki_position.yaml - -clients: - - url: http://<%= @LOKIENDPOINT %>/loki/api/v1/push - -scrape_configs: - - job_name: loki - static_configs: - - targets: - - localhost - labels: - job: loki - hostname: <%= @HOSTNAME %> - __path__: /var/log/loki.log - - pipeline_stages: - - match: - selector: '{job="loki"}' - stages: - - drop: - expression: 'entry out of order' - - - regex: - expression: '^[^ ]+ +[0-9]+ [0-9]+:[0-9]+:[0-9]+ [^ ]+ loki[^ ]+ .*level=(?P[^ ]+) ts=(?P[^ ]+) (?P.+)$' - - - timestamp: - source: timestamp - format: 2006-01-02T15:04:05.999999999Z - location: Etc/GMT - - - template: - source: level - template: '{{ regexReplaceAllLiteral "warn" .Value "warning" }}' - - - labels: - level: - - - output: - source: message From bc60251904dce2c66cc2c9d16434466962671061 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Mon, 1 Apr 2024 17:15:48 +0900 Subject: [PATCH 06/10] Delete `systemd` config for `Promtail`. --- .../files/etc/systemd/system/promtail-loki.service | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 cookbooks/loki/files/etc/systemd/system/promtail-loki.service diff --git a/cookbooks/loki/files/etc/systemd/system/promtail-loki.service b/cookbooks/loki/files/etc/systemd/system/promtail-loki.service deleted file mode 100644 index 48f8d37..0000000 --- a/cookbooks/loki/files/etc/systemd/system/promtail-loki.service +++ /dev/null @@ -1,12 +0,0 @@ -[Unit] -Description=Grafana Promtail -Documentation=https://github.com/grafana/loki -After=network-online.target - -[Service] -User=root -Restart=always -ExecStart=/usr/local/bin/promtail --config.file=/etc/promtail/loki.yaml - -[Install] -WantedBy=multi-user.target From 7b745ad4a890cd04370f00f952515a9f6dbc6b71 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Wed, 3 Apr 2024 14:32:39 +0900 Subject: [PATCH 07/10] Deploy `vector` config for `Loki`. --- cookbooks/loki/setup.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cookbooks/loki/setup.rb b/cookbooks/loki/setup.rb index 00a2134..58c3256 100644 --- a/cookbooks/loki/setup.rb +++ b/cookbooks/loki/setup.rb @@ -64,6 +64,25 @@ remote_file '/etc/logrotate.d/loki' do mode '644' end +# Deploy the config file for `vector`: +remote_file '/etc/vector/loki.toml' do + owner 'root' + group 'root' + mode '644' +end + +# Deploy the `systemd` configuration: +remote_file '/lib/systemd/system/vector-loki.service' do + owner 'root' + group 'root' + mode '644' +end + +# Service setting: +service 'vector-loki' do + action [ :enable, :restart ] +end + # Firewall settings here: %w( 3100/tcp ).each do |p| execute "ufw allow #{p}" do From ec63d103719df4fccb0d988123baca9b52ef7409 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Wed, 3 Apr 2024 14:40:07 +0900 Subject: [PATCH 08/10] Add loki.toml --- cookbooks/loki/files/etc/vector/loki.toml | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 cookbooks/loki/files/etc/vector/loki.toml diff --git a/cookbooks/loki/files/etc/vector/loki.toml b/cookbooks/loki/files/etc/vector/loki.toml new file mode 100644 index 0000000..264e825 --- /dev/null +++ b/cookbooks/loki/files/etc/vector/loki.toml @@ -0,0 +1,54 @@ +data_dir = "/var/lib/vector/" + +[sources.loki] + type = "file" + include = [ "/var/log/loki.log" ] + read_from = "beginning" + +[transforms.loki_transform] + type = "remap" + inputs = ["loki"] + source = ''' + . |= parse_syslog!(.message) + + . |= parse_key_value!(.message) + + del(.message) + del(.host) + + .message = .msg + del(.msg) + ''' + +[sinks.loki_output] + type = "file" + inputs = [ "loki_transform" ] + compression = "none" + path = "/tmp/loki-%Y-%m-%d.log" + + [sinks.loki_output.encoding] + codec = "json" + + [sinks.loki_output.buffer] + max_size = 268435488 + type = "disk" + +[sinks.loki_loki] +type = "loki" +inputs = [ "loki_transform" ] +endpoint = "http://loki.service.consul:3100" +compression = "snappy" +out_of_order_action = "accept" + + [sinks.loki_loki.labels] + level = "{{ level }}" + hostname = "{{ hostname }}" + job = "loki" + filename = "/var/log/loki.log" + + [sinks.loki_loki.encoding] + codec = "json" + + [sinks.loki_loki.buffer] + max_size = 268435488 + type = "disk" From 66a32f6d5427d482466f5217682ec9bcac9e8e91 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Wed, 3 Apr 2024 14:40:30 +0900 Subject: [PATCH 09/10] Add vector-loki.service --- .../files/lib/systemd/system/vector-loki.service | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 cookbooks/loki/files/lib/systemd/system/vector-loki.service diff --git a/cookbooks/loki/files/lib/systemd/system/vector-loki.service b/cookbooks/loki/files/lib/systemd/system/vector-loki.service new file mode 100644 index 0000000..4abbc57 --- /dev/null +++ b/cookbooks/loki/files/lib/systemd/system/vector-loki.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/loki.toml +ExecReload=/bin/kill -HUP $MAINPID +Restart=always +StandardOutput=journal +StandardError=journal +SyslogIdentifier=vector + +[Install] +WantedBy=multi-user.target From bbe302764ee3f6a5671c6d8c9abf6ac62ad229d2 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Wed, 3 Apr 2024 14:40:40 +0900 Subject: [PATCH 10/10] Get IPAddress of the server. --- cookbooks/loki/attributes.rb | 13 +++++++++++++ cookbooks/loki/setup.rb | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cookbooks/loki/attributes.rb b/cookbooks/loki/attributes.rb index bffdee8..9fed441 100644 --- a/cookbooks/loki/attributes.rb +++ b/cookbooks/loki/attributes.rb @@ -1,8 +1,21 @@ # ------------------------------------------- # Specifying the default settings: # ------------------------------------------- +case run_command('grep VERSION_ID /etc/os-release | awk -F\" \'{print $2}\'').stdout.chomp +when "20.04", "22.04" + cmd = 'LANG=C ip a | grep "inet " | grep -v -E "(127|172)" | cut -d" " -f6 | perl -pe "s/\/.+//g"' + +when "18.04" + cmd = 'LANG=C /sbin/ifconfig | grep "inet " | grep -v -E "(127|172)" | cut -d" " -f10' + +else + cmd = 'LANG=C /sbin/ifconfig | grep "inet addr" | grep -v -E "(127|172)" | awk "{print $2;}" | cut -d: -f2 | cut -f 1 -d " " | tail -1' +end +ipaddr = run_command(cmd).stdout.chomp + node.reverse_merge!({ 'loki' => { + 'ipaddr' => ipaddr, 'url' => 'https://github.com/grafana/loki/releases/download/', 'zip' => 'loki-linux-amd64.zip', 'storage' => '/opt/loki/', diff --git a/cookbooks/loki/setup.rb b/cookbooks/loki/setup.rb index 58c3256..591e83e 100644 --- a/cookbooks/loki/setup.rb +++ b/cookbooks/loki/setup.rb @@ -13,7 +13,7 @@ template '/etc/loki/loki-config.yml' do group 'root' mode '644' - variables(ipaddr: node['consul']['ipaddr']) + variables(ipaddr: node['loki']['ipaddr']) notifies :restart, 'service[loki]' end @@ -36,7 +36,7 @@ template '/etc/consul.d/service-loki.json' do group 'consul' mode '644' - variables(ipaddr: node['consul']['ipaddr']) + variables(ipaddr: node['loki']['ipaddr']) notifies :reload, 'service[consul]' end