summary refs log tree commit diff
path: root/modules/services/monitoring/zabbix-server.nix
blob: 9f875d455deb55ed41f36031a19323e57217b7da (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
# Zabbix server daemon.
{ config, pkgs, ... }:

with pkgs.lib;

let

  cfg = config.services.zabbixServer;

  stateDir = "/var/run/zabbix";

  logDir = "/var/log/zabbix";

  libDir = "/var/lib/zabbix";

  pidFile = "${stateDir}/zabbix_server.pid";

  configFile = pkgs.writeText "zabbix_server.conf"
    ''
      LogFile = ${logDir}/zabbix_server
  
      PidFile = ${pidFile}

      DBHost = ${cfg.dbServer}

      DBName = zabbix

      DBUser = zabbix

      ${optionalString (cfg.dbPassword != "") ''
        DBPassword = ${cfg.dbPassword}
      ''}
    '';

in
  
{

  ###### interface

  options = {
  
    services.zabbixServer.enable = mkOption {
      default = false;
      description = ''
        Whether to run the Zabbix server on this machine.
      '';
    };

    services.zabbixServer.dbServer = mkOption {
      default = "localhost";
      description = "Hostname or IP address of the database server.";
    };

    services.zabbixServer.dbPassword = mkOption {
      default = "";
      description = "Password used to connect to the database server.";
    };

  };

  ###### implementation

  config = mkIf cfg.enable {

    services.postgresql.enable = cfg.dbServer == "localhost";

    users.extraUsers = singleton
      { name = "zabbix";
        uid = config.ids.uids.zabbix;
        description = "Zabbix daemon user";
      };

    jobs.zabbix_server =
      { name = "zabbix-server";

        description = "Zabbix server daemon";

        startOn = "started postgresql";
        stopOn = "stopping postgresql";

        preStart =
          ''
            mkdir -m 0755 -p ${stateDir} ${logDir} ${libDir}
            chown zabbix ${stateDir} ${logDir} ${libDir}

            if ! test -e "${libDir}/db-created"; then
                ${pkgs.postgresql}/bin/createuser --no-superuser --no-createdb --no-createrole zabbix || true
                ${pkgs.postgresql}/bin/createdb --owner zabbix zabbix || true
                cat ${pkgs.zabbix.server}/share/zabbix/db/schema/postgresql.sql | ${pkgs.su}/bin/su -s "$SHELL" zabbix -c '${pkgs.postgresql}/bin/psql zabbix'
                cat ${pkgs.zabbix.server}/share/zabbix/db/data/data.sql | ${pkgs.su}/bin/su -s "$SHELL" zabbix -c '${pkgs.postgresql}/bin/psql zabbix'
                cat ${pkgs.zabbix.server}/share/zabbix/db/data/images_pgsql.sql | ${pkgs.su}/bin/su -s "$SHELL" zabbix -c '${pkgs.postgresql}/bin/psql zabbix'
                touch "${libDir}/db-created"
            fi
          '';

        # Zabbix doesn't have an option not to daemonize, and doesn't
        # daemonize in a way that allows Upstart to track it.  So to
        # make sure that we notice when it goes down, we start Zabbix
        # with an open connection to a fifo, with a `cat' on the other
        # side.  If Zabbix dies, then `cat' will exit as well, so we
        # just monitor `cat'.
        script =
          ''
            export PATH=${pkgs.nettools}/bin:$PATH
            rm -f ${stateDir}/dummy
            mkfifo ${stateDir}/dummy
            cat ${stateDir}/dummy &
            pid=$!
            ${pkgs.zabbix.server}/sbin/zabbix_server --config ${configFile} 100>${stateDir}/dummy
            wait "$pid"
          '';

        postStop =
          ''
            pid=$(cat ${pidFile} 2> /dev/null || true)
            (test -n "$pid" && kill "$pid") || true
            # Wait until they're really gone.
            while ${pkgs.procps}/bin/pkill -u zabbix zabbix_server; do true; done
          '';
      };
      
  };

}