about summary refs log tree commit diff
path: root/nix/hosts/corrino/www/promtail.emile.space.nix
blob: 7205bf986cbd21fc519659d3fe9a615a84d2b50c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{ config, ... }:

{
  # allow the promtail user to read the nginx access files
  users.users.promtail.extraGroups = [ "nginx" ];

  services = {
    promtail = {
      enable = true;
      configuration = {
        server = {
          http_listen_port = config.emile.ports.promtail;
          grpc_listen_port = 0;
        };
        positions.filename = "/tmp/positions.yml";
        clients = [{
          url = "http://localhost:${toString config.services.loki.configuration.server.http_listen_port}/loki/api/v1/push";
        }];
        scrape_configs = [

          # systemd
          {
            job_name = "journal";
            journal = {
              max_age = "12h";
              labels = {
                job = "systemd-journal";
                host = config.networking.hostName;
              };
            };
            relabel_configs = [
              {
                source_labels = [ "__journal__systemd_unit" ];
                target_label = "unit";
              }
            ];
          }

          # nginx error log
          {
            job_name = "nginx-error-logs";
            static_configs = [{
              targets = [ "localhost" ];
              labels = {
                job = "nginx-error-logs";
                host = "corrino";
                __path__ = "/var/log/nginx/*error.log";
              };
            }];
          }

          # nginx
          {
            job_name = "nginx";
            static_configs = [
              {
                targets = [ "localhost" ];
                labels = {
                  job = "nginx";
                  host = "corrino";
                  __path__ = "/var/log/nginx/*access.log";
                };   
              }
            ];
            pipeline_stages = [
              # {
              #   regex = {
              #     expression = "(?:[0-9]{1,3}\.){3}([0-9]{1,3})";
              #     replace = "***";
              #   };
              # }
              {
                regex = {
                  expression = ''(?P<remote_addr>.+) - - \[(?P<time_local>.+)\] "(?P<method>.+) (?P<url>.+) (HTTP\/(?P<version>\d.\d))" (?P<status>\d{3}) (?P<body_bytes_sent>\d+) (["](?P<http_referer>(\-)|(.+))["]) (["](?P<http_user_agent>.+)["])'';
                };
              }
              {
                labels = {
                  remote_addr = null;
                  time_local = null;
                  method = null;
                  url = null;
                  status = null;
                  body_bytes_sent = null;
                  http_referer = null;
                  http_user_agent = null;
                };
              }
              # {
              #   timestamp = {
              #     source = "time_local";
              #     format = "02/Jan/2006:15:04:05 -0700";
              #   };
              # }
              {
                drop = {
                  source = "url";
                  expression = ''/(_matrix|.well-known|notifications|api|identity).*'';
                };
              }
              {
                drop = {
                  source = "url";
                  expression = ''grafana.*'';
                };
              }
            ];
          }

        ];
      };
    };
  };
}