From 909ae01260f3b3094ee9c6d09440ffc5528f4f92 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Mon, 14 Sep 2020 01:25:31 +0900 Subject: [PATCH] Deploy `promtail` config for `promtail`. --- cookbooks/prometheus/default.rb | 25 ++++ .../system/promtail-prometheus.service | 12 ++ .../templates/etc/promtail/prometheus.yaml | 110 ++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 cookbooks/prometheus/files/lib/systemd/system/promtail-prometheus.service create mode 100644 cookbooks/prometheus/templates/etc/promtail/prometheus.yaml diff --git a/cookbooks/prometheus/default.rb b/cookbooks/prometheus/default.rb index f40634d..61f6819 100644 --- a/cookbooks/prometheus/default.rb +++ b/cookbooks/prometheus/default.rb @@ -11,6 +11,31 @@ if node['prometheus']['manager'] include_recipe './alertmanager_webhook_install.rb' include_recipe './alertmanager_webhook_setup.rb' + + # Deploy /etc/hosts file: + HOSTNAME = run_command('uname -n').stdout.chomp + + template '/etc/promtail/prometheus.yaml' do + owner 'root' + group 'root' + mode '644' + + variables(HOSTNAME: HOSTNAME, LOKIENDPOINT: node['promtail']['lokiendpoint']) + + notifies :restart, 'service[promtail-prometheus]' + end + + # Deploy the `systemd` configuration: + remote_file '/lib/systemd/system/promtail-prometheus.service' do + owner 'root' + group 'root' + mode '644' + end + + # Service setting: + service 'promtail-prometheus' do + action [ :enable, :restart ] + end end # Install the node_exporter here: diff --git a/cookbooks/prometheus/files/lib/systemd/system/promtail-prometheus.service b/cookbooks/prometheus/files/lib/systemd/system/promtail-prometheus.service new file mode 100644 index 0000000..a52887f --- /dev/null +++ b/cookbooks/prometheus/files/lib/systemd/system/promtail-prometheus.service @@ -0,0 +1,12 @@ +[Unit] +Description=Grafana Promtail for Prometheus +Documentation=https://github.com/grafana/loki +After=network-online.target + +[Service] +User=root +Restart=always +ExecStart=/usr/local/bin/promtail --config.file=/etc/promtail/prometheus.yaml + +[Install] +WantedBy=multi-user.target diff --git a/cookbooks/prometheus/templates/etc/promtail/prometheus.yaml b/cookbooks/prometheus/templates/etc/promtail/prometheus.yaml new file mode 100644 index 0000000..8b091c8 --- /dev/null +++ b/cookbooks/prometheus/templates/etc/promtail/prometheus.yaml @@ -0,0 +1,110 @@ +server: + disable: true + +positions: + filename: /var/opt/promtail/promtail_prometheus_position.yaml + +clients: + - url: http://192.168.10.118:3100/loki/api/v1/push + +scrape_configs: + - job_name: prometheus + static_configs: + - targets: + - localhost + labels: + job: prometheus + hostname: <%= @HOSTNAME %> + __path__: /var/log/supervisor/prometheus.log + + pipeline_stages: + - match: + selector: '{job="prometheus"}' + stages: + - drop: + expression: "^[^l]" + + - regex: + expression: '^level=(?P[^ ]+) ts=(?P[^ ]+) (?P.+)$' + + - timestamp: + source: timestamp + format: 2006-01-02T15:04:05.999Z + location: Etc/UTC + + - labels: + level: + + - output: + source: message + + - job_name: alertmanager + static_configs: + - targets: + - localhost + labels: + job: prometheus + hostname: <%= @HOSTNAME %> + __path__: /var/log/supervisor/alertmanager.log + + pipeline_stages: + - match: + selector: '{job="prometheus"}' + stages: + - drop: + expression: "^[^l]" + + - regex: + expression: '^level=(?P[^ ]+) ts=(?P[^ ]+) (?P.+)$' + + - timestamp: + source: timestamp + format: 2006-01-02T15:04:05.999Z + location: Etc/UTC + + - labels: + level: + + - output: + source: message + + - job_name: alertmanager-webhook-logger + static_configs: + - targets: + - localhost + labels: + job: prometheus + hostname: <%= @HOSTNAME %> + __path__: /var/log/supervisor/alertmanager-webhook-logger.log + + pipeline_stages: + - match: + selector: '{job="prometheus"}' + stages: + - drop: + expression: "^[0-9]+" + + - json: + expressions: + timestamp: timestamp + message: description + level: status + + - timestamp: + source: timestamp + format: 2006-01-02T15:04:05.999999999Z + location: Etc/UTC + + - template: + source: level + template: '{{ regexReplaceAllLiteral "firing" .Value "error" }}' + + - template: + source: level + template: '{{ regexReplaceAllLiteral "resolved" .Value "notice" }}' + + - labels: + level: + + - output: + source: message