From c599fccc2b99a58c2db6355892bc24dc9bb6f402 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Mon, 7 Sep 2020 23:15:53 +0900 Subject: [PATCH 1/3] Deploy `promtail` config & `systemd` config. --- cookbooks/nginx/setup.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cookbooks/nginx/setup.rb b/cookbooks/nginx/setup.rb index 2b4d8f9..bbe449a 100644 --- a/cookbooks/nginx/setup.rb +++ b/cookbooks/nginx/setup.rb @@ -68,3 +68,26 @@ end service 'nginx' do action [ :enable, :start ] end + +# Deploy `promtail` config file: +HOSTNAME = run_command('uname -n').stdout.chomp + +template '/etc/promtail/nginx.yaml' do + owner 'root' + group 'root' + mode '644' + + variables(HOSTNAME: HOSTNAME, LOKIENDPOINT: node['promtail']['lokiendpoint']) +end + +# Deploy the `systemd` configuration: +remote_file '/lib/systemd/system/promtail-nginx.service' do + owner 'root' + group 'root' + mode '644' +end + +# Service setting: +service 'promtail-nginx' do + action [ :enable, :restart ] +end From 110f8ae8bcac6a37550c1af1a7d27c1261370bbc Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Mon, 7 Sep 2020 23:17:33 +0900 Subject: [PATCH 2/3] `systemd` config for `prmtail` nginx. --- .../files/lib/systemd/system/promtail-nginx.service | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 cookbooks/nginx/files/lib/systemd/system/promtail-nginx.service diff --git a/cookbooks/nginx/files/lib/systemd/system/promtail-nginx.service b/cookbooks/nginx/files/lib/systemd/system/promtail-nginx.service new file mode 100644 index 0000000..dc8c2b6 --- /dev/null +++ b/cookbooks/nginx/files/lib/systemd/system/promtail-nginx.service @@ -0,0 +1,12 @@ +[Unit] +Description=Grafana Promtail (nginx) +Documentation=https://github.com/grafana/loki +After=network-online.target + +[Service] +User=root +Restart=always +ExecStart=/usr/local/bin/promtail --config.file=/etc/promtail/nginx.yaml + +[Install] +WantedBy=multi-user.target From f621f97e7508d6beafe3300512199598ce4b114a Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Mon, 7 Sep 2020 23:23:55 +0900 Subject: [PATCH 3/3] `promtail` config for `nginx`. --- .../nginx/templates/etc/promtail/nginx.yaml | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 cookbooks/nginx/templates/etc/promtail/nginx.yaml diff --git a/cookbooks/nginx/templates/etc/promtail/nginx.yaml b/cookbooks/nginx/templates/etc/promtail/nginx.yaml new file mode 100644 index 0000000..5e14af3 --- /dev/null +++ b/cookbooks/nginx/templates/etc/promtail/nginx.yaml @@ -0,0 +1,79 @@ +server: + disable: true + +positions: + filename: /var/opt/promtail/promtail_nginx_position.yaml + +clients: + - url: http://<%= @LOKIENDPOINT %>/loki/api/v1/push + + +scrape_configs: + - job_name: nginx-access + static_configs: + - targets: + - localhost + labels: + job: nginx + hostname: <%= @HOSTNAME %> + __path__: /var/log/nginx/*access.log + + pipeline_stages: + - match: + selector: '{job="nginx"}' + stages: + - regex: + expression: "^time:(?P[^\t]+)\thost:(?P[^\t]+)\tforwardedfor:(?P[^\t]+)\tmethod:(?P[^\t]+)\tpath:(?P[^\t]+)\tprotocol:(?P[^\t]+)\tstatus:(?P[^\t]+)\tsize:(?P[^\t]+)\treferer:(?P[^\t]+)\tua:(?P[^\t]+)\ttaken_sec:(?P[^\t]+)\tbackend:(?P[^\t]+)\tbackend_status:(?P[^\t]+)\tcache:(?P[^\t]+)\tbackend_runtime:(?P[^\t]+)\tvhost:(?P[^\t]+)$" + + - timestamp: + source: timestamp + format: 02/Jan/2006:15:04:05 -0700 + + - template: + source: level + template: '{{ .status }}' + + - template: + source: level + template: '{{ regexReplaceAllLiteral "(2|3)[0-9]+" .Value "info" }}' + + - template: + source: level + template: '{{ regexReplaceAllLiteral "4[0-9]+" .Value "warning" }}' + + - template: + source: level + template: '{{ regexReplaceAllLiteral "5[0-9]+" .Value "error" }}' + + - labels: + hostname: + level: + vhost: + + - job_name: nginx-error + static_configs: + - targets: + - localhost + labels: + job: nginx + hostname: <%= @HOSTNAME %> + level: error + __path__: /var/log/nginx/*error.log + + pipeline_stages: + - match: + selector: '{job="nginx"}' + stages: + - regex: + expression: '^(?P[0-9\/]+ [0-9:]+) \[(?P[^ ]+)\] (?P.+), host: "(?P[^"]+)"(?P.*)$' + + - timestamp: + source: timestamp + format: 2006/01/02 15:04:05 + location: Asia/Tokyo + + - labels: + vhost: + + - output: + source: message