[rt-users] Nagios Plugin modified to do the same for Zabbix e-mails

Bart bart at pleh.info
Thu Sep 20 05:04:33 EDT 2012


Hi everyone,


In our setup we're using Zabbix for monitoring and thus Zabbix sends
e-mails to RT for our support-desk.
Before Zabbix we used Nagios and thus also had the Nagios plugin installed
for merging similar e-mails together and closing a ticket when an e-mail
comes with the status RECOVERED.

As such I've modified the actual script for the Nagios plugin so that it
handles the Zabbix e-mails instead of Nagios e-mails.

This script requirers the following settings in your Zabbix action which
sends the e-mail:

   - Subject: {TRIGGER.STATUS} - {TRIGGER.SEVERITY} - {TRIGGER.NAME}
   - Conditions:
      - Type: OR
      - Trigger severity = Disaster
      - Trigger severity = High
      - Trigger severity = Average
      - Trigger severity = Warning

In my opinion the default subject is a little limiting, I've added the
severity to make it a little more informative for the person reading it.
Other then that the content of the e-mail that Zabbix sends doesn't matter,
this plugin only checks the subject.

The actual modifications that I've made were done in the following file:

/opt/rt4/local/plugins/RT-Extension-Nagios/lib/RT/Action/UpdateNagiosTickets.pm


Below you'll find the content of that file with the modifications needed
for Zabbix, the modifications are marked with bold text:

package RT::Action::UpdateNagiosTickets;

use strict;
use warnings;

use base qw(RT::Action);

sub Describe {
    my $self = shift;
    return ( ref $self );
}

sub Prepare {
    return (1);
}

sub Commit {
    my $self = shift;

    my $attachment = $self->TransactionObj->Attachments->First;
    return 1 unless $attachment;
    my $new_ticket    = $self->TicketObj;
    my $new_ticket_id = $new_ticket->id;

    my $subject = $attachment->GetHeader('Subject');
    return unless $subject;
*    if ( my ( $type, $problem_severity, $problem_type ) =*
        $subject =~
*
m{(PROBLEM|OK|UNKNOWN)\s+-\s+(Warning|Average|High|Disaster)\s+-\s+([^/]+)/?(.*)}i
*
      )
    {
        $problem_type ||= '';
        $RT::Logger->info(
*"Extracted type, problem_severity and problem_type from*
*subject with values $type, $problem_severity and $problem_type"*
        );
        my $tickets = RT::Tickets->new( $self->CurrentUser );
        $tickets->LimitQueue( VALUE => $new_ticket->Queue )
          unless RT->Config->Get('NagiosSearchAllQueues');
        $tickets->LimitSubject(
*            VALUE => "$problem_type",*
            OPERATOR => 'LIKE',
        );
        my @active = RT::Queue->ActiveStatusArray();
        for my $active (@active) {
            $tickets->LimitStatus(
                VALUE    => $active,
                OPERATOR => '=',
            );
        }

        my $resolved = RT->Config->Get('NagiosResolvedStatus') ||
'resolved';

        if ( my $merge_type = RT->Config->Get('NagiosMergeTickets') ) {
            my $merged_ticket;

            $tickets->OrderBy(
                FIELD => 'Created',
                ORDER => $merge_type > 0 ? 'DESC' : 'ASC',
            );
            $merged_ticket = $tickets->Next;

            while ( my $ticket = $tickets->Next ) {
                my ( $ret, $msg ) = $ticket->MergeInto( $merged_ticket->id
);
                if ( !$ret ) {
                    $RT::Logger->error( 'failed to merge ticket '
                          . $ticket->id
                          . " into "
                          . $merged_ticket->id
                          . ": $msg" );
                }
            }

*            if ( $type eq 'OK' ) {*
                my ( $ret, $msg ) = $merged_ticket->SetStatus($resolved);
                if ( !$ret ) {
                    $RT::Logger->error( 'failed to resolve ticket '
                          . $merged_ticket->id
                          . ":$msg" );
                }
            }
        }
*        elsif ( $type eq 'OK' ) {*
            while ( my $ticket = $tickets->Next ) {
                my ( $ret, $msg ) = $ticket->Comment(
                    Content => 'going to be resolved by ' . $new_ticket_id,
                    Status  => $resolved,
                );
                if ( !$ret ) {
                    $RT::Logger->error(
                        'failed to comment ticket ' . $ticket->id . ":
$msg" );
                }

                ( $ret, $msg ) = $ticket->SetStatus($resolved);
                if ( !$ret ) {
                    $RT::Logger->error(
                        'failed to resolve ticket ' . $ticket->id . ":
$msg" );
                }
            }
            my ( $ret, $msg ) = $new_ticket->SetStatus($resolved);
            if ( !$ret ) {
                $RT::Logger->error(
                    'failed to resolve ticket ' . $new_ticket->id . ":$msg"
);
            }
        }
    }
}

1;

I haven't made any other changes to the Nagios plugin, by default the
plugin does what it should do. The only difference is that the subject
regex is different and the RECOVERED status with Zabbix is OK.
Below my settings for the Nagios plugin in the RT_SiteConfig:

Set($NagiosSearchAllQueues, 1);
Set($NagiosMergeTickets, -1);
Set($NagiosResolvedStatus, "resolved");


You can find the Nagios plugin on this page:

   -
   http://search.cpan.org/dist/RT-Extension-Nagios/lib/RT/Extension/Nagios.pm

I'm sadly not handy enough with these plugins to make a new plugin out of
this, or to make the required modifications to update the Nagios plugin
e.g. allow setting the regex + RECOVERY/OK status, etc. in the
RT_SiteConfig. Basically making it more customizable.

So these modifications will have to do, I hope this helps a few others out
there that also use Zabbix for monitoring.


-- 
Bart
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120920/faef50ee/attachment.htm>


More information about the rt-users mailing list