Skip to content

Commit fcc955d

Browse files
Merge pull request #93 from pyouroboros/pushover
add pushover functionality. Finishes other half of #80
2 parents 3315387 + ea6b8b7 commit fcc955d

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

pyouroboros/config.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
class Config(object):
66
options = ['INTERVAL', 'PROMETHEUS', 'DOCKER_SOCKETS', 'MONITOR', 'IGNORE', 'LOG_LEVEL', 'PROMETHEUS_ADDR',
77
'PROMETHEUS_PORT', 'WEBHOOK_URLS', 'REPO_USER', 'REPO_PASS', 'CLEANUP', 'RUN_ONCE', 'LATEST',
8-
'INFLUX_URL', 'INFLUX_PORT', 'INFLUX_USERNAME', 'INFLUX_PASSWORD', 'INFLUX_DATABASE',
9-
'INFLUX_SSL', 'INFLUX_VERIFY_SSL', 'DATA_EXPORT']
8+
'INFLUX_URL', 'INFLUX_PORT', 'INFLUX_USERNAME', 'INFLUX_PASSWORD', 'INFLUX_DATABASE', 'INFLUX_SSL',
9+
'INFLUX_VERIFY_SSL', 'DATA_EXPORT', 'PUSHOVER_TOKEN', 'PUSHOVER_USER', 'PUSHOVER_DEVICE']
1010

1111
interval = 300
1212
docker_sockets = 'unix://var/run/docker.sock'
1313
monitor = []
1414
ignore = []
15-
webhook_urls = []
16-
webhook_type = 'slack'
1715
data_export = None
1816
log_level = 'info'
1917
latest = False
@@ -36,6 +34,12 @@ class Config(object):
3634
influx_password = 'root'
3735
influx_database = None
3836

37+
webhook_urls = []
38+
39+
pushover_token = None
40+
pushover_user = None
41+
pushover_device = None
42+
3943
def __init__(self, environment_vars, cli_args):
4044
self.cli_args = cli_args
4145
self.environment_vars = environment_vars
@@ -76,6 +80,12 @@ def parse(self):
7680
setattr(self, option.lower(), opt)
7781
except ValueError as e:
7882
print(e)
83+
elif option in ['LATEST', 'CLEANUP', 'RUN_ONCE', 'INFLUX_SSL', 'INFLUX_VERIFY_SSL']:
84+
if self.environment_vars[option].lower() == 'true':
85+
setattr(self, option.lower(), True)
86+
else:
87+
self.logger.error('%s is not a valid option for %s. setting to false',
88+
self.environment_vars[option], option)
7989
else:
8090
setattr(self, option.lower(), self.environment_vars[option])
8191
elif vars(self.cli_args).get(option):
@@ -93,4 +103,16 @@ def parse(self):
93103
string_list = getattr(self, option)
94104
setattr(self, option, [string.strip(' ') for string in string_list.split(' ')])
95105

106+
# Config sanity checks
107+
if self.data_export == 'influxdb' and not self.influx_database:
108+
self.logger.error("You need to specify an influx database if you want to export to influxdb. Disabling "
109+
"influxdb data export.")
110+
111+
pushover_config = [self.pushover_token, self.pushover_device, self.pushover_user]
112+
if any(pushover_config) and not all(pushover_config):
113+
self.logger.error('You must specify a pushover user, token, and device to use pushover. Disabling '
114+
'pushover notifications')
115+
elif all(pushover_config):
116+
self.webhook_urls.append('https://api.pushover.net/1/messages.json')
117+
96118
self.config_blacklist()

pyouroboros/logger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ class BlacklistFilter(Filter):
66
Log filter for blacklisted tokens and passwords
77
"""
88

9-
blacklisted_keys = ['repo_user', 'repo_pass', 'auth_json', 'webhook_urls', 'docker_sockets',
10-
'prometheus_exporter_addr', 'influx_username', 'influx_password', 'influx_url']
9+
blacklisted_keys = ['repo_user', 'repo_pass', 'auth_json', 'webhook_urls', 'docker_sockets', 'pushover_user',
10+
'prometheus_addr', 'influx_username', 'influx_password', 'influx_url', 'pushover_token',
11+
'pushover_device']
1112

1213
def __init__(self, filteredstrings):
1314
super().__init__()

pyouroboros/notifiers.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def send(self, container_tuples=None, socket=None, notification_type='data'):
2323
format_type = 'discord'
2424
elif 'slack' in webhook_url:
2525
format_type = 'slack'
26+
elif 'pushover' in webhook_url:
27+
format_type = 'pushover'
2628
elif 'hc-ping' in webhook_url:
2729
continue
2830
else:
@@ -35,7 +37,7 @@ def send(self, container_tuples=None, socket=None, notification_type='data'):
3537
def format(self, container_tuples, socket, format_type):
3638
clean_socket = socket.split("//")[1]
3739
now = str(datetime.now(timezone.utc)).replace(" ", "T")
38-
if format_type in ['slack', 'default']:
40+
if format_type in ['slack', 'default', 'pushover']:
3941
text = "Host Socket: {}\n".format(clean_socket)
4042
text += "Containers Monitored: {}\n".format(self.data_manager.monitored_containers[socket])
4143
text += "Containers Updated: {}\n".format(self.data_manager.total_updated[socket])
@@ -46,7 +48,17 @@ def format(self, container_tuples, socket, format_type):
4648
new_image.short_id.split(":")[1]
4749
)
4850
text += now
49-
json = {"text": text}
51+
if format_type == 'pushover':
52+
json = {
53+
"html": 1,
54+
"token": self.config.pushover_token,
55+
"user": self.config.pushover_user,
56+
"device": self.config.pushover_device,
57+
"title": "Ouroboros updated containers:",
58+
"message": text
59+
}
60+
else:
61+
json = {"text": text}
5062
return json
5163

5264
elif format_type == 'discord':

pyouroboros/ouroboros.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,24 @@ def main():
9696
data_group.add_argument('-V', '--influx-verify-ssl', default=False, dest='INFLUX_VERIFY_SSL', action='store_true',
9797
help='Verify SSL certificate when connecting to influxdb')
9898

99-
data_group.add_argument('-w', '--webhook-urls', nargs='+', default=Config.webhook_urls, dest='WEBHOOK_URLS',
100-
help='Webhook POST urls\n'
101-
'EXAMPLE: -w https://domain.tld/1234/asdf http://123.123.123.123:4040/re235')
99+
notification_group = parser.add_argument_group('Notifications', 'Configuration of notification functionality')
100+
notification_group.add_argument('-w', '--webhook-urls', nargs='+', default=Config.webhook_urls, dest='WEBHOOK_URLS',
101+
help='Webhook POST urls\n'
102+
'EXAMPLE: -w https://domain.tld/1234/asdf http://123.123.123.123:4040/re235')
102103

103-
args = parser.parse_args()
104+
notification_group.add_argument('-y', '--pushover-token', default=Config.pushover_token, dest='PUSHOVER_TOKEN',
105+
help='Pushover token to authenticate against application\n'
106+
'EXAMPLE: -y af2r52352asd')
104107

105-
if environ.get('DATA_EXPORT'):
106-
data_export = environ['DATA_EXPORT']
107-
else:
108-
data_export = args.DATA_EXPORT
108+
notification_group.add_argument('-Y', '--pushover-device', default=Config.pushover_device, dest='PUSHOVER_DEVICE',
109+
help='Device to receive pushover notification\n'
110+
'EXAMPLE: -Y SamsungGalaxyS8')
109111

110-
if data_export == 'influxdb' and not (environ.get('INFLUX_DATABASE') or args.INFLUX_DATABASE):
111-
exit("You need to specify an influx database if you want to export to influxdb")
112+
notification_group.add_argument('-z', '--pushover-user', default=Config.pushover_user, dest='PUSHOVER_USER',
113+
help='Pushover user bound to application\n'
114+
'EXAMPLE: -y johndoe123')
115+
116+
args = parser.parse_args()
112117

113118
if environ.get('LOG_LEVEL'):
114119
log_level = environ.get('LOG_LEVEL')

0 commit comments

Comments
 (0)