#!/usr/bin/env python3 # # Imports import boto3 import argparse import os import datetime parser = argparse.ArgumentParser() parser.add_argument('PhoneNumber', help="Mobile Number in E.164 format") parser.add_argument("subject", help="Alert Subject") parser.add_argument('body', help="Message Body") args = parser.parse_args() sns = boto3.client('sns',region_name='us-east-1') response = sns.publish( PhoneNumber=args.PhoneNumber, Message=args.body, MessageAttributes = { 'AWS.SNS.SMS.SMSType': { 'DataType': 'String', 'StringValue': 'Transactional' } } ) NOW = datetime.datetime.now() logfile = '/etc/zabbix/alertscripts/direct-to-mobile.log' #create log file if it doesn't exist if not os.path.exists(logfile): with open(logfile, 'w+') as headings: headings.write('Timestamp,PhoneNumber,Subject,Status\n') os.chmod(logfile, 0o660) # Append to the logfile with open(logfile, 'a') as log: line = '{timestamp},{to},{subject},{status}\n'.format(timestamp=NOW.strftime('%Y%m%dT%H%M%S'), to=args.PhoneNumber,subject=args.subject,status=response['ResponseMetadata']['HTTPStatusCode']) log.write(line)