New scripts for Icinga and Observium
This commit is contained in:
parent
1feabffe2b
commit
1745ad0be4
3 changed files with 72 additions and 83 deletions
45
icinga_host_notify.py
Executable file
45
icinga_host_notify.py
Executable file
|
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pydbus import SystemBus
|
||||
from gi.repository import GLib
|
||||
import sms_functions
|
||||
|
||||
def assemble_sms():
|
||||
str_list = []
|
||||
str_list.append("Icinga {}\n".format(os.environ.get('NOTIFICATIONTYPE')))
|
||||
str_list.append("{} is {} [{}]\n".format(os.environ.get('HOSTALIAS'), os.environ.get('HOSTSTATE'), os.environ.get('HOSTADDRESS')))
|
||||
str_list.append("since {}\n".format(os.environ.get('LASTSTATECHANGE')))
|
||||
str_list.append("Comment: [{}] {}".format(os.environ.get('NOTIFICATIONAUTHORNAME'), os.environ.get('NOTIFICATIONCOMMENT')))
|
||||
return ''.join(str_list)
|
||||
|
||||
# Setup logging
|
||||
logfile = "/opt/sms/sms.log"
|
||||
FORMAT = '%(asctime)-15s %(message)s'
|
||||
logging.basicConfig(format=FORMAT,filename=logfile,level=10)
|
||||
logger = logging.getLogger('observiumsms')
|
||||
|
||||
# check param
|
||||
smsrcpt = os.environ.get('USERMOBILE')
|
||||
if not smsrcpt:
|
||||
logger.error('No SMS number given')
|
||||
raise Exception('No SMS number given for message')
|
||||
|
||||
# Assemble SMS
|
||||
smsstring = assemble_sms()
|
||||
logger.info("Sending SMS: %s", smsstring)
|
||||
|
||||
success = False
|
||||
retries = 0
|
||||
max_retries = 3
|
||||
|
||||
while not success and retries < max_retries:
|
||||
retries += 1
|
||||
try:
|
||||
# Contact ModemManager, unlock SIM and send SMS to emergency numbers
|
||||
sms_functions.send_sms([ smsrcpt ], smsstring)
|
||||
except sms_functions.ModemManagerError as err:
|
||||
logger.error('Error when sending SMS: {0}'.format(e.value))
|
||||
else:
|
||||
success = True
|
||||
Loading…
Add table
Add a link
Reference in a new issue