root/test/t/testconfig.pm

Revision f387f75fc42c18a92cc4f5b9c14bba9ea420b842, 7.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

fixes for the apiclient, caused a bit of a wild goose chase. refs #230

  • Property mode set to 100644
Line 
1 package testconfig;
2 use DBI;
3 use Cwd;
4 use Exporter 'import';
5 use Data::Dumper;
6
7 my $noit_pid = 0;
8 my $stratcon_pid = 0;
9
10
11 @EXPORT = qw($NOIT_TEST_DB $NOIT_TEST_DB_PORT
12              $NOIT_API_PORT $NOIT_CLI_PORT
13              pg make_noit_config start_noit stop_noit
14              $MODULES_DIR $LUA_DIR $all_noit_modules);
15
16 our $all_noit_modules = {
17   'selfcheck' => { 'image' => 'selfcheck' },
18   'ping_icmp' => { 'image' => 'ping_icmp' },
19   'snmp' => { 'image' => 'snmp' },
20   'ssh2' => { 'image' => 'ssh2' },
21   'mysql' => { 'image' => 'mysql' },
22   'postgres' => { 'image' => 'postgres' },
23   'varnish' => { 'loader' => 'lua', 'object' => 'noit.module.varnish' },
24   'http' => { 'loader' => 'lua', 'object' => 'noit.module.http' },
25   'resmon' => { 'loader' => 'lua', 'object' => 'noit.module.resmon' },
26   'smtp' => { 'loader' => 'lua', 'object' => 'noit.module.smtp' },
27   'tcp' => { 'loader' => 'lua', 'object' => 'noit.module.tcp' },
28 };
29
30 our $NOIT_TEST_DB = "/tmp/noit-test-db";
31 our $NOIT_TEST_DB_PORT = 23816;
32 our $NOIT_API_PORT = 42364;
33 our $NOIT_CLI_PORT = 42365;
34
35 our ($MODULES_DIR, $LUA_DIR);
36
37 sub pg {
38   my $db = shift || 'postgres';
39   my $user = shift || $ENV{USER};
40   return DBI->connect(
41     "dbi:Pg:host=localhost;port=$NOIT_TEST_DB_PORT;database=$db", $user, ''
42   );
43 }
44
45 sub make_eventer_config {
46   my ($o, $opts) = @_;
47   my $cwd = $opts->{cwd};
48   $opts->{eventer_config}->{default_queue_threads} ||= 10;
49   $opts->{eventer_config}->{default_ca_chain} ||= "$cwd/../test-ca.crt";
50   print $o qq{
51   <eventer>
52     <config>
53       <default_queue_threads>$opts->{eventer_config}->{default_queue_threads}</default_queue_threads>
54       <default_ca_chain>$opts->{eventer_config}->{default_ca_chain}</default_ca_chain>
55     </config>
56   </eventer>
57 };
58 }
59 sub make_log_section {
60   my ($o, $type, $dis) = @_;
61   print $o qq{      <$type>
62         <outlet name="$type"/>
63 };
64   while (my ($t, $d) = each %$dis) {
65     next unless length($t);
66     print $o qq{        <log name="$type/$t" disabled="$d"/>\n};
67   }
68   print $o qq{      </$type>\n};
69 }
70 sub make_logs_config {
71   my ($o, $opts) = @_;
72   my $cwd = $opts->{cwd};
73   my @logtypes = qw/collectd dns eventer external lua mysql ping_icmp postgres
74                     selfcheck snmp ssh2/;
75   # These are disabled attrs, so they look backwards
76   if(!exists($opts->{logs_error})) {
77     $opts->{logs_error}->{''} ||= 'false';
78   }
79   if(!exists($opts->{logs_debug})) {
80     $opts->{logs_debug}->{''} ||= 'true';
81   }
82   foreach(@logtypes) {
83     $opts->{logs_error}->{$_} ||= 'false';
84     $opts->{logs_debug}->{$_} ||= 'false';
85   }
86  
87   print $o qq{
88   <logs>
89     <console_output>
90       <outlet name="stderr"/>
91       <log name="error" disabled="$opts->{logs_error}->{''}"/>
92       <log name="debug" disabled="$opts->{logs_debug}->{''}"/>
93     </console_output>
94     <feeds>
95       <log name="feed" type="jlog" path="$cwd/logs/$opts->{name}.feed(stratcon)"/>
96     </feeds>
97     <components>
98 };
99   make_log_section($o, 'error', $opts->{logs_error});
100   make_log_section($o, 'debug', $opts->{logs_debug});
101   print $o qq{
102     </components>
103     <feeds>
104       <outlet name="feed"/>
105       <log name="check">
106         <outlet name="error"/>
107       </log>
108       <log name="status"/>
109       <log name="metrics"/>
110       <log name="config"/>
111     </feeds>
112   </logs>
113 };
114 }
115 sub make_modules_config {
116   my ($o, $opts) = @_;
117   my $cwd = $opts->{cwd};
118   $opts->{modules} = $all_noit_modules unless exists($opts->{modules});
119   print $o qq{
120   <modules directory="$cwd/../../src/modules">
121     <loader image="lua" name="lua">
122       <config><directory>$cwd/../../src/modules-lua/?.lua</directory></config>
123     </loader>
124 };
125   foreach(keys %{$opts->{modules}}) {
126     print $o qq{    <module };
127     print $o qq{ image="$opts->{modules}->{$_}->{image}"}
128       if(exists($opts->{modules}->{$_}->{image}));
129     print $o qq{ loader="$opts->{modules}->{$_}->{loader}"}
130       if(exists($opts->{modules}->{$_}->{loader}));
131     print $o qq{ object="$opts->{modules}->{$_}->{object}"}
132       if(exists($opts->{modules}->{$_}->{object}));
133     print $o qq{ name="$_"/>\n};
134   }
135   print $o qq{</modules>\n};
136 }
137 sub make_listeners_config {
138   my ($o, $opts) = @_;
139   my $cwd = $opts->{cwd};
140   $options->{noit_api_port} ||= $NOIT_API_PORT;
141   $options->{noit_cli_port} ||= $NOIT_CLI_PORT;
142   print $o qq{
143   <listeners>
144     <sslconfig>
145       <optional_no_ca>false</optional_no_ca>
146       <certificate_file>$cwd/../test-noit.crt</certificate_file>
147       <key_file>$cwd/../test-noit.key</key_file>
148       <ca_chain>$cwd/../test-ca.crt</ca_chain>
149     </sslconfig>
150     <consoles type="noit_console">
151       <listener address="*" port="$options->{noit_cli_port}">
152         <config>
153           <line_protocol>telnet</line_protocol>
154         </config>
155       </listener>
156     </consoles>
157     <listener type="control_dispatch" address="*" port="$options->{noit_api_port}" ssl="on">
158       <config>
159         <log_transit_feed_name>feed</log_transit_feed_name>
160       </config>
161     </listener>
162   </listeners>
163 };
164 }
165 sub do_check_print {
166   my $o = shift;
167   my $list = shift;
168   return unless $list;
169   foreach my $node (@$list) {
170     print $o qq{<$node->[0]};
171     while(my ($k, $v) = each %{$node->[1]}) {
172       print $o qq{ $k="$v"};
173     }
174     if($node->[2]) {
175       print $o qq{>\n};
176       do_check_print($o, $node->[2]);
177       print $o qq{</check>\n};
178     }
179     else {
180       print $o qq{/>\n};
181     }
182   }
183 }
184 sub make_checks_config {
185   my ($o, $opts) = @_;
186   my $cwd = $opts->{cwd};
187   print $o qq{  <checks max_initial_stutter="1000" filterset="default">\n};
188   do_check_print($o, $options->{checks});
189   print $o qq{  </checks>\n};
190 }
191 sub make_filtersets_config {
192   my ($o, $opts) = @_;
193   my $cwd = $opts->{cwd};
194   print $o qq{<filtersets>\n};
195   while (my ($name, $set) = each %{$opts->{filtersets}}) {
196     print $o qq{  <filterset name="$name">\n};
197     foreach my $rule (@$set) {
198       print $o qq{    <rule };
199       while(my ($k,$v) = each %$rule) {
200         print $o qq{ $k="$v"};
201       }
202       print $o qq{/>\n};
203     }
204     print $o qq{  </filterset>\n};
205   }
206   print $o qq{</filtersets>\n};
207 }
208
209 sub make_noit_config {
210   my $name = shift;
211   my $options = shift;
212   $options->{cwd} ||= cwd();
213   my $cwd = $options->{cwd};
214   my $file = "$cwd/logs/${name}_noit.conf";
215   open (my $o, ">$file") || BAIL_OUT("can't write config: $file");
216   print $o qq{<?xml version="1.0" encoding="utf8" standalone="yes"?>\n};
217   print $o qq{<noit>};
218   make_eventer_config($o, $options);
219   make_logs_config($o, $options);
220   make_modules_config($o, $options);
221   make_listeners_config($o, $options);
222   make_checks_config($o, $options);
223   make_filtersets_config($o, $options);
224   print $o qq{</noit>\n};
225   close($o);
226   return $file;
227 }
228
229 sub start_noit {
230   my $name = shift;
231   my $options = shift;
232   $options->{name} = $name;
233   return 0 if $noit_pid;
234   my $conf = make_noit_config($name, $options);
235   $noit_pid = fork();
236   mkdir "logs";
237   if($noit_pid == 0) {
238     close(STDIN);
239     open(STDIN, "</dev/null");
240     close(STDOUT);
241     open(STDOUT, ">/dev/null");
242     close(STDERR);
243     open(STDERR, ">logs/${name}_noit.log");
244     my @args = ( 'noitd', '-D', '-c', $conf );
245     exec { '../../src/noitd' } @args;
246     exit(-1);
247   }
248   return $noit_pid;
249 }
250 sub stop_noit {
251   kill 9, $noit_pid if($noit_pid && kill 1, $noit_pid);
252   $noit_pid = 0;
253   return 1;
254 }
255
256 END {
257   kill 9, $noit_pid if($noit_pid && kill 1, $noit_pid);
258   kill 9, $stratcon_pid if($stratcon_pid && kill 1, $stratcon_pid);
259 }
260 1;
Note: See TracBrowser for help on using the browser.