moved all un-necessary files out of webroot directory.

This commit is contained in:
Tristan Ancelet 2023-05-13 13:25:45 -05:00
parent 716af24e80
commit bef8309d20
5 changed files with 133 additions and 1 deletions

View File

@ -0,0 +1,54 @@
CREATE TABLE IF NOT EXISTS log_levels (
log_level_id INTEGER PRIMARY KEY,
log_level_name VARCHAR(10) NOT NULL
);
INSERT INTO log_levels (log_level_id, log_level_name) VALUES
(0, 'INFO'),
(1, 'CRITICAL'),
(2, 'WARNING');
CREATE TABLE IF NOT EXISTS logs (
log_id INTEGER PRIMARY KEY AUTOINCREMENT,
log_level_id INTEGER NOT NULL DEFAULT 0,
log_source VARCHAR NOT NULL, -- This will be a unix path '/path/to/script'
log_message TEXT NOT NULl,
log_entry_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (log_level_id) REFERENCES log_levels(log_level_id)
);
CREATE VIEW IF NOT EXISTS log_statistics AS
SELECT levels.log_level_name, COUNT(logs.log_id) FROM logs
JOIN log_levels levels ON log_levels.log_level_id = logs.log_level_id
GROUP BY logs.log_level_id;
CREATE VIEW IF NOT EXISTS log_statistics_last_7_days AS
SELECT log_levels.log_level_name, COUNT(logs.log_id) as count FROM logs
JOIN log_levels ON log_levels.log_level_id = logs.log_level_id
WHERE logs.log_entry_timestamp > DATETIME(CURRENT_TIMESTAMP, '-7 day')
GROUP BY logs.log_level_id;
CREATE TABLE IF NOT EXISTS job_history (
job_id INTEGER PRIMARY KEY AUTOINCREMENT,
job_source TEXT NOT NULL, -- This will be the path to the script
job_result VARCHAR(7) NOT NULL, -- will be N/A, Fail or Success
job_exit_code INTEGER NOT NULL,
job_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE VIEW IF NOT EXISTS last_ten_failed_jobs AS
SELECT job_timestamp, job_source, job_exit_code FROM job_history
WHERE job_result = "Fail"
ORDER BY job_timestamp DESC
LIMIT 10;
CREATE TABLE IF NOT EXISTS crontabs (
crontab_id INTEGER PRIMARY KEY AUTOINCREMENT,
crontab_path VARCHAR NOT NULL UNIQUE, -- this will be the parent directory of the crontab file
crontab_created_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
crontab_modified_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
crontab_data BLOB NOT NULL
);

2
Site

@ -1 +1 @@
Subproject commit 77ac940c7a5952ccc772b2cdbf83fd98756b34aa Subproject commit 9a1139edaeab309f9cf7eaf4b6431c339638be41

19
count-crontabs.sh Executable file
View File

@ -0,0 +1,19 @@
total_count=0
declare -a crontab_dirs=(
/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.weekly
/var/spool/cron
)
for dir in ${crontab_dirs[@]}; do
count=0;
for file in $dir/*; do
count=` ls -1 $dir | wc -l `
done
echo "- $dir: $count<br>"
total_count=$(( $count + $total_count ))
done
echo "Total: $total_count"

59
update-database.sh Executable file
View File

@ -0,0 +1,59 @@
#!/usr/bin/bash
[[ -f ~/.bashrc ]] && . .bashrc
declare -a crontab_dirs=(
/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.weekly
/var/spool/cron
)
declare -a crontabs_in_db
sqlite_cmd='sqlite3 $WEBCRON_DB'
for name in $( $sqlite_cmd <<< "SELECT crontab_path FROM crontabs;"); do
crontabs_in_db+=( "$name" )
done
# This will go through and count the number of files in each directory (to determine if they need to be checked)
for dir in ${crontab_dirs[@]}; do
# If the directory has less than 1 crontab (aka empty) it will be skipped
if [[ $( ls -1 $dir | wc -l ) -ge 1 ]]; then
for file in $dir/* ; do
# For each file in the directory it will check to see if the current file
# path is already stored in the DB
# if not it will create one and it will be be indexed in the DB
if [[ -f $file ]] && [[ ! "${crontabs_in_db[@]}" == *$file* ]]; then
$sqlite_cmd <<< "
INSERT INTO crontabs (crontab_path, crontab_data) VALUES
('$file',
'$(<$file)')
"
fi
done
fi
done
# This will request all of the information from the db relating to all of the indexed crontab's
IFS='|'
while read crontab_id crontab_path; do
# It will initially request the id and path of the file, and then will load in the actual data of the
# file to be compared to the file content's on disk.
data=`sqlite3 $WEBCRON_DB <<< "SELECT crontab_data FROM crontabs WHERE crontab_id = $crontab_id"`
# this will check to make sure that the file exists on disk and
# will compare the disk and db versions of the file to make sure if a change is needed to be
# loaded into the db.
if [[ -f $crontab_path ]] && [[ ! "$data" == "$(<$crontab_path)" ]]; then
sqlite3 $WEBCRON_DB <<<"
UPDATE crontabs SET crontab_data = '$(<$crontab_path)' WHERE crontab_id = $crontab_id
"
fi
done <<< ` sqlite3 $WEBCRON_DB <<< "
SELECT crontab_id crontab_name, crontab_path FROM crontabs;
" `
unset IFS

BIN
webcron.db Normal file

Binary file not shown.