BZ-Client
view release on metacpan or search on metacpan
eg/nagios2bugzilla/talktobugzilla.pl view on Meta::CPAN
}
catch {
$log->logdie('Failed to retrieve server version? ' . Dumper($_))
};
# Wrapper of Nagios::Cmd
sub nagios_command {
my $line = shift or return;
return $cmd->nagios_cmd(sprintf '[%s] %s', time(), $line);
}
# Generate the bug alias
sub bugalias {
return sprintf( $aliastmpl, $_[0] )
}
# Bring the key=value lines in to our hash
sub decode {
my @content = @_;
return unless @content;
return
map { my ( $foo, $bar ) = $_ =~ m/^([^=]+)=([^=]*)/; +( $foo => $bar ) }
@content
}
my @pleaseclose;
{
# keep track of new bugs so we can tag them
my @newbugs;
# keep track of updated bugs so we can tag them
my @updatedbugs;
# tags are private to the user, so they arent useful
sub tag_bugs {
unless (@newbugs or @updatedbugs) {
$log->debug('No bugs to tag');
return 1
}
try {
BZ::Client::Bug->update_tags( $client,
{ ids => [ @newbugs, @updatedbugs ], tags => { add => [ 'nagiosgenerated' ] } } );
}
catch {
$log->warn('Failed to tag bugs, error: ' . Dumper( $_ ) );
};
$log->info("Tagged new bugs @newbugs with 'nagiosgenerated'") if @newbugs;
$log->info("Tagged bugs @updatedbugs with 'nagiosgenerated'") if @updatedbugs;
return 1
}
# adds a comment in nagios
sub nagios_comment {
my ($host, $service, $bugid) = @_;
$log->info( 'Telling nagios about new bug ' . $bugid );
# ADD_SVC_COMMENT;<host_name>;<service_description>;<persistent>;<author>;<comment>
nagios_command(
sprintf('ADD_SVC_COMMENT;%s;%s;1;bugzilla;%s',
$host, $service, 'Auto-Bz http://bugzilla.domain.com/show_bug.cgi?id='.$bugid )
);
$log->info( 'Nagios was told about new bug ' . $bugid );
return 1
}
# problems we have already observed
my %problems;
sub create_bug {
my $f = shift;
my $alias = shift;
my $params = shift;
$log->info("Lets create a bug for $alias");
$log->debug('Bug params: ' . Dumper( $params )) if $log->is_debug;
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';
};
my $description = sprintf(
<<'EOF', $0, hostname(), $nagiosuri, @{$params}{qw( HOSTNAME SERVICEDESC SERVICESTATE SERVICEOUTPUT )}, join( "\n", map { "$_=$params->{$_}" } sort keys %$params ) );
A fault has been detected by Nagios. Your urgent action is required.
Alert Details:
Hostname: %4$s
Service: %5$s
Status: %6$s
Information: %7$s
Status Details for this Host:
%3$s/cgi-bin/status.cgi?host=%4$s
Note:
Whilst this Bz remains open (or is reopened), this automation
will provide additional updates to the fault status as Nagios
detects changes.
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
( run in 0.486 second using v1.01-cache-2.11-cpan-5735350b133 )