Skip to content

Commit

Permalink
Merge pull request #93 from pyouroboros/pushover
Browse files Browse the repository at this point in the history
add pushover functionality. Finishes other half of #80
  • Loading branch information
dirtycajunrice authored Jan 13, 2019
2 parents 3315387 + ea6b8b7 commit fcc955d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
30 changes: 26 additions & 4 deletions pyouroboros/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
class Config(object):
options = ['INTERVAL', 'PROMETHEUS', 'DOCKER_SOCKETS', 'MONITOR', 'IGNORE', 'LOG_LEVEL', 'PROMETHEUS_ADDR',
'PROMETHEUS_PORT', 'WEBHOOK_URLS', 'REPO_USER', 'REPO_PASS', 'CLEANUP', 'RUN_ONCE', 'LATEST',
'INFLUX_URL', 'INFLUX_PORT', 'INFLUX_USERNAME', 'INFLUX_PASSWORD', 'INFLUX_DATABASE',
'INFLUX_SSL', 'INFLUX_VERIFY_SSL', 'DATA_EXPORT']
'INFLUX_URL', 'INFLUX_PORT', 'INFLUX_USERNAME', 'INFLUX_PASSWORD', 'INFLUX_DATABASE', 'INFLUX_SSL',
'INFLUX_VERIFY_SSL', 'DATA_EXPORT', 'PUSHOVER_TOKEN', 'PUSHOVER_USER', 'PUSHOVER_DEVICE']

interval = 300
docker_sockets = 'unix://var/run/docker.sock'
monitor = []
ignore = []
webhook_urls = []
webhook_type = 'slack'
data_export = None
log_level = 'info'
latest = False
Expand All @@ -36,6 +34,12 @@ class Config(object):
influx_password = 'root'
influx_database = None

webhook_urls = []

pushover_token = None
pushover_user = None
pushover_device = None

def __init__(self, environment_vars, cli_args):
self.cli_args = cli_args
self.environment_vars = environment_vars
Expand Down Expand Up @@ -76,6 +80,12 @@ def parse(self):
setattr(self, option.lower(), opt)
except ValueError as e:
print(e)
elif option in ['LATEST', 'CLEANUP', 'RUN_ONCE', 'INFLUX_SSL', 'INFLUX_VERIFY_SSL']:
if self.environment_vars[option].lower() == 'true':
setattr(self, option.lower(), True)
else:
self.logger.error('%s is not a valid option for %s. setting to false',
self.environment_vars[option], option)
else:
setattr(self, option.lower(), self.environment_vars[option])
elif vars(self.cli_args).get(option):
Expand All @@ -93,4 +103,16 @@ def parse(self):
string_list = getattr(self, option)
setattr(self, option, [string.strip(' ') for string in string_list.split(' ')])

# Config sanity checks
if self.data_export == 'influxdb' and not self.influx_database:
self.logger.error("You need to specify an influx database if you want to export to influxdb. Disabling "
"influxdb data export.")

pushover_config = [self.pushover_token, self.pushover_device, self.pushover_user]
if any(pushover_config) and not all(pushover_config):
self.logger.error('You must specify a pushover user, token, and device to use pushover. Disabling '
'pushover notifications')
elif all(pushover_config):
self.webhook_urls.append('https://api.pushover.net/1/messages.json')

self.config_blacklist()
5 changes: 3 additions & 2 deletions pyouroboros/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class BlacklistFilter(Filter):
Log filter for blacklisted tokens and passwords
"""

blacklisted_keys = ['repo_user', 'repo_pass', 'auth_json', 'webhook_urls', 'docker_sockets',
'prometheus_exporter_addr', 'influx_username', 'influx_password', 'influx_url']
blacklisted_keys = ['repo_user', 'repo_pass', 'auth_json', 'webhook_urls', 'docker_sockets', 'pushover_user',
'prometheus_addr', 'influx_username', 'influx_password', 'influx_url', 'pushover_token',
'pushover_device']

def __init__(self, filteredstrings):
super().__init__()
Expand Down
16 changes: 14 additions & 2 deletions pyouroboros/notifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def send(self, container_tuples=None, socket=None, notification_type='data'):
format_type = 'discord'
elif 'slack' in webhook_url:
format_type = 'slack'
elif 'pushover' in webhook_url:
format_type = 'pushover'
elif 'hc-ping' in webhook_url:
continue
else:
Expand All @@ -35,7 +37,7 @@ def send(self, container_tuples=None, socket=None, notification_type='data'):
def format(self, container_tuples, socket, format_type):
clean_socket = socket.split("//")[1]
now = str(datetime.now(timezone.utc)).replace(" ", "T")
if format_type in ['slack', 'default']:
if format_type in ['slack', 'default', 'pushover']:
text = "Host Socket: {}\n".format(clean_socket)
text += "Containers Monitored: {}\n".format(self.data_manager.monitored_containers[socket])
text += "Containers Updated: {}\n".format(self.data_manager.total_updated[socket])
Expand All @@ -46,7 +48,17 @@ def format(self, container_tuples, socket, format_type):
new_image.short_id.split(":")[1]
)
text += now
json = {"text": text}
if format_type == 'pushover':
json = {
"html": 1,
"token": self.config.pushover_token,
"user": self.config.pushover_user,
"device": self.config.pushover_device,
"title": "Ouroboros updated containers:",
"message": text
}
else:
json = {"text": text}
return json

elif format_type == 'discord':
Expand Down
25 changes: 15 additions & 10 deletions pyouroboros/ouroboros.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,24 @@ def main():
data_group.add_argument('-V', '--influx-verify-ssl', default=False, dest='INFLUX_VERIFY_SSL', action='store_true',
help='Verify SSL certificate when connecting to influxdb')

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

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

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

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

args = parser.parse_args()

if environ.get('LOG_LEVEL'):
log_level = environ.get('LOG_LEVEL')
Expand Down

0 comments on commit fcc955d

Please sign in to comment.