PageRenderTime 95ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/zabbix-alert-smtp.sh

https://bitbucket.org/superdaigo/zabbix-alert-smtp
Shell | 99 lines | 70 code | 12 blank | 17 comment | 11 complexity | 2aa282c15a455829b89cd9333769303c MD5 | raw file
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Zabbix SMTP Alert script (Gmail and SES available).
  5. """
  6. import sys
  7. import smtplib
  8. from email.MIMEText import MIMEText
  9. from email.Header import Header
  10. from email.Utils import formatdate
  11. try:
  12. from settings import (SENDER_EMAIL, SMTP_USERNAME, SMTP_PASSWORD, SMTP_SERVER,
  13. SMTP_PORT, SENDER_NAME, SMTP_SSL_TYPE)
  14. except ImportError as e:
  15. pass
  16. SETTINGS_EXAMPLE = """# settings.py - zabbix-alert-smtp
  17. # Mail Account
  18. SENDER_NAME = u'Zabbix Alert'
  19. SENDER_EMAIL = 'your.account@gmail.com'
  20. # Gmail
  21. SMTP_USERNAME = 'your.account@gmail.com'
  22. SMTP_PASSWORD = 'your mail password'
  23. # Mail Server
  24. SMTP_SERVER = 'smtp.gmail.com'
  25. SMTP_PORT = 587
  26. # SSL Type ('SMTP_TLS': Gmail, 'SMTP_SSL': SES, None: no SSL)
  27. SMTP_SSL_TYPE = SMTP_TLS
  28. # # Amazon SES
  29. # SMTP_USERNAME = 'Access Key Id'
  30. # SMTP_PASSWORD = 'Secret Access Key'
  31. #
  32. # # Mail Server
  33. # SMTP_SERVER = 'email-smtp.us-east-1.amazonaws.com'
  34. # SMTP_PORT = 587
  35. #
  36. # # SSL Type ('SMTP_TLS': Gmail, 'SMTP_SSL': SES, None: no SSL)
  37. # SMTP_SSL_TYPE = SMTP_SSL
  38. """
  39. SETTINGS_ERROR = """Create your own settings.py file and edit it.
  40. $ ./zabbix-alert-smtp.sh example > settings.py
  41. """
  42. ARGUMENT_ERROR = """requires 3 parameters (recipient, subject, body)
  43. $ zabbix-alert-smtp.sh recipient subject body
  44. """
  45. def send_mail(recipient, subject, body, encoding='utf-8'):
  46. session = None
  47. msg = MIMEText(body, 'plain', encoding)
  48. msg['Subject'] = Header(subject, encoding)
  49. msg['From'] = Header(u'"{0}" <{1}>'.format(SENDER_NAME, SENDER_EMAIL), encoding)
  50. msg['To'] = recipient
  51. msg['Date'] = formatdate()
  52. try:
  53. if SMTP_SSL_TYPE == 'SMTP_SSL':
  54. session = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
  55. else:
  56. session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
  57. if SMTP_SSL_TYPE == 'SMTP_TLS':
  58. session.ehlo()
  59. session.starttls()
  60. session.ehlo()
  61. session.login(SMTP_USERNAME, SMTP_PASSWORD)
  62. session.sendmail(SENDER_EMAIL, recipient, msg.as_string())
  63. except Exception as e:
  64. raise e
  65. finally:
  66. # close session
  67. if session:
  68. session.quit()
  69. if __name__ == '__main__':
  70. """
  71. recipient = sys.argv[1]
  72. subject = sys.argv[2]
  73. body = sys.argv[3]
  74. """
  75. try:
  76. SENDER_EMAIL
  77. except:
  78. print SETTINGS_ERROR
  79. else:
  80. if len(sys.argv) == 4:
  81. send_mail(recipient=sys.argv[1],
  82. subject=sys.argv[2],
  83. body=sys.argv[3])
  84. elif (len(sys.argv) == 2 and sys.argv[1] == 'example'):
  85. print SETTINGS_EXAMPLE
  86. else:
  87. print ARGUMENT_ERROR