diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..a30fabe --- /dev/null +++ b/install.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Check if the script is being run as root. +if [ "$(id -u)" -ne 0 ]; then + echo "Rerun the script with escelated privelages" + exit 1 +fi + +# Define paths +BINARY_PATH="/opt/bin/system-reporting" +SOURCE_BINARY="./system-reporting" + +if [ ! -f $SOURCE_BINARY ]; then + echo "Run ./build.sh first to build the executable" +fi + +# Check if /opt/bin exists, create it if not +if [ ! -d "/opt/bin" ]; then + echo "Creating /opt/bin..." + mkdir -p /opt/bin + chmod 755 /opt/bin + echo "/opt/bin created." +else + echo "/opt/bin already exists." +fi + +# Check if /opt/bin/system-reporting exists and is outdated compared to the source +if [ -f "$BINARY_PATH" ]; then + echo "Checking if the installed binary is outdated..." + # Compare modification timestamps of the existing binary and the new one + if [ "$SOURCE_BINARY" -nt "$BINARY_PATH" ]; then + echo "Installed binary is outdated. Replacing it..." + $ESC cp -f "$SOURCE_BINARY" "$BINARY_PATH" + echo "Binary updated successfully." + else + echo "Installed binary is up to date." + fi +else + echo "Installing binary to /opt/bin..." + cp -f "$SOURCE_BINARY" "$BINARY_PATH" + echo "Binary installed successfully." +fi + +# Ensure /opt/bin is in PATH +if ! echo "$PATH" | grep -qE "(^|:)/opt/bin(:|$)"; then + echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++' + echo '+ Add "export PATH="/opt/bin:$PATH" to your /etc/profile +' + echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++' + echo "You may need to log out and log back in for changes to take effect." + echo + echo "To apply the change immediately you can run:" + echo ". /etc/profile" + echo +else + echo "/opt/bin is already in PATH." +fi diff --git a/runit-install.sh b/runit-install.sh new file mode 100755 index 0000000..3932813 --- /dev/null +++ b/runit-install.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Check if the script is being run as root. +if [ "$(id -u)" -ne 0 ]; then + echo "Rerun the script with escelated privelages" + exit 1 +fi + +./install.sh + +SERVICE_DIR="/etc/sv/system-reporting" +BINARY="/opt/bin/system-reporting" + +if [ ! -f "$BINARY" ]; then + echo "Error: Daemon binary not found at $BINARY" + exit 1 +fi + +echo "Creating service directory at $SERVICE_DIR..." +mkdir -p "$SERVICE_DIR" + +echo "Creating the 'run' script..." +cat <"$SERVICE_DIR/run" +#!/bin/sh +exec /opt/bin/system-reporting +EOF + +chmod +x "$SERVICE_DIR/run" + +echo "Creating the 'env' directory for environment variables..." +mkdir -p "$SERVICE_DIR/env" + +echo "http://192.168.1.83:8086/write?db=metrics" >"$SERVICE_DIR/env/INFLUX_HOST" + +chmod 600 $SERVICE_DIR/env/* + +echo "Finished creating service files!" +echo "" +echo "Complete the installation by linking to your /var/service directory" +echo "$ESC ln -s $SERVICE_DIR /var/service" +echo +echo "Then enable and start the service" +echo "sv enable system-reporting" +echo "sv start system-reporting" diff --git a/system-reporting.go b/system-reporting.go index a22d06d..58487d5 100644 --- a/system-reporting.go +++ b/system-reporting.go @@ -12,7 +12,6 @@ import ( "time" "github.com/charmbracelet/log" - "github.com/shirou/gopsutil/v4/host" "github.com/shirou/gopsutil/v4/load" "github.com/shirou/gopsutil/v4/mem" "github.com/shirou/gopsutil/v4/sensors" @@ -83,17 +82,13 @@ func main() { mwriter := io.MultiWriter(os.Stdout, logFile) log.SetOutput(mwriter) - log.SetFormatter(log.TextFormatter) + log.SetFormatter(log.LogfmtFormatter) log.SetLevel(log.InfoLevel) - if config.Device != "" { - hostname = config.Device - } else { - hostname, err = host.HostID() - log.Info("Using HostID", "id", hostname) - if hostname == "" || err != nil { - log.Error("Couldn't get proper hostname!", "err", err) - hostname="ERROR" + if hostname = os.Getenv("DEVICE"); hostname == "" { + hostname, err = os.Hostname() + if err != nil { + log.Fatal("Error getting device hostname", "err", err) } }