BZ-Client

 view release on metacpan or  search on metacpan

eg/nagios2bugzilla/talktobugzilla.pl  view on Meta::CPAN

If this Bz is marked resolved or closed (including resolved as a
duplicate), then this automation will provide no additional updates
and make no further changes.

Raw details provided by Nagios:

----BEGIN RAW----
%8$s
----END RAW----

This bug was automatically generated by %1$s running on %2$s

EOF

    my %bug = (

        %team,

        summary     => $summary,
        severity    => $severity,
        priority    => $priority,
        alias       => $alias,
        description => $description,

        # assigned_to, ommit so it goes to default
        # cc, we can cc people

    );

    $log->debug('Bug will contain: ' . Dumper( \%bug )) if $log->is_debug;

    my $newbug = try {
        return BZ::Client::Bug->create( $client, \%bug );
    }
    catch {
        $log->logdie('Failed to create new bug, error: ' . Dumper( $_ ) );
    };

    $log->info("Bug for $alias is $newbug");

    my $get = try {
        return BZ::Client::Bug->get( $client, { ids => [ $newbug ] } );
    }
    catch {
        $log->logdie('Failed to retrieve newly created bug, with error: ' . Dumper( $_ ));
    };

    push @newbugs, $newbug;

    $problems{ $alias } = $get->[0];
    try { if ($delete) { $log->debug('Removing file: ' .$f->basename); $f->remove() }}
    catch { $log->warn(sprintf 'Failed to delete %s because: %s', $f->basename, $_) };

    # telling nagios
    nagios_comment( @{$params}{qw( HOSTNAME SERVICEDESC )}, $newbug );

    return 1

}

sub update_bug {

    my $f = shift;
    my $bug = shift;
    my $params = shift;

    # ignore closed bugs
    unless ( $bug->is_open() ) {

        $log->info('Bug is closed, not updating');

        try { if ($delete) { $log->debug('Removing file: ' .$f->basename); $f->remove() }}
        catch { $log->warn(sprintf 'Failed to delete %s because: %s', $f->basename, $_) };

        return
    }

    my $lastalias = $bug->alias
        or $log->logdie(sprintf('Bug %d has no alias so it cannot be updated, how did we even get here?', $bug->id()));
    my $newalias = bugalias($params->{SERVICEPROBLEMID});

    $log->info('Lets update a bug for ' . $bug->alias);
    $log->debug('Bug params: ' . Dumper( $params )) if $log->is_debug;

    my $comment = 'Nagios has updated this alert';

    # aliase handling
    if ($lastalias ne $newalias) {
        $log->info("Changing alias from $lastalias to $newalias");
        $comment .= "\n\n". sprintf('Changed service problem ID: %d -> %d',
                               $params->{LASTSERVICEPROBLEMID}, $params->{SERVICEPROBLEMID});
    }

# FIXME
#    my $summary = sprintf q|Server: '%s', Fault: '%s'|,
#      @{$params}{qw( HOSTNAME SERVICEDESC )};

    my $severity = 'major';
    my $priority = 'P2';
    if ( $params->{SERVICESTATE} eq 'CRITICAL' ) {
        $severity = 'critical';
        $priority = 'P1';
    };

    # highlight the change in severity
    if ($severity ne $bug->severity() ) {
        if ($severity eq 'critical') { # upgraded to 'critical'
            $comment .= "\nSeverity upgraded to: $severity"
        }
    }

    # highlight the change in priority
    if ($priority ne $bug->priority() ) {
        if ($priority eq 'P1') { # upgraded to 'P1'
            $comment .= "\nPriority now: $severity"
        }
    }

    $comment .= sprintf(
        <<'EOF', $0, hostname(), join( "\n", map { "$_=$params->{$_}" } sort keys %$params ) );



( run in 0.702 second using v1.01-cache-2.11-cpan-2398b32b56e )