1. Introduction #
Monitoring the local host
The need #
Why collect locally?
Available in: ✓ OSS ✓ Enterprise ✓ Cloud
Although Grafana Alloy excels at routing remote telemetry, its first use is often to monitor the machine it is installed on. Alloy natively includes integrations (similar to node_exporter or promtail) to collect CPU/RAM usage and disk space, and to read system log files (such as /var/log/syslog) or its own operational logs.
2. The web interface (UI) approach #
Simple but strongly discouraged in production
The convenience trap #
Add Connection -> Linux
In Grafana (and especially Grafana Cloud), there is a Connections > Add new connection menu that lets you configure a Linux server in a few clicks. The interface then generates a long Bash command to copy and paste on your server, which installs Alloy and downloads an automatically generated Alloy configuration.
Why avoid it? #
The wizard's limits
Although this method is ideal for a quick test (PoC), it carries major risks in a company:
- No versioning (GitOps): the configuration escapes your version control (Git). If someone changes the configuration from the UI, you create configuration drift.
- Opacity: the script installs and configures components implicitly. When there is a parsing or permission error, troubleshooting is complex.
- Incompatible with automation: in production, deployment must be industrialized with Ansible, Terraform or Helm, which the graphical wizard makes obsolete.
3. The "configuration as code" approach (Alloy syntax) #
The recommended method
Collecting system metrics (CPU, RAM...) #
Using prometheus.exporter.unix
Alloy includes an equivalent of node_exporter. To collect local metrics and send them to Mimir or Prometheus, add this block:
// 1. Activation de l'exportateur système
prometheus.exporter.unix "local_system" {
enable_collectors = ["cpu", "meminfo", "diskstats", "netdev"]
}
// 2. Scraping des métriques exposées en local
prometheus.scrape "scrape_system" {
targets = prometheus.exporter.unix.local_system.targets
forward_to = [prometheus.remote_write.mimir.receiver]
scrape_interval = "15s"
}
// 3. Point de destination
prometheus.remote_write "mimir" {
endpoint {
url = "http://mon-mimir:9009/api/v1/push"
}
}What the blocks do:
prometheus.exporter.unix: enables the collection of system metrics (CPU, RAM, disks, etc.) directly on the host, acting as a built-in node_exporter.prometheus.scrape: scrapes the configured exporter at regular intervals (here every 15 seconds) to retrieve the metrics.prometheus.remote_write: defines the final destination where the collected metrics are sent (for example a Mimir or Prometheus instance).
Collecting system logs #
Local files to Loki
To collect the classic logs of a Linux system (auth.log, syslog):
local.file_match "system_logs" {
path_targets = [
{"__path__" = "/var/log/syslog"},
{"__path__" = "/var/log/auth.log"}
]
}
loki.source.file "read_logs" {
targets = local.file_match.system_logs.targets
forward_to = [loki.write.loki_endpoint.receiver]
}
loki.write "loki_endpoint" {
endpoint {
url = "http://mon-loki:3100/loki/api/v1/push"
}
}Note: make sure the alloy system user has read access to /var/log (e.g. through the adm group).
What the blocks do:
local.file_match: discovers log files on the local disk based on the specified paths.loki.source.file: tails the discovered files to continuously extract new log lines.loki.write: sends the collected log lines to a Loki backend.
Self-monitoring: Alloy's logs #
Monitoring the collector itself
If Alloy runs as a systemd service, the most robust way to retrieve its own logs and detect whether it has problems is to read journald:
loki.source.journal "alloy_journal" {
format_as_json = true
max_age = "12h"
forward_to = [loki.write.loki_endpoint.receiver]
relabel_rules = discovery.relabel.journal_filter.rules
}
discovery.relabel "journal_filter" {
// Ne garder que les logs du service systemd "alloy"
rule {
source_labels = ["__journal__systemd_unit"]
regex = "alloy\\.service"
action = "keep"
}
// Renommer le label pour Loki
rule {
source_labels = ["__journal__systemd_unit"]
target_label = "unit"
}
}What the blocks do:
loki.source.journal: reads logs directly from systemd-journald.discovery.relabel: filters journal entries to keep only those of thealloy.serviceservice and renames the metadata (labels) so they are readable in Loki.
Alternative: reading files directly with OpenTelemetry (otel)
If Alloy writes its logs to a static file (e.g. /var/log/alloy/alloy.log), you can use the native OpenTelemetry components to read these files instead of the Loki-specific components:
otelcol.receiver.filelog "alloy_file" {
include = ["/var/log/alloy/alloy.log"]
output {
logs = [otelcol.exporter.loki.default.input]
}
}
otelcol.exporter.loki "default" {
forward_to = [loki.write.loki_endpoint.receiver]
}What the blocks do:
otelcol.receiver.filelog: a native OpenTelemetry component that reads the specified log files directly.otelcol.exporter.loki: converts the OpenTelemetry log format (OTLP) into a Loki-compatible format so it can be sent to aloki.writecomponent.
4. Conclusion #
The benefits of code
Full control #
Key takeaways
By writing your .river files yourself rather than using Grafana's generator interface, you understand exactly which component does what. This lets you:
- Easily isolate a problem if Alloy's CPU consumption spikes.
- Version your configuration in Git.
- Deploy it dynamically with tools such as Ansible, replacing the endpoints (Mimir/Loki) with variables specific to each environment.
