cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2290
Views
0
Helpful
12
Replies

N5k EEM script to send email when a fex error is logged

scott.hammond
Level 1
Level 1

I thought I was pretty okay with EEM, but then then I dive into NX-OS and the very simple action of sending an email is now gone and I have to write some sort of TCL code to do it.

SO I spent the last 2 hours reading through posts about that very thing and though I have learned a great deal I am still largely confused. What I dont understand is how to even start!

I see examples of scripts but not how to actually make it work from end to end.

Specifically my challenge is a bug with the B22 FEX's. They start throwing an invalid temp alarm about two weeks before they detach and have to be physically reseated. TAC says there is no planned fix. So my mitigation strategy is to write an EEM to look for that temp alarm that we can proactively reseat the FEX prior to the inevitable crash.

Here are examples of the log

2016 Mar 19 20:31:38 DAY_CR_CR03 %SATCTRL-FEX132-2-SOHMS_ENV_ERROR: FEX-132 Module 1 temperature sensor Die-1 fa
iled.
2016 Mar 19 20:32:17 DAY_CR_CR03 %SATCTRL-FEX132-2-SOHMS_ENV_ERROR: FEX-132 Module 1 temperature sensor Outlet-1
failed.
2016 Mar 19 20:32:17 DAY_CR_CR03 %SATCTRL-FEX132-2-SOHMS_ENV_ERROR: FEX-132 Module 1 temperature sensor Inlet-1
failed.
2016 Mar 19 20:32:17 DAY_CR_CR03 %SATCTRL-FEX132-2-SOHMS_ENV_ERROR: FEX-132 Module 1 temperature sensor Inlet-2
failed.

I am not a coder. TCL is pretty foreign to me. The examples I have found have been "close" to what I need but I lack the knowledge to adapt them properly. I keep getting syntax errors when I try to run the script from the tcl shell.

Can anyone give me a quick "for dummies" nudge in the right direction?

Thanks a ton

12 Replies 12

Joe Clarke
Cisco Employee
Cisco Employee

Tcl will not work for you on NX-OS.  I mean, technically it will, but you will need to write the entire SMTP client in Tcl.  It would be better to code this in Python, but I don't know if the N5K Python environment comes with the SMTP lib.

If it did work, the Python would look something like:

import smtplib
import sys

msg = """From: {}
To: {}
Subject: Test email

{}
""".format(arr_envinfo['_email_from'], arr_envinfo['_email_to'], "This is a test")

try:
    s = smtplib.SMTP(arr_envinfo['_email_server'])
    s.sendmail(arr_envinfo['_email_from'], [arr_envinfo['_email_to']], msg)
except SMTPException as e:
    print('Failed to send email {}'.format(e.args[0]))

Thanks for the response, though I am afraid I am too dumb to understand it.

I lack the foundational information to understand any of this as it relates to the gear. Reading all the EEM whitepapers that I can find but they all assume a certain level of knowledge, or at least that people know what to do with things like you just sent me.

I know what I library is, but I have no concept of how it relates to IOS or NXOS and what I do at the cli.

So take this example you sent me for example. I dont know what to do with it. Do I just paste it in from global config mode? probably not. Do I got into some other shell and put it in?

I see some brief references in the guide about importing scripts into flash, that makes sense. So I put some stuff in notepad, named it test.tcl and tftp'ed it to bootflash: ...So I can go to the tclsh and run it....and see if fail. 

its very frustrating for me. Thinking I will engage tac and hope they are willing to put up with my noobie questions.

Thanks for trying but I think you are so far above my level I wont understand.

though I did copy that into the script folder and I think I ran it. So I am somewhat proud of myself.

DAY_CR_CR03# source test.py
ERROR: ld.so: object '/isan/lib/libsandbox.so' from LD_PRELOAD cannot be preloaded: ignored.
no sandbox
disable sandbox before enabled
no sandbox
String found where operator expected at (eval 1) line 9, near """"From: {}
To: {}
Subject: Test email

{}
""
(Might be a runaway multi-line "" string starting on line 4)
(Missing operator before "From: {}
To: {}
Subject: Test email

{}
"?)
String found where operator expected at (eval 1) line 9, near ""From: {}
To: {}
Subject: Test email

{}
""""
(Missing operator before ""?)
Bareword found where operator expected at (eval 1) line 11, near ")

try"
(Missing operator before try?)
ERROR(perl): Substitution pattern not terminated at (eval 1) line 12.

DAY_CR_CR03#

The first line of the an actual script needs to be:

#!/usr/bin/env python

Then you'll need to fill in some variables in this script to make it work.  In particular, replace arr_envinfo['_email_from'] with your "from" address, arr_envinfo['_email_to'] with your "to" address, and arr_envinfo['_email_server'] with your SMTP server address.

Hi there, thanks for the response.

TAC sent me this one, but sadly it doesnt work. The bottom "print" statement executes but nothing else.

Any thoughts?

#!/bin/env python

import os
import sys
import re

#set_vrf ('default')

if len(sys.argv) > 1:
    helpcall = re.search("__cli_script.*help", sys.argv[1])
    if helpcall:
        if sys.argv[1] == "__cli_script_help":
            print "Send mail using python. Use sendemail.py <10.15.11.11> <from> <to> \"Interface down \""
        if sys.argv[1] == "__cli_script_args_help":
            print "__man_page"
            print ""
            print "  Send mail using python. Use sendemail.py <server> <from> <to> \"Interface down \""
            print ""
            print "    server   10.15.11.11"
            print "    from     bisingh2@cisco.com"
            print "    to       manianan@cisco.com"
            print "    message  FEX is down"
        exit(0)

if len(sys.argv) > 4:
    import smtplib
    from email.mime.text import MIMEText

    sender = sys.argv[2]
    receivers = str.split(sys.argv[3], ",")
    message = " ".join(sys.argv[4:])
    if message[0] == '"' or message[0] == "'":
        message = message[1:(len(message)-1)]
    if receivers:
        message = MIMEText(message)
        try:
           smtpObj = smtplib.SMTP(sys.argv[1])
           smtpObj.sendmail(sender, receivers, message.as_string())
           smtpObj.quit()
        except smtplib.SMTPException:
           print "Error: unable to send email to "+sys.argv[3]

    exit(0)

print 'Use sendemail.py 10.15.11.11 <bisingh2@cisco.com> <manianan@cisco.com> "Interface down"'

You need to call this script with arguments. as it specifies in the message that's printed out.  The first argument is the SMTP server, the second is the list of recipients (separated by commas), the third is the sender address, and the fourth is the message itself.

When you say "call" you mean run it right?

when I run it, it just prints the bottom line and doesnt actually send the email. So I believe it is not running the

if len(sys.argv) > 1:

Which seems to be an argument but I profess I dont really understand. The extent of my coding skill was "if, then, goto" in grade school, in the 80's

:)

Right, you need to run it with those arguments.  Without any arguments, the script just prints a usage line to give you an example of how to run it.

That makes perfect sense, but I don't know how to do it.

How are you running the script now?

#python

>>>source nxos_email.py

then it prints the bottom line and never sends an email

Then you need to run it:

source nxos_email.py SMTP_SERVER TO_ADDR FROM_ADDR MSG

Where SMTP_SERVER is your SMTP server IP, TO_ADDR is a comma-separated list of email addresses, FROM_ADDR is an email address, and MSG is the quoted email message.

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Innovations in Cisco Full Stack Observability - A new webinar from Cisco