Init commit

This commit is contained in:
2025-11-02 13:11:42 -08:00
commit 650b481463
16 changed files with 983 additions and 0 deletions

31
defaults/main.yaml Normal file
View File

@ -0,0 +1,31 @@
---
# required packages
ssd_health_packages:
- smartmontools
- python3-docker
- python3-packaging
- python3-venv
- sqlite3
# autologin vars
autologin_password: "kingduy"
autologin: true
autologin_user: "ssd_health"
# php container vars
container_name: "ssd_dashboard"
container_http_port: "8088"
extra_volumes: ""
# api service vars
service_name: "drive_index"
service_folder: "/opt/ssd_health"
# other vars
db_path: "{{ service_folder }}/drive_records.db"
hello_there_url: "https://docs.theregion.beer/hello-there.png"
sector_size: "512"
install_kiosk: false
...

87
files/dashboard/index.php Normal file
View File

@ -0,0 +1,87 @@
<?php
// Function to fetch SSD information from the API
function fetchSSDData() {
$url = 'http://172.17.0.1:5000/drives';
$options = [
'http' => [
'header' => "Content-type: application/json\r\n",
'method' => 'GET',
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
die('Error Fetching data');
}
return json_decode($result, true); // Decode JSON as an associative array
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSD Health Dashboard</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>SSD Health Dashboard</h1>
<?php
$i=0;
$ssdData = fetchSSDData(); // Fetch data from the API
echo '<div class="group-columns">';
foreach ($ssdData as $ssd):
if ($i % 2 == 0) {
echo '</div><div class="group-columns">';
}
echo <<<EOL
<div class="meter">
<table>
<tr><td align ="right">
Disk ID:
</td><td align ="left">
{$ssd['id']}
</td></tr><tr>
<tr><td align ="right">
Model String:
</td><td align ="left">
{$ssd['model']}
</td></tr><tr>
<tr><td align ="right">
Serial Number:
</td><td align ="left">
{$ssd['serial']}
</td></tr><tr>
<tr><td align ="right">
TB Written:
</td><td align ="left">
{$ssd['TBW']}
</td></tr><tr>
<tr><td align ="right">
Disk Capacity:
</td><td align ="left">
{$ssd['capacity']}
</td></tr><tr>
<tr><td align ="right">
Disk Flavor:
</td><td align ="left">
{$ssd['flavor']}
</td></tr><tr>
<tr><td align ="right">
SMART Result:
</td><td align ="left">
{$ssd['smart']}
</td></tr>
</table>
</div>
EOL;
$i++;
endforeach;
echo '</div>';
?>
</div>
</body>
</html>

View File

@ -0,0 +1,80 @@
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #2c3e50; /* Dark background color */
color: #bdc3c7; /* Dimmer text color */
}
.hidden-info {
display: none;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #34495e; /* Darker background for container */
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3); /* Slightly darker shadow */
margin-top: 20px;
}
h1, h2, h3, h4 {
color: #bdc3c7; /* Dimmer text color */
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 10px;
color: #bdc3c7; /* Dimmer text color */
}
.group-columns {
display: flex;
}
.group-rows {
display: flex;
flex-wrap: wrap;
justify-content: flex-start; /* Left justification */
margin-top: 10px;
}
.group-column {
flex: 0 0 calc(33% - 10px); /* Adjust width of each column */
}
.column {
flex: 1;
padding: 0 10px; /* Adjust spacing between columns */
}
.subcolumn {
margin-left: 10px;
}
.grid {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
margin-top: 5px;
}
.meter {
width: calc(90% - 5px);
max-width: calc(45% - 5px);
margin-bottom: 5px;
border: 1px solid #7f8c8d; /* Light border color */
border-radius: 5px;
padding: 5px;
text-align: center;
background-color: #2c3e50; /* Dark background for meter */
}

54
files/img2txt.py Normal file
View File

@ -0,0 +1,54 @@
"""
@author: Viet Nguyen <nhviet1009@gmail.com>
"""
import argparse
import cv2
import numpy as np
def get_args():
parser = argparse.ArgumentParser("Image to ASCII")
parser.add_argument("--input", type=str, default="data/input.jpg", help="Path to input image")
parser.add_argument("--output", type=str, default="data/output.txt", help="Path to output text file")
parser.add_argument("--mode", type=str, default="complex", choices=["simple", "complex"],
help="10 or 70 different characters")
parser.add_argument("--num_cols", type=int, default=150, help="number of character for output's width")
args = parser.parse_args()
return args
def main(opt):
if opt.mode == "simple":
CHAR_LIST = '@%#*+=-:. '
else:
CHAR_LIST = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
num_chars = len(CHAR_LIST)
num_cols = opt.num_cols
image = cv2.imread(opt.input)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
height, width = image.shape
cell_width = width / opt.num_cols
cell_height = 2 * cell_width
num_rows = int(height / cell_height)
if num_cols > width or num_rows > height:
print("Too many columns or rows. Use default setting")
cell_width = 6
cell_height = 12
num_cols = int(width / cell_width)
num_rows = int(height / cell_height)
output_file = open(opt.output, 'w')
for i in range(num_rows):
for j in range(num_cols):
output_file.write(
CHAR_LIST[min(int(np.mean(image[int(i * cell_height):min(int((i + 1) * cell_height), height),
int(j * cell_width):min(int((j + 1) * cell_width),
width)]) * num_chars / 255), num_chars - 1)])
output_file.write("\n")
output_file.close()
if __name__ == '__main__':
opt = get_args()
main(opt)

54
files/scripts/app.py Normal file
View File

