1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.systemhealth;
7
8 systemhealth = with pkgs; stdenv.mkDerivation {
9 name = "systemhealth-1.0";
10 src = fetchurl {
11 url = "http://www.brianlane.com/static/downloads/systemhealth/systemhealth-1.0.tar.bz2";
12 sha256 = "1q69lz7hmpbdpbz36zb06nzfkj651413n9icx0njmyr3xzq1j9qy";
13 };
14 buildInputs = [ python ];
15 installPhase = ''
16 mkdir -p $out/bin
17 # Make it work for kernels 3.x, not so different than 2.6
18 sed -i 's/2\.6/4.0/' system_health.py
19 cp system_health.py $out/bin
20 '';
21 };
22
23 rrdDir = "/var/lib/health/rrd";
24 htmlDir = "/var/lib/health/html";
25
26 configFile = rrdDir + "/.syshealthrc";
27 # The program will try to read $HOME/.syshealthrc, so we set the proper home.
28 command = "HOME=${rrdDir} ${systemhealth}/bin/system_health.py";
29
30 cronJob = ''
31 */5 * * * * wwwrun ${command} --log
32 5 * * * * wwwrun ${command} --graph
33 '';
34
35 nameEqualName = s: "${s} = ${s}";
36 interfacesSection = concatStringsSep "\n" (map nameEqualName cfg.interfaces);
37
38 driveLine = d: "${d.path} = ${d.name}";
39 drivesSection = concatStringsSep "\n" (map driveLine cfg.drives);
40
41in
42{
43 options = {
44 services.systemhealth = {
45 enable = mkOption {
46 default = false;
47 description = ''
48 Enable the system health monitor and its generation of graphs.
49 '';
50 };
51
52 urlPrefix = mkOption {
53 default = "/health";
54 description = ''
55 The URL prefix under which the System Health web pages appear in httpd.
56 '';
57 };
58
59 interfaces = mkOption {
60 default = [ "lo" ];
61 example = [ "lo" "eth0" "eth1" ];
62 description = ''
63 Interfaces to monitor (minimum one).
64 '';
65 };
66
67 drives = mkOption {
68 default = [ ];
69 example = [ { name = "root"; path = "/"; } ];
70 description = ''
71 Drives to monitor.
72 '';
73 };
74 };
75 };
76
77 config = mkIf cfg.enable {
78 services.cron.systemCronJobs = [ cronJob ];
79
80 system.activationScripts.systemhealth = stringAfter [ "var" ]
81 ''
82 mkdir -p ${rrdDir} ${htmlDir}
83 chown wwwrun:wwwrun ${rrdDir} ${htmlDir}
84
85 cat >${configFile} << EOF
86 [paths]
87 rrdtool = ${pkgs.rrdtool}/bin/rrdtool
88 loadavg_rrd = loadavg
89 ps = /run/current-system/sw/bin/ps
90 df = /run/current-system/sw/bin/df
91 meminfo_rrd = meminfo
92 uptime_rrd = uptime
93 rrd_path = ${rrdDir}
94 png_path = ${htmlDir}
95
96 [processes]
97
98 [interfaces]
99 ${interfacesSection}
100
101 [drives]
102 ${drivesSection}
103
104 [graphs]
105 width = 400
106 time = ['-3hours', '-32hours', '-8days', '-5weeks', '-13months']
107 height = 100
108
109 [external]
110
111 EOF
112
113 chown wwwrun:wwwrun ${configFile}
114
115 ${pkgs.su}/bin/su -s "/bin/sh" -c "${command} --check" wwwrun
116 ${pkgs.su}/bin/su -s "/bin/sh" -c "${command} --html" wwwrun
117 '';
118
119 services.httpd.extraSubservices = [
120 { function = f: {
121 extraConfig = ''
122 Alias ${cfg.urlPrefix} ${htmlDir}
123
124 <Directory ${htmlDir}>
125 Order allow,deny
126 Allow from all
127 </Directory>
128 '';
129 };
130 }
131 ];
132 };
133}