Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding slack and pushbullet #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions snapraid-runner.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,21 @@ password =
enabled = false
percentage = 12
older-than = 10


[pushbullet]
; set to true to enable Pushbullet notification support
enabled = true
title = [SnapRAID] Status Report
; API Key: https://www.pushbullet.com/account
apikey =

[slack]
; set to true to enable Pushbullet notification support
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-paste error 😉

enabled = true
title = [SnapRAID] Status Report
; API Key: https://www.pushbullet.com/account
apikey =
channel = #
user =
icon =
64 changes: 60 additions & 4 deletions snapraid-runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/python
# -*- coding: utf8 -*-
import argparse
import ConfigParser
Expand All @@ -9,9 +10,11 @@
import threading
import time
import traceback
import json
import requests
from collections import Counter, defaultdict
from cStringIO import StringIO

from slackclient import SlackClient
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need really to add this dependency for everyone - could instead make it optional and import it within send_slack? (cf.send_email)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I don't use the slack client with my other code at this point. I use requests to make slack calls

# Global variables
config = None
email_log = None
Expand Down Expand Up @@ -101,7 +104,58 @@ def send_email(success):
server.quit()


def send_pushbullet(success):

headers = {"Accept": "application/json", "Content-Type": "application/json", "User-Agent": "Snapraid-runner"}

if success:
body = "SnapRAID job completed successfully\n\n\n"
else:
body = "Error during SnapRAID job\n\n\n"

postdata = {"type": "note", "title": config["pushbullet"]["title"], "body": body}

resp = requests.post('https://api.pushbullet.com/api/pushes',data=postdata, auth=(config["pushbullet"]["apikey"],''))


def send_slack(success):

if success:
body = "SnapRAID job completed successfully\n\n\n"
else:
body = "Error during SnapRAID job\n\n\n"

slack_token = config["slack"]["apikey"]

sc = SlackClient(slack_token)

sc.api_call(
"chat.postMessage",
username=config["slack"]["user"],
icon_emoji=config["slack"]["icon"],
channel=config["slack"]["channel"],
text=body
)




def finish(is_success):
if config["pushbullet"]["enabled"]:
try:
logging.info("Attempting to send Pushbullet notification.")
send_pushbullet(is_success)

except:
logging.exception("Pushbullet notification failed.")
if config["slack"]["enabled"]:
try:
logging.info("Attempting to send Slack notification.")
send_slack(is_success)

except:
logging.exception("Slack notification failed.")

if ("error", "success")[is_success] in config["email"]["sendon"]:
try:
send_email(is_success)
Expand All @@ -118,7 +172,7 @@ def load_config(args):
global config
parser = ConfigParser.RawConfigParser()
parser.read(args.conf)
sections = ["snapraid", "logging", "email", "smtp", "scrub"]
sections = ["snapraid", "logging", "email", "smtp", "pushbullet", "slack", "scrub"]
config = dict((x, defaultdict(lambda: "")) for x in sections)
for section in parser.sections():
for (k, v) in parser.items(section):
Expand All @@ -137,6 +191,8 @@ def load_config(args):
config["smtp"]["ssl"] = (config["smtp"]["ssl"].lower() == "true")
config["scrub"]["enabled"] = (config["scrub"]["enabled"].lower() == "true")
config["email"]["short"] = (config["email"]["short"].lower() == "true")
config["pushbullet"]["enabled"] = (config["pushbullet"]["enabled"].lower() == "true")
config["slack"]["enabled"] = (config["slack"]["enabled"].lower() == "true")

if args.scrub is not None:
config["scrub"]["enabled"] = args.scrub
Expand All @@ -156,7 +212,7 @@ def setup_logger():
root_logger.addHandler(console_logger)

if config["logging"]["file"]:
max_log_size = min(config["logging"]["maxsize"], 0) * 1024
max_log_size = max(config["logging"]["maxsize"], 0) * 1024
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks accidental?

file_logger = logging.handlers.RotatingFileHandler(
config["logging"]["file"],
maxBytes=max_log_size,
Expand All @@ -178,7 +234,7 @@ def setup_logger():
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--conf",
default="snapraid-runner.conf",
default="snapraid-runner ",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks accidental?

metavar="CONFIG",
help="Configuration file (default: %(default)s)")
parser.add_argument("--no-scrub", action='store_false',
Expand Down