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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
{
inputs,
povSelf,
config,
lib,
pkgs,
...
}:
let
inherit (lib) types;
cfg = lib.getAttrFromPath povSelf config;
in
{
options = {
enable = {
type = types.bool;
default = false;
};
vnstati.enable = {
type = types.bool;
default = false;
};
};
config = lib.mkIf cfg.enable {
services.vnstat.enable = true;
modules.filesystem.impermanence.system.dirs = [ "/var/lib/vnstat" ];
services.nginx.virtualHosts."${config.networking.fqdn}" = lib.mkIf cfg.vnstati.enable {
locations."/vnstat/" = {
alias = "/var/run/vnstati/";
index = "index.html";
};
};
systemd.services.vnstati = lib.mkIf cfg.vnstati.enable {
wantedBy = [ "multi-user.target" ];
after = [ "vnstat.service" ];
startAt = "*-*-* *:0/10:00";
serviceConfig = {
User = "vnstatd";
Group = "vnstatd";
RuntimeDirectoryPreserve = true;
RuntimeDirectory = "vnstati";
RuntimeDirectoryMode = 775;
PrivateTmp = true;
ProtectHome = true;
ProtectSystem = "strict";
};
path = with pkgs; [
vnstat
jq
nix
];
script = ''
set -x
ifaces=$(vnstat --json | jq -r .interfaces[].name | grep -v "^lo$")
echo $ifaces
cat > /var/run/vnstati/index.html << EOF
<!DOCTYPE html>
<html lang="en">
<head>
<title>${config.networking.hostName} - stats</title>
<meta charset="utf-8">
<meta name="robots" content="none">
<link rel="icon" href="static/favicon.ico" type="image/x-icon">
<style>
body {
padding: 0;
margin: 0;
background: #eee;
font-family: monospace;
min-height: 100vh;
}
img:not(:last-child) {
margin-bottom: 15px;
}
img {
flex-shrink: 0;
}
.ui.segment {
margin: 0 1em 10px 1em;
padding: 1em;
background-color: white;
box-shadow: 0 1px 2px 0 rgba(34,36,38,.15);
border-radius: .286rem;
border: 1px solid rgba(34,36,38,.15);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
div.heading {
min-width: 100vw;
height: 70px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
div.table.vertical {
overflow-y: auto;
min-width: 100vw;
height: calc(100vh - 70px);
display: flex;
flex-direction: row;
}
div.iface {
display: flex;
flex-direction: column;
align-items: center;
flex-grow: 1;
}
</style>
</head>
<body>
<div class="heading">
<h1>${config.networking.hostName}</h1>
</div>
<div class="table vertical">
EOF
for iface in $ifaces
do
cat >> /var/run/vnstati/index.html << EOF
<div class="iface">
<h3>''${iface}</h3>
<div class="ui segment"><img src="''${iface}-summary.png" alt="Summary"></div>
<div class="ui segment"><img src="''${iface}-hourly.png" alt="Hourly"></div>
<div class="ui segment"><img src="''${iface}-daily.png" alt="Daily"></div>
<div class="ui segment"><img src="''${iface}-monthly.png" alt="Monthly"></div>
<div class="ui segment"><img src="''${iface}-top10.png" alt="Top 10"></div>
</div>
EOF
vnstati -s -nh -i $iface -o /var/run/vnstati/$iface-summary.png
vnstati -h -nh -i $iface -o /var/run/vnstati/$iface-hourly.png
vnstati -d -nh -i $iface -o /var/run/vnstati/$iface-daily.png
vnstati -m -nh -i $iface -o /var/run/vnstati/$iface-monthly.png
vnstati -t -nh -i $iface -o /var/run/vnstati/$iface-top10.png
done
cat >> /var/run/vnstati/index.html << EOF
</div>
</body>
</html>
EOF
'';
};
};
}