Send events to Zenoss from scripts

ZenossWorking at Zenoss the company, our IT group uses Zenoss the application to monitor our infrastructure. Initially, we setup the needed monitoring for various resources, added thresholds and received email alerts if there were problems. That was all well and good, but in scripts written to perform various maintenance tasks on our systems, problems encountered were instead logged and emailed.

One of my clever co-workers noted it would make more sense to send such problems to our Zenoss instance, in order to incorporate them into our existing alerting schemes, as well as quickly see if the problem encountered had been caused by other problems on the same machine, etc. As it turns out, it’s actually quite easy to do this! This howto explains the process fairly well, giving basic examples in several languages. I’ll provide a more robust Python example here, and explain how we used a non-documented feature to great effect.

I found it best to create a function to try to send the event to Zenoss, falling back to sending an email if problems were encountered. This would then be called whenever an event needs to be sent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# For sending events to Zenoss:
from xmlrpclib import ServerProxy

def send_zenoss_event(server, component, message, eventclass):
    """
    Send an INFO event to defined instance of Zenoss.
    """

    # Setup connection:
    serv = ServerProxy('http://USER:PASSWORD@ZENOSS_SERVER:8080/zport/dmd/ZenEventManager')
    # Define the event:
    evt = {'device':server,
            'component':component,
            'summary':message,
            'severity':2,
            'eventClass':eventclass}
    logger.info("Sending %s event for %s to Zenoss..." % \
        (component, server))
    try:
        serv.sendEvent(evt)
    except:
        send_alert("Couldn't send event to Zenoss for %s." % server)
        sys.exit(-1)
    logger.info("%s event for %s sent." % (component, server))

You might want to handle problems sending the event differently of course :-) xmlrpclib is part of the standard library, so you don’t need to install anything. send_alert() is a function I commonly include in my sysadmin scripts. It simply logs the message passed (on how to setup logging, see this post), then emails it to the appropriate party. This way you can ensure that even if something is up with the Zenoss instance, someone still finds out there’s a problem. You could also use this same function as a way to exit your problem cleanly if problems are encountered that can’t be resolved, or connect it to another system that handles this.

On the event attributes:

  • The value for ‘device’ should match a device name in Zenoss.
  • Severity is a numeric value mapped to what sort of event it is. The following options are available:
    • Severity: Description
    • 5: Critical
    • 4: Error
    • 3: Warning
    • 2: Info
    • 1: Debug
    • 0: Clear

    It might be useful to change severity in the above function to a parameter, so the full range of event types would be available.

  • Component is an object contained by a device, such as CPU or hard drive space. It’s basically free form, although there are a number setup in Zenoss by default. You might decide to utilize and augment this structure, or you might create your own custom one.
  • Event classes exist to categorize events. A number of these are available in Zenoss by default, and you can also add your own.

There is an additional attribute that turned out to be quite useful: timeout. When a timeout is passed, it is added to the events.heartbeat table in the Zenoss database. Once the specified time has passed, an alert will be raised for that device. This is why the device name passed needs to match the device name in Zenoss, otherwise nothing happens after the timeout. To send it, simply add these two lines to the evt dictionary defined in the above function:

1
2
'eventClass':'/Heartbeat',
'timeout':timeout # An integer

We took advantage of timeout to keep track of our backup processes. When the backup process for a device started, it sent an event to Zenoss, the timeout being the time recorded for the last backup, plus some buffer. Then if the backup didn’t complete in the proper period of time, an alert was raised for that device, with the component of “/Backup”. In addition, when the backup completed, another event was sent to Zenoss, with a new timeout of the time between backups of the current server minus the time taken for the current backup, again plus some buffer. Then if the next backup didn’t start (and thus reset the timer) in time, an alert was also sent. This system combined with email fall back means that we always know if our backups don’t start and/or don’t complete.

Post to Twitter Post to Delicious Post to Digg Post to Reddit

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in Programming and tagged , , , . Bookmark the permalink.

7 Responses to Send events to Zenoss from scripts

  1. Trevor Rosen says:

    This is really nice — might be even nicer with a few screenshots of the screens you’re using in Zenoss?

  2. Not sure what I’d show, we aren’t really using any of the web interface aspects of Zenoss, just the alerting infrastructure. Being able to look at the other metrics for a device with a problematic backup is basically a side effect, although we could setup a graph showing backup success/fail.

    I’d like to be able to view all the current backup heartbeats, a dashboard of backup status. Perhaps I’ll finally get around to making a ZenPack… :-)

  3. Jack says:

    hi Samual,

    nice article and zenpack is would be very usefull but in simple words are you using zenoss to monitor the backup status ? Why Iam asking because in our enviroment we have many backup running everywhere and I dont have any status page except we view the latest backup files. I interested to implement something you had done. Its ok if I do like, when any backup script stated, it will send info event to zenoss and after it finish it will send clear info to zenoss and if I want to see the status just go to Event history page ? thanks in advance for your help and apologies for my english.

  4. Dustin Sysko says:

    This is exactly what I have in mind for a current project, and I was looking forward to seeing a bit more in the how-to document. Unfortunately, the how-to link seems to point to the general Zenoss forums, and not to a specific article, or the link is not valid so that it defaults to the top of the forums. A search for send_event did not yeild anything useful.

    Would you mind updating the link to point to the permanent article?

  5. @ Dustin Glad it was helpful as a start at least! Sorry about the howto link, it’s now updated to the right place. You’d think since I admin that site too that I wouldn’t have this problem :-) Good luck!

  6. Pingback: Monitoring bespoke software with Zenoss | Coding Answers

  7. Pingback: Monitoring bespoke software with Zenoss - Admins Goodies

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>