Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

Commit

Permalink
- Fix message formatting where ". " is found in quoted text
Browse files Browse the repository at this point in the history
- Fix uninitialized variable
- Fix bug in exception handling
- Strip unexpected whitespace from environment variables
- Add update instructions to README.md
  • Loading branch information
TheCatLady committed Jan 13, 2021
1 parent 63e00d4 commit 42641fc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ If you would prefer to pull from GHCR, simply replace `thecatlady/qnap-pushover`

### Docker Compose (recommended)

Add the following volume and service definitions to a `docker-compose.yaml` file:

```yaml
---
version: "2.1"
volumes:
qnap-pushover:
services:
Expand All @@ -46,10 +46,31 @@ services:
restart: always
```

Then, run the following command from the directory containing your `docker-compose.yaml` file:

```bash
docker-compose up -d
```

To update the container when a new image is available, run the following:

```bash
docker-compose pull qnap-pushover
docker-compose up -d
docker image prune
```

### Docker CLI

Run the following command to create the required named volume:

```bash
docker volume create qnap-pushover
```

Then, run the following command to create the container:

```bash
docker run -d \
--name=qnap-pushover \
-e TZ=America/New_York `#optional` \
Expand All @@ -67,6 +88,15 @@ docker run -d \
thecatlady/qnap-pushover
```

To update the container when a new image is available, run the commands below followed by your `docker run` command:

```bash
docker stop qnap-pushover
docker rm qnap-pushover
docker pull thecatlady/qnap-pushover
docker image prune
```

## Parameters

The container image is configured using the following parameters passed at runtime:
Expand Down
29 changes: 15 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@
finally:
if NOTIFY_TYPE == 0:
logging.info("NOTIFY_LEVEL is set to INFO. " \
"All system log events will trigger notifications.")
"Sending notifications for INFO, WARN, and ERROR system log events (all event types).")
elif NOTIFY_TYPE == 1:
logging.info("NOTIFY_LEVEL is set to WARN. " \
"Only WARN and ERROR system log events will trigger notifications; INFO events will not trigger notifications.")
"Sending notifications only for WARN and ERROR system log events; INFO events will not trigger notifications.")
else:
logging.info("NOTIFY_LEVEL is set to ERROR. " \
"ONLY ERROR system log events will not trigger notifications; INFO and WARN events will not trigger notifications.")
"Sending notifications only for ERROR system log events; INFO and WARN events will not trigger notifications.")

try:
POLL_INTERVAL = int(os.environ['POLL_INTERVAL'])
Expand Down Expand Up @@ -91,7 +91,7 @@
logging.info("EXCLUDE keyword filter is not set.")

try:
TESTING_MODE = bool(os.getenv('TESTING_MODE', 'false').lower() in ['true', '1'])
TESTING_MODE = bool(os.getenv('TESTING_MODE', 'false').lower().strip() in ['true', '1'])
except:
TESTING_MODE = False
finally:
Expand All @@ -101,8 +101,8 @@
logging.info("Testing mode is not enabled.")

try:
PUSHOVER_TOKEN = os.environ['PUSHOVER_TOKEN']
PUSHOVER_RECIPIENT = os.environ['PUSHOVER_RECIPIENT']
PUSHOVER_TOKEN = os.environ['PUSHOVER_TOKEN'].strip()
PUSHOVER_RECIPIENT = ''.join(os.environ['PUSHOVER_RECIPIENT'].split())
pushover_client = pushover.Client(PUSHOVER_RECIPIENT, api_token=PUSHOVER_TOKEN)
logging.info(f"Using Pushover application API token {PUSHOVER_TOKEN} and recipient user/group key(s) {PUSHOVER_RECIPIENT}.")

Expand Down Expand Up @@ -181,11 +181,11 @@
message = event_desc

if ". " in message:
message_segments = message.split('. ')
prev = ""
first_line = True
prev = ""
quotes = 0

for s in message_segments:
for s in message.split('. '):
quotes += len(re.findall('"', s))

if quotes % 2 == 0:
Expand All @@ -198,7 +198,7 @@
prev = ""
quotes = 0
else:
prev += s
prev += f"{s}. "
else:
message = f"<font color=\"{COLOR[event_type]}\"><b>{message}</b></font><small>"

Expand All @@ -221,9 +221,10 @@
timestamp = int(time.mktime(time.strptime(event_datetime, "%Y-%m-%d %H:%M:%S")))
priority = event_type - 1

answer = pushover_client.send_message(message, html=1, priority=priority, timestamp=timestamp, title=title).answer
pushover_answer = pushover_client.send_message(message, html=1, priority=priority, timestamp=timestamp, title=title).answer
logging.debug(f"Pushover API response: {pushover_answer}")

if answer["status"] != 1:
if pushover_answer["status"] != 1:
time.sleep(5) # wait an extra 5 seconds
raise Exception("Unable to connect to Pushover API.")

Expand All @@ -237,7 +238,7 @@
f"Skipping the current event and re-queuing the remaining {new_event_count - i} unprocessed events.")
except Exception as e:
last_event_id += i
logging.warning(f"{e.strerror} " \
logging.warning(f"{e}. " \
f"Only {i} of {new_event_count} new system log events were processed successfully. " \
f"Re-queuing the remaining {new_event_count - i} unprocessed events.")
else:
Expand All @@ -252,4 +253,4 @@
except (pushover.InitError, pushover.UserError):
logging.critical(f"Pushover application API token and recipient must be set.")
except Exception as e:
logging.critical(f"{e}")
logging.critical(f"{e}.")

0 comments on commit 42641fc

Please sign in to comment.