add mattermost msg support

This commit is contained in:
2020-02-04 17:57:00 -05:00
parent b607e652fe
commit bfc34ca14c
+63 -9
View File
@@ -196,6 +196,14 @@ def email(s, msg):
pass
return ret
def pushmsg(msg):
if pushsrv == "pushover":
return pushover(msg)
elif pushsrv == "mattermost":
return pushmattermost(msg)
else:
print(msg)
def pushover(msg):
if not SEND_PUSHOVER:
@@ -211,6 +219,32 @@ def pushover(msg):
except:
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
# return: None if ok, else error text
@@ -292,7 +326,11 @@ def initlog(logfile):
return open(logfile, "a+")
except:
pass
return open(logfile, "w")
try:
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 h in watchhosts:
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))
@@ -409,19 +447,19 @@ def readsock(sock):
log(name, "booted")
if name in watchhosts:
email("booted", m)
pushover(m)
pushmsg(m)
if message:
log(name, "msg: %s" % message, service=service)
if name in watchhosts:
email("msg", message)
pushover(message)
pushmsg(message)
conn, res = host.conndata(cid, addr[0], rtt, now)
if res:
log(name, res)
if name in watchhosts:
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:
lasts = conn.state
@@ -430,7 +468,7 @@ def readsock(sock):
log(name, m)
if name in watchhosts:
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:
host.upcount = host.doesack
@@ -442,7 +480,7 @@ def readsock(sock):
log(name, "%s shutdown" % conn.afam)
if name in watchhosts:
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)
if interval > 0:
@@ -759,9 +797,10 @@ def pickleit():
#
# Main
#
PUSHSRVS = [ "pushover", "mattermost" ]
helpflag = False
forground = False
pushsrv = "pushover" # mattermost
optlist = []
args = []
home = os.environ['HOME']
@@ -769,7 +808,7 @@ cmdargs = []
configfile = "%s/.hbrc" % home
try:
optlist, args = getopt.getopt(sys.argv[1:], 'c:dfh:vx')
optlist, args = getopt.getopt(sys.argv[1:], 'c:dfh:p:vx')
except:
helpflag = True
@@ -785,10 +824,25 @@ for o, a in optlist:
elif o == '-v':
verbose = True
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':
DEBUG += 1
cmdargs += [o]
if pushsrv == "mattermost":
try:
from mattermostdriver import Driver
except:
print("warning: mattermostdriver python module missing, reverting to pushover")
pushsrv = "pushover"
if helpflag:
print("hbc HeartBeatDaemon")
print("usage: hbd [-dfhvx] [-c configfile]")