Skip to content

Instantly share code, notes, and snippets.

@ryanhoskin
Created June 12, 2015 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanhoskin/65ca2fed0f385811167e to your computer and use it in GitHub Desktop.
Save ryanhoskin/65ca2fed0f385811167e to your computer and use it in GitHub Desktop.
Pandora FMS
#! /usr/bin/perl
use LWP::UserAgent;
use JSON qw(decode_json);
use File::Basename;
### Configuration parameters ###
# Directory where is stored necessary files with match up ids between Pandora FMS and PagerDuty
# One hidden tiny file per alert will be created
my $matchup_dir = dirname(__FILE__);
################################
my $ua = LWP::UserAgent->new;
my $server_endpoint = "https://events.pagerduty.com/generic/2010-04-15/create_event.json";
exit if ($#ARGV < 3);
my $service_key = $ARGV[0];
my $incident_key = $ARGV[1];
my $event_type = $ARGV[2];
my $description = $ARGV[3];
my $fired_timestamp = $ARGV[4];
my $severity = $ARGV[5];
my $agent_name = $ARGV[6];
my $module_name = $ARGV[7];
my $module_data = $ARGV[8];
# Compatibility with recover prefixes system
$service_key =~ s/\[RECOVER\]//g;
# set custom HTTP request header fields
my $req = HTTP::Request->new(POST => $server_endpoint);
$req->header('content-type' => 'application/json');
# add POST data to HTTP request body
my $post_data = '{ "service_key": "' . $service_key . '",
"event_type": "' . $event_type . '",
"description": "' . $description . '",
"incident_key": "' . $incident_key . '",
"details": {
"fired_timestamp": "'. $fired_timestamp . '",
"severity": "'. $severity . '",
"agent": "'. $agent_name . '",
"module": "'. $module_name . '",
"module_data": "'. $module_data . '"
}
}';
$req->content($post_data);
my $resp = $ua->request($req);
if ($resp->is_success) {
my $response = decode_json($resp->decoded_content);
if($response->{'status'} eq 'success') {
print $response->{'message'} . "\n";
print "PagerDuty ID of the incident: " . $response->{'incident_key'} . "\n";
print FILE $response->{'incident_key'};
exit 0;
}
else {
print "Status: " . $response->{'status'} . "\n";
exit 1;
}
}
else {
print "HTTP POST error code: ", $resp->code, "\n";
print "HTTP POST error message: ", $resp->message, "\n";
exit 1;
}
close FILE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment