Add support for reporting status to an HTTP webhook https://github.com/webmin/webmin/issues/1733

This commit is contained in:
Jamie Cameron
2023-04-09 16:01:12 -07:00
parent c94e5d9aa0
commit 850030ed17
6 changed files with 59 additions and 3 deletions

View File

@@ -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' => '*' };

View File

@@ -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'} ] ]);

View File

@@ -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

View File

@@ -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
{

View File

@@ -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'};

View File

@@ -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;
}