File Coverage

blib/lib/Siebel/Lbconfig.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Siebel::Lbconfig;
2 1     1   13756 use strict;
  1         1  
  1         22  
3 1     1   3 use warnings;
  1         1  
  1         22  
4 1     1   860 use Siebel::Srvrmgr::Daemon::Light 0.26;
  0            
  0            
5             use Siebel::Srvrmgr::Daemon::Command 0.26;
6             use Set::Tiny 0.03;
7             use Siebel::Srvrmgr::Util::IniDaemon 0.26 qw(create_daemon);
8             use Config::IniFiles 2.88;
9             use Carp;
10             use Exporter 'import';
11             use File::Spec;
12             use Cwd;
13             use Siebel::Lbconfig::Daemon::Action::ListServers;
14             use Siebel::Lbconfig::Daemon::Action::AOM;
15              
16             our $VERSION = '0.001'; # VERSION
17              
18             =pod
19              
20             =head1 NAME
21              
22             Siebel::Lbconfig - helper to generate optimied lbconfig file
23              
24             =head1 DESCRIPTION
25              
26             The distribution Siebel-Lbconfig was created based on classes from L.
27              
28             The command line utility C will connect to a Siebel Enterprise with C and generate a optimized
29             lbconfig.txt file by search all active AOMs that take can take advantage of the native load balancer.
30              
31             =cut
32              
33             our @EXPORT_OK = qw(recover_info get_daemon create_files);
34              
35             =head1 FUNCTIONS
36              
37             =head2 create_daemon
38              
39             Creates a L to connects to the Siebel Enterprise and retrieve the required information to create the lbconfig.txt.
40              
41             It expects as parameters:
42              
43             =over
44              
45             =item *
46              
47             A string of the complete path to a configuration file that is understandle by L (a INI file).
48              
49             =back
50              
51             Check the section "Configuration file" of this Pod for details about how to create and maintain the INI file.
52              
53             Return two values:
54              
55             =over
56              
57             =item *
58              
59             The daemon instance
60              
61             =item *
62              
63             A L instance.
64              
65             =back
66              
67             =head2 get_daemon
68              
69             Expects the path to a INI file as parameter.
70              
71             Returns an instance of L subclass configured in the INI file.
72              
73             =cut
74              
75             sub get_daemon {
76             my $cfg_file = shift;
77             my $daemon = create_daemon($cfg_file);
78              
79             # LoadPreferences does not add anything into ActionStash, so it's ok use a second action here
80             $daemon->push_command(
81             Siebel::Srvrmgr::Daemon::Command->new(
82             {
83             command => 'list comp',
84             action => 'Siebel::Lbconfig::Daemon::Action::AOM'
85             }
86             )
87             );
88             $daemon->push_command(
89             Siebel::Srvrmgr::Daemon::Command->new(
90             {
91             command => 'list servers',
92             action => 'Siebel::Lbconfig::Daemon::Action::ListServers'
93             }
94             )
95             );
96             my $stash = Siebel::Srvrmgr::Daemon::ActionStash->instance();
97             return $daemon, $stash;
98             }
99              
100             =head2 recover_info
101              
102             Expects as parameter the L instance returned by C and
103             the Siebel Connection Broker TCP port.
104              
105             Returns a hash array reference with all the rows data of the lbconfig.txt.
106              
107             =cut
108              
109             sub recover_info {
110             my ( $stash, $scb_port ) = @_;
111             my @data;
112             my $comps_ref = $stash->shift_stash();
113             my $servers_ref = $stash->shift_stash();
114             my $underscore = qr/_/;
115             my @sorted = sort( keys( %{$comps_ref} ) );
116              
117             foreach my $comp_alias (@sorted) {
118             my $virtual;
119              
120             if ( $comp_alias =~ $underscore ) {
121             my @parts = split( '_', $comp_alias );
122             $parts[0] =~ s/ObjMgr//;
123             $virtual = $parts[0] . uc( $parts[1] ) . '_VS';
124             }
125             else {
126             $virtual = $comp_alias . '_VS';
127             }
128              
129             my @row;
130              
131             foreach my $server ( @{ $comps_ref->{$comp_alias} } ) {
132              
133             if ( exists( $servers_ref->{$server} ) ) {
134             push(
135             @row,
136             (
137             $servers_ref->{$server} . ':'
138             . $server . ':'
139             . $scb_port
140             )
141             );
142             }
143             else {
144             confess
145             "'$server' is not part of the retrieved Sievel Server names!";
146             }
147             }
148              
149             push(
150             @data,
151             {
152             comp_alias => $comp_alias,
153             vs => $virtual,
154             servers => ( join( ';', @row ) . ';' )
155             }
156             );
157             }
158              
159             return \@data;
160             }
161              
162             =head2 create_files
163              
164             Creates the lbconfig.txt and eapps*.cfg files.
165              
166             Expects as parameters the directory where the eapps*.cfg file will be located. Those files will be used as templates, they will be copied to a new version of them,
167             with the ConnectionString modified and all other content as is. The copied will have the C<.new> file "extension" attached to them.
168              
169             Also, espects a data reference passed as the second parameter. The expected format is the same returned by the C sub.
170              
171             Returns nothing. The lbconfig.txt file will be located at the current directory.
172              
173             =cut
174              
175             sub create_files {
176             my ( $dir, $data_ref ) = @_;
177             my $lbconfig = File::Spec->catfile( getcwd(), 'lbconfig.txt' );
178             open( my $out, '>', $lbconfig ) or confess "Cannot create $lbconfig: $!";
179             my %aliases;
180              
181             foreach my $row ( @{$data_ref} ) {
182             print $out $row->{vs}, '=', $row->{servers}, "\n";
183             $aliases{ $row->{comp_alias} } = $row->{vs};
184             }
185              
186             close($out);
187              
188             my $pattern = File::Spec->catfile( $dir, 'eapps*.cfg' );
189             my @eapps = glob($pattern);
190             my $conn_regex = qr#^ConnectString#;
191             my $replace_regex =
192             qr#^(ConnectString\s?\=\s?siebel\.TCPIP\.\w+\.\w+\://)\w+(/\w+/)(\w+)#;
193             foreach my $file (@eapps) {
194             my $new = "$file.new";
195             open( my $old, '<', $file ) or confess "Cannot read $file: $!";
196             open( my $out, '>', $new ) or confess "Cannot create $new: $!";
197              
198             while (<$old>) {
199              
200             #ConnectString = siebel.TCPIP.None.None://VirtualServer/foobar/ERMObjMgr_chs
201             if ( $_ =~ $conn_regex ) {
202             chomp();
203              
204             if ( $_ =~ $replace_regex ) {
205              
206             if ( exists( $aliases{$3} ) ) {
207             print $out $1, $aliases{$3}, $2, $3, "\n";
208             }
209             else {
210             print $out $_, "\n";
211             }
212              
213             }
214             else {
215             print $out $_, "\n";
216             }
217              
218             }
219             else {
220             print $out $_;
221             }
222              
223             }
224              
225             close($old);
226             close($out);
227              
228             }
229             }
230              
231             =head1 CONFIGURATION FILE
232              
233             The configuration file must have a INI format, which is supported by the L module.
234              
235             Here is an example of the required parameters with a description:
236              
237             [GENERAL]
238             gateway=foobar:1055
239             enterprise=MyEnterprise
240             user=sadmin
241             password=123456
242             srvrmgr= /foobar/bin/srvrmgr
243             load_prefs = 1
244             type = light
245             time_zone = America/Sao_Paulo
246              
247             Beware that the commands executed by C requires that the output has a specific configuration set: setting
248             C is not optional here, but a requirement!
249              
250             Also make sure to use C because this distribution really doesn't need L and is
251             intended to work on MS Windows.
252              
253             =head1 SEE ALSO
254              
255             =over
256              
257             =item *
258              
259             L
260              
261             =item *
262              
263             L
264              
265             =item *
266              
267             L
268              
269             =back
270              
271             =head1 AUTHOR
272              
273             Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
274              
275             =head1 COPYRIGHT AND LICENSE
276              
277             This software is copyright (c) 2016 of Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
278              
279             This file is part of Siebel Monitoring Tools.
280              
281             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
282             it under the terms of the GNU General Public License as published by
283             the Free Software Foundation, either version 3 of the License, or
284             (at your option) any later version.
285              
286             Siebel Monitoring Tools is distributed in the hope that it will be useful,
287             but WITHOUT ANY WARRANTY; without even the implied warranty of
288             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
289             GNU General Public License for more details.
290              
291             You should have received a copy of the GNU General Public License
292             along with Siebel Monitoring Tools. If not, see .
293              
294             =cut
295              
296             1;