Skip to content

Commit

Permalink
Runas goveebttemplogger (#73)
Browse files Browse the repository at this point in the history
* running as goveebttemplogger

* modified:   CMakeLists.txt
	modified:   goveebttemplogger.service
	modified:   postinst

* modified:   CMakeLists.txt
	modified:   goveebttemplogger.service
	modified:   postinst

* modified:   README.md

* move all handling of old format filename to gvh-organizelogs

* modified:   CMakeLists.txt
	modified:   postinst

---------

Co-authored-by: William C Bonner <[email protected]>
  • Loading branch information
wcbonner and William C Bonner authored Oct 1, 2024
1 parent 1c878d9 commit 4b8e5c2
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 152 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (POLICY CMP0115)
endif()

project (GoveeBTTempLogger
VERSION 3.20240925.0
VERSION 3.20241001.0
DESCRIPTION "Listen and log Govee Thermometer Bluetooth Low Energy Advertisments via BlueZ and DBus"
HOMEPAGE_URL https://github.com/wcbonner/GoveeBTTempLogger
)
Expand Down Expand Up @@ -55,7 +55,7 @@ if (CMAKE_VERSION VERSION_GREATER 3.12)
endif()

target_include_directories(goveebttemplogger PUBLIC
"${PROJECT_BINARY_DIR}"
"${PROJECT_BINARY_DIR}"
${EXTRA_INCLUDES}
${dbus_INCLUDE_DIRS}
)
Expand Down Expand Up @@ -108,7 +108,6 @@ set(CPACK_DEBIAN_PACKAGE_MAINTAINER "William C Bonner <${CPACK_PACKAGE_CONTACT}>
include(InstallRequiredSystemLibraries)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
# set(CPACK_DEBIAN_PACKAGE_RELEASE ${CMAKE_PROJECT_VERSION_PATCH})
set(CPACK_DEBIAN_PACKAGE_SECTION custom)
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/postinst" "${CMAKE_CURRENT_SOURCE_DIR}/prerm" "${CMAKE_CURRENT_SOURCE_DIR}/postrm")
set(CPACK_STRIP_FILES YES)
Expand Down
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,35 @@ cmake --build GoveeBTTempLogger/build
pushd GoveeBTTempLogger/build && cpack . && popd
```

The install package will install a systemd unit `goveebttemplogger.service` which will automatically start GoveeBTTempLogger. The service can be configured using environment variables via
The install package will creates a systemd unit `goveebttemplogger.service` which will automatically start GoveeBTTempLogger. The service can be configured via
the `systemctl edit goveebttemplogger.service` command. By default, it writes logs to `/var/log/goveebttemplogger` and writes SVG files to `/var/www/html/goveebttemplogger`.

There are several `ExecStartPre` lines to confirm the directories used exist. To use different directories, both this location and the parameter on the ExecStart line need to be changed.

The [postinst](https://github.com/wcbonner/GoveeBTTempLogger/blob/master/postinst) install routine creates a user and three directories.
It also will change the permissions on those dirctories to be owned by and writable by the newly created user.
```
adduser --system --ingroup www-data goveebttemplogger
mkdir --verbose --mode 0755 --parents /var/log/goveebttemplogger /var/cache/goveebttemplogger /var/www/html/goveebttemplogger
chown --changes --recursive goveebttemplogger:www-data /var/log/goveebttemplogger /var/cache/goveebttemplogger /var/www/html/goveebttemplogger
chmod --changes --recursive 0644 /var/log/goveebttemplogger/* /var/cache/goveebttemplogger/* /var/www/html/goveebttemplogger/*
sudo setcap 'cap_net_raw,cap_net_admin+eip' /usr/local/bin/goveebttemplogger
```

The systemd unit file section `ExecStart` to start the service has been broken into several lines for clarity.

```
[Service]
Type=simple
Restart=always
RestartSec=5
ExecStartPre=/bin/mkdir -p /var/log/goveebttemplogger
ExecStartPre=/bin/mkdir -p /var/www/html/goveebttemplogger
ExecStartPre=/bin/mkdir -p /var/cache/goveebttemplogger
RestartSec=30
User=goveebttemplogger
Group=www-data
ExecStart=/usr/local/bin/goveebttemplogger \
--verbose 0 \
--log /var/log/goveebttemplogger \
--time 60 \
--svg /var/www/html/goveebttemplogger --battery 8 --minmax 8 \
--cache /var/cache/goveebttemplogger \
--download
--cache /var/cache/goveebttemplogger
KillSignal=SIGINT
```

Expand Down
30 changes: 2 additions & 28 deletions goveebttemplogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,8 @@ std::filesystem::path GenerateLogFileName(const bdaddr_t &a, time_t timer = 0)
//OutputFilename << "gvh507x_";
//OutputFilename << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << int(a.b[1]);
//OutputFilename << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << int(a.b[0]);
OutputFilename << "gvh507x_";
// The New Format Log File Name includes the entire Bluetooth Address, making it much easier to recognize and add to MRTG config files.
OutputFilename << "gvh-";
std::string btAddress(ba2string(a));
for (auto pos = btAddress.find(':'); pos != std::string::npos; pos = btAddress.find(':'))
btAddress.erase(pos, 1);
Expand All @@ -1158,34 +1159,7 @@ std::filesystem::path GenerateLogFileName(const bdaddr_t &a, time_t timer = 0)
if (!((UTC.tm_year == 70) && (UTC.tm_mon == 0) && (UTC.tm_mday == 1)))
OutputFilename << "-" << std::dec << UTC.tm_year + 1900 << "-" << std::setw(2) << std::setfill('0') << UTC.tm_mon + 1;
OutputFilename << ".txt";
std::filesystem::path OldFormatFileName(LogDirectory / OutputFilename.str());

// The New Format Log File Name includes the entire Bluetooth Address, making it much easier to recognize and add to MRTG config files.
OutputFilename.str("");
OutputFilename << "gvh-";
OutputFilename << btAddress;
if (!((UTC.tm_year == 70) && (UTC.tm_mon == 0) && (UTC.tm_mday == 1)))
OutputFilename << "-" << std::dec << UTC.tm_year + 1900 << "-" << std::setw(2) << std::setfill('0') << UTC.tm_mon + 1;
OutputFilename << ".txt";
std::filesystem::path NewFormatFileName(LogDirectory / OutputFilename.str());

// This is a temporary hack to transparently change log file name formats
std::ifstream OldFile(OldFormatFileName);
if (OldFile.is_open())
{
OldFile.close();
try
{
std::filesystem::rename(OldFormatFileName, NewFormatFileName);
std::cerr << "[ ] Renamed " << OldFormatFileName << " to " << NewFormatFileName << std::endl;
}
catch (const std::filesystem::filesystem_error& ia)
{
std::cerr << "[ ] " << ia.what() << std::endl;
std::cerr << "[ ] Unable to Rename " << OldFormatFileName << " to " << NewFormatFileName << std::endl;
}
}

return(NewFormatFileName);
}
bool GenerateLogFile(std::map<bdaddr_t, std::queue<Govee_Temp>> &AddressTemperatureMap, std::map<bdaddr_t, time_t> &PersistenceData)
Expand Down
14 changes: 11 additions & 3 deletions goveebttemplogger.service
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
# Contents of /etc/systemd/system/goveebttemplogger.service

[Unit]
Description=GoveeBTTempLogger service
After=bluetooth.target dbus-org.bluez.service network-online.target
Requires=bluetooth.target
StartLimitBurst=5
StartLimitIntervalSec=33

# The user and directories are created by the postinst script with the following commands:
# adduser --system --ingroup www-data goveebttemplogger
# mkdir --verbose --mode 0755 --parents /var/log/goveebttemplogger /var/cache/goveebttemplogger /var/www/html/goveebttemplogger
# chown --changes --recursive goveebttemplogger:www-data /var/log/goveebttemplogger /var/cache/goveebttemplogger /var/www/html/goveebttemplogger
# chmod --changes --recursive 0644 /var/log/goveebttemplogger/* /var/cache/goveebttemplogger/* /var/www/html/goveebttemplogger/*
#
# They are run as root during the installation of the service. The service runs as the newly crteated user.

[Service]
Type=simple
Restart=always
RestartSec=30
ExecStartPre=/bin/mkdir --parents /var/log/goveebttemplogger
ExecStartPre=/bin/mkdir --parents /var/www/html/goveebttemplogger
ExecStartPre=/bin/mkdir --parents /var/cache/goveebttemplogger
User=goveebttemplogger
Group=www-data
ExecStart=/usr/local/bin/goveebttemplogger \
--verbose 0 \
--log /var/log/goveebttemplogger \
Expand Down
Loading

0 comments on commit 4b8e5c2

Please sign in to comment.