Compare commits

...

5 Commits

Author SHA1 Message Date
8568c6ea0d disk_free stats 2025-01-28 16:44:29 +01:00
d2dbf7269b Add psql script 2025-01-28 16:30:52 +01:00
655039756b mem_avail stats 2025-01-28 16:07:05 +01:00
0b124687a7 Use /proc/uptime 2025-01-28 15:24:54 +01:00
7cb003defd Uptime stats 2025-01-28 15:18:28 +01:00
5 changed files with 83 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env

7
psql Normal file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -e
export $(cat .env | xargs)
psql -w

29
schema.sql Normal file
View File

@ -0,0 +1,29 @@
CREATE TABLE host (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
name VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE host_uptime (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
host_id INT NOT NULL REFERENCES host(id),
-- uptime in seconds
uptime INT NOT NULL
);
CREATE TABLE host_mem (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
host_id INT NOT NULL REFERENCES host(id),
-- available memory in kB
mem_avail INT NOT NULL
);
CREATE TABLE host_disk (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
host_id INT NOT NULL REFERENCES host(id),
-- free disk space in kB
disk_free INT NOT NULL
);

41
status Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env bash
set -e
export $(cat .env | xargs)
psql -w <<EOF
WITH host_upsert AS (
INSERT INTO host (name) VALUES ('$(hostname)')
ON CONFLICT (name) DO UPDATE SET name = EXCLUDED.name
RETURNING id
)
INSERT INTO host_uptime (host_id, uptime)
SELECT id, '$(cat /proc/uptime | awk -F. '{print $1}')'
FROM host_upsert
RETURNING *;
EOF
psql -w <<EOF
WITH host_upsert AS (
INSERT INTO host (name) VALUES ('$(hostname)')
ON CONFLICT (name) DO UPDATE SET name = EXCLUDED.name
RETURNING id
)
INSERT INTO host_mem (host_id, mem_avail)
SELECT id, '$(cat /proc/meminfo | grep 'MemAvailable' | awk '{print $2}')'
FROM host_upsert
RETURNING *;
EOF
psql -w <<EOF
WITH host_upsert AS (
INSERT INTO host (name) VALUES ('$(hostname)')
ON CONFLICT (name) DO UPDATE SET name = EXCLUDED.name
RETURNING id
)
INSERT INTO host_disk (host_id, disk_free)
SELECT id, '$(df / | awk 'NR==2{print $4}')'
FROM host_upsert
RETURNING *;
EOF

5
template.env Normal file
View File

@ -0,0 +1,5 @@
PGHOST=
PGPORT=
PGUSER=
PGPASSWORD=
PGDATABASE=