Skip to content

Commit

Permalink
agents: make command clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Jun 3, 2024
1 parent e893c35 commit 9e4bef8
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 10 deletions.
13 changes: 13 additions & 0 deletions docs/documentation/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,19 @@ Replaces the string "GH#123" with the link:
<a href="https://github.com/sni/Thruk/issues/123">123</a>


=== copy_paste_link

Define pattern to be replaced with copy / paste buttons in the plugin output.

Syntax is: ```(pre text regex pattern)(link text regex pattern)(post text regex pattern)```

Can be set multiple times.

ex.:

copy_paste_link = (ticket id: ")(.*?)(")


=== perf_bar_mode
This option enables a performance bar inside the status/host list which
create a graph from the performance data of the plugin output. Available
Expand Down
2 changes: 2 additions & 0 deletions lib/Thruk/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ my $base_defaults = {
'show_logout_button' => 1,
'logout_link' => '',
'short_link' => [],
'copy_paste_link' => ['(run command: )(.*?)( \\))'],
'commandline_obfuscate_pattern' => [],
'backends_with_obj_config' => {},
'use_feature_configtool' => 0,
Expand Down Expand Up @@ -814,6 +815,7 @@ sub get_toolkit_config {
'escape_html' => \&Thruk::Utils::Filter::escape_html,
'lc' => \&Thruk::Utils::Filter::lc,
'replace_links' => \&Thruk::Utils::Filter::replace_links,
'replace_copy_paste' => \&Thruk::Utils::Filter::replace_copy_paste,
},
'PRE_DEFINE' => {
# subs from Thruk::Utils::Filter will be added automatically
Expand Down
28 changes: 28 additions & 0 deletions lib/Thruk/Utils/Filter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,34 @@ sub replace_links {
return($txt);
}

########################################

=head2 replace_copy_paste
replace_copy_paste()
return text with copy/paste links replaced with clickable links
=cut
sub replace_copy_paste {
my($txt) = @_;

my $c = $Thruk::Globals::c or die("not initialized!");

my $link = '$1$2 <button class="inline iconOnly px-1 copy_button" onclick="copyCode(event, \'$3\'); return false;"><i class="uil uil-copy" title="Copy to clipboard"></i></button>$4';

# copy paste links
for my $pattern (@{$c->config->{'copy_paste_link'}}) {
## no critic
my $re1 = qr($pattern);
$txt =~ s|$re1|&_replace_dollars($link, $1, $2, escape_js($2), $3)|ge;
## use critic
}

return($txt);
}


########################################
sub _replace_link {
my($name, $url) = @_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,12 +637,12 @@ sub _check_inventory {
}
}
if(scalar @need_update > 0) {
return(sprintf("WARNING - %s check%s could re-apply defaults. |%s\n%s\n\n(thruk agents -II %s)\n",
return(sprintf("WARNING - %s check%s could re-apply defaults. |%s\n( run command: thruk agents -II '%s' )\nservices:\n%s\n",
scalar @need_update,
(scalar @need_update != 1 ? 's' : ''),
$perfdata,
join("\n", @need_update),
$hostname,
join("\n", @need_update),
), 1);
}

Expand Down
4 changes: 2 additions & 2 deletions templates/_plugin_output.tt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[% plugin_output = remove_html_comments(obj.plugin_output) %]
[% long_plugin_output = remove_html_comments(obj.long_plugin_output) %]
[% IF obj.has_been_checked == 0 %]obj has not been checked yet[% END %]
[% IF escape_html_tags %][% nl2br(escape_html(plugin_output)) | replace_links %][% ELSE %][% nl2br(plugin_output) | replace_links %][% END %]
[% IF escape_html_tags %][% nl2br(escape_html(plugin_output)) | replace_links | replace_copy_paste %][% ELSE %][% nl2br(plugin_output) | replace_links | replace_copy_paste %][% END %]
[% IF long_plugin_output %]
<br>[% IF escape_html_tags %][% nl2br(escape_html(long_plugin_output)) | replace_links %][% ELSE %][% nl2br(long_plugin_output) | replace_links %][% END %]
<br>[% IF escape_html_tags %][% nl2br(escape_html(long_plugin_output)) | replace_links | replace_copy_paste %][% ELSE %][% nl2br(long_plugin_output) | replace_links | replace_copy_paste %][% END %]
[% END %]
6 changes: 3 additions & 3 deletions templates/_status_detail_table.tt
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@
>
<div class="status col_plugin_output flex items-center truncate">
[% IF long_plugin_output and show_long_plugin_output == 'popup' %]
<div class="status col_plugin_output has_long_plugin_output font-bold link clickable flex-1 truncate">[% plugin_output | replace_links %]</div>
<div class="status col_plugin_output has_long_plugin_output font-bold link clickable flex-1 truncate">[% plugin_output | replace_links | replace_copy_paste %]</div>
[% ELSE %]
<div class="status col_plugin_output flex-1 truncate">
[% plugin_output | replace_links %]
[% plugin_output | replace_links | replace_copy_paste %]
</div>
[% END %]
[% IF long_plugin_output and show_long_plugin_output == 'inline' %]
<br>[% nl2br(long_plugin_output) | replace_links %]
<br>[% nl2br(long_plugin_output) | replace_links | replace_copy_paste %]
[% END %]
<div>
[% PROCESS _perfdata_table add_link="1" state=s.state plugin_output=orig_plugin_output perfdata=s.perf_data check_command=s.check_command hst=s.host_name svc=s.description pnp_url=get_pnp_url(c, s, 1) %]
Expand Down
6 changes: 3 additions & 3 deletions templates/_status_hostdetail_table.tt
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@
>
<div class="status col_plugin_output flex items-center truncate">
[% IF long_plugin_output and show_long_plugin_output == 'popup' %]
<div class="status col_plugin_output has_long_plugin_output font-bold link clickable flex-1 truncate">[% plugin_output | replace_links %]</div>
<div class="status col_plugin_output has_long_plugin_output font-bold link clickable flex-1 truncate">[% plugin_output | replace_links | replace_copy_paste %]</div>
[% ELSE %]
<div class="status col_plugin_output flex-1 truncate">
[% plugin_output | replace_links %]
[% plugin_output | replace_links | replace_copy_paste %]
</div>
[% END %]
[% IF long_plugin_output and show_long_plugin_output == 'inline' %]
<br>[% nl2br(long_plugin_output) | replace_links %]
<br>[% nl2br(long_plugin_output) | replace_links | replace_copy_paste %]
[% END %]
<div>
[% PROCESS _perfdata_table add_link="1" state=h.state plugin_output=orig_plugin_output perfdata=h.perf_data check_command=h.check_command hst=h.name svc="_HOST_" pnp_url=get_pnp_url(c, h, 1) %]
Expand Down
3 changes: 3 additions & 0 deletions thruk.conf
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ show_contacts = 1
#short_link = |(INC\d+)|[$1](https://tickets.company.com/ticket/$1)|
#short_link = |GH#(\d+)|[Issue $1](https://github.com/sni/Thruk/issues/$1)|

# replace copy/paste pattern with clickable links, ex.: in plugin output, comments and downtimes.
#copy_paste_link = (run command: )(.*?)( \\))

######################################
# Command Settings
# Set this if a contact should be allowed to send commands unless
Expand Down

0 comments on commit 9e4bef8

Please sign in to comment.