93 lines
2.2 KiB
Plaintext
93 lines
2.2 KiB
Plaintext
<% if @ay_manage_loki == true -%>
|
|
|
|
// Define file discovery for your logs
|
|
local.file_match "system_logs" {
|
|
path_targets = [
|
|
<% @log_targets_array.each do |target| -%>
|
|
{__path__ = "<%= target %>"},
|
|
<% end -%>
|
|
]
|
|
sync_period = "5s"
|
|
}
|
|
|
|
// Scrape the logs from the matched files
|
|
loki.source.file "system_logs" {
|
|
targets = local.file_match.system_logs.targets
|
|
forward_to = [loki.relabel.add_labels.receiver]
|
|
}
|
|
|
|
// Parse hostname from log lines (if structured)
|
|
loki.process "parse_hostname" {
|
|
forward_to = [loki.relabel.add_labels.receiver]
|
|
stage.match {
|
|
selector = "{filename=~\"/var/log/.*\"}"
|
|
stage.regex {
|
|
expression = "^(?P<timestamp>[^ ]+ [^ ]+) (?P<hostname>[^ ]+) .*"
|
|
source = "__content__"
|
|
}
|
|
stage.labels {
|
|
values = {
|
|
hostname = "hostname", // Extracted from log line
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Refine labels
|
|
loki.relabel "add_labels" {
|
|
forward_to = [loki.write.loki.receiver]
|
|
rule {
|
|
action = "replace"
|
|
target_label = "job"
|
|
replacement = "syslogs"
|
|
}
|
|
rule {
|
|
source_labels = ["__path__"]
|
|
target_label = "filename"
|
|
}
|
|
// Prefer parsed hostname, fallback to system.hostname
|
|
rule {
|
|
source_labels = ["hostname"]
|
|
target_label = "hostname"
|
|
}
|
|
//rule {
|
|
// source_labels = ["hostname_fallback"]
|
|
// target_label = "hostname"
|
|
// action = "replace"
|
|
// replacement = "${__label:hostname_fallback}" // Only if hostname is unset
|
|
}
|
|
}
|
|
|
|
loki.write "loki" {
|
|
endpoint {
|
|
url = "<%= @ay_loki_url %>"
|
|
basic_auth {
|
|
username = "<%= @ay_loki_username %>"
|
|
password = "<%= @ay_loki_userpass %>"
|
|
}
|
|
}
|
|
}
|
|
<% end -%>
|
|
<% if @ay_manage_prom == true -%>
|
|
// Metrics collection
|
|
prometheus.exporter.unix "system_metrics" {
|
|
include_exporter_metrics = true
|
|
disable_collectors = ["mdadm"]
|
|
}
|
|
|
|
prometheus.scrape "scrape_system" {
|
|
targets = prometheus.exporter.unix.system_metrics.targets
|
|
forward_to = [prometheus.remote_write.prometheus.receiver]
|
|
scrape_interval = "15s"
|
|
}
|
|
|
|
prometheus.remote_write "prometheus" {
|
|
endpoint {
|
|
url = "<%= @ay_prom_url %>"
|
|
basic_auth {
|
|
username = "<%= @ay_loki_username %>"
|
|
password = "<%= @ay_loki_userpass %>"
|
|
}
|
|
}
|
|
}
|
|
<% end %> |