Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-04-2010, 03:31 PM   PM User | #1
nanite51
New to the CF scene

 
Join Date: Sep 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
nanite51 is an unknown quantity at this point
Question trying to fix a plugin for Nagios

I am trying to fix a plug-in for Nagios (http://www.nagios.org) called "Nagios Alerts via gmail and python" (http://www.monitoringexchange.org/in...ail-and-python). I've tried contacting the original author of the plug-in with no success and it looks like nothing has been done with it since 2008.

Anyway, besides placing the python program into /usr/local/nagios/libexec, it involves editing /usr/local/nagios/etc/objects/commands.cfg with the following:

Code:
#old method
# 'notify-host-by-email' command definition
#define command{
#	command_name	notify-host-by-email
#	command_line	/usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
#	}

#old method
# 'notify-service-by-email' command definition
#define command{
#	command_name	notify-service-by-email
#	command_line	/usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
#	}

#notify host via gmail using python script. \\nnn will insert return, use for body of mssg
define command{
	command_name	notify-host-by-email
	command_line	/usr/bin/python /usr/local/nagios/libexec/send_gmail.py -a $CONTACTEMAIL$ -b "*****Nagios*****\\nnn\\nnnNotification_Type:$NOTIFICATIONTYPE$\\nnnHost:$HOSTNAME$\\nnnState:$HOSTSTATE$\\nnnAddress:$HOSTADDRESS$\\nnnInfo:$HOSTOUTPUT$\\nnnDate_and_Time:$LONGDATETIME$" -s \"**$NOTIFICATIONTYPE$_Host_Alert:$HOSTNAME$_is_$HOSTSTATE$**\"
	}

#notify service via gmail using python script. \\nnn will insert return, use for body of mssg
define command{
	command_name	notify-service-by-email
	command_line	/usr/bin/python /usr/local/nagios/libexec/send_gmail.py -a $CONTACTEMAIL$ -b "*****Nagios*****\\nnn\\nnnNotification_Type:$NOTIFICATIONTYPE$\\nnnService:$SERVICEDESC$\\nnnHost:$HOSTALIAS$\\nnnAddress:$HOSTADDRESS$\\nnnState:$SERVICESTATE$\\nnnDate_and_Time:$LONGDATETIME$\\nnnAdditional_Info:$SERVICEOUTPUT$\" -s \"**$NOTIFICATIONTYPE$_Service_Alert:$HOSTALIAS$/$SERVICEDESC$_is_$SERVICESTATE$**\"
	}
Here is the code for send_gmail.py:
Code:
#!/usr/bin/python
import string, os, sys, time 
import os.path
import smtplib
from optparse import OptionParser
import optparse

"""
This quick and dirty script can be used to send email messages from command line using a gmail account.
It was designed to be a simple replacement for the default mail program (sendmail) used by Nagios.
You must configure this file to work with your gmail account...
Note: this script may be unsecure as the password is stored as plain text in the script.

Requires smtplib, python 2.4.4c1+ and optparse

Usage:
send_gmail.py -a [to address] -s [subject] -b [message body]

New lines can be inserted in the message body by using   \nnn 
Multiple to addresses must be seperated by commas (a space character may preceed and/or follow the comma)

Example:
send_gmail.py -a "someone@somedomain.null,someone@smsgateway.null" -s "This is the subject line..." -b "Body line one\nnnAnd this is line two"

code based on http://mail.python.org/pipermail/python-list/2007-January/423569.html

**Update/Revision History**
2008.04.09 - Version 1.0.1 posted, fixed problem where script would not handle multiple recipents,
smtplib's .sendmail() requires an array to send to multiple addresses. If all addresses are passed as a string, it will only send to the first address.

2008.01.24 - Version 1.0.0 posted

"""

def main():
    p = optparse.OptionParser( )
    p.add_option('--address', '-a', action='store', type='string')
    p.add_option('--body', '-b', action='store', type='string')
    p.add_option('--subject', '-s', action='store', type='string')
    options, arguments = p.parse_args()
   
    body = options.body
    address = options.address
    Addresses = address.split(',') #turns string into array by splitting string at commas.
    subject = options.subject  
    
    body = '\n'.join(body.split('\\nnn'))
            
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.set_debuglevel(1) #0 for quiet or 1 for verbosity
    server.ehlo('YOUR_ADDRESS_HERE@gmail.com')
    server.starttls()
    server.ehlo('YOUR_ADDRESS_HERE@gmail.com')  # say hello again
    server.login('YOUR_ADDRESS_HERE@gmail.com', 'YOUR_PASSWORD_HERE')
    
    server.sendmail('YOUR_ADDRESS_HERE@gmail.com', Addresses, "Subject: " + subject + '\nTo:' + address + '\n\n' + body)
    
    server.quit()

if __name__ == '__main__':
    main()
Anyway, now onto the problem, it will occasionally send parts of the e-mail alert, not send any at all, or rarely send full e-mail messages.

For testing purposes, I tried the following command from the command line:
Code:
root@nagios-test01:/usr/local/nagios/libexec# ./send_gmail.py -a nighthawk3001@gmail.com -b "*****Nagios*****\\nnn\\nnnNotification_Type:$NOTIFICATIONTYPE$\\nnnService:$SERVICEDESC$\\nnnHost:$HOSTALIAS$\\nnnAddress:$HOSTADDRESS$\\nnnState:$SERVICESTATE$\\nnnDate_and_Time:$LONGDATETIME$\\nnnAdditional_Info:$SERVICEOUTPUT$\" -s "**$NOTIFICATIONTYPE$_Service_Alert:$HOSTALIAS$/$SERVICEDESC$_is_$SERVICESTATE$**\"
It then gave me the following result:
Code:
send: 'ehlo nighthawk3001@gmail.com\r\n'
reply: '250-mx.google.com at your service, [69.43.65.70]\r\n'
reply: '250-SIZE 35651584\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-STARTTLS\r\n'
reply: '250 ENHANCEDSTATUSCODES\r\n'
reply: retcode (250); Msg: mx.google.com at your service, [69.43.65.70]
SIZE 35651584
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
send: 'STARTTLS\r\n'
reply: '220 2.0.0 Ready to start TLS\r\n'
reply: retcode (220); Msg: 2.0.0 Ready to start TLS
send: 'ehlo nighthawk3001@gmail.com\r\n'
reply: '250-mx.google.com at your service, [69.43.65.70]\r\n'
reply: '250-SIZE 35651584\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-AUTH LOGIN PLAIN XOAUTH\r\n'
reply: '250 ENHANCEDSTATUSCODES\r\n'
reply: retcode (250); Msg: mx.google.com at your service, [69.43.65.70]
SIZE 35651584
8BITMIME
AUTH LOGIN PLAIN XOAUTH
ENHANCEDSTATUSCODES
send: 'AUTH PLAIN AG5pZ2h0aGF3azMwMDFAZ21haWwuY29tAFdrXk1GV2teJlNUQQ==\r\n'
reply: '235 2.7.0 Accepted\r\n'
reply: retcode (235); Msg: 2.7.0 Accepted
Traceback (most recent call last):
  File "./send_gmail.py", line 61, in <module>
    main()
  File "./send_gmail.py", line 56, in main
    server.sendmail('nighthawk3001@gmail.com', Addresses, "Subject: " + subject + '\nTo:' + address + '\n\n' + body)
TypeError: cannot concatenate 'str' and 'NoneType' objects
I have tried entering a plain e-mail with the python script and it did work then. I also tried the above command without the \\nnn pieces with the same result.

Later on, Nagios used the python program to send the following e-mail:
Code:
Subject: "**PROBLEM_Host_Alert:nagios-testclient01_is_DOWN**"
Body:
*****Nagios*****

Notification_Type:PROBLEM
Host:nagios-testclient01
State:DOWN
Address:10.99.18.15
Info:CRITICAL - Host Unreachable (10.99.18.15)
Date_and_Time:Wed Aug 4 09:14:01 EDT 2010


I was looking back to the send_gmail.py file and line 40 looked a little weird to me:
Code:
options, arguments = p.parse_args()
Sorry for the huge amount of info. I've been working on this for a little while and this is what I have so far.

Once again, I am not the original author of this python program. I am simply trying to fix it and I don't know python that well.

I'm not sure what options is supposed to be. Figure it might be related to when the program is grabbing input from the user. It almost looks like options isn't being initialized. Could that be part of the problem?
nanite51 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:15 PM.


Advertisement
Log in to turn off these ads.