root/test/t/testconfig.pm

Revision 65401ed4ea2de6867afad03da41e4ebfcb28bef9, 14.7 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

basic stratcon test, refs #230

  • Property mode set to 100644
Line 
1 package testconfig;
2 use Fcntl;
3 use DBI;
4 use Cwd;
5 use Exporter 'import';
6 use Data::Dumper;
7 use strict;
8 use vars qw/@EXPORT/;
9
10 my $noit_pid = 0;
11 my $stratcon_pid = 0;
12
13
14 @EXPORT = qw($NOIT_TEST_DB $NOIT_TEST_DB_PORT
15              $NOIT_API_PORT $NOIT_CLI_PORT
16              $STRATCON_API_PORT $STRATCON_CLI_PORT
17              pg make_noit_config start_noit stop_noit
18              make_stratcon_config start_stratcon stop_stratcon
19              $MODULES_DIR $LUA_DIR $all_noit_modules $all_stratcon_modules);
20
21 our $all_noit_modules = {
22   'selfcheck' => { 'image' => 'selfcheck' },
23   'ping_icmp' => { 'image' => 'ping_icmp' },
24   'snmp' => { 'image' => 'snmp' },
25   'ssh2' => { 'image' => 'ssh2' },
26   'mysql' => { 'image' => 'mysql' },
27   'postgres' => { 'image' => 'postgres' },
28   'varnish' => { 'loader' => 'lua', 'object' => 'noit.module.varnish' },
29   'http' => { 'loader' => 'lua', 'object' => 'noit.module.http' },
30   'resmon' => { 'loader' => 'lua', 'object' => 'noit.module.resmon' },
31   'smtp' => { 'loader' => 'lua', 'object' => 'noit.module.smtp' },
32   'tcp' => { 'loader' => 'lua', 'object' => 'noit.module.tcp' },
33 };
34
35 our $NOIT_TEST_DB = "/tmp/noit-test-db";
36 our $NOIT_TEST_DB_PORT = 23816;
37 our $NOIT_API_PORT = 42364;
38 our $NOIT_CLI_PORT = 42365;
39 our $STRATCON_API_PORT = 42366;
40 our $STRATCON_CLI_PORT = 42367;
41 our $STRATCON_WEB_PORT = 42368;
42
43 our ($MODULES_DIR, $LUA_DIR);
44
45 sub pg {
46   my $db = shift || 'postgres';
47   my $user = shift || $ENV{USER};
48   return DBI->connect(
49     "dbi:Pg:host=localhost;port=$NOIT_TEST_DB_PORT;database=$db", $user, ''
50   );
51 }
52
53 sub make_eventer_config {
54   my ($o, $opts) = @_;
55   my $cwd = $opts->{cwd};
56   $opts->{eventer_config}->{default_queue_threads} ||= 10;
57   $opts->{eventer_config}->{default_ca_chain} ||= "$cwd/../test-ca.crt";
58   print $o qq{
59   <eventer>
60     <config>
61       <default_queue_threads>$opts->{eventer_config}->{default_queue_threads}</default_queue_threads>
62       <default_ca_chain>$opts->{eventer_config}->{default_ca_chain}</default_ca_chain>
63     </config>
64   </eventer>
65 };
66 }
67 sub make_log_section {
68   my ($o, $type, $dis) = @_;
69   print $o qq{      <$type>
70         <outlet name="$type"/>
71 };
72   while (my ($t, $d) = each %$dis) {
73     next unless length($t);
74     print $o qq{        <log name="$type/$t" disabled="$d"/>\n};
75   }
76   print $o qq{      </$type>\n};
77 }
78 sub make_logs_config {
79   my ($o, $opts) = @_;
80   my $cwd = $opts->{cwd};
81   my @logtypes = qw/collectd dns eventer external lua mysql ping_icmp postgres
82                     selfcheck snmp ssh2/;
83   # These are disabled attrs, so they look backwards
84   if(!exists($opts->{logs_error})) {
85     $opts->{logs_error}->{''} ||= 'false';
86   }
87   if(!exists($opts->{logs_debug})) {
88     $opts->{logs_debug}->{''} ||= 'true';
89   }
90   foreach(@logtypes) {
91     $opts->{logs_error}->{$_} ||= 'false';
92     $opts->{logs_debug}->{$_} ||= 'false';
93   }
94  
95   print $o qq{
96   <logs>
97     <console_output>
98       <outlet name="stderr"/>
99       <log name="error" disabled="$opts->{logs_error}->{''}"/>
100       <log name="debug" disabled="$opts->{logs_debug}->{''}"/>
101     </console_output>
102     <feeds>
103       <log name="feed" type="jlog" path="$cwd/logs/$opts->{name}.feed(stratcon)"/>
104     </feeds>
105     <components>
106 };
107   make_log_section($o, 'error', $opts->{logs_error});
108   make_log_section($o, 'debug', $opts->{logs_debug});
109   print $o qq{
110     </components>
111     <feeds>
112       <outlet name="feed"/>
113       <log name="check">
114         <outlet name="error"/>
115       </log>
116       <log name="status"/>
117       <log name="metrics"/>
118       <log name="config"/>
119     </feeds>
120   </logs>
121 };
122 }
123 sub make_modules_config {
124   my ($o, $opts) = @_;
125   my $cwd = $opts->{cwd};
126   print $o qq{
127   <modules directory="$cwd/../../src/modules">
128     <loader image="lua" name="lua">
129       <config><directory>$cwd/../../src/modules-lua/?.lua</directory></config>
130     </loader>
131 };
132   foreach(keys %{$opts->{generics}}) {
133     print $o qq{    <generic };
134     print $o qq{ image="$opts->{generics}->{$_}->{image}"}
135       if(exists($opts->{generics}->{$_}->{image}));
136     print $o qq{ name="$_"/>\n};
137   }
138   foreach(keys %{$opts->{modules}}) {
139     print $o qq{    <module };
140     print $o qq{ image="$opts->{modules}->{$_}->{image}"}
141       if(exists($opts->{modules}->{$_}->{image}));
142     print $o qq{ loader="$opts->{modules}->{$_}->{loader}"}
143       if(exists($opts->{modules}->{$_}->{loader}));
144     print $o qq{ object="$opts->{modules}->{$_}->{object}"}
145       if(exists($opts->{modules}->{$_}->{object}));
146     print $o qq{ name="$_"/>\n};
147   }
148   print $o qq{</modules>\n};
149 }
150 sub make_noit_listeners_config {
151   my ($o, $opts) = @_;
152   my $cwd = $opts->{cwd};
153   $opts->{noit_api_port} ||= $NOIT_API_PORT;
154   $opts->{noit_cli_port} ||= $NOIT_CLI_PORT;
155   print $o qq{
156   <listeners>
157     <sslconfig>
158       <optional_no_ca>false</optional_no_ca>
159       <certificate_file>$cwd/../test-noit.crt</certificate_file>
160       <key_file>$cwd/../test-noit.key</key_file>
161       <ca_chain>$cwd/../test-ca.crt</ca_chain>
162     </sslconfig>
163     <consoles type="noit_console">
164       <listener address="*" port="$opts->{noit_cli_port}">
165         <config>
166           <line_protocol>telnet</line_protocol>
167         </config>
168       </listener>
169     </consoles>
170     <listener type="control_dispatch" address="*" port="$opts->{noit_api_port}" ssl="on">
171       <config>
172         <log_transit_feed_name>feed</log_transit_feed_name>
173       </config>
174     </listener>
175   </listeners>
176 };
177 }
178 sub do_check_print {
179   my $o = shift;
180   my $list = shift;
181   return unless $list;
182   foreach my $node (@$list) {
183     print $o qq{<$node->[0]};
184     while(my ($k, $v) = each %{$node->[1]}) {
185       print $o qq{ $k="$v"};
186     }
187     if($node->[2]) {
188       print $o qq{>\n};
189       do_check_print($o, $node->[2]);
190       print $o qq{</check>\n};
191     }
192     else {
193       print $o qq{/>\n};
194     }
195   }
196 }
197 sub make_checks_config {
198   my ($o, $opts) = @_;
199   my $cwd = $opts->{cwd};
200   print $o qq{  <checks max_initial_stutter="1000" filterset="default">\n};
201   do_check_print($o, $opts->{checks});
202   print $o qq{  </checks>\n};
203 }
204 sub make_filtersets_config {
205   my ($o, $opts) = @_;
206   my $cwd = $opts->{cwd};
207   print $o qq{<filtersets>\n};
208   while (my ($name, $set) = each %{$opts->{filtersets}}) {
209     print $o qq{  <filterset name="$name">\n};
210     foreach my $rule (@$set) {
211       print $o qq{    <rule };
212       while(my ($k,$v) = each %$rule) {
213         print $o qq{ $k="$v"};
214       }
215       print $o qq{/>\n};
216     }
217     print $o qq{  </filterset>\n};
218   }
219   print $o qq{</filtersets>\n};
220 }
221
222 sub make_noit_config {
223   my $name = shift;
224   my $options = shift;
225   $options->{cwd} ||= cwd();
226   $options->{modules} = $all_noit_modules unless exists($options->{modules});
227   my $cwd = $options->{cwd};
228   my $file = "$cwd/logs/${name}_noit.conf";
229   open (my $o, ">$file") || BAIL_OUT("can't write config: $file");
230   print $o qq{<?xml version="1.0" encoding="utf8" standalone="yes"?>\n};
231   print $o qq{<noit>};
232   make_eventer_config($o, $options);
233   make_logs_config($o, $options);
234   make_modules_config($o, $options);
235   make_noit_listeners_config($o, $options);
236   make_checks_config($o, $options);
237   make_filtersets_config($o, $options);
238   print $o qq{</noit>\n};
239   close($o);
240   return $file;
241 }
242
243 sub make_stratcon_noits_config {
244   my ($o, $opts) = @_;
245   my $cwd = $opts->{cwd};
246   $opts->{noit_api_port} ||= $NOIT_API_PORT;
247   print $o qq{
248   <noits>
249     <sslconfig>
250       <certificate_file>$cwd/../test-stratcon.crt</certificate_file>
251       <key_file>$cwd/../test-stratcon.key</key_file>
252       <ca_chain>$cwd/../test-ca.crt</ca_chain>
253     </sslconfig>
254     <config>
255       <reconnect_initial_interval>1000</reconnect_initial_interval>
256       <reconnect_maximum_interval>15000</reconnect_maximum_interval>
257     </config>
258 };
259   foreach my $n (@{$opts->{noits}}) {
260     print $o qq{    <noit};
261     while (my ($k,$v) = each %$n) {
262       print $o qq{ $k=\"$v\"};
263     }
264     print $o qq{/>\n};
265   }
266   print $o qq{</noits>\n};
267 }
268
269 sub make_stratcon_listeners_config {
270   my ($o, $opts) = @_;
271   my $cwd = $opts->{cwd};
272   $opts->{stratcon_api_port} ||= $STRATCON_API_PORT;
273   $opts->{stratcon_web_port} ||= $STRATCON_WEB_PORT;
274   print $o qq{
275   <listeners>
276     <sslconfig>
277       <certificate_file>$cwd/../test-stratcon.crt</certificate_file>
278       <key_file>$cwd/../test-stratcon.key</key_file>
279       <ca_chain>$cwd/../test-ca.crt</ca_chain>
280     </sslconfig>
281     <realtime type="http_rest_api">
282       <listener address="*" port="$opts->{stratcon_web_port}">
283         <config>
284           <hostname>stratcon.noit.example.com</hostname>
285           <document_domain>noit.example.com</document_domain>
286         </config>
287       </listener>
288     </realtime>
289     <listener type="control_dispatch" address="*" port="$opts->{stratcon_api_port}" ssl="on" />
290   </listeners>
291 };
292 }
293
294 sub make_iep_config {
295   my ($o, $opts) = @_;
296   my $cwd = $opts->{cwd};
297   $opts->{iep}->{disabled} ||= 'false';
298   mkdir("$cwd/logs/$opts->{name}_iep_root");
299   open(my $run, "<$cwd/../../src/java/run-iep.sh") ||
300     BAIL_OUT("cannot open source run-iep.sh");
301   sysopen(my $newrun, "$cwd/logs/$opts->{name}_iep_root/run-iep.sh", O_WRONLY|O_CREAT, 0755) ||
302     BAIL_OUT("cannot open target run-iep.sh");
303   while(<$run>) {
304     s%^DIRS="%DIRS="$cwd/../../src/java $cwd/../../src/java/lib %;
305     print $newrun $_;
306   }
307   close($run);
308   close($newrun);
309   print $o qq{
310   <iep disabled="$opts->{iep}->{disabled}">
311     <start directory="$cwd/logs/$opts->{name}_iep_root"
312            command="$cwd/logs/$opts->{name}_iep_root/run-iep.sh" />
313 };
314   foreach my $mqt (keys %{$opts->{iep}->{mq}}) {
315     print $o qq{    <mq type="$mqt">\n};
316     while (my ($k,$v) = each %{$opts->{iep}->{mq}->{mqt}}) {
317       print $o qq{      <$k>$v</$k>\n};
318     }
319     print $o qq{    </mq>\n};
320   }
321   foreach my $bt (keys %{$opts->{iep}->{broker}}) {
322     print $o qq{    <broker adapter="$bt">\n};
323     while (my ($k,$v) = each %{$opts->{iep}->{broker}->{bt}}) {
324       print $o qq{      <$k>$v</$k>\n};
325     }
326     print $o qq{    </broker>\n};
327   }
328   print $o qq{</iep>\n};
329 }
330 sub make_database_config {
331   my ($o, $opts) = @_;
332   my $cwd = $opts->{cwd};
333   print $o qq{
334   <database>
335     <journal>
336       <path>$cwd/logs/$opts->{name}_stratcon.persist</path>
337     </journal>
338     <dbconfig>
339       <host>localhost</host>
340       <port>$NOIT_TEST_DB_PORT</port>
341       <dbname>reconnoiter</dbname>
342       <user>stratcon</user>
343       <password>stratcon</password>
344     </dbconfig>
345     <statements>
346       <allchecks><![CDATA[
347         SELECT remote_address, id, target, module, name
348           FROM check_currently
349       ]]></allchecks>
350       <findcheck><![CDATA[
351         SELECT remote_address, id, target, module, name
352           FROM check_currently
353          WHERE sid = \$1
354       ]]></findcheck>
355       <allstoragenodes><![CDATA[
356         SELECT storage_node_id, fqdn, dsn
357           FROM stratcon.storage_node
358       ]]></allstoragenodes>
359       <findstoragenode><![CDATA[
360         SELECT fqdn, dsn
361           FROM stratcon.storage_node
362          WHERE storage_node_id = \$1
363       ]]></findstoragenode>
364       <mapallchecks><![CDATA[
365         SELECT id, sid, noit as remote_cn, storage_node_id, fqdn, dsn
366           FROM stratcon.map_uuid_to_sid LEFT JOIN stratcon.storage_node USING (storage_node_id)
367       ]]></mapallchecks>
368       <mapchecktostoragenode><![CDATA[
369         SELECT o_storage_node_id as storage_node_id, o_sid as sid,
370                o_fqdn as fqdn, o_dsn as dsn
371           FROM stratcon.map_uuid_to_sid(\$1,\$2)
372       ]]></mapchecktostoragenode>
373       <check><![CDATA[
374         INSERT INTO check_archive_%Y%m%d
375                     (remote_address, whence, sid, id, target, module, name)
376              VALUES (\$1, 'epoch'::timestamptz + (\$2 || ' seconds')::interval,
377                      \$3, \$4, \$5, \$6, \$7)
378       ]]></check>
379       <status><![CDATA[
380         INSERT INTO check_status_archive_%Y%m%d
381                     (whence, sid, state, availability, duration, status)
382              VALUES ('epoch'::timestamptz + (\$1 || ' seconds')::interval,
383                      \$2, \$3, \$4, \$5, \$6)
384       ]]></status>
385       <metric_numeric><![CDATA[
386         INSERT INTO metric_numeric_archive_%Y%m%d
387                     (whence, sid, name, value)
388              VALUES ('epoch'::timestamptz + (\$1 || ' seconds')::interval,
389                      \$2, \$3, \$4)
390       ]]></metric_numeric>
391       <metric_text><![CDATA[
392         INSERT INTO metric_text_archive_%Y%m%d
393                     ( whence, sid, name,value)
394              VALUES ('epoch'::timestamptz + (\$1 || ' seconds')::interval,
395                      \$2, \$3, \$4)
396       ]]></metric_text>
397       <config><![CDATA[
398         SELECT stratcon.update_config
399                (\$1, \$2, \$3,
400                 'epoch'::timestamptz + (\$4 || ' seconds')::interval,
401                 \$5)
402       ]]></config>
403       <findconfig><![CDATA[
404         SELECT config FROM stratcon.current_node_config WHERE remote_cn = \$1
405       ]]></findconfig>
406     </statements>
407   </database>
408 };
409 }
410
411 sub make_stratcon_config {
412   my $name = shift;
413   my $options = shift;
414   $options->{cwd} ||= cwd();
415   $options->{generics} ||= { 'stomp_driver' => { image => 'stomp_driver' } };
416   $options->{iep}->{mq} ||= { 'stomp' => {} };
417   my $cwd = $options->{cwd};
418   my $file = "$cwd/logs/${name}_stratcon.conf";
419   open (my $o, ">$file") || BAIL_OUT("can't write config: $file");
420   print $o qq{<?xml version="1.0" encoding="utf8" standalone="yes"?>\n};
421   print $o qq{<stratcon>};
422   make_eventer_config($o, $options);
423   make_stratcon_noits_config($o, $options);
424   make_logs_config($o, $options);
425   make_modules_config($o, $options);
426   make_stratcon_listeners_config($o, $options);
427   make_database_config($o, $options);
428   make_iep_config($o, $options);
429   print $o qq{</stratcon>\n};
430   close($o);
431   return $file;
432 }
433
434 sub start_noit {
435   my $name = shift;
436   my $options = shift;
437   $options->{name} = $name;
438   return 0 if $noit_pid;
439   my $conf = make_noit_config($name, $options);
440   $noit_pid = fork();
441   mkdir "logs";
442   if($noit_pid == 0) {
443     close(STDIN);
444     open(STDIN, "</dev/null");
445     close(STDOUT);
446     open(STDOUT, ">/dev/null");
447     close(STDERR);
448     open(STDERR, ">logs/${name}_noit.log");
449     my @args = ( 'noitd', '-D', '-c', $conf );
450     exec { '../../src/noitd' } @args;
451     exit(-1);
452   }
453   return $noit_pid;
454 }
455 sub stop_noit {
456   kill 9, $noit_pid if($noit_pid && kill 1, $noit_pid);
457   $noit_pid = 0;
458   return 1;
459 }
460
461 sub start_stratcon {
462   my $name = shift;
463   my $options = shift;
464   $options->{name} = $name;
465   return 0 if $stratcon_pid;
466   my $conf = make_stratcon_config($name, $options);
467   $stratcon_pid = fork();
468   mkdir "logs";
469   if($stratcon_pid == 0) {
470     close(STDIN);
471     open(STDIN, "</dev/null");
472     close(STDOUT);
473     open(STDOUT, ">/dev/null");
474     close(STDERR);
475     open(STDERR, ">logs/${name}_stratcon.log");
476     my @args = ( 'stratcond', '-D', '-c', $conf );
477     exec { '../../src/stratcond' } @args;
478     exit(-1);
479   }
480   return $stratcon_pid;
481 }
482 sub stop_stratcon {
483   kill 9, $stratcon_pid if($stratcon_pid && kill 1, $stratcon_pid);
484   $stratcon_pid = 0;
485   return 1;
486 }
487
488 END {
489   kill 9, $noit_pid if($noit_pid && kill 1, $noit_pid);
490   kill 9, $stratcon_pid if($stratcon_pid && kill 1, $stratcon_pid);
491 }
492 1;
Note: See TracBrowser for help on using the browser.