add mattermost msg support

This commit is contained in:
2020-02-04 17:57:00 -05:00
parent b607e652fe
commit bfc34ca14c
+62 -8
View File
@@ -196,6 +196,14 @@ def email(s, msg):
pass pass
return ret return ret
def pushmsg(msg):
if pushsrv == "pushover":
return pushover(msg)
elif pushsrv == "mattermost":
return pushmattermost(msg)
else:
print(msg)
def pushover(msg): def pushover(msg):
if not SEND_PUSHOVER: if not SEND_PUSHOVER:
@@ -211,6 +219,32 @@ def pushover(msg):
except: except:
pass pass
CHANNEL = "Monitoring"
TOKEN = "rxz6b3886iygxnhbzpmgbsrocy"
HOST = "192.168.10.101"
ICON = "https://in-transit.ca/HeartBeat.png"
USERNAME = "nagios"
def pushmattermost(msg):
ses = {
'url': HOST,
'scheme':'http',
'basepath': '/api/v4',
'port':8065,
}
mm = Driver(ses)
msg = {
"text": msg,
"channel": CHANNEL,
"username": USERNAME,
"icon_url": ICON
}
rc = mm.webhooks.call_webhook(TOKEN, msg)
if not rc:
print(rc)
# nsupdate: set the DNS A record for a fqdn # nsupdate: set the DNS A record for a fqdn
# return: None if ok, else error text # return: None if ok, else error text
@@ -292,7 +326,11 @@ def initlog(logfile):
return open(logfile, "a+") return open(logfile, "a+")
except: except:
pass pass
try:
return open(logfile, "w") return open(logfile, "w")
except Exception as e:
print("cannot open loffile %s, using STDERR" % logfile)
return sys.stderr
# #
@@ -314,7 +352,7 @@ def checkoverdue():
if pmsg != []: if pmsg != []:
if h in watchhosts: if h in watchhosts:
email("overdue", "%s overdue" % " and ".join(pmsg)) email("overdue", "%s overdue" % " and ".join(pmsg))
pushover("%s %s overdue" % (h, " and ".join(pmsg))) pushmsg("%s %s overdue" % (h, " and ".join(pmsg)))
log(h, "%s overdue" % " and ".join(pmsg)) log(h, "%s overdue" % " and ".join(pmsg))
@@ -409,19 +447,19 @@ def readsock(sock):
log(name, "booted") log(name, "booted")
if name in watchhosts: if name in watchhosts:
email("booted", m) email("booted", m)
pushover(m) pushmsg(m)
if message: if message:
log(name, "msg: %s" % message, service=service) log(name, "msg: %s" % message, service=service)
if name in watchhosts: if name in watchhosts:
email("msg", message) email("msg", message)
pushover(message) pushmsg(message)
conn, res = host.conndata(cid, addr[0], rtt, now) conn, res = host.conndata(cid, addr[0], rtt, now)
if res: if res:
log(name, res) log(name, res)
if name in watchhosts: if name in watchhosts:
email("address change", "%s %s" % (host.name, res)) email("address change", "%s %s" % (host.name, res))
pushover("%s %s" % (host.name, res)) pushmsg("%s %s" % (host.name, res))
if conn.getstate() != hbdclass.Connection.up: # XXX and interval > 0: if conn.getstate() != hbdclass.Connection.up: # XXX and interval > 0:
lasts = conn.state lasts = conn.state
@@ -430,7 +468,7 @@ def readsock(sock):
log(name, m) log(name, m)
if name in watchhosts: if name in watchhosts:
email("%s back" % conn.afam, name) email("%s back" % conn.afam, name)
pushover("%s %s is back" % (name, conn.afam)) pushmsg("%s %s is back" % (name, conn.afam))
if boot or newh: if boot or newh:
host.upcount = host.doesack host.upcount = host.doesack
@@ -442,7 +480,7 @@ def readsock(sock):
log(name, "%s shutdown" % conn.afam) log(name, "%s shutdown" % conn.afam)
if name in watchhosts: if name in watchhosts:
email("shutdown", "%s %s shutdown" % (name, conn.afam)) email("shutdown", "%s %s shutdown" % (name, conn.afam))
pushover("%s %s shutdown" % (name, conn.afam)) pushmsg("%s %s shutdown" % (name, conn.afam))
conn.newstate(hbdclass.Connection.down, now) conn.newstate(hbdclass.Connection.down, now)
if interval > 0: if interval > 0:
@@ -759,9 +797,10 @@ def pickleit():
# #
# Main # Main
# #
PUSHSRVS = [ "pushover", "mattermost" ]
helpflag = False helpflag = False
forground = False forground = False
pushsrv = "pushover" # mattermost
optlist = [] optlist = []
args = [] args = []
home = os.environ['HOME'] home = os.environ['HOME']
@@ -769,7 +808,7 @@ cmdargs = []
configfile = "%s/.hbrc" % home configfile = "%s/.hbrc" % home
try: try:
optlist, args = getopt.getopt(sys.argv[1:], 'c:dfh:vx') optlist, args = getopt.getopt(sys.argv[1:], 'c:dfh:p:vx')
except: except:
helpflag = True helpflag = True
@@ -785,10 +824,25 @@ for o, a in optlist:
elif o == '-v': elif o == '-v':
verbose = True verbose = True
cmdargs += [o] cmdargs += [o]
elif o == '-p':
if a in PUSHSRVS:
pushsrv = a
cmdargs += [o, a]
else:
print("invalid push service, use of of %s" % PUSHSRVS)
helpflag = True
elif o == '-x': elif o == '-x':
DEBUG += 1 DEBUG += 1
cmdargs += [o] cmdargs += [o]
if pushsrv == "mattermost":
try:
from mattermostdriver import Driver
except:
print("warning: mattermostdriver python module missing, reverting to pushover")
pushsrv = "pushover"
if helpflag: if helpflag:
print("hbc HeartBeatDaemon") print("hbc HeartBeatDaemon")
print("usage: hbd [-dfhvx] [-c configfile]") print("usage: hbd [-dfhvx] [-c configfile]")