Skip to content

Commit

Permalink
Log file size (#174)
Browse files Browse the repository at this point in the history
* added version into log file
* moved to log rotation of venus os - limit to 2 logfiles max at 150 Kilobytes
* Set Logging Level to CRITICAL as default
* Update documentation and add section on how to update the driver
  • Loading branch information
henne49 authored May 27, 2024
1 parent fdf1040 commit 3aff48a
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 43 deletions.
36 changes: 28 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ The following commands should do everything for you:
wget -O main.zip https://github.com/henne49/dbus-opendtu/archive/refs/heads/main.zip
unzip main.zip "dbus-opendtu-main/*" -d /data
mv /data/dbus-opendtu-main /data/dbus-opendtu
chmod a+x /data/dbus-opendtu/install.sh
chmod a+x /data/dbus-opendtu/*.sh
```

⚠️**Edit the following configuration file according to your needs before proceeding**⚠️ see [Configuration](#configuration) for details.
Expand All @@ -72,11 +72,33 @@ Tha last step is to install the service and remove the downloaded files:

```bash
/data/dbus-opendtu/install.sh
/data/dbus-opendtu/restart.sh
rm main.zip
```

Check configuration after that - because the service is already installed and running. In case of wrong connection data (host, username, pwd) you will spam the log-file! Also, check to **set a** proper (minimal) **log level**

### Update the code
Just grap a copy of the main branch and copy the content to `/data/` e.g. `/data/dbus-opendtu`. The process will preserve your existing config ini.
After that call the `install.sh script.

```bash
cp /data/dbus-opendtu/config.ini /data/dbus-opendtu/config.backup
wget -O main.zip https://github.com/henne49/dbus-opendtu/archive/refs/heads/main.zip
unzip main.zip "dbus-opendtu-main/*" -d /data
mv /data/dbus-opendtu-main/config.ini /data/dbus-opendtu-main/config.template
cp -R /data/dbus-opendtu-main/* /data/dbus-opendtu
rm -rf /data/dbus-opendtu-main/
chmod a+x /data/dbus-opendtu/*.sh
/data/dbus-opendtu/uninstall.sh
/data/dbus-opendtu/install.sh
/data/dbus-opendtu/restart.sh
rm main.zip
```
The last 4 step is to install the service and remove the downloaded files:

If the script does not work or start, please check the config.template file and update your config.ini. Or reconfigure the config.template with your configuration and save as config.ini. The process also creates a copy of your old config.ini called config.backup.

### Configuration

Within the project there is a file `/data/dbus-opendtu/config.ini`. Most important is the DTU variant, Host and Username and Password, if you use authentication.
Expand All @@ -85,7 +107,7 @@ Within the project there is a file `/data/dbus-opendtu/config.ini`. Most importa

| Config value | Explanation |
|-------------------- | ------------- |
| SignOfLifeLog | Time in minutes how often a status is added to the log-file `current.log` with log-level INFO |
| SignOfLifeLog | Time in minutes how often a status is added to the log-file `current` with log-level INFO |
| NumberOfTemplates | Number ob Template Inverter to query |
| DTU | Which DTU to be used ahoy, opendtu or template REST devices Valid options: opendtu, ahoy, template |
| NumberOfInvertersToQuery | Number of Inverters to query. Set a value larger than "0" when not all inverters should be considered. *1 |
Expand Down Expand Up @@ -192,7 +214,7 @@ A Basic configuration could look like this:
DTU=ahoy

#Possible Options for Log Level: CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET
#To keep current.log small use ERROR
#To keep current log small use ERROR or CRITICAL
Logging=ERROR

#IP of Device to query <-- THIS IS THE IP OF THE DTU
Expand Down Expand Up @@ -229,7 +251,7 @@ A Basic configuration could look like this:
DTU=ahoy

#Possible Options for Log Level: CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET
#To keep current.log small use ERROR
#To keep current log small use ERROR or CRITICAL
Logging=ERROR

#IP of Device to query <-- THIS IS THE IP OF THE DTU
Expand Down Expand Up @@ -257,7 +279,7 @@ These are some useful commands which help to use the script or to debug.

### Check if the script is running

`svstat /service/dbus-opendtu` show if the service (our script) is running. If the number of seconds shown is low, it is probably restarting and you should look into `/data/dbus-opendtu/current.log`.
`svstat /service/dbus-opendtu` show if the service (our script) is running. If the number of seconds shown is low, it is probably restarting and you should look into `/var/log/dbus-opendtu/current`.

### How to debug

Expand All @@ -275,8 +297,6 @@ This also activates the service, so you don't need to run `svcadm enable /servic

`/data/dbus-opendtu/restart.sh` restarts the service - e.g. after a config.ini change.

This also clears the logfile, so you can see the latest output in `/data/dbus-opendtu/current.log`.

### How to uninstall

`/data/dbus-opendtu/uninstall.sh` stops the service and prevents it from being restarted (e.g. after a reboot).
Expand Down Expand Up @@ -332,7 +352,7 @@ All [configuration](#configuration) is done via config.ini. Examples are comment

Please open a new issue on github, only here we can work on your problem in a structured way: <https://github.com/henne49/dbus-opendtu/issues/new/choose>

⚠️ **Change the Logging Parameter under DEFAULT in /data/dbus-opendtu/config.ini to Logging = DEBUG, please revert, once debugging and troubleshooting is complete. Rerun the script and share the current.log file**.
⚠️ **Change the Logging Parameter under DEFAULT in /data/dbus-opendtu/config.ini to Logging = DEBUG, please revert back to ERROR or CRITICAL, once debugging and troubleshooting is complete. Rerun the script and share the current log file in /var/log/dbus-opendtu/current**.

Please provide the config.ini and JSON file and upload to the github issues, you can download the JSON file using your browser or using a commandline like tool like curl

Expand Down
38 changes: 18 additions & 20 deletions dbus-opendtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,32 @@ def main():
config.read(f"{(os.path.dirname(os.path.realpath(__file__)))}/config.ini")
logging_level = config["DEFAULT"]["Logging"].upper()
dtuvariant = config["DEFAULT"]["DTU"]
number_of_inverters = int(config["DEFAULT"]["NumberOfInvertersToQuery"])

try:
number_of_templates = int(config["DEFAULT"]["NumberOfTemplates"])
except Exception:
number_of_templates = 0

log_rotate_handler = logging.handlers.RotatingFileHandler(
maxBytes=5*1024*1024*10,
backupCount=2,
encoding=None,
delay=0,
filename="%s/current.log" % (os.path.dirname(os.path.realpath(__file__)))
)
logging.basicConfig(
format="%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging_level,
handlers=[
logging.StreamHandler(),
log_rotate_handler
],
)

try:
number_of_inverters = int(config["DEFAULT"]["NumberOfInvertersToQuery"])
except (KeyError, ValueError) as ex:
logging.warning("NumberOfInvertersToQuery: %s", ex)
logging.warning("NumberOfInvertersToQuery not set, using default")
number_of_inverters = 0

try:
number_of_templates = int(config["DEFAULT"]["NumberOfTemplates"])
except (KeyError, ValueError) as ex:
logging.warning("NumberOfTemplates: %s", ex)
logging.warning("NumberOfTemplates not set, using default")
number_of_templates = 0


tests.run_tests()

try:
logging.info("Start")
logging.critical("Start")

from dbus.mainloop.glib import DBusGMainLoop # pylint: disable=E0401,C0415

Expand Down Expand Up @@ -106,7 +104,7 @@ def _v(_p, value: float) -> str:
}

if dtuvariant != constants.DTUVARIANT_TEMPLATE:
logging.info("Registering dtu devices")
logging.critical("Registering dtu devices")
servicename = get_config_value(config, "Servicename", "INVERTER", 0, "com.victronenergy.pvinverter")
service = DbusService(
servicename=servicename,
Expand Down Expand Up @@ -134,7 +132,7 @@ def _v(_p, value: float) -> str:
)

for actual_template in range(number_of_templates):
logging.info("Registering Templates")
logging.critical("Registering Templates")
servicename = get_config_value(
config,
"Servicename",
Expand Down
10 changes: 5 additions & 5 deletions dbus_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(
else:
self._read_config_template(actual_inverter)

logging.debug("%s /DeviceInstance = %d", servicename, self.deviceinstance)
logging.critical("%s /DeviceInstance = %d", servicename, self.deviceinstance)

# Allow for multiple Instance per process in DBUS
dbus_conn = (
Expand Down Expand Up @@ -205,8 +205,8 @@ def _read_config_dtu(self, actual_inverter):
try:
self.max_age_ts = int(config["DEFAULT"]["MaxAgeTsLastSuccess"])
except (KeyError, ValueError) as ex:
logging.debug("MaxAgeTsLastSuccess: %s", ex)
logging.debug("MaxAgeTsLastSuccess not set, using default")
logging.warning("MaxAgeTsLastSuccess: %s", ex)
logging.warning("MaxAgeTsLastSuccess not set, using default")
self.max_age_ts = 600

self.dry_run = is_true(get_default_config(config, "DryRun", False))
Expand Down Expand Up @@ -260,8 +260,8 @@ def _read_config_template(self, template_number):
try:
self.max_age_ts = int(config["DEFAULT"]["MaxAgeTsLastSuccess"])
except (KeyError, ValueError) as ex:
logging.debug("MaxAgeTsLastSuccess: %s", ex)
logging.debug("MaxAgeTsLastSuccess not set, using default")
logging.warning("MaxAgeTsLastSuccess: %s", ex)
logging.warning("MaxAgeTsLastSuccess not set, using default")
self.max_age_ts = 600

self.dry_run = is_true(get_default_config(config, "DryRun", False))
Expand Down
8 changes: 8 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SERVICE_NAME=$(basename $SCRIPT_DIR)

# delete old logs if they exist
if [ -f /data/dbus-opendtu/current.log ]; then
rm /data/dbus-opendtu/current.log*
fi

# set permissions for script files
chmod a+x $SCRIPT_DIR/restart.sh
chmod 744 $SCRIPT_DIR/restart.sh
Expand All @@ -12,6 +17,9 @@ chmod 744 $SCRIPT_DIR/uninstall.sh
chmod a+x $SCRIPT_DIR/service/run
chmod 755 $SCRIPT_DIR/service/run

chmod a+x $SCRIPT_DIR/service/log/run
chmod 755 $SCRIPT_DIR/service/log/run

# create sym-link to run script in deamon
ln -s $SCRIPT_DIR/service /service/$SERVICE_NAME

Expand Down
3 changes: 0 additions & 3 deletions restart.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

rm $SCRIPT_DIR/current.log

kill $(pgrep -f "python $SCRIPT_DIR/dbus-opendtu.py")
4 changes: 4 additions & 0 deletions service/log/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
exec 2>&1
# documentation on how to use multilog https://manpages.debian.org/stretch/daemontools/multilog.8.en.html
exec multilog t s153600 n2 /var/log/dbus-opendtu
9 changes: 8 additions & 1 deletion service/run
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/bin/sh
#Script Directory
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

#Define the version filename
FILENAME=$(realpath $SCRIPT_DIR/../version.txt)
exec 2>&1
#Check if the file exists
if [ -f "$FILENAME" ]; then
#If the file exists, print its contents to stdout
cat "$FILENAME"
fi
python $(realpath $SCRIPT_DIR/../dbus-opendtu.py)
14 changes: 9 additions & 5 deletions uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SERVICE_NAME=$(basename $SCRIPT_DIR)
filename=/data/rc.local

#remove the service
rm /service/$SERVICE_NAME

# end the dbus-opendtu process
kill $(pgrep -f 'supervise dbus-opendtu')
chmod a-x $SCRIPT_DIR/service/run
$SCRIPT_DIR/restart.sh
STARTUP=$SCRIPT_DIR/install.sh
#sed -i "\~/data/dbus-opendtu/install.sh~d" $filename
sed -i "\~$STARTUP~d" $filename

# delete old logs if they exist
if [ -f /data/dbus-opendtu/current.log ]; then
rm /data/dbus-opendtu/current.log*
fi
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Version: 1.1.20
Version: 1.2.0

0 comments on commit 3aff48a

Please sign in to comment.