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

Unanswered Question
Mar 28th, 2017
User Badges:

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

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 0 (0 ratings)
Loading.
Joe Clarke Wed, 03/29/2017 - 06:19
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

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]))
scott.hammond Wed, 03/29/2017 - 06:41
User Badges:

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.

scott.hammond Wed, 03/29/2017 - 07:00
User Badges:

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#


Joe Clarke Mon, 04/03/2017 - 11:38
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

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.

scott.hammond Mon, 04/03/2017 - 12:03
User Badges:

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     [email protected]"
            print "    to       [email protected]"
            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 <[email protected]> <[email protected]> "Interface down"'

Joe Clarke Mon, 04/03/2017 - 12:57
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

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.

scott.hammond Mon, 04/03/2017 - 13:09
User Badges:

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

:)



Joe Clarke Mon, 04/03/2017 - 13:55
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

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.

Joe Clarke Tue, 04/04/2017 - 09:41
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

How are you running the script now?

scott.hammond Tue, 04/04/2017 - 09:49
User Badges:

#python

>>>source nxos_email.py

then it prints the bottom line and never sends an email

Joe Clarke Tue, 04/04/2017 - 09:52
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

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.

Actions

This Discussion