diff --git a/Database/create-database.sql b/Database/create-database.sql new file mode 100644 index 0000000..e6eb32b --- /dev/null +++ b/Database/create-database.sql @@ -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 +); \ No newline at end of file diff --git a/Site b/Site index 77ac940..9a1139e 160000 --- a/Site +++ b/Site @@ -1 +1 @@ -Subproject commit 77ac940c7a5952ccc772b2cdbf83fd98756b34aa +Subproject commit 9a1139edaeab309f9cf7eaf4b6431c339638be41 diff --git a/count-crontabs.sh b/count-crontabs.sh new file mode 100755 index 0000000..9ea45d8 --- /dev/null +++ b/count-crontabs.sh @@ -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
" + total_count=$(( $count + $total_count )) +done + +echo "Total: $total_count" \ No newline at end of file diff --git a/update-database.sh b/update-database.sh new file mode 100755 index 0000000..4704072 --- /dev/null +++ b/update-database.sh @@ -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 \ No newline at end of file diff --git a/webcron.db b/webcron.db new file mode 100644 index 0000000..db0c4e9 Binary files /dev/null and b/webcron.db differ