diff --git a/status/edit_mon.cgi b/status/edit_mon.cgi index 1ea953f6c..a0431afc0 100755 --- a/status/edit_mon.cgi +++ b/status/edit_mon.cgi @@ -18,7 +18,7 @@ if ($in{'type'}) { } else { # Totally new - $serv = { 'notify' => 'email pager snmp sms', + $serv = { 'notify' => 'email pager snmp sms webhook', 'fails' => 1, 'nosched' => 0, 'remote' => '*' }; diff --git a/status/edit_sched.cgi b/status/edit_sched.cgi index 8868e4a77..b0f57e50f 100755 --- a/status/edit_sched.cgi +++ b/status/edit_sched.cgi @@ -95,6 +95,11 @@ print &ui_table_row($text{'sched_subject'}, [ 2, $text{'sched_subject2'}, &ui_textbox("subject", $smode == 2 ? $config{'sched_subject'} : "", 40) ] ]), 3); +# HTTP webhook URL +print &ui_table_row($text{'sched_webhook'}, + &ui_opt_textbox("webhook", $config{'sched_webhook'}, 60, + $text{'sched_webhookno'}, $text{'sched_webhookyes'}), 3); + print &ui_table_end(); print &ui_form_end([ [ "save", $text{'save'} ] ]); diff --git a/status/lang/en b/status/lang/en index fc707dfff..31e76539f 100644 --- a/status/lang/en +++ b/status/lang/en @@ -121,6 +121,7 @@ mon_notifyemail=Email mon_notifypager=Pager mon_notifysnmp=SNMP mon_notifysms=SMS +mon_notifywebhook=Webhook mon_email=Also send email for this service to mon_depend=Don't check if monitor is down mon_edepend=A monitor cannot depend on itself @@ -189,6 +190,10 @@ sched_subject0=None (alert is in the body) sched_subject1=Alert text (leave body empty) sched_subject2=Custom text sched_esubject=Missing SMS message subject +sched_webhook=Send status to webhook +sched_webhookno=Don't send +sched_webhookyes=HTTP or HTTPS URL +sched_ewebhook=Missing or invalid URL for webhook up_since=Up since $1 depends_mod=The module $1 is not installed on your system diff --git a/status/monitor.pl b/status/monitor.pl index 4d05699a0..10fe0a8c1 100755 --- a/status/monitor.pl +++ b/status/monitor.pl @@ -200,6 +200,9 @@ foreach $serv (@services) { $thisemail .= "\n"; $ecount++; } + if ($notify{'webhook'}) { + push(@webhooks, [ $serv, $stat, $suffix, $host ]); + } $lastsent{$serv->{'id'}} = $nowunix; } $newstats->{$r} = $up; @@ -283,17 +286,21 @@ if ($pager_msg && !$config{'sched_single'}) { if ($sms_msg && !$config{'sched_single'}) { &send_status_sms($sms_msg); } +foreach $w (@webhooks) { + &send_status_webhook(@$w); + } # send_status_email(text, subject, email-to) sub send_status_email { -return if (!$_[2]); +local ($text, $subject, $to) = @_; +return if (!$to); &foreign_require("mailboxes", "mailboxes-lib.pl"); # Construct and send the email (using correct encoding for body) local $from = $config{'sched_from'} ? $config{'sched_from'} : &mailboxes::get_from_address(); -&mailboxes::send_text_mail($from, $_[2], undef, $_[1], $_[0], +&mailboxes::send_text_mail($from, $to, undef, $subject, $text, $config{'sched_smtp'}); } @@ -423,6 +430,34 @@ if (!$@) { print STDERR "No SNMP perl module found\n"; } +# send_status_webhook(&monitor, &status, what, host) +# Make an HTTP call with monitor details and status as params +sub send_status_webhook +{ +my ($serv, $stat, $suffix, $host) = @_; +return undef if (!$config{'sched_webhook'}); +my %params = ( 'status_value' => $stat->{'value'}, + 'status_nice_value' => $stat->{'nice_value'}, + 'status_desc' => $stat->{'desc'}, + 'status' => $text{'mon_'.$suffix}, + 'host' => $host, + $suffix => 1, + ); +foreach my $k (keys %$serv) { + next if ($k =~ /^_/); + next if ($serv->{$k} eq ""); + $params{'service_'.$k} = $serv->{$k}; + } +my ($host, $port, $page, $ssl) = &parse_http_url($config{'sched_webhook'}); +my $params = join("&", map { $_."=".&urlize($params{$_}) } keys %params); +$page .= ($page =~ /\?/ ? "?" : "&"); +$page .= $params; +my ($out, $err); +&http_download($host, $port, $page, \$out, \$err, undef, $ssl, undef, undef, + 5, 0, 1); +return $err; +} + # run_on_command(&serv, command, remote-host) sub run_on_command { diff --git a/status/save_sched.cgi b/status/save_sched.cgi index b7921f929..b1f5114f9 100755 --- a/status/save_sched.cgi +++ b/status/save_sched.cgi @@ -61,6 +61,14 @@ else { } $config{'sched_smtp'} = $in{'smtp'}; } +if ($in{'webhook_def'}) { + delete($config{'sched_webhook'}); + } +else { + $in{'webhook'} =~ /^(http|https):\/\/\S+$/ || + &error($text{'sched_ewebhook'}); + $config{'sched_webhook'} = $in{'webhook'}; + } $config{'sched_mode'} = $in{'mode'}; $in{'int'} =~ /^\d+$/ || &error($text{'sched_eint'}); $config{'sched_int'} = $in{'int'}; diff --git a/status/status-lib.pl b/status/status-lib.pl index e7e5a7fd4..35a6d548d 100755 --- a/status/status-lib.pl +++ b/status/status-lib.pl @@ -411,6 +411,9 @@ if ($config{'snmp_server'}) { if ($config{'sched_carrier'} && $config{'sched_sms'}) { push(@rv, "sms"); } +if ($config{'sched_webhook'}) { + push(@rv, "webhook"); + } return @rv; }