summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/bosun.nix
blob: 7a53ce1745424c9dc983d24b79899292acb5f2e6 (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.bosun;

  configFile = pkgs.writeText "bosun.conf" ''
    tsdbHost = ${cfg.opentsdbHost}
    httpListen = ${cfg.listenAddress}
    stateFile = ${cfg.stateFile}
    checkFrequency = 5m

    ${cfg.extraConfig}
  '';

in {

  options = {

    services.bosun = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to run bosun.
        '';
      };

      package = mkOption {
        type = types.package;
        example = literalExample "pkgs.bosun";
        description = ''
          bosun binary to use.
        '';
      };

      user = mkOption {
        type = types.string;
        default = "bosun";
        description = ''
          User account under which bosun runs.
        '';
      };

      group = mkOption {
        type = types.string;
        default = "bosun";
        description = ''
          Group account under which bosun runs.
        '';
      };

      opentsdbHost = mkOption {
        type = types.string;
        default = "localhost:4242";
        description = ''
          Host and port of the OpenTSDB database that stores bosun data.
        '';
      };

      listenAddress = mkOption {
        type = types.string;
        default = ":8070";
        description = ''
          The host address and port that bosun's web interface will listen on.
        '';
      };

      stateFile = mkOption {
        type = types.string;
        default = "/var/lib/bosun/bosun.state";
        description = ''
          Path to bosun's state file.
        '';
      };

      extraConfig = mkOption {
        type = types.string;
        default = "";
        description = ''
          Extra configuration options for Bosun. You should describe your
          desired templates, alerts, macros, etc through this configuration
          option.

          A detailed description of the supported syntax can be found at-spi2-atk
          http://bosun.org/configuration.html
        '';
      };

    };

  };

  config = mkIf cfg.enable {
  
    services.bosun.package = mkDefault pkgs.bosun; 

    systemd.services.bosun = {
      description = "bosun metrics collector (part of Bosun)";
      wantedBy = [ "multi-user.target" ];

      preStart =
        ''
        mkdir -p `dirname ${cfg.stateFile}`;
        touch ${cfg.stateFile}
        touch ${cfg.stateFile}.tmp

        if [ "$(id -u)" = 0 ]; then
          chown ${cfg.user}:${cfg.group} ${cfg.stateFile}
          chown ${cfg.user}:${cfg.group} ${cfg.stateFile}.tmp
        fi
        '';

      serviceConfig = {
        PermissionsStartOnly = true;
        User = cfg.user;
        Group = cfg.group;
        ExecStart = ''
          ${cfg.package}/bin/bosun -c ${configFile}
        '';
      };
    };

    users.extraUsers.bosun = {
      description = "bosun user";
      group = "bosun";
      uid = config.ids.uids.bosun;
    };

    users.extraGroups.bosun.gid = config.ids.gids.bosun;

  };

}