{ config, pkgs, ... }: 

{
  services.grafana = {
    enable = true;
    domain = "grafana.home.feal.no";
    port = 2342;
    addr = "127.0.0.1";
  };

  services.prometheus = {
    # Server config
    enable = true;
    port = 9001;

    scrapeConfigs = [
      {
        job_name = "node";
        static_configs = [{
          targets = [ 
            "chapel.home.feal.no:${toString config.services.prometheus.exporters.node.port}" 
            "sulu.home.feal.no:9100"
            "mccoy.home.feal.no:9100"
            "borg.home.feal.no:9100"
            "dlink-feal.home.feal.no:9100"
          ];
        }];
      }
      {
        job_name = "openwrt";
        static_configs = [{ targets = [ "dlink-feal.home.feal.no:9100" ]; } ];
      }
      {
        job_name = "snmp";

        static_configs = [{
          targets = [
            "feal-syn1.home.feal.no"
            "feal-syn2.home.feal.no"
          ];
        }];
        metrics_path = "/snmp";
        params.module = ["synology"];
        relabel_configs = [
          {
            source_labels = ["__address__"];
            target_label = "__param_target";
          }
          {
            source_labels = ["__param_target"];
            target_label = "instance";
          }
          {
            target_label = "__address__";
            replacement = "127.0.0.1:9116";
          }
        ];
      }
    ];

    # Client config
    exporters.node = {
      enable = true;
      enabledCollectors = [ "systemd" ];
      port = 9002;
    };
  };

  services.loki = {
    enable = true;
    configFile = /var/prometheus/loki-local-config.yaml;
  };
	
	systemd.services.promtail = {
    description = "Promtail service for Loki";
    wantedBy = [ "multi-user.target" ];

    serviceConfig = {
      ExecStart = ''
        ${pkgs.grafana-loki}/bin/promtail --config.file /var/prometheus/promtail.yaml
      '';
    };
  };

  services.nginx.virtualHosts.${config.services.grafana.domain} = {
    locations."/" = {
      proxyPass = "http://127.0.0.1:${toString config.services.grafana.port}";
      proxyWebsockets = true;
      extraConfig = ''
        proxy_buffers 8 1024k;  
        proxy_buffer_size 1024k;
      '';
    };
  };

  systemd.services.grafana-matrix-forwarder = {
    enable = true;
    description = "Forward Grafana Alerts to a matrix room";
    unitConfig = {
      Type = "simple";
    };
    serviceConfig = {
      EnvironmentFile = "/var/prometheus/GRAFANASECRETS";
      ExecStart = "/var/prometheus/grafana-matrix-forwarder --user $MATRIX_USER --password $MATRIX_PASSWORD --homeserver $MATRIX_HOMESERVER --host 127.0.0.1";
    };
    wantedBy = [ "multi-user.target" ];
  };

  environment.systemPackages = [
    pkgs.prometheus-snmp-exporter
  ];

  systemd.services.prometheus-snmp-exporter = {
    enable = true;
    description = "Gather data from SNMP (Synology)";
    unitConfig = {
      Type = "simple";
    };
    serviceConfig = {
      ExecStart = "${pkgs.prometheus-snmp-exporter}/bin/snmp_exporter --config.file='/var/prometheus/snmp.yml'";
    };
    wantedBy = [ "multi-user.target" ];
  };

}