@ -0,0 +1,54 @@
from flask import Flask, jsonify, request
import sqlite3
import json
app = Flask(__name__)
# Function to get all drive records from the database
def get_all_drive_records():
conn = sqlite3.connect('drive_records.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM drive_records")
rows = cursor.fetchall()
conn.close()
drives = []
for row in rows:
drive = {
'id': row[0],
'serial': row[1],
'model': row[2],
'flavor': row[3],
'capacity': row[4],
'TBW': row[5],
'smart': row[6]
}
drives.append(drive)
return jsonify(drives)
# Function to check if a serial number exists in the database
def check_serial_exists(serial):
conn = sqlite3.connect('drive_records.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM drive_records WHERE serial=?", (serial,))
row = cursor.fetchone()
conn.close()
return bool(row)
# Route to check if a serial number exists in the database
@app.route('/check', methods=['GET'])
def check():
serial_lookup = request.args.get('serial_lookup')
if not serial_lookup:
return jsonify({'error': 'No serial number provided'}), 400
exists = check_serial_exists(serial_lookup)
return jsonify({'serial_number_exists': exists})
# Route to get all drive records in JSON format
@app.route('/drives', methods=['GET'])
def index():
return get_all_drive_records()
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)

View File

@ -0,0 +1,210 @@
#!/bin/bash
# script for handling adding and updating local drive database
# Function to display usage
usage() {
echo "Usage: $0 [-i] [-v] [-x] -d /path/to/drive_records.db [-a 'serial,model,flavor,capacity,TBW,smart' OR -u 'serial,TBW,smart'] "
echo "Options - choose only one of a, u, or i, v and x are optional and not exclusive, and always provide the d"
echo " -d /path/to/drive_records.db Specify path to DB, required"
echo " -a 'serial,model,flavor,capacity,TBW,smart' Add new drive to sqlite db"
echo " -u 'serial,TBW,smart' Update drive data in sqlite db"
echo " -i Initialize database if not present"
echo " -v Output verbose information"
echo " -x Output debug information"
exit 1
}
# init_db subroutine
init_db() {
if [ "$BE_VERBOSE" == "true" ] ; then
echo "initializing database"
fi
# Check if the file does not exist
if [ ! -e "$DB_FILE" ]; then
if [ "$BE_VERBOSE" == "true" ] ; then
echo "No database file, initializing at $DB_FILE"
fi
sqlite3 "$DB_FILE" "$CREATE_TABLE"
chmod 666 $DB_FILE
else
if [ "$BE_VERBOSE" == "true" ] ; then
ls -lah $DB_FILE
echo "Database file exists, checking tables."
fi
DB_PRESENT=$(sqlite3 $DB_FILE .tables | grep drive_records)
if [ -z "$DB_PRESENT" ]; then
sqlite3 "$DB_FILE" "$CREATE_TABLE"
chmod 666 $DB_FILE
fi
fi
}
# Define variables
DB_FILE="drive_records.db"
NEEDS_ARGS=false
INIT_ONLY=false
ADD_DRIVE=false
UPDATE_DRIVE=false
DB_PROVIDED=false
VALID_FLAGS=false
BE_VERBOSE=false
OUTPUT_DEBUG=false
CREATE_TABLE="CREATE TABLE drive_records (
id INTEGER PRIMARY KEY,
serial TEXT NOT NULL,
model TEXT NOT NULL,
flavor TEXT NOT NULL,
capacity TEXT NOT NULL,
TBW TEXT NOT NULL,
smart TEXT NOT NULL
);"
# Parse command line options
while getopts ":d:a:u:ivx" opt; do
case ${opt} in
v ) # process option v
echo "Be Verbose"
BE_VERBOSE=true
;;
x ) # process option x
echo "Debug Info Enabled"
OUTPUT_DEBUG=true
;;
d ) # process option d
if [ "$BE_VERBOSE" == "true" ] ; then
echo "database path provided"
fi
DB_PROVIDED=true
DB_FILE=$OPTARG
;;
a ) # process option a
if [ "$BE_VERBOSE" == "true" ] ; then
echo "add new drive"
fi
ADD_DRIVE=true
NEEDS_ARGS=true
VALID_FLAGS=true
DRIVE_DATA=$OPTARG
;;
u ) # process option u
if [ "$BE_VERBOSE" == "true" ] ; then
echo "update existing drive"
fi
UPDATE_DRIVE=true
NEEDS_ARGS=true
VALID_FLAGS=true
DRIVE_DATA=$OPTARG
;;
i ) # process option i
if [ "$BE_VERBOSE" == "true" ] ; then
echo "initialize database"
fi
VALID_FLAGS=true
init_db
;;
\? ) usage
;;
esac
done
shift $((OPTIND -1))
# Check if all required options are provided
if [ "$DB_PROVIDED" == "false" ] ; then
if [ "$BE_VERBOSE" == "true" ] ; then
echo "Database Not Provided"
fi
usage
fi
if [ "$VALID_FLAGS" == "false" ] ; then
if [ "$BE_VERBOSE" == "true" ] ; then
echo "Invalid Flags"
fi
usage
fi
if [ "$NEEDS_ARGS" == "true" ] ; then
if [ "$BE_VERBOSE" == "true" ] ; then
echo "NEEDS_ARGS: $NEEDS_ARGS"
fi
if [ -z "$DRIVE_DATA" ] ; then
if [ "$BE_VERBOSE" == "true" ] ; then
echo "Missing Arguments"
fi
usage
fi
fi
# add new drive
if [ "$ADD_DRIVE" == "true" ]; then
if [ "$BE_VERBOSE" == "true" ] ; then
echo "Adding new drive"
fi
if [ "$OUTPUT_DEBUG" == "true" ] ; then
echo "DRIVE_DATA for new drive:"
echo "$DRIVE_DATA"
fi
# Extract the values from the argument string
IFS=',' read -ra data <<< "$DRIVE_DATA"
# Check if we have exactly 6 arguments
if [ ${#data[@]} -ne 6 ]; then
echo "Exactly 6 arguments are required."
exit 1
fi
# Check if the file does not exist, this should never fail
if [ ! -e "$DB_FILE" ]; then
if [ "$BE_VERBOSE" == "true" ] ; then
echo "No database file, exiting"
exit 2
fi
fi
DRIVE_EXISTS=$(curl -s http://0.0.0.0:5000/check?serial_lookup=${data[0]} | jq .serial_number_exists)
if [ "$DRIVE_EXISTS" == "false" ]; then
# Insert the values into the database
sqlite3 "$DB_FILE" <<EOF
INSERT INTO drive_records (serial, model, flavor, capacity, TBW, smart)
VALUES ('${data[0]}', '${data[1]}', '${data[2]}', '${data[3]}', '${data[4]}', '${data[5]}');
EOF
else
if [ "$BE_VERBOSE" == "true" ] ; then
echo "Drive already exists, skipping"
fi
fi
fi
# update existing drive
if [ "$UPDATE_DRIVE" == "true" ]; then
if [ "$BE_VERBOSE" == "true" ] ; then
echo "Update existing drive"
fi
if [ "$OUTPUT_DEBUG" == "true" ] ; then
echo "DRIVE_DATA for new drive:"
echo "$DRIVE_DATA"
fi
# Extract the values from the argument string
IFS=',' read -ra data <<< "$DRIVE_DATA"
# Check if we have exactly 3 arguments
if [ ${#data[@]} -ne 3 ]; then
echo "Exactly 3 arguments are required."
exit 1
fi
# Check if the file does not exist, this should never fail
if [ ! -e "$DB_FILE" ]; then
if [ "$BE_VERBOSE" == "true" ] ; then
echo "No database file, exiting"
exit 3
fi
fi
DRIVE_EXISTS=$(curl -s http://0.0.0.0:5000/check?serial_lookup=${data[0]} | jq .serial_number_exists)
if [ "$DRIVE_EXISTS" == "true" ]; then
# Update the values in the database
sqlite3 "$DB_FILE" <<EOF
UPDATE drive_records
SET TBW = '${data[1]}', smart = '${data[2]}'
WHERE serial = '${data[0]}';
EOF
else
if [ "$BE_VERBOSE" == "true" ] ; then
echo "Drive does not exist, skipping"
fi
fi
fi

66
files/scripts/test.sh Normal file
View File

@ -0,0 +1,66 @@
#!/bin/bash
# Function to display usage information
usage() {
echo "Usage: $0 -a \"serial,model,capacity,TBW,smart\""
exit 1
}
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
usage
fi
# Parse command-line arguments
while getopts ":a:" opt; do
case ${opt} in
a)
# Extract the comma-separated list of attributes
IFS=',' read -ra ATTRS <<< "$OPTARG"
;;
\?)
echo "Invalid option: $OPTARG" 1>&2
usage
;;
esac
done
shift $((OPTIND -1))
# Check if the database file exists, otherwise create it and initialize the table
DB_FILE="drive_records.db"
if [ ! -f "$DB_FILE" ]; then
sqlite3 "$DB_FILE" <<EOF
CREATE TABLE drive_records (id INTEGER PRIMARY KEY, serial TEXT NOT NULL, model TEXT NOT NULL, capacity TEXT NOT NULL, TBW TEXT NOT NULL, smart TEXT NOT NULL);
EOF
fi
# Read the input data from stdin
while read -r line; do
# Extract values for each attribute and insert into the database
VALUES=()
IFS=' ' read -ra PARTS <<< "$line"
for ((i = 0; i < ${#ATTRS[@]}; i++)); do
case "${ATTRS[i]}" in
serial)
VALUES+=("${PARTS[0]}")
;;
model)
VALUES+=("${PARTS[1]}")
;;
capacity)
VALUES+=("${PARTS[2]}")
;;
TBW)
VALUES+=("${PARTS[3]}")
;;
smart)
VALUES+=("${PARTS[4]}")
;;
esac
done
# Insert the values into the database
sqlite3 "$DB_FILE" <<EOF
INSERT INTO drive_records (serial, model, capacity, TBW, smart) VALUES ('${VALUES[0]}', '${VALUES[1]}', '${VALUES[2]}', '${VALUES[3]}', '${VALUES[4]}');
EOF
done <<< "$(cat -)"

49
tasks/autologin.yaml Normal file
View File

@ -0,0 +1,49 @@
---
- name: autologin - edit logind to reduce vterms to one
lineinfile:
dest: /etc/systemd/logind.conf
regexp: '^#NAutoVTs=6'
line: 'NAutoVTs=1'
backrefs: yes
- name: autologin - create getty tty1 service folder
file:
path: /etc/systemd/system/getty@tty1.service.d/
state: directory
mode: '0700'
- name: autologin - "create {{ autologin_user }} .bashrc"
copy:
#dest: "/home/{{ autologin_user }}/.bash_aliases"
dest: "/home/{{ autologin_user }}/.bash_aliases"
owner: "{{ autologin_user }}"
group: "{{ autologin_user }}"
mode: '0600'
content: |
clear
cat {{ service_folder }}/hello-there.txt
sleep 4
{{ service_folder }}/drive_check.sh
- name: autologin - add autologin override to getty tty1 service
copy:
dest: /etc/systemd/system/getty@tty1.service.d/override.conf
content: |
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin {{ autologin_user }} --noclear %I 38400 linux
# create hello_there ASCII art
- name: autologin - generate hello_there.txt for the lulz
include_tasks: hello_there.yaml
- name: autologin - Restart getty@tty1 service
systemd:
name: getty@tty1.service
state: restarted
enabled: true
masked: false
daemon_reload: true
...

50
tasks/dashboard.yaml Normal file
View File

@ -0,0 +1,50 @@
---
###############################################
# This part sets up drive history dashboard
###############################################
- name: drive history dashboard web files
block:
- name: drive history dashboard - set docker folder variable
set_fact:
service_control_web_folder: "{{ service_folder }}/web"
# Create docker Folder
- name: drive history dashboard - create service_control_web_folder folder
file:
path: "{{ service_control_web_folder }}"
state: directory
mode: '0755'
owner: root
group: root
- name: drive history dashboard - copy files for docker container
copy:
src: "dashboard/"
dest: "{{ service_control_web_folder }}/html"
mode: 0755
owner: root
group: root
###############################################
# Start service_control_website
###############################################
- name: start drive history dashboard
block:
- name: service_control_website - template config
template:
src: docker-compose-php.yaml
dest: "{{ service_control_web_folder }}/docker-compose.yaml"
mode: 0644
- name: "service_control_website - Start container at 0.0.0.0:{{ container_http_port }}"
shell: "docker-compose -f {{ service_control_web_folder }}/docker-compose.yaml up -d"
register: docker_output
- debug: |
msg="{{ docker_output.stdout_lines }}"
msg="{{ docker_output.stderr_lines }}"
...

95
tasks/drive_index.yaml Normal file
View File

@ -0,0 +1,95 @@
---
- name: Drive Index - Install Packages
apt:
name:
- "{{ ssd_health_packages_item }}"
state: present
loop: "{{ ssd_health_packages }}"
loop_control:
loop_var: ssd_health_packages_item
# Create Service Folder
- name: Drive Index - create ssd_check service folder
file:
path: "{{ service_folder }}"
state: directory
owner: "{{ autologin_user }}"
group: "{{ autologin_user }}"
mode: '0755'
- name: Drive Index - copy script files
copy:
src: scripts/
dest: "{{ service_folder }}/"
owner: "{{ autologin_user }}"
group: "{{ autologin_user }}"
mode: 0755
# Create python service venv
- name: Drive Index - create python venv requirement file
copy:
dest: "{{ service_folder }}/requirements.txt"
content: |
flask
pytz
requests
opencv-python
owner: "{{ autologin_user }}"
group: "{{ autologin_user }}"
mode: 0644
# build venv
- name: Drive Index - build python venv
pip:
virtualenv: "{{ service_folder }}/venv"
requirements: "{{ service_folder }}/requirements.txt"
virtualenv_command: python3 -m venv
state: present
# stop service
- name: "Drive Index - {{ service_name }} api - stop api service if running"
ignore_errors: yes
systemd:
name: "{{ service_name }}.service"
state: stopped
# Create service_control api service
- name: "Drive Index - template {{ service_name }}.service"
template:
src: drive_index.service
dest: /etc/systemd/system/drive_index.service
mode: 0644
# Create drive_check.sh
- name: "Drive Index - template drive_check.sh"
template:
src: drive_check.sh
dest: "{{ service_folder }}/drive_check.sh"
mode: 0755
owner: "{{ autologin_user }}"
group: "{{ autologin_user }}"
# daemon reload
- name: "Drive Index - {{ service_name }} api - daemon reload"
systemd:
daemon_reload: yes
# Enable and start
- name: "Drive Index - {{ service_name }} api - enable and start service api"
systemd:
name: "{{ service_name }}.service"
state: started
enabled: yes
- name: "Drive Index - {{ service_name }} api - enable and start service timeoue api"
systemd:
name: "{{ service_name }}.service"
state: started
enabled: yes
- name: Drive Index - initialize db
shell: "{{ service_folder }}/store_drive.sh -i -d {{ db_path }}"
...

31
tasks/hello_there.yaml Normal file
View File

@ -0,0 +1,31 @@
---
- name: Hello there - copy img2txt.py
copy:
src: img2txt.py
dest: "{{ service_folder }}/img2txt.py"
owner: "{{ autologin_user }}"
group: "{{ autologin_user }}"
mode: 0755
- name: Hello there - Download hello there
get_url:
url: "{{ hello_there_url }}"
dest: "{{ service_folder }}/hello-there.png"
mode: 0755
owner: "{{ autologin_user }}"
group: "{{ autologin_user }}"
- name: Hello there - get tty1 width
shell: stty size -F /dev/tty1 | cut -d ' ' -f 2
register: tty1_width
- name: Hello there - generate hello-there.txt
shell: |
{{ service_folder }}/venv/bin/python3 \
{{ service_folder }}/img2txt.py \
--input {{ service_folder }}/hello-there.png \
--output {{ service_folder }}/hello-there.txt \
--num_cols {{ tty1_width.stdout_lines[0] }} --mode simple
...

21
tasks/main.yaml Normal file
View File

@ -0,0 +1,21 @@
---
# create and configure user account
- name: Drive health - set up user account
include_tasks: user_setup.yaml
# create drive index service
- name: Drive health - drive_index service
include_tasks: drive_index.yaml
# create drive history dashboard
- name: Drive health - drive history dashboard
include_tasks: dashboard.yaml
# set up autologin
- name: Drive health - configure autologin
when: not install_kiosk | bool
include_tasks: autologin.yaml
...

37
tasks/user_setup.yaml Normal file
View File

@ -0,0 +1,37 @@
---
- name: "User setup - create {{ autologin_user }} user"
user:
name: "{{ autologin_user }}"
groups: disk
password: "{{ autologin_password | password_hash('sha512') }}"
shell: /bin/bash
- name: "User setup - ensure {{ autologin_user }} home folder exists"
file:
path: "/home/{{ autologin_user }}"
state: directory
owner: "{{ autologin_user }}"
group: "{{ autologin_user }}"
mode: '0700'
- name: User setup - update permissions on smartctl & fdisk
shell: |
chmod 755 /usr/sbin/smartctl
chmod u+s /usr/sbin/smartctl
chmod u+s /usr/sbin/fdisk
- name: User setup - create symlink for smartctl & fdisk
ignore_errors: yes
shell: |
ln /usr/sbin/smartctl /usr/bin/smartctl
ln /usr/sbin/fdisk /usr/bin/fdisk
- name: "User setup - allow {{ autologin_user }} to smartctl"
copy:
dest: /etc/sudoers.d/smartctl
content: |
{{ autologin_user }} ALL=(ALL) NOPASSWD: /usr/sbin/smartctl
{{ autologin_user }} ALL=(ALL) NOPASSWD: /usr/sbin/fdisk
...

View File

@ -0,0 +1,12 @@
services:
{{ container_name }}:
container_name: {{ container_name }}
image: php:8.0-apache
ports:
- {{ container_http_port }}:80
volumes:
- ./html:/var/www/html/
{{ extra_volumes }}
network_mode: bridge
restart: always

91
templates/drive_check.sh Normal file
View File

@ -0,0 +1,91 @@
#!/bin/bash
# this is a big loop
# it shows SSD health data
exec 2> /dev/null
while true; do
clear
# get all disks
DISK_LIST=$(ls -lo /dev/sd? | awk '{print $9}')
# process each disk
IFS=$'\n' read -rd '' -a DISK_ARRAY <<< "$DISK_LIST"
for DISK in "${DISK_ARRAY[@]}"; do
# store smartctl data once
SMART_DATA=$(smartctl -x $DISK)
NVME_CHECK=$(echo "$SMART_DATA" | grep "NVMe Version")
SSD_CHECK=$(echo "$SMART_DATA" | grep "Rotation Rate" | grep "Solid State")
# if either SATA SSD or NVMe
if [ -n "$NVME_CHECK" ] || [ -n "$SSD_CHECK" ]; then
BLOCK_SIZE=$(fdisk -l $DISK | grep 'Sector size' | awk '{print $4}' )
# SATA Logic
if [ -n "$SSD_CHECK" ] ; then
# Set Variables
TBW=$(echo "$SMART_DATA" | grep "Logical Sectors Written" | \
awk -v BLOCK_SIZE="$BLOCK_SIZE" '{print $4 * BLOCK_SIZE / (2 ^ 40)}')
PLR=$(echo "$SMART_DATA" | grep Percent_Lifetime_Remain | awk '{print $4}')
CAPACITY=$(echo "$SMART_DATA" | grep "User Capacity" | cut -d '[' -f 2 | sed 's/]//g')
SERIAL=$(echo "$SMART_DATA" | grep "Serial Number" | cut -d ":" -f 2 | xargs)
MODEL=$(echo "$SMART_DATA" | grep "Device Model" | cut -d ":" -f 2 | xargs)
SMART=$(echo "$SMART_DATA" | grep "self-assessment test result" | cut -d ":" -f 2 | xargs)
FLAVOR="SATA SSD"
DRIVE_EXISTS=$(curl -s 0.0.0.0:5000/check?serial_lookup=$SERIAL | jq .serial_number_exists)
# Display drive data
echo "============ $DISK Disk Info - SSD: ============"
#echo "DRIVE_EXISTS: $DRIVE_EXISTS"
echo "Serial Number: $SERIAL"
echo "Model String: $MODEL"
echo "SMART Check: $SMART"
echo "Disk capacity: $CAPACITY"
echo "TB Written: $TBW TB"
if [ -z "$PLR" ] ; then
echo "Percent Lifetime Remaining data not available"
else
echo "$DISK has $PLR% lifetime remaining"
fi
echo
# database handler
if [ "$DRIVE_EXISTS" == "false" ] ; then
#echo "{{ service_folder }}/store_drive.sh -a '$SERIAL,$MODEL,$FLAVOR,$CAPACITY,$TBW,$SMART' -d {{ db_path }}"
{{ service_folder }}/store_drive.sh -a "$SERIAL,$MODEL,$FLAVOR,$CAPACITY,$TBW,$SMART" -d {{ db_path }}
else
#echo "{{ service_folder }}/store_drive.sh -u '$SERIAL,$TBW,$SMART' -d {{ db_path }}"
{{ service_folder }}/store_drive.sh -u "$SERIAL,$TBW,$SMART" -d {{ db_path }}
fi
# NVMe Logic
elif [ -n "$NVME_CHECK" ] ; then
# Set Variables
MODEL=$(echo "$SMART_DATA" | grep "Model Number" | cut -d ":" -f 2 | xargs)
SERIAL=$(echo "$SMART_DATA" | grep "Serial Number" | cut -d ":" -f 2 | xargs)
TBW=$(echo "$SMART_DATA" | grep "Data Units Written" | sed 's/,//g' | \
awk -v BLOCK_SIZE="$BLOCK_SIZE" '{print $4 * BLOCK_SIZE / (2 ^ 30)}')
AVAIL_SPARE=$(echo "$SMART_DATA" | grep "Available Spare:" | cut -d ':' -f 2 | xargs)
CAPACITY=$(echo "$SMART_DATA" | grep "amespace 1 Size" | cut -d '[' -f 2 | sed 's/]//g')
SMART=$(echo "$SMART_DATA" | grep "self-assessment test result" | cut -d ":" -f 2 | xargs)
FLAVOR="NVMe"
DRIVE_EXISTS=$(curl -s 0.0.0.0:5000/check?serial_lookup=$SERIAL | jq .serial_number_exists)
# Display Disk Info
echo "============ $DISK Disk Info - NVMe: ============"
#echo "DRIVE_EXISTS: $DRIVE_EXISTS"
echo "Serial Number: $SERIAL"
echo "Model String: $MODEL"
echo "SMART Check: $SMART"
echo "Disk capacity: $CAPACITY"
echo "TB Written: $TBW TB"
echo "NAND spare blocks: $AVAIL_SPARE"
echo
# database handler
if [ "$DRIVE_EXISTS" == "false" ] ; then
#echo "{{ service_folder }}/store_drive.sh -a '$SERIAL,$MODEL,$FLAVOR,$CAPACITY,$TBW,$SMART' -d {{ db_path }}"
{{ service_folder }}/store_drive.sh -a "$SERIAL,$MODEL,$FLAVOR,$CAPACITY,$TBW,$SMART" -d {{ db_path }}
else
#echo "{{ service_folder }}/store_drive.sh -u '$SERIAL,$TBW,$SMART' -d {{ db_path }}"
{{ service_folder }}/store_drive.sh -u "$SERIAL,$TBW,$SMART" -d {{ db_path }}
fi
fi
else
echo "Skipping $DISK, not SATA SSD or NVMe"
fi
done
# wait 15 seconds, loop again
echo "Sleeping 15 seconds so you can read this"
sleep 15
done

View File

@ -0,0 +1,15 @@
[Unit]
Description={{ service_name }} API
After=network.target
[Service]
User=root
Group=root
WorkingDirectory={{ service_folder }}
ExecStartPre=/bin/sleep 5
ExecStart={{ service_folder }}/venv/bin/python {{ service_folder }}/app.py
Restart=always
[Install]
WantedBy=multi-user.target