The python script is used to send emails to nagios

Source: Internet
Author: User


The python script is used to send emails to nagios


Previously, the company's nagios email sending script was written using reverse CT, but there has always been a drawback that the nagios email text cannot be wrapped and can only be displayed on one line. It seems very difficult to send an alert each time. I always wanted to change it. This line feed problem was solved by using the python script this time.

To put it bluntly, use the script:

 
 
  1. #! /Usr/bin/python

  2. Import smtplib

  3. Import string

  4. Import sys

  5. Import getopt

  6. Def usage ():

  7. Print "" sendmail is a send mail Plugins

  8. Usage:

  9. Sendmail [-h | -- help] [-t | -- to] [-s | -- subject] [-m | -- message]

  10. Options:

  11. -- Help |-h)

  12. Print sendmail help.

  13. -- To |-t)

  14. Sets sendmail to email.

  15. -- Subject |-s)

  16. Sets the mail subject.

  17. -- Message |-m)

  18. Sets the mail body

  19. Example:

  20. Only one to email user

  21. ./Sendmail-t 'Eric @ nginxs.com '-s 'Hello eric'-m' hello eric, this is sendmail test!

  22. Failed to email user

  23. ./Sendmail-t 'Eric @ nginxs.com, zhangsan@nginxs.com '-S' hello eric'-m' hello eric, this is sendmail test! """

  24. Sys. exit (3)

  25. Try:

  26. Options, args = getopt. getopt (sys. argv [1:], "ht: s: m:", ["help", "to =", "subject =", "message ="])

  27. Counter t getopt. GetoptError:

  28. Usage ()

  29. For name, value in options:

  30. If name in ("-h", "-- help "):

  31. Usage ()

  32. If name in ("-t", "-- "):

  33. # Accept message user

  34. TO = value

  35. TO = TO. split (",")

  36. If name in ("-s", "-- title "):

  37. SUBJECT = value

  38. If name in ("-m", "-- message "):

  39. MESSAGE = value

  40. MESSAGE = MESSAGE. split ('\ n ')

  41. MESSAGE = '\ n'. join (MESSAGE)

  42. # Smtp HOST

  43. HOST = "smtp.126.com" # change it to your post office smtp host address

  44. # Smtp port

  45. PORT = "25" # change to the smtp port of your post office

  46. # FROM mail user

  47. USER = 'Eric '# change to your email USER name

  48. # FROM mail password

  49. PASSWD = '000000' # change your email password

  50. # FROM EMAIL

  51. FROM = "test@163.com" # change to your email address

  52. Try:

  53. BODY = string. join ((

  54. "From: % s" % FROM,

  55. "To: % s" %,

  56. "Subject: % s" % SUBJECT,

  57. "",

  58. MESSAGE), "\ r \ n ")

  59. Smtp = smtplib. SMTP ()

  60. Smtp. connect (HOST, PORT)

  61. Smtp. login (USER, PASSWD)

  62. Smtp. sendmail (FROM, TO, BODY)

  63. Smtp. quit ()

  64. Except t:

  65. Print "unknown error"

  66. Print "please look help"

  67. Print "./sendmail-h"



Usage:

Send only to one user:

Nagios $>./sendmail-t 'test @ 163.com '-s 'Hello'-m' hello, this is sendmail test!

Send to multiple users:

./Sendmail-t 'test@163.com, test02@163.com '-S' hello'-m' hello, this is sendmail test!

Applied to nagios for alarm:



[Root @ master ~] # Vim/etc/nagios/objects/commands. cfg


Define command {

Command_name sort y-host-by-email

Command_line $ USER1 $/sendmail-t $ CONTACTEMAIL $-s "** $ icationicationtype $ Host Alert: $ HOSTNAME $ is $ HOSTSTATE $ ** "-m" ****** Nagios ***** \ n \ nNotification Type: $ icationicationtype $ \ nHost: $ HOSTNAME $ \ nState: $ HOSTSTATE $ \ nAddress: $ HOSTADDRESS $ \ nInfo: $ HOSTOUTPUT $ \ n \ nDate/Time: $ LONGDATETIME $ \ n"

}


Define command {

Command_name sort y-service-by-email

Command_line $ USER1 $/sendmail-t $ CONTACTEMAIL $-s "** $ icationicationtype $ Service Alert: $ HOSTALIAS $/$ SERVICEDESC $ is $ SERVICESTATE $ ** "-m" ****** Nagios ***** \ n \ nNotification Type: $ icationicationtype $ \ n \ nService: $ SERVICEDESC $ \ nHost: $ HOSTALIAS $ \ nAddress: $ HOSTADDRESS $ \ nState: $ SERVICESTATE $ \ n \ nDate/Time: $ LONGDATETIME $ \ n \ nAdditional Info: \ n $ SERVICEOUTPUT $"

}

[Root @ master ~] #/Etc/init. d/nagios reload

In this way, you can.




This article from the "dream into reality" blog, please be sure to keep this source http://zhhmj.blog.51cto.com/1666742/990830

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.