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 %>" } } 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/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 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" 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 diff --git a/cookbooks/loki/setup.rb b/cookbooks/loki/setup.rb index 36eaff3..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 @@ -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' @@ -89,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 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