Compare commits

..

No commits in common. "9342a93783216fbb88035c4f3f8cae300dda8e00" and "f12f3e5019ec683533419e8314ed8afaebfd2095" have entirely different histories.

2 changed files with 28 additions and 13 deletions

View File

@ -7,7 +7,7 @@ CREATE TABLE host (
CREATE TABLE host_uptime ( CREATE TABLE host_uptime (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
host_id INT NOT NULL REFERENCES host(id) ON UPDATE CASCADE, host_id INT NOT NULL REFERENCES host(id),
-- uptime in seconds -- uptime in seconds
uptime INT NOT NULL uptime INT NOT NULL
); );
@ -16,7 +16,7 @@ CREATE INDEX host_uptime_host_id_created_at_idx ON host_uptime(host_id, created_
CREATE TABLE host_mem ( CREATE TABLE host_mem (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
host_id INT NOT NULL REFERENCES host(id) ON UPDATE CASCADE, host_id INT NOT NULL REFERENCES host(id),
-- available memory in kB -- available memory in kB
mem_avail INT NOT NULL mem_avail INT NOT NULL
); );
@ -25,7 +25,7 @@ CREATE INDEX host_mem_host_id_created_at_idx ON host_mem(host_id, created_at);
CREATE TABLE host_disk ( CREATE TABLE host_disk (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
host_id INT NOT NULL REFERENCES host(id) ON UPDATE CASCADE, host_id INT NOT NULL REFERENCES host(id),
-- free disk space in kB -- free disk space in kB
disk_free INT NOT NULL disk_free INT NOT NULL
); );
@ -34,7 +34,7 @@ CREATE INDEX host_disk_host_id_created_at_idx ON host_disk(host_id, created_at);
CREATE TABLE host_cpu ( CREATE TABLE host_cpu (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
host_id INT NOT NULL REFERENCES host(id) ON UPDATE CASCADE, host_id INT NOT NULL REFERENCES host(id),
-- cpu load for last minute -- cpu load for last minute
cpu_load_1m FLOAT NOT NULL cpu_load_1m FLOAT NOT NULL
); );

33
status
View File

@ -5,34 +5,49 @@ set -e
export $(cat .env | xargs) export $(cat .env | xargs)
psql -w <<EOF psql -w <<EOF
INSERT INTO host (name) VALUES ('$(hostname)') WITH host_upsert AS (
ON CONFLICT (name) DO NOTHING; INSERT INTO host (name) VALUES ('$(hostname)')
EOF ON CONFLICT (name) DO UPDATE SET name = EXCLUDED.name
RETURNING id
psql -w <<EOF )
INSERT INTO host_uptime (host_id, uptime) INSERT INTO host_uptime (host_id, uptime)
SELECT id, '$(cat /proc/uptime | awk -F. '{print $1}')' SELECT id, '$(cat /proc/uptime | awk -F. '{print $1}')'
FROM host WHERE name = '$(hostname)' FROM host_upsert
RETURNING *; RETURNING *;
EOF EOF
psql -w <<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) INSERT INTO host_mem (host_id, mem_avail)
SELECT id, '$(cat /proc/meminfo | grep 'MemAvailable' | awk '{print $2}')' SELECT id, '$(cat /proc/meminfo | grep 'MemAvailable' | awk '{print $2}')'
FROM host WHERE name = '$(hostname)' FROM host_upsert
RETURNING *; RETURNING *;
EOF EOF
psql -w <<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) INSERT INTO host_disk (host_id, disk_free)
SELECT id, '$(df / | awk 'NR==2{print $4}')' SELECT id, '$(df / | awk 'NR==2{print $4}')'
FROM host WHERE name = '$(hostname)' FROM host_upsert
RETURNING *; RETURNING *;
EOF EOF
psql -w <<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_cpu (host_id, cpu_load_1m) INSERT INTO host_cpu (host_id, cpu_load_1m)
SELECT id, '$(top -bn1 | head -1 | awk -F'load average: ' '{print $2}' | awk -F, '{print $1}')' SELECT id, '$(top -bn1 | head -1 | awk -F'load average: ' '{print $2}' | awk -F, '{print $1}')'
FROM host WHERE name = '$(hostname)' FROM host_upsert
RETURNING *; RETURNING *;
EOF EOF