Grafana: Difference between revisions

From NixOS Wiki
Add section on how to configure Grafana using `nix`
Klinger (talk | contribs)
 
(3 intermediate revisions by 2 users not shown)
Line 52: Line 52:
=== Declarative configuration ===
=== Declarative configuration ===


<syntaxhighlight lang=nix>
<syntaxhighlight lang="nix">
services.grafana = {
services.grafana = {
  enable = true;
   declarativePlugins = with pkgs.grafanaPlugins; [ ... ];
   declarativePlugins = with pkgs.grafanaPlugins; [ ... ];


Line 67: Line 65:


     datasources.settings.datasources = [
     datasources.settings.datasources = [
       # "Built-in" datasources can be provisioned https://grafana.com/docs/grafana/latest/administration/provisioning/#data-sources
       # "Built-in" datasources can be provisioned - c.f. https://grafana.com/docs/grafana/latest/administration/provisioning/#data-sources
       {
       {
         name = "Prometheus";
         name = "Prometheus";
         type = "prometheus";
         type = "prometheus";
         url = "${config.services.prometheus.listenAddress}:${toString config.services.prometheus.port}";
         url = "http://${config.services.prometheus.listenAddress}:${toString config.services.prometheus.port}";
       }
       }
       # Some plugins also can https://grafana.com/docs/plugins/yesoreyeram-infinity-datasource/latest/setup/provisioning/
       # Some plugins also can - c.f. https://grafana.com/docs/plugins/yesoreyeram-infinity-datasource/latest/setup/provisioning/
       {
       {
         name = "Infinity";
         name = "Infinity";
         type = "yesoreyeram-infinity-datasource";
         type = "yesoreyeram-infinity-datasource";
       }
       }
       # But not all, c.f. https://github.com/fr-ser/grafana-sqlite-datasource/issues/141
       # But not all - c.f. https://github.com/fr-ser/grafana-sqlite-datasource/issues/141
     ];
     ];


Line 99: Line 97:
* [https://grafana.com/ grafana.com]
* [https://grafana.com/ grafana.com]
* [https://xeiaso.net/blog/prometheus-grafana-loki-nixos-2020-11-20 How to Setup Prometheus, Grafana and Loki on NixOS]
* [https://xeiaso.net/blog/prometheus-grafana-loki-nixos-2020-11-20 How to Setup Prometheus, Grafana and Loki on NixOS]
[[Category:Server]]
[[Category:Web Applications]]

Latest revision as of 17:29, 25 August 2024

Grafana is an open-source, general purpose dashboarding tool, which runs as a web application. It can be used to create a variety of time-series graphs and also for displaying logs. It supports Prometheus, graphite, InfluxDB, opentsdb, Grafana Loki, PostgreSQL and many other data sources.

See Grafana options

Installation

Grafana is available as NixOS module, it can be enabled using the following config:

services.grafana = {
  enable = true;
  settings = {
    server = {
      # Listening Address
      http_addr = "127.0.0.1";
      # and Port
      http_port = 3000;
      # Grafana needs to know on which domain and URL it's running
      domain = "your.domain";
      root_url = "https://your.domain/grafana/"; # Not needed if it is `https://your.domain/`
      serve_from_sub_path = true;
    };
  };
};

This will make Grafana available only at localhost. On a server, it might be used through SSH tunnel or made publicly available using nginx with TLS. For example the follwing Nginx configuration can be used:

services.nginx.virtualHosts."your.domain" = {
  addSSL = true;
  enableACME = true;
  locations."/grafana/" = {
      proxyPass = "http://${toString config.services.grafana.settings.server.http_addr}:${toString config.services.grafana.settings.server.http_port}";
      proxyWebsockets = true;
      recommendedProxySettings = true;
  };
};

Configuration

Everything else (data sources, users, dashboards, ...) can be configured either in the Web UI, or as code.

Via Web UI

Log into the Grafana web application (using default user: admin, password: admin). Refer to the official documentation on how to do it:

Declarative configuration

services.grafana = {
  declarativePlugins = with pkgs.grafanaPlugins; [ ... ];

  provision = {
    enable = true;

    dashboards.settings.providers = [{
      name = "my dashboards";
      options.path = "/etc/grafana-dashboards";
    }];

    datasources.settings.datasources = [
      # "Built-in" datasources can be provisioned - c.f. https://grafana.com/docs/grafana/latest/administration/provisioning/#data-sources
      {
        name = "Prometheus";
        type = "prometheus";
        url = "http://${config.services.prometheus.listenAddress}:${toString config.services.prometheus.port}";
      }
      # Some plugins also can - c.f. https://grafana.com/docs/plugins/yesoreyeram-infinity-datasource/latest/setup/provisioning/
      {
        name = "Infinity";
        type = "yesoreyeram-infinity-datasource";
      }
      # But not all - c.f. https://github.com/fr-ser/grafana-sqlite-datasource/issues/141
    ];

    # Note: removing attributes from the above `datasources.settings.datasources` is not enough for them to be deleted on `grafana`;
    # One needs to use the following option:
    # datasources.settings.deleteDatasources = [ { name = "foo"; orgId = 1; } { name = "bar"; orgId = 1; } ];
  };
};

environment.etc = [{
  source = ./. + "/grafana-dashboards/some-dashboard.json";
  group = "grafana";
  user = "grafana";
}];

External Links