Netdata.pm

filter: Statementstmt 87.2% 89 / 102 Branchbran 80.5% 29 / 36 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 92.3% 24 / 26 Podpod 100% 1 / 1 totaltotal 81.5% 146 / 179 CC 26 SCAR 16.2
CC
26
Cov
82%
_build_collectors
14.7
start
12.0
_build_config
6.0
CRAP 5.0
« Netdata/Util.pm ↑ Summary Netdata/Chart.pm »
1 package Mojo::Netdata;
2 2 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::Netdata::Util qw(logf);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.04';
9  
10 1 ▶has collectors => sub ($self) { $self->_build_collectors };
Subroutine __ANON__: called CC=1 SCAR=0.0
11 2 ▶has config => sub ($self) { $self->_build_config };
Subroutine __ANON__: called CC=1 SCAR=0.0
12 4 ▶has user_config_dir => sub ($self) { $ENV{NETDATA_USER_CONFIG_DIR} || '/etc/netdata' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"NETDATA_USER_CONFIG_DIR"} or "/etc/netdata" 3 1 4
13 2 ▶has stock_config_dir => sub ($self) { $ENV{NETDATA_STOCK_CONFIG_DIR} || '/usr/lib/netdata/conf.d' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"NETDATA_STOCK_CONFIG_DIR"} or "/usr/lib/netdata/conf.d" 1 1 2
14 2 ▶has plugins_dir => sub ($self) { $ENV{NETDATA_PLUGINS_DIR} || '' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"NETDATA_PLUGINS_DIR"} or "" 1 1 2
15 2 ▶has web_dir => sub ($self) { $ENV{NETDATA_WEB_DIR} || '' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"NETDATA_WEB_DIR"} or "" 1 1 2
16 2 ▶has cache_dir => sub ($self) { $ENV{NETDATA_CACHE_DIR} || '' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"NETDATA_CACHE_DIR"} or "" 1 1 2
17 2 ▶has log_dir => sub ($self) { $ENV{NETDATA_LOG_DIR} || '' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"NETDATA_LOG_DIR"} or "" 1 1 2
18 2 ▶has host_prefix => sub ($self) { $ENV{NETDATA_HOST_PREFIX} || '' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"NETDATA_HOST_PREFIX"} or "" 1 1 2
19 2 ▶has debug_flags => sub ($self) { $ENV{NETDATA_DEBUG_FLAGS} || '' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"NETDATA_DEBUG_FLAGS"} or "" 1 1 2
20 2 ▶has update_every => sub ($self) { $ENV{NETDATA_UPDATE_EVERY} || 1 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"NETDATA_UPDATE_EVERY"} or 1 1 1 2
21  
22 0 ▶sub start ($self) {
Subroutine start: not called CC=3 SCAR=24.8
Pod documented
23 logf(
24 info => 'Starting %s with debug_flags=%s host_prefix=%s update_every=%s',
25 0 ref($self), map { $self->$_ } qw(debug_flags host_prefix update_every),
26 );
27 0 ▶ return 0 unless @{$self->collectors};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->collectors;} 0 0 0
28 0 $_->emit_charts->recurring_update_p for @{$self->collectors};
29 0 return int @{$self->collectors};
30 }
31  
32 2 ▶sub _build_config ($self) {
Subroutine _build_config: called CC=6 SCAR=17.9
33 2 my $config = {};
34  
35 2 my $file = path($self->user_config_dir, 'mojo.conf.pl')->to_abs;
36 2 ▶ if (-r $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $file) 1 1 2
37 1 logf(debug => 'Loading config file %s into config().', $file);
38 1 $config = _eval_file($file);
39 }
40  
41 2 my $conf_d = path($self->user_config_dir, 'mojo.conf.d')->to_abs->list->sort;
42 2 for my $file ($conf_d->each) {
43 3 ▶ next unless $file->basename =~ m!\.conf\.pl$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->basename =~ /\.conf\.pl$/u 1 2 3
44 2 ▶ next unless my $d = _eval_file($file);
Branch: 1 branch
BranchTrueFalseTotal
unless my $d = _eval_file($file) 0 2 2
45  
46 2 ▶ if ($d->{collector}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{'collector'}) { } 1 1 2
47 1 logf(debug => 'Adding config file %s to "collectors".', $file);
48 1 push @{$config->{collectors}}, $d;
49 }
50 else {
51 1 logf(debug => 'Merging config file %s into config().', $file);
52 1 @$config{keys(%$d)} = values %$d;
53 }
54 }
55  
56 2 return $config;
57 }
58  
59 1 ▶sub _build_collectors ($self) {
Subroutine _build_collectors: called CC=9 SCAR=26.8
60 1 ▶ my $fh = $self->{stdout} // \*STDOUT; # for testing
Condition50%1/2
$self->{"stdout"} // \*STDOUT
l!l
01
MC/DC0%0/1
$self->{"stdout"} // \*STDOUT
$self->{"stdout"}
Truth table50%1/2
A
A= $self->{"stdout"}
Aresult
11
00
61 1 $fh->autoflush;
62  
63 1 local $@;
64 1 my @collectors;
65 1 ▶ for my $collector_config (@{$self->config->{collectors} || []}) {
Condition50%1/2
$self->config->{'collectors'} || []
l!l
10
MC/DC0%0/1
$self->config->{'collectors'} || []
$self->config->{'collectors'}
Truth table50%1/2
A
A= $self->config->{'collectors'}
Aresult
11
00
66 2 my $collector_class = $collector_config->{collector};
67  
68 2 ▶ unless ($collector_class and $collector_class =~ m!^[\w:]+$!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($collector_class and $collector_class =~ /^[\w:]+$/u) 0 2 2
Condition33%1/3
$collector_class and $collector_class =~ /^[\w:]+$/u
!ll&&!rl&&r
002
MC/DC0%0/2
$collector_class and $collector_class =~ /^[\w:]+$/u
$collector_class $collector_class =~ /^[\w:]+$/u
Truth table33%1/3
A and B
A= $collector_classB= $collector_class =~ /^[\w:]+$/u
ABresult
0X0
100
111
69 0 ▶ logf(error => 'Invalid collector_class %s', $collector_class || 'missing');
Condition0%0/2
$collector_class || 'missing'
l!l
00
MC/DC0%0/1
$collector_class || 'missing'
$collector_class
Truth table0%0/2
A
A= $collector_class
Aresult
11
00
70 0 next;
71 }
72 2 ▶ unless (eval "require $collector_class;1") {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require $collector_class;1") 0 2 2
73 0 logf(error => 'Load %s FAIL %s', $collector_class, $@);
74 0 next;
75 }
76  
77 2 ▶ next unless my $collector = $collector_class->new->register($collector_config, $self);
Branch: 1 branch
BranchTrueFalseTotal
unless my $collector = $collector_class->new->register($collector_config, $self) 0 2 2
78 0 ▶ $collector->on(stdout => sub ($collector, $str) { $fh->print($str) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
79 2 logf(debug => 'Loaded and set up %s', $collector_class);
80 2 push @collectors, $collector;
81 }
82  
83 1 return \@collectors;
84 }
85  
86 3 ▶sub _eval_file ($file) {
Subroutine _eval_file: called CC=2 SCAR=6.9
87 3 local $@;
88 3 my $prefix = 'package Mojo::Netdata::Config; no warnings; use Mojo::Base -strict;';
89 3 ▶ my $config = eval $prefix . $file->slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
90 3 ▶ logf(error => 'Config file "%s" is invalid: %s', $file, $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
91 3 return $config;
92 }
93  
94 2 1;
95  
96 =encoding utf8
97  
98 =head1 NAME
99  
100 Mojo::Netdata - https://netdata.cloud plugin for Perl
101  
102 =head1 SYNOPSIS
103  
104 =head2 Installation
105  
106 sudo -i
107 apt install -y cpanminus
108 cpanm -n https://github.com/jhthorsen/mojo-netdata/archive/refs/heads/main.tar.gz
109 ln -s $(which mojo-netdata) /etc/netdata/custom-plugins.d/mojo-netdata.plugin
110  
111 # See "Config file" below for information on what to place inside mojo.conf.pl
112 $EDITOR /etc/netdata/mojo.conf.pl
113  
114 =head2 Config files
115  
116 The config files are located in C</etc/netdata/mojo.conf.d>. The files are
117 plain Perl files, which means you can define variables and call functions. The
118 only important part is that the last statement in the file is a hash-ref.
119  
120 Any hash-refs that has the "collector" key will be placed into L</collectors>,
121 while everything else will be merged into L</config>. Example:
122  
123 # /etc/netdata/mojo.conf.d/foo.conf.pl
124 {foo => 42, bar => 100}
125  
126 # /etc/netdata/mojo.conf.d/bar.conf.pl
127 {collector => 'Mojo::Netdata::Collector::HTTP', jobs => []}
128  
129 The two config files above will result in this L</config>:
130  
131 {
132 foo => 42,
133 bar => 100,
134 collectors => [
135 {collector => 'Mojo::Netdata::Collector::HTTP', jobs => []},
136 },
137 }
138  
139 See L<Mojo::Netdata::Collector::HTTP/SYNOPSIS> for an example config file.
140  
141 =head2 Log file
142  
143 The output from this Netdata plugin can be found in
144 C</var/log/netdata/error.log>.
145  
146 =head1 DESCRIPTION
147  
148 L<Mojo::Netdata> is a plugin for L<Netdata|https://netdata.cloud>. It can load
149 custom L<Mojo::Netdata::Collector> classes and write data back to Netdata on a
150 given interval.
151  
152 This module is currently EXPERIMENTAL, and the API might change without
153 warning.
154  
155 =head1 ATTRIBUTES
156  
157 =head2 cache_dir
158  
159 $path = $netdata->cache_dir;
160  
161 Holds the C<NETDATA_CACHE_DIR> environment variable. See
162 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#environment-variables>
163 for more details.
164  
165 =head2 config
166  
167 $hash_ref = $netdata->config;
168  
169 Holds the config for L<Mojo::Netdata> and all L</collectors>.
170  
171 =head2 collectors
172  
173 $array_ref = $netdata->collectors;
174  
175 An array-ref of L<Mojo::Netdata::Collector> objects.
176  
177 =head2 debug_flags
178  
179 $str = $netdata->debug_flags;
180  
181 Defaults to the C<NETDATA_DEBUG_FLAGS> environment variable. See
182 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#environment-variables>
183 for more details.
184  
185 =head2 host_prefix
186  
187 $str = $netdata->host_prefix;
188  
189 Defaults to the C<NETDATA_HOST_PREFIX> environment variable. See
190 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#environment-variables>
191 for more details.
192  
193 =head2 log_dir
194  
195 $path = $netdata->log_dir;
196  
197 Holds the C<NETDATA_LOG_DIR> environment variable. See
198 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#environment-variables>
199 for more details.
200  
201 =head2 plugins_dir
202  
203 $path = $netdata->plugins_dir;
204  
205 Holds the C<NETDATA_PLUGINS_DIR> environment variable. See
206 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#environment-variables>
207 for more details.
208  
209 =head2 stock_config_dir
210  
211 $path = $netdata->stock_config_dir;
212  
213 Holds the C<NETDATA_STOCK_CONFIG_DIR> environment variable. See
214 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#environment-variables>
215 for more details.
216  
217 =head2 update_every
218  
219 $num = $netdata->update_every;
220  
221 Defaults to the C<NETDATA_UPDATE_EVERY> environment variable. See
222 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#environment-variables>
223 for more details.
224  
225 =head2 user_config_dir
226  
227 $path = $netdata->user_config_dir;
228  
229 Holds the C<NETDATA_USER_CONFIG_DIR> environment variable. See
230 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#environment-variables>
231 for more details.
232  
233 =head2 web_dir
234  
235 $path = $netdata->web_dir;
236  
237 Holds the C<NETDATA_WEB_DIR> environment variable. See
238 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#environment-variables>
239 for more details.
240  
241 =head1 METHODS
242  
243 =head2 start
244  
245 $bool = $netdata->start;
246  
247 Reads the L</config> and return 1 if any L</collectors> got registered.
248  
249 =head1 AUTHOR
250  
251 Jan Henning Thorsen
252  
253 =head1 COPYRIGHT AND LICENSE
254  
255 Copyright (C) Jan Henning Thorsen
256  
257 This program is free software, you can redistribute it and/or modify it under
258 the terms of the Artistic License version 2.0.
259  
260 =head1 SEE ALSO
261  
262 L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d>.
263  
264 =cut
« Netdata/Util.pm ↑ Summary Netdata/Chart.pm »