Skip to content

Commit

Permalink
Style fixups with perltidy
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-eisenhart committed Jul 26, 2023
1 parent dbfa678 commit f756d22
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions pd2zabbix.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ warn("SECURITY WARNING: Logs may get sensitive data (auth tokens) with debug>=3\
# CGI object for later use.
our $cgi = CGI->new();


# If superearlysuccess is set, respond to PagerDuty with success regardless
# of whether or not we succeed.
if ( $config->get('superearlysuccess') ) {
Expand Down Expand Up @@ -172,7 +171,7 @@ print $cgi->header( -status => '202 Accepted Complete Success' );
# "superearlysuccess" param makes the error just a log, not returned to PD,
# otherwise PD will see this as an error.
sub pagerduty_validate_authentication {
warn("pagerduty_validate_authentication args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "pagerduty_validate_authentication args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ( $config, $cgi ) = @_;

if ( $config->get('pdauthtoken') ) {
Expand Down Expand Up @@ -206,7 +205,7 @@ sub pagerduty_validate_authentication {

# PagerDuty webhook handler -- most of the work happens here
sub pagerduty_handle_webhook {
warn("pagerduty_handle_webhook args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "pagerduty_handle_webhook args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ($payload) = @_;

# pull the "event" (main data) out of the WebHook payload
Expand All @@ -224,9 +223,9 @@ sub pagerduty_handle_webhook {
warn( "event: " . $j->encode($event) );
return 1;
}

# Special case: merging incidents
if ( $event_type eq 'incident.resolved' && $event->{'data'}{'resolve_reason'}{'type'} eq 'merge_resolve_reason') {
if ( $event_type eq 'incident.resolved' && $event->{'data'}{'resolve_reason'}{'type'} eq 'merge_resolve_reason' ) {
warn("Merging this incident into another\n") if $DEBUG >= 1;
pagerduty_handle_merged_incidents($event);
return 1;
Expand All @@ -240,19 +239,19 @@ sub pagerduty_handle_webhook {
my $html_url = ( $event->{'data'}{'html_url'} || $event->{'data'}{'incident'}{'html_url'} );
warn("html_url: $html_url\n") if $DEBUG >= 2;

my @pagerduty_alerts = @{pagerduty_get_incident_alerts($self_url)};
my @pagerduty_alerts = @{ pagerduty_get_incident_alerts($self_url) };

warn( "pagerduty_alerts count: " . scalar(@pagerduty_alerts), "\n" ) if $DEBUG >= 2;

warn ("pagerduty_alerts count: ".scalar(@pagerduty_alerts), "\n") if $DEBUG >= 2;

if (scalar(@pagerduty_alerts) <= 0) {
if ( scalar(@pagerduty_alerts) <= 0 ) {
warn("No alerts found, trying get_incident_details\n") if $DEBUG >= 1;
push @pagerduty_alerts, pagerduty_get_incident_details($self_url);
}

foreach my $pagerduty_alert (@pagerduty_alerts) {
warn( "alert: " . $j->encode($pagerduty_alert) . "\n" ) if $DEBUG >= 2;
warn( "alert: " . $j->encode($pagerduty_alert) . "\n" ) if $DEBUG >= 2;
my $zabbix_event_id = zabbix_get_event_id_from_pd_object($pagerduty_alert);
local $@; # exception-handling
local $@; # exception-handling

# If we couldn't work out a zabbix event id, we can't update zabbix.
# In case this was due to transient PagerDuty API issues, return "429"
Expand All @@ -264,7 +263,6 @@ sub pagerduty_handle_webhook {
next;
}


# Do appropriate actions on incident event types

# triggered==created (or maybe also end of silencing)
Expand Down Expand Up @@ -322,7 +320,7 @@ sub pagerduty_handle_webhook {
warn("Don't know how to handle event type $event_type\n");
}

if (my $exception = $@) {
if ( my $exception = $@ ) {
warn $exception;
}
}
Expand All @@ -341,25 +339,25 @@ sub pagerduty_handle_webhook {
# Then tell Zabbix that "parent" (event.data.resolve_reason.incident.self) is a "cause",
# and that "child" (event.data.self) is a "symptom" with cause_eventid of parent.
sub pagerduty_handle_merged_incidents {
warn("pagerduty_handle_merged_incidents args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
my ($event) = @_;
my $parent_incident = pagerduty_get_incident_details($event->{'data'}{'resolve_reason'}{'incident'}{'self'});
my $child_incident = pagerduty_get_incident_details($event->{'data'}{'self'});
my $parent_id = zabbix_get_event_id_from_pd_object($parent_incident);
my $child_id = zabbix_get_event_id_from_pd_object($child_incident);

zabbix_events_merge($parent_id, $child_id, $event);
warn( "pagerduty_handle_merged_incidents args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ($event) = @_;
my $parent_incident = pagerduty_get_incident_details( $event->{'data'}{'resolve_reason'}{'incident'}{'self'} );
my $child_incident = pagerduty_get_incident_details( $event->{'data'}{'self'} );
my $parent_id = zabbix_get_event_id_from_pd_object($parent_incident);
my $child_id = zabbix_get_event_id_from_pd_object($child_incident);

zabbix_events_merge( $parent_id, $child_id, $event );
}

# Use PD event API to get additional details on the incident.
# Needed for fetching info that includes the zabbix event ID for the zabbix API.
sub pagerduty_get_incident_details {
warn("pagerduty_get_incident_details args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "pagerduty_get_incident_details args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ($self_url) = @_;
my $pdtoken = $config->get('pdtoken');

my $pd_response = $ua->get( "${self_url}?include[]=body", 'Authorization' => "Token token=${pdtoken}", );
warn( $j->encode( $pd_response ) ) if $DEBUG >= 4;
warn( $j->encode($pd_response) ) if $DEBUG >= 4;
if ( $pd_response->is_success ) {
my $pd_json_content = $pd_response->content();
my $content = decode_json($pd_json_content);
Expand All @@ -373,22 +371,22 @@ sub pagerduty_get_incident_details {

# Use PD event/incident API to get a list of alerts on the incident
sub pagerduty_get_incident_alerts {
warn("pagerduty_get_incident_alerts args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "pagerduty_get_incident_alerts args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ($self_url) = @_;
my $pdtoken = $config->get('pdtoken');

warn("pagerduty_get_incident_alerts: self_url: $self_url\n") if $DEBUG >= 2;

my $pd_response = $ua->get( "${self_url}/alerts", 'Authorization' => "Token token=${pdtoken}", );
warn( $j->encode( $pd_response ) ) if $DEBUG >= 4;
warn( $j->encode($pd_response) ) if $DEBUG >= 4;
if ( $pd_response->is_success ) {
my $pd_json_content = $pd_response->content();
my $content = decode_json($pd_json_content);
return $content->{'alerts'};
}
else {
warn "Unable to fetch alerts from PagerDuty\n";
warn $pd_response->as_string(). "\n";
warn $pd_response->as_string() . "\n";
die;
}

Expand All @@ -397,7 +395,7 @@ sub pagerduty_get_incident_alerts {
# Get the Zabbix event ID associated with a PagerDuty incident
# TODO: explore backup options in case dedup_key not there... zabbix URL has it, for instance.
sub zabbix_get_event_id_from_pd_object {
warn("zabbix_get_event_id_from_pd_object args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "zabbix_get_event_id_from_pd_object args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ($pagerduty_details) = @_;
my $eventid = '';

Expand All @@ -422,12 +420,16 @@ sub zabbix_get_event_id_from_pd_object {
# If don't find event id on its own, try parsing out of the URL to the zabbix event
unless ($eventid) {
my $zabbixurl = '';

# First place to find zabbix event URL in an alert
$zabbixurl ||= $pagerduty_details->{'body'}{'contexts'}[0]{'href'};

# Second place to find zabbix event URL in an alert
$zabbixurl ||= $pagerduty_details->{'body'}{'cef_details'}{'contexts'}[0]{'href'};

# First place to find zabbix event URL in an incident
$zabbixurl ||= $pagerduty_details->{'body'}{'details'}{'links'}[0]{'href'};

# Second place to find zabbix event URL in an incident
$zabbixurl ||= $pagerduty_details->{'body'}{'details'}{'contexts'}[0]{'href'};
if ( $zabbixurl && $zabbixurl =~ m/[?&]eventid=(\d+)/ ) {
Expand All @@ -439,8 +441,8 @@ sub zabbix_get_event_id_from_pd_object {

# Merge child into parent as symptom/cause
sub zabbix_events_merge {
warn("zabbix_events_merge args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
my ($parent_zabbix_event_id, $child_zabbix_event_id, $event) = @_;
warn( "zabbix_events_merge args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ( $parent_zabbix_event_id, $child_zabbix_event_id, $event ) = @_;
my $who = $event->{'agent'}{'summary'};
$who ||= 'API';
warn("Merging $child_zabbix_event_id into $parent_zabbix_event_id by $who\n") if $DEBUG;
Expand All @@ -449,27 +451,26 @@ sub zabbix_events_merge {

# First, make sure the parent is a "cause":
my %parent_params = (
eventids => $parent_zabbix_event_id,
action => ZABBIX_CHANGE_TO_CAUSE,
eventids => $parent_zabbix_event_id,
action => ZABBIX_CHANGE_TO_CAUSE,
);
warn( "zabbix_events_merge parent_params: " . $j->encode( \%parent_params ) ) if $DEBUG >= 2;
zabbix_event_update(%parent_params);

# Then mark the child as a symptom of the parent and comment how that happened
my %child_params = (
eventids => $child_zabbix_event_id,
action => ZABBIX_CHANGE_TO_SYMPTOM ^ ZABBIX_ADD_MSG,
eventids => $child_zabbix_event_id,
action => ZABBIX_CHANGE_TO_SYMPTOM ^ ZABBIX_ADD_MSG,
cause_eventid => $parent_zabbix_event_id,
message => $message,
message => $message,
);
warn( "zabbix_events_merge child_params: " . $j->encode( \%child_params ) ) if $DEBUG >= 2;
zabbix_event_update(%child_params);
}


# Add a message to the zabbix event.
sub zabbix_event_annotate {
warn("zabbix_event_annotate args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "zabbix_event_annotate args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ( $zabbix_event_id, $message ) = @_;
warn("Annotating Zabbix event $zabbix_event_id with message: $message\n") if $DEBUG;

Expand All @@ -485,7 +486,7 @@ sub zabbix_event_annotate {
# Update Zabbix event w/acknowledgement.
# Includes a "ACK'd in PD by $person" note.
sub zabbix_event_acknowledge {
warn("zabbix_event_acknowledge args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "zabbix_event_acknowledge args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ( $zabbix_event_id, $event, $event_details ) = @_;
my $who = $event->{'agent'}{'summary'};
my $message = "ACK'd in PD";
Expand All @@ -507,7 +508,7 @@ sub zabbix_event_acknowledge {
# Update Zabbix event w/unacknowledgement.
# Includes an "un-ACK'd in PD by $person" note.
sub zabbix_event_unacknowledge {
warn("zabbix_event_unacknowledge args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "zabbix_event_unacknowledge args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ( $zabbix_event_id, $event, $event_details ) = @_;
my $who = $event->{'agent'}{'summary'};
my $message = "un-ACK'd in PD";
Expand All @@ -533,7 +534,7 @@ sub zabbix_event_unacknowledge {
# Adds a "Resolved in PD by $person" note.
# Note: can only close some events and silently ignores when it can't.
sub zabbix_event_close {
warn("zabbix_event_close args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "zabbix_event_close args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ( $zabbix_event_id, $event, $event_details ) = @_;
my $who = $event->{'agent'}{'summary'};

Expand Down Expand Up @@ -570,7 +571,7 @@ sub zabbix_event_close {
# Update zabbix event priority
# TODO: make this configurable?
sub zabbix_event_update_priority {
warn("zabbix_event_update_priority args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "zabbix_event_update_priority args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my ( $zabbix_event_id, $event, $event_details ) = @_;
my $who = $event->{'agent'}{'summary'};
my %priorities = (
Expand Down Expand Up @@ -609,7 +610,7 @@ sub zabbix_event_update_priority {
# Makes multiple attempts, in case a clustered config has something down. With slight backoff.
# TODO: make retry stuff configurable.
sub zabbix_event_update {
warn("zabbix_event_update args: " . $j->encode(\@_) . "\n") if $DEBUG >= 5;
warn( "zabbix_event_update args: " . $j->encode( \@_ ) . "\n" ) if $DEBUG >= 5;
my %params = @_;

warn("Updating zabbix event\n") if $DEBUG >= 2;
Expand Down Expand Up @@ -653,17 +654,17 @@ OUTER: for my $zabbixbaseurl (@$zabbixbaseurls) {
);

if ( $zabbixresponse && $zabbixresponse->is_success ) {
warn("Zabbix API update successful on try $zabbixretries\n") if $DEBUG;
warn( "Response from Zabbix: " . $j->encode( $zabbixresponse) ) if $DEBUG >= 2;
warn("Zabbix API update successful on try $zabbixretries\n") if $DEBUG;
warn( "Response from Zabbix: " . $j->encode($zabbixresponse) ) if $DEBUG >= 2;
last OUTER;
}
else {
warn("Zabbix API attempt $zabbixretries\n")
if ( $DEBUG >= 2 or ( $DEBUG >= 1 and $zabbixretries >= 3 ) );
warn( "Response from Zabbix: " . $j->encode( $zabbixresponse ) ) if $DEBUG >= 2;
warn( "Response from Zabbix: " . $j->encode($zabbixresponse) ) if $DEBUG >= 2;

if ( $zabbixretries >= $maxretries ) {
warn $j->encode( $zabbixresponse );
warn $j->encode($zabbixresponse);
warn "Couldn't talk to zabbix API after $zabbixretries attempts.\n";
next OUTER;
}
Expand All @@ -676,5 +677,5 @@ OUTER: for my $zabbixbaseurl (@$zabbixbaseurls) {
}
}

warn( $j->encode( $zabbixresponse ) ) if $DEBUG >= 4;
warn( $j->encode($zabbixresponse) ) if $DEBUG >= 4;
}

0 comments on commit f756d22

Please sign in to comment.