File Coverage

blib/lib/Monitoring/Generator/TestConfig.pm
Criterion Covered Total %
statement 412 463 88.9
branch 137 212 64.6
condition 23 50 46.0
subroutine 32 32 100.0
pod 2 2 100.0
total 606 759 79.8


line stmt bran cond sub pod time code
1             package Monitoring::Generator::TestConfig;
2              
3 3     3   253452 use 5.005;
  3         21  
4 3     3   17 use strict;
  3         6  
  3         62  
5 3     3   16 use warnings;
  3         6  
  3         79  
6 3     3   15 use Carp;
  3         15  
  3         189  
7 3     3   1872 use POSIX qw(ceil);
  3         27246  
  3         18  
8 3     3   6330 use Data::Dumper;
  3         20707  
  3         260  
9 3     3   2123 use IPC::Cmd qw(can_run);
  3         142550  
  3         210  
10 3     3   1948 use Monitoring::Generator::TestConfig::ServiceCheckData;
  3         10  
  3         91  
11 3     3   1314 use Monitoring::Generator::TestConfig::HostCheckData;
  3         9  
  3         88  
12 3     3   1351 use Monitoring::Generator::TestConfig::InitScriptData;
  3         8  
  3         93  
13 3     3   1366 use Monitoring::Generator::TestConfig::P1Data;
  3         10  
  3         83  
14 3     3   1326 use Monitoring::Generator::TestConfig::Modules::Shinken;
  3         8  
  3         129  
15 3     3   1361 use Monitoring::Generator::TestConfig::ShinkenInitScriptData;
  3         10  
  3         17059  
16              
17             our $VERSION = '0.50';
18              
19             =head1 NAME
20              
21             Monitoring::Generator::TestConfig - generate monitoring configurations (nagios/icinga/shinken)
22              
23             =head1 SYNOPSIS
24              
25             use Monitoring::Generator::TestConfig;
26             my $ngt = Monitoring::Generator::TestConfig->new( 'output_dir' => '/tmp/test_monitoring' );
27             $ngt->create();
28              
29             =head1 DESCRIPTION
30              
31             This modul generates test configurations for your monitoring. This can be useful if you
32             want for doing load tests or testing addons and plugins.
33              
34             =head1 CONSTRUCTOR
35              
36             =over 4
37              
38             =item new ( [ARGS] )
39              
40             Creates an C object. C takes at least the output_dir.
41             Arguments are in key-value pairs.
42              
43             verbose verbose mode
44             output_dir export directory
45             overwrite_dir overwrite contents of an existing directory. Default: false
46             layout which config should be generated, valid options are "nagios", "icinga", "shinken" and "omd"
47             user user, defaults to the current user
48             group group, defaults to the current users group
49             prefix prefix to all hosts / services / contacts / contactgroups
50             binary path to your nagios/icinga bin
51             hostcount amount of hosts to export, Default 10
52             hostcheckcmd use custom hostcheck command line
53             servicecheckcmd use custom servicecheck command line
54             routercount amount of router to export, Default 5 ( exported as host and used as parent )
55             services_per_host amount of services per host, Default 10
56             host_settings key/value settings for use in the define host
57             service_settings key/value settings for use in the define service
58             main_cfg overwrite/add settings from the nagios.cfg/icinga.cfg
59             hostfailrate chance of a host to fail, Default 2%
60             servicefailrate chance of a service to fail, Default 5%
61             host_types key/value settings for percentage of hosttypes, possible keys are up,down,flap,random,block
62             router_types key/value settings for percentage of hosttypes for router
63             service_types key/value settings for percentage of servicetypes, possible keys are ok,warning,critical,unknown,flap,random,block
64             skip_dependencies no service dependencies will be exported
65             contacts_count amount of contacts to export, Default 1
66             contactgroups_count amount of contactgroups to export, Default 1
67             contacts_per_group amount of contacts to export, Default 1
68             contacts_per_host amount of contacts per host, Default 0
69             contactgroups_per_host amount of contactgroups per host, Default 1
70             contacts_per_service amount of contacts per service, Default 0
71             contactgroups_per_service amount of contactgroups per service, Default 1
72             skip_hostgroups no hostgroups will be exported
73             skip_servicegroups no servicegroups will be exported
74              
75             =back
76              
77             =cut
78              
79             ########################################
80             sub new {
81 5     5 1 33021 my($class,%options) = @_;
82 5         154 my $self = {
83             'verbose' => 0,
84             'output_dir' => undef,
85             'layout' => undef,
86             'user' => undef,
87             'group' => undef,
88             'prefix' => '',
89             'overwrite_dir' => 0,
90             'binary' => undef,
91             'contacts_count' => 1,
92             'contactgroups_count' => 1,
93             'contacts_per_group' => 1,
94             'contacts_per_host' => 0,
95             'contactgroups_per_host' => 1,
96             'contacts_per_service' => 0,
97             'contactgroups_per_service' => 1,
98             'routercount' => 5,
99             'hostcount' => 10,
100             'hostcheckcmd' => undef,
101             'servicecheckcmd' => undef,
102             'services_per_host' => 10,
103             'main_cfg' => {},
104             'host_settings' => {},
105             'service_settings' => {},
106             'servicefailrate' => 5,
107             'hostfailrate' => 2,
108             'skip_dependencies' => 0,
109             'skip_hostgroups' => 0,
110             'skip_servicegroups' => 0,
111             'fixed_length' => 0,
112             'router_types' => {
113             'down' => 10,
114             'up' => 50,
115             'flap' => 10,
116             'random' => 20,
117             'pending' => 10,
118             'block' => 0,
119             },
120             'host_types' => {
121             'down' => 5,
122             'up' => 50,
123             'flap' => 5,
124             'random' => 35,
125             'pending' => 5,
126             'block' => 0,
127             },
128             'service_types' => {
129             'ok' => 50,
130             'warning' => 5,
131             'unknown' => 5,
132             'critical' => 5,
133             'pending' => 5,
134             'flap' => 5,
135             'random' => 25,
136             'block' => 0,
137             },
138             };
139 5         16 bless $self, $class;
140              
141 5         21 for my $opt_key (keys %options) {
142 13 50       42 if(exists $self->{$opt_key}) {
143             # set option to scalar because getopt now uses objects instead of scalars
144 13 100 66     83 if(!defined $self->{$opt_key} and ref $options{$opt_key} eq '' and defined $options{$opt_key}) {
    50 66        
    0          
145 8         25 $self->{$opt_key} = ''.$options{$opt_key};
146             }
147             elsif(ref $self->{$opt_key} eq ref $options{$opt_key}) {
148 5         12 $self->{$opt_key} = $options{$opt_key};
149             }
150             elsif(ref $options{$opt_key} eq 'Getopt::Long::CallBack') {
151 0         0 $self->{$opt_key} = ''.$options{$opt_key};
152             }
153             else {
154 0         0 croak('unknown type for option '.$opt_key.': '.(ref $options{$opt_key}));
155             }
156             }
157             else {
158 0         0 croak("unknown option: $opt_key");
159             }
160             }
161              
162 5 100       22 if(!defined $self->{'layout'}) {
163 2 50       8 if(defined $ENV{'OMD_ROOT'}) {
164 0         0 $self->{'layout'} = "omd";
165             } else {
166 2         5 $self->{'layout'} = "nagios";
167             }
168             }
169              
170 5 50 100     33 if($self->{'layout'} ne 'nagios' and $self->{'layout'} ne 'icinga' and $self->{'layout'} ne 'shinken' and $self->{'layout'} ne 'omd') {
      66        
      33        
171 0         0 croak('valid layouts are: nagios, icinga, shinken and omd');
172             }
173              
174             # set some defaults for OMD
175 5 50       17 if($self->{'layout'} eq "omd") {
176 0 0       0 if(!defined $ENV{'OMD_ROOT'}) {
177 0         0 croak('please use omd layout only as OMD siteuser');
178             }
179 0         0 $self->{'output_dir'} = $ENV{'OMD_ROOT'};
180 0         0 $self->{'overwrite_dir'} = 1;
181 0         0 $self->{'binary'} = $ENV{'OMD_ROOT'}."/bin/nagios";
182 0         0 $self->{'user'} = $ENV{'OMD_SITE'};
183 0         0 $self->{'group'} = $ENV{'OMD_SITE'};
184 0         0 $self->{'prefix'} = $ENV{'OMD_SITE'}."_";
185             }
186              
187 5 50       19 if(!defined $self->{'output_dir'}) {
188 0         0 croak('no output_dir given');
189             }
190              
191             # strip off last slash
192 5         19 $self->{'output_dir'} =~ s/\/$//mx;
193              
194 5 50 33     96 if(-e $self->{'output_dir'} and !$self->{'overwrite_dir'}) {
195 0         0 croak('output_dir '.$self->{'output_dir'}.' does already exist and overwrite_dir not set');
196             }
197              
198             # set some defaults
199 5         18 my($user, $group);
200 5 50       23 if($^O eq "MSWin32") {
201 0         0 $user = getlogin();
202 0         0 $group = "nagios";
203             } else {
204 5         2294 $user = getpwuid($<);
205 5         491 my @userinfo = getpwnam($user);
206 5         386 my @groupinfo = getgrgid($userinfo[3]);
207 5         27 $group = $groupinfo[0];
208             }
209              
210 5 50       27 $self->{'user'} = $user unless defined $self->{'user'};
211 5 50       22 $self->{'group'} = $group unless defined $self->{'group'};
212              
213             # we dont want the root user to run the core
214 5 50       19 if($self->{'user'} eq 'root') {
215 5         8266 print STDERR "warning: root user is not recommended, using user '".$self->{'layout'}."' instead!\n";
216 5         37 $self->{'user'} = $self->{'layout'};
217 5         27 $self->{'group'} = $self->{'layout'};
218             }
219              
220             # try to find a binary in path
221 5 50       27 if(!defined $self->{'binary'}) {
222 5         15 my @possible_bin_locations;
223 5 100       30 if($self->{'layout'} eq 'nagios') {
    100          
    50          
224 3   50     27 $self->{'binary'} = can_run('nagios3') || can_run('nagios') || undef;
225 3         360329 @possible_bin_locations = qw|/usr/sbin/nagios3 /usr/bin/nagios3 /usr/local/bin/nagios3 /usr/sbin/nagios /usr/bin/nagios /usr/local/bin/nagios|;
226             } elsif($self->{'layout'} eq 'icinga' ) {
227 1   50     10 $self->{'binary'} = can_run('icinga') || undef;
228 1         396 @possible_bin_locations = qw|/usr/sbin/icinga /usr/bin/icinga /usr/local/bin/icinga|;
229             } elsif($self->{'layout'} eq 'shinken' ) {
230 1   50     9 $self->{'binary'} = can_run('shinken-arbiter') || '/usr/local/shinken/bin/shinken-arbiter';
231             }
232              
233             # still not defined?
234 5 100       413 if(!defined $self->{'binary'}) {
235 4         10 for my $loc (@possible_bin_locations) {
236 21 50       195 if(-x $loc) {
237 0         0 $self->{'binary'} = $loc;
238 0         0 last;
239             }
240             }
241             }
242             }
243              
244 5 100       26 if(!defined $self->{'binary'}) {
245             #carp('found no monitoring binary in path and none defined by the \'binary\' option, using fallback /usr/bin/nagios');
246 4         12 $self->{'binary'} = '/usr/bin/nagios';
247             }
248              
249 5         33 return $self;
250             }
251              
252              
253             ########################################
254              
255             =head1 METHODS
256              
257             =over 4
258              
259             =item create
260              
261             create()
262              
263             generates and writes the configuration
264             Returns true on success or undef on errors.
265              
266             =cut
267             sub create {
268 1     1 1 599 my $self = shift;
269              
270             # set open umask, so the webserver can read those files
271 1         11 umask(0022);
272              
273 1 50       26 if(!-d $self->{'output_dir'}."/.") {
274 0 0       0 mkdir($self->{'output_dir'}) or croak('failed to create output_dir '.$self->{'output_dir'}.':'.$!);
275             }
276              
277             # write out main config file
278 1 50       8 unless($self->{'layout'} eq 'omd') {
279 1         4 my $mainconfigfilename = $self->{'layout'}.'.cfg';
280 1 50       87 open(my $fh, '>', $self->{'output_dir'}.'/'.$mainconfigfilename) or die('cannot write: '.$!);
281 1         9 print $fh $self->_get_main_cfg();
282 1         60 close $fh;
283             }
284              
285             # create some missing dirs
286 1 50       11 unless($self->{'layout'} eq 'omd') {
287 1         4 for my $dir (qw{etc etc/conf.d var var/checkresults var/tmp plugins archives init.d}) {
288 8 50       170 if(!-d $self->{'output_dir'}.'/'.$dir) {
289             mkdir($self->{'output_dir'}.'/'.$dir)
290 8 50       345 or croak('failed to create dir ('.$self->{'output_dir'}.'/'.$dir.') :' .$!);
291             }
292             }
293             }
294              
295             # export config files and plugins
296 1         22 my $init = $self->{'layout'};
297 1         3 my $objects = {};
298 1         7 my($contactnames, $contactgroupnames) = $self->_set_contacts_cfg($objects);
299 1         5 $objects = $self->_set_hosts_cfg($objects, $contactnames, $contactgroupnames);
300 1 50       7 $objects = $self->_set_hostgroups_cfg($objects) unless $self->{'skip_hostgroups'};
301 1         5 $objects = $self->_set_services_cfg($objects, $contactnames, $contactgroupnames);
302 1 50       7 $objects = $self->_set_servicegroups_cfg($objects) unless $self->{'skip_servicegroups'};
303 1         5 $objects = $self->_set_commands_cfg($objects);
304 1         6 $objects = $self->_set_timeperiods_cfg($objects);
305 1         3 my $obj_prefix = '/etc/conf.d';
306 1         3 my $plg_prefix = '/plugins';
307 1 50       4 if($self->{'layout'} eq 'omd') {
308 0         0 $obj_prefix = '/etc/nagios/conf.d/generated';
309 0         0 $plg_prefix = '/local/lib/nagios/plugins';
310             }
311             my $exportedFiles = [
312             { file => $obj_prefix.'/hosts.cfg', data => $self->_create_object_conf('host', $objects->{'host'}) },
313             { file => $obj_prefix.'/hostgroups.cfg', data => $self->_create_object_conf('hostgroup', $objects->{'hostgroup'}) },
314             { file => $obj_prefix.'/services.cfg', data => $self->_create_object_conf('service', $objects->{'service'}) },
315             { file => $obj_prefix.'/servicegroups.cfg', data => $self->_create_object_conf('servicegroup', $objects->{'servicegroup'}) },
316             { file => $obj_prefix.'/contacts.cfg', data => $self->_create_object_conf('contactgroup', $objects->{'contactgroup'})
317             .$self->_create_object_conf('contact', $objects->{'contact'}) },
318             { file => $obj_prefix.'/commands.cfg', data => $self->_create_object_conf('command', $objects->{'command'}) },
319             { file => $plg_prefix.'/test_servicecheck.pl', data => Monitoring::Generator::TestConfig::ServiceCheckData->get_test_servicecheck() },
320             { file => $plg_prefix.'/test_hostcheck.pl', data => Monitoring::Generator::TestConfig::HostCheckData->get_test_hostcheck() },
321 1         6 { file => '/recreate.pl', data => $self->_get_recreate_pl($self->_get_used_libs($self->{'output_dir'}.'/recreate.pl')) },
322             ];
323              
324 1 50       10 if($self->{'layout'} ne 'omd') {
325 1         4 push(@{$exportedFiles}, { file => $obj_prefix.'/timeperiods.cfg', data => $self->_create_object_conf('timeperiod', $objects->{'timeperiod'}) });
  1         7  
326 1         3 push(@{$exportedFiles}, { file => '/etc/resource.cfg', data => '$USER1$='.$self->{'output_dir'}."/plugins\n" });
  1         6  
327             }
328              
329 1 50 33     6 if ($self->{'layout'} eq 'nagios' or $self->{'layout'} eq 'icinga') {
330 1         2 push(@{$exportedFiles}, { file => '/plugins/p1.pl', data => Monitoring::Generator::TestConfig::P1Data->get_p1_script() });
  1         12  
331 1         13 push(@{$exportedFiles}, { file => '/init.d/'.$init, data => Monitoring::Generator::TestConfig::InitScriptData->get_init_script(
332             $self->{'output_dir'},
333             $self->{'binary'},
334             $self->{'user'},
335             $self->{'group'},
336 1         3 $self->{'layout'}
337             )});
338             }
339 1 50       7 if ($self->{'layout'} eq 'shinken') {
340 0         0 push(@{$exportedFiles}, { file => '/etc/shinken-specific.cfg', data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_specific_cfg($self) });
  0         0  
341 0         0 push(@{$exportedFiles}, { file => '/etc/schedulerd.cfg', data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_schedulerd_cfg($self) });
  0         0  
342 0         0 push(@{$exportedFiles}, { file => '/etc/pollerd.cfg', data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_pollerd_cfg($self) });
  0         0  
343 0         0 push(@{$exportedFiles}, { file => '/etc/brokerd.cfg', data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_brokerd_cfg($self) });
  0         0  
344 0         0 push(@{$exportedFiles}, { file => '/etc/reactionnerd.cfg', data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_reactionnerd_cfg($self) });
  0         0  
345 0         0 push(@{$exportedFiles}, { file => '/init.d/'.$init, data => Monitoring::Generator::TestConfig::ShinkenInitScriptData->get_init_script(
346             $self->{'output_dir'},
347 0         0 $self->{'binary'},
348             ) });
349             }
350              
351             # export service dependencies
352 1         3 my $servicedependency = "";
353 1 50       3 unless ($self->{'skip_dependencies'} ) {
354 1         6 $objects = $self->_set_servicedependency_cfg($objects);
355 1         4 $servicedependency = $self->_create_object_conf('servicedependency', $objects->{'servicedependency'});
356             }
357 1         4 push(@{$exportedFiles}, { file => $obj_prefix.'/dependencies.cfg', data => $servicedependency });
  1         7  
358              
359 1 50       25 if( !-d $self->{'output_dir'}."/".$obj_prefix ) {
360 0 0       0 mkdir($self->{'output_dir'}."/".$obj_prefix) or croak('failed to create output_dir '.$self->{'output_dir'}."/".$obj_prefix.':'.$!);
361             }
362              
363 1         4 for my $exportFile (@{$exportedFiles}) {
  1         4  
364 14 50       833 open(my $fh, '>', $self->{'output_dir'}.$exportFile->{'file'}) or die('cannot write '.$self->{'output_dir'}.$exportFile->{'file'}.': '.$!);
365 14         206 print $fh $exportFile->{'data'};
366 14         406 close $fh;
367             }
368              
369 1 50       31 chmod 0755, $self->{'output_dir'}.$plg_prefix.'/test_servicecheck.pl' or die("cannot change modes: $!");
370 1 50       35 chmod 0755, $self->{'output_dir'}.$plg_prefix.'/test_hostcheck.pl' or die("cannot change modes: $!");
371 1         18 chmod 0755, $self->{'output_dir'}.'/plugins/p1.pl';
372 1         17 chmod 0755, $self->{'output_dir'}.'/init.d/'.$init;
373 1         16 chmod 0755, $self->{'output_dir'}.'/recreate.pl';
374              
375             # check user/group
376 1 50 33     18 if( $^O ne "MSWin32" and $< == 0 ) {
377 1         4808 `chown -R $self->{'user'}:$self->{'group'} $self->{'output_dir'}`;
378             }
379              
380 1 50       49 if($self->{'layout'} eq 'omd') {
381 0         0 print "exported omd test config to: ".$self->{'output_dir'}.$obj_prefix."\n";
382 0         0 print "check your configuration with: ~/etc/init.d/nagios checkconfig\n";
383             } else {
384 1         67 print "exported ".$self->{'layout'}." test config to: $self->{'output_dir'}\n";
385 1         27 print "check your configuration with: $self->{'output_dir'}/init.d/".$init." checkconfig\n";
386             }
387 1         18 print "configuration can be adjusted and recreated with $self->{'output_dir'}/recreate.pl\n";
388              
389 1         1382 return 1;
390             }
391              
392              
393             ########################################
394             sub _set_hosts_cfg {
395 1     1   4 my($self, $objects, $contactnames, $contactgroupnames) = @_;
396              
397 1 50       5 $objects->{'host'} = [] unless defined $objects->{'host'};
398              
399 1         15 my $hostconfig = {
400             'name' => 'generic-mgt-test-host',
401             'notifications_enabled' => 1,
402             'event_handler_enabled' => 1,
403             'flap_detection_enabled' => 1,
404             'failure_prediction_enabled' => 1,
405             'process_perf_data' => 1,
406             'retain_status_information' => 1,
407             'retain_nonstatus_information' => 1,
408             'max_check_attempts' => 5,
409             'check_interval' => 1,
410             'retry_interval' => 1,
411             'check_period' => '24x7',
412             'notification_interval' => 0,
413             'notification_period' => '24x7',
414             'notification_options' => 'd,u,r',
415             'register' => 0,
416             };
417              
418 1         3 my $merged = $self->_merge_config_hashes($hostconfig, $self->{'host_settings'});
419 1         2 push @{$objects->{'host'}}, $merged;
  1         3  
420 1         3 my @router;
421              
422             # router
423 1 50       3 if($self->{'routercount'} > 0) {
424 1         2 my @routertypes = @{$self->_fisher_yates_shuffle($self->_get_types($self->{'routercount'}, $self->{'router_types'}))};
  1         6  
425              
426 1   33     7 my $nr_length = $self->{'fixed_length'} || length($self->{'routercount'});
427 1         5 for(my $x = 0; $x < $self->{'routercount'}; $x++) {
428 5         8 my $hostgroup = "router";
429 5         17 my $nr = sprintf("%0".$nr_length."d", $x);
430 5         11 my $type = shift @routertypes;
431 5         21 push @router, $self->{'prefix'}."router_$nr";
432              
433             my $host = {
434             'host_name' => $self->{'prefix'}."router_".$nr,
435 5         43 'alias' => $self->{'prefix'}.$type."_".$nr,
436             'use' => 'generic-mgt-test-host',
437             'address' => '127.0.'.$x.'.1',
438             'hostgroups' => $hostgroup,
439             'check_command' => 'test-check-host-alive!'.$type,
440             'icon_image' => '../../docs/images/switch.png',
441             };
442 5 50       15 $host->{'active_checks_enabled'} = '0' if $type eq 'pending';
443 5 50       12 delete $host->{'hostgroups'} if $self->{'skip_hostgroups'};
444              
445             # first router gets additional infos
446 5 100       12 if($x == 0) {
447 1         4 $host->{'notes_url'} = 'http://search.cpan.org/dist/Monitoring-Generator-TestConfig/README';
448 1         4 $host->{'notes'} = 'just a notes string';
449 1         3 $host->{'icon_image_alt'} = 'icon alt string';
450 1         3 $host->{'action_url'} = 'http://search.cpan.org/dist/Monitoring-Generator-TestConfig/';
451             }
452 5 100       11 if($x == 1) {
453 1         4 $host->{'notes_url'} = 'http://search.cpan.org/dist/Monitoring-Generator-TestConfig/README';
454 1         2 $host->{'action_url'} = 'http://search.cpan.org/dist/Monitoring-Generator-TestConfig/';
455             }
456 5 100       12 if($x == 2) {
457 1         3 $host->{'notes_url'} = 'http://google.com/?q=$HOSTNAME$';
458 1         3 $host->{'action_url'} = 'http://google.com/?q=$HOSTNAME$';
459             }
460              
461             # add contacts
462 5         9 my @contacts = ();
463 5         12 for(my $z = 0; $z < $self->{'contacts_per_host'}; $z++) {
464 0         0 push @contacts, $contactnames->[ rand @{$contactnames} ];
  0         0  
465             }
466 5 50       12 $host->{'contacts'} = join(',', @contacts) if scalar @contacts > 0;
467              
468             # add contactgroups
469 5         9 my @contactgroups = ();
470 5         19 for(my $z = 0; $z < $self->{'contactgroups_per_host'}; $z++) {
471 5         11 push @contactgroups, $contactgroupnames->[ rand @{$contactgroupnames} ];
  5         17  
472             }
473 5 50       19 $host->{'contact_groups'} = join(',', @contactgroups) if scalar @contactgroups > 0;
474              
475 5         11 $host = $self->_merge_config_hashes($host, $self->{'host_settings'});
476 5         8 push @{$objects->{'host'}}, $host;
  5         19  
477             }
478             }
479              
480             # hosts
481 1         3 my @hosttypes = @{$self->_fisher_yates_shuffle($self->_get_types($self->{'hostcount'}, $self->{'host_types'}))};
  1         4  
482              
483 1   33     8 my $nr_length = $self->{'fixed_length'} || length($self->{'hostcount'});
484 1         5 for(my $x = 0; $x < $self->{'hostcount'}; $x++) {
485 10         24 my $hostgroup = "hostgroup_01";
486 10 100       25 $hostgroup = "hostgroup_02" if $x%5 == 1;
487 10 100       22 $hostgroup = "hostgroup_03" if $x%5 == 2;
488 10 100       21 $hostgroup = "hostgroup_04" if $x%5 == 3;
489 10 100       19 $hostgroup = "hostgroup_05" if $x%5 == 4;
490 10         35 my $nr = sprintf("%0".$nr_length."d", $x);
491 10         18 my $type = shift @hosttypes;
492 10         19 my $num_router = scalar @router + 1;
493 10         18 my $cur_router = $x % $num_router;
494             my $host = {
495             'host_name' => $self->{'prefix'}."host_".$nr,
496 10         57 'alias' => $self->{'prefix'}.$type."_".$nr,
497             'use' => 'generic-mgt-test-host',
498             'address' => '127.0.'.$cur_router.'.'.($x + 1),
499             'hostgroups' => $hostgroup.','.$type,
500             'check_command' => 'test-check-host-alive!'.$type,
501             };
502 10 100       27 if(defined $router[$cur_router]) {
503 9         18 $host->{'parents'} = $router[$cur_router];
504 9         25 $host->{'check_command'} = 'test-check-host-alive-parent!'.$type.'!$HOSTSTATE:'.$router[$cur_router].'$';
505             }
506 10 100       22 $host->{'active_checks_enabled'} = '0' if $type eq 'pending';
507 10 50       19 delete $host->{'hostgroups'} if $self->{'skip_hostgroups'};
508              
509             # add contacts
510 10         20 my @contacts = ();
511 10         35 for(my $z = 0; $z < $self->{'contacts_per_host'}; $z++) {
512 0         0 push @contacts, $contactnames->[ rand @{$contactnames} ];
  0         0  
513             }
514 10 50       29 $host->{'contacts'} = join(',', @contacts) if scalar @contacts > 0;
515              
516             # add contactgroups
517 10         16 my @contactgroups = ();
518 10         21 for(my $z = 0; $z < $self->{'contactgroups_per_host'}; $z++) {
519 10         19 push @contactgroups, $contactgroupnames->[ rand @{$contactgroupnames} ];
  10         32  
520             }
521 10 50       36 $host->{'contact_groups'} = join(',', @contactgroups) if scalar @contactgroups > 0;
522              
523 10         25 $host = $self->_merge_config_hashes($host, $self->{'host_settings'});
524 10         13 push @{$objects->{'host'}}, $host;
  10         35  
525             }
526              
527 1         5 return($objects);
528             }
529              
530             ########################################
531             sub _set_hostgroups_cfg {
532 1     1   2 my $self = shift;
533 1         2 my $objects = shift;
534              
535 1 50       6 $objects->{'hostgroup'} = [] unless defined $objects->{'hostgroup'};
536 1         2 push @{$objects->{'hostgroup'}},
  1         17  
537             { hostgroup_name => 'router', alias => 'All Router Hosts' },
538             { hostgroup_name => 'hostgroup_01', alias => 'hostgroup_alias_01' },
539             { hostgroup_name => 'hostgroup_02', alias => 'hostgroup_alias_02' },
540             { hostgroup_name => 'hostgroup_03', alias => 'hostgroup_alias_03' },
541             { hostgroup_name => 'hostgroup_04', alias => 'hostgroup_alias_04' },
542             { hostgroup_name => 'hostgroup_05', alias => 'hostgroup_alias_05' },
543             { hostgroup_name => 'up', alias => 'All Up Hosts' },
544             { hostgroup_name => 'down', alias => 'All Down Hosts' },
545             { hostgroup_name => 'pending', alias => 'All Pending Hosts' },
546             { hostgroup_name => 'random', alias => 'All Random Hosts' },
547             { hostgroup_name => 'flap', alias => 'All Flapping Hosts' },
548             { hostgroup_name => 'block', alias => 'All Blocking Hosts' };
549              
550 1         3 return($objects);
551             }
552              
553             ########################################
554             sub _set_services_cfg {
555 1     1   3 my($self, $objects, $contactnames, $contactgroupnames) = @_;
556              
557 1 50       5 $objects->{'service'} = [] unless defined $objects->{'service'};
558              
559 1         25 my $serviceconfig = {
560             'name' => 'generic-mgt-test-service',
561             'active_checks_enabled' => 1,
562             'passive_checks_enabled' => 1,
563             'parallelize_check' => 1,
564             'obsess_over_service' => 1,
565             'check_freshness' => 0,
566             'notifications_enabled' => 1,
567             'event_handler_enabled' => 1,
568             'flap_detection_enabled' => 1,
569             'failure_prediction_enabled' => 1,
570             'process_perf_data' => 1,
571             'retain_status_information' => 1,
572             'retain_nonstatus_information' => 1,
573             'notification_interval' => 0,
574             'is_volatile' => 0,
575             'check_period' => '24x7',
576             'check_interval' => 1,
577             'retry_interval' => 1,
578             'max_check_attempts' => 3,
579             'notification_period' => '24x7',
580             'notification_options' => 'w,u,c,r',
581             'register' => 0,
582             };
583              
584 1         5 my $merged = $self->_merge_config_hashes($serviceconfig, $self->{'service_settings'});
585 1         3 push @{$objects->{'service'}}, $merged;
  1         3  
586              
587 1         2 my @servicetypes = @{$self->_fisher_yates_shuffle($self->_get_types($self->{'hostcount'} * $self->{'services_per_host'}, $self->{'service_types'}))};
  1         5  
588              
589 1   33     13 my $hostnr_length = $self->{'fixed_length'} || length($self->{'hostcount'});
590 1   33     6 my $servicenr_length = $self->{'fixed_length'} || length($self->{'services_per_host'});
591 1         5 for(my $x = 0; $x < $self->{'hostcount'}; $x++) {
592 10         34 my $host_nr = sprintf("%0".$hostnr_length."d", $x);
593 10         31 for(my $y = 0; $y < $self->{'services_per_host'}; $y++) {
594 100         307 my $service_nr = sprintf("%0".$servicenr_length."d", $y);
595 100         142 my $servicegroup = "servicegroup_01";
596 100 100       210 $servicegroup = "servicegroup_02" if $y%5 == 1;
597 100 100       194 $servicegroup = "servicegroup_03" if $y%5 == 2;
598 100 100       187 $servicegroup = "servicegroup_04" if $y%5 == 3;
599 100 100       190 $servicegroup = "servicegroup_05" if $y%5 == 4;
600 100         150 my $type = shift @servicetypes;
601              
602             my $service = {
603             'host_name' => $self->{'prefix'}."host_".$host_nr,
604 100         504 'service_description' => $self->{'prefix'}.$type."_".$service_nr,
605             'check_command' => 'check_service!'.$type,
606             'use' => 'generic-mgt-test-service',
607             'servicegroups' => $servicegroup.','.$type,
608             };
609              
610 100 100       225 $service->{'active_checks_enabled'} = '0' if $type eq 'pending';
611 100 50       196 delete $service->{'servicegroups'} if $self->{'skip_servicegroups'};
612              
613             # first router gets additional infos
614 100 100       182 if($y == 0) {
615 10         26 $service->{'notes_url'} = 'http://search.cpan.org/dist/Monitoring-Generator-TestConfig/README';
616 10         22 $service->{'notes'} = 'just a notes string';
617 10         21 $service->{'icon_image_alt'} = 'icon alt string';
618 10         19 $service->{'icon_image'} = 'tux.png';
619 10         17 $service->{'action_url'} = 'http://search.cpan.org/dist/Monitoring-Generator-TestConfig/';
620             }
621 100 100       177 if($y == 1) {
622 10         16 $service->{'notes_url'} = 'http://search.cpan.org/dist/Monitoring-Generator-TestConfig/README';
623 10         19 $service->{'action_url'} = 'http://search.cpan.org/dist/Monitoring-Generator-TestConfig/';
624             }
625 100 100       186 if($y == 2) {
626 10         16 $service->{'notes_url'} = 'http://google.com/?q=$HOSTNAME$';
627 10         18 $service->{'action_url'} = 'http://google.com/?q=$HOSTNAME$';
628             }
629              
630             # add contacts
631 100         157 my @contacts = ();
632 100         211 for(my $z = 0; $z < $self->{'contacts_per_service'}; $z++) {
633 0         0 push @contacts, $contactnames->[ rand @{$contactnames} ];
  0         0  
634             }
635 100 50       243 $service->{'contacts'} = join(',', @contacts) if scalar @contacts > 0;
636              
637             # add contactgroups
638 100         165 my @contactgroups = ();
639 100         211 for(my $z = 0; $z < $self->{'contactgroups_per_service'}; $z++) {
640 100         153 push @contactgroups, $contactgroupnames->[ rand @{$contactgroupnames} ];
  100         293  
641             }
642 100 50       305 $service->{'contact_groups'} = join(',', @contactgroups) if scalar @contactgroups > 0;
643              
644 100         226 $service = $self->_merge_config_hashes($service, $self->{'service_settings'});
645 100         151 push @{$objects->{'service'}}, $service;
  100         336  
646             }
647             }
648              
649 1         7 return($objects);
650             }
651              
652             ########################################
653             sub _set_servicegroups_cfg {
654 1     1   3 my $self = shift;
655 1         2 my $objects = shift;
656              
657 1 50       5 $objects->{'servicegroup'} = [] unless defined $objects->{'servicegroup'};
658 1         3 push @{$objects->{'servicegroup'}},
  1         17  
659             { servicegroup_name => 'servicegroup_01', alias => 'servicegroup_alias_01' },
660             { servicegroup_name => 'servicegroup_02', alias => 'servicegroup_alias_02' },
661             { servicegroup_name => 'servicegroup_03', alias => 'servicegroup_alias_03' },
662             { servicegroup_name => 'servicegroup_04', alias => 'servicegroup_alias_04' },
663             { servicegroup_name => 'servicegroup_05', alias => 'servicegroup_alias_05' },
664             { servicegroup_name => 'ok', alias => 'All Ok Services' },
665             { servicegroup_name => 'warning', alias => 'All Warning Services' },
666             { servicegroup_name => 'unknown', alias => 'All Unknown Services' },
667             { servicegroup_name => 'critical', alias => 'All Critical Services' },
668             { servicegroup_name => 'pending', alias => 'All Pending Services' },
669             { servicegroup_name => 'random', alias => 'All Random Services' },
670             { servicegroup_name => 'flap', alias => 'All Flapping Services' },
671             { servicegroup_name => 'block', alias => 'All Blocking Services' };
672              
673 1         3 return($objects);
674             }
675              
676             ########################################
677             sub _set_servicedependency_cfg {
678 1     1   2 my $self = shift;
679 1         2 my $objects = shift;
680              
681 1 50       5 $objects->{'servicedependency'} = [] unless defined $objects->{'servicedependency'};
682              
683 1         2 my $service_size = scalar @{$objects->{'service'}};
  1         4  
684 1         4 for(my $x = 2; $x < $service_size; $x+=5) {
685 20         40 my $dependent = $objects->{'service'}->[$x];
686 20         33 my $master = $objects->{'service'}->[$x-1];
687 20 50       43 next unless defined $dependent->{'host_name'};
688 20 50       42 next unless defined $master->{'host_name'};
689              
690 20         105 push @{$objects->{'servicedependency'}}, {
691             'dependent_host_name' => $dependent->{'host_name'},
692             'dependent_service_description' => $dependent->{'service_description'},
693             'host_name' => $master->{'host_name'},
694 20         26 'service_description' => $master->{'service_description'},
695             'execution_failure_criteria' => 'w,u,c',
696             'notification_failure_criteria' => 'w,u,c',
697             };
698             }
699              
700 1         4 return $objects;
701             }
702              
703             ########################################
704             sub _set_contacts_cfg {
705 1     1   4 my($self, $objects) = @_;
706              
707 1 50       6 $objects->{'contact'} = [] unless defined $objects->{'contact'};
708 1         2 my @contacts = ();
709 1   33     9 my $nr_length = $self->{'fixed_length'} || length($self->{'contacts_count'});
710 1         5 for(my $x = 0; $x < $self->{'contacts_count'}; $x++) {
711 1         7 my $nr = sprintf("%0".$nr_length."d", $x);
712 1         3 my $name = $self->{'prefix'}."contact_".$nr;
713 1         3 push @contacts, $name;
714 1         14 push @{$objects->{'contact'}}, {
715             'contact_name' => $name,
716 1         2 'alias' => $self->{'prefix'}."contactalias_".$nr,
717             'service_notification_period' => '24x7',
718             'host_notification_period' => '24x7',
719             'service_notification_options' => 'w,u,c,r',
720             'host_notification_options' => 'd,r',
721             'service_notification_commands' => 'notify-service',
722             'host_notification_commands' => 'notify-host',
723             'email' => 'nobody@localhost',
724             };
725             }
726              
727 1         2 my @contactgroups = ();
728 1 50       5 $objects->{'contactgroup'} = [] unless defined $objects->{'contactgroup'};
729 1   33     7 $nr_length = $self->{'fixed_length'} || length($self->{'contactgroups_count'});
730 1         5 for(my $x = 0; $x < $self->{'contactgroups_count'}; $x++) {
731 1         4 my $nr = sprintf("%0".$nr_length."d", $x);
732 1         3 my @members = ();
733 1         4 for(my $y = 0; $y < $self->{'contacts_per_group'}; $y++) {
734 1         6 push @members, $contacts[ rand @contacts ];
735             }
736 1         4 my $name = $self->{'prefix'}."contactgroup_".$nr;
737 1         3 push @contactgroups, $name;
738 1         9 push @{$objects->{'contactgroup'}}, {
739             'contactgroup_name' => $name,
740 1         2 'alias' => $self->{'prefix'}."groupalias_".$nr,
741             'members' => join(',', @members),
742             };
743             }
744              
745 1         4 return(\@contacts, \@contactgroups);
746             }
747              
748             ########################################
749             sub _set_commands_cfg {
750 1     1   13 my $self = shift;
751 1         3 my $objects = shift;
752              
753 1         2 my $usr_macro = "USER1";
754 1 50       5 if($self->{'layout'} eq 'omd') {
755 0         0 $usr_macro = "USER2";
756             }
757              
758 1 50       22 $objects->{'command'} = [] unless defined $objects->{'command'};
759 1         25 push @{$objects->{'command'}}, {
760             'command_name' => 'test-check-host-alive',
761             'command_line' => (defined $self->{'hostcheckcmd'} ? $self->{'hostcheckcmd'} : '$'.$usr_macro.'$/test_hostcheck.pl --type=$ARG1$ --failchance='.$self->{'hostfailrate'}.'% --previous-state=$HOSTSTATE$ --state-duration=$HOSTDURATIONSEC$ --hostname $HOSTNAME$'),
762             }, {
763             'command_name' => 'test-check-host-alive-parent',
764             'command_line' => (defined $self->{'hostcheckcmd'} ? $self->{'hostcheckcmd'} : '$'.$usr_macro.'$/test_hostcheck.pl --type=$ARG1$ --failchance='.$self->{'hostfailrate'}.'% --previous-state=$HOSTSTATE$ --state-duration=$HOSTDURATIONSEC$ --parent-state=$ARG2$ --hostname $HOSTNAME$'),
765             }, {
766             'command_name' => 'notify-host',
767             'command_line' => 'sleep 1 && /bin/true',
768             }, {
769             'command_name' => 'notify-service',
770             'command_line' => 'sleep 1 && /bin/true',
771             }, {
772             'command_name' => 'check_service',
773 1 50       3 'command_line' => (defined $self->{'servicecheckcmd'} ? $self->{'servicecheckcmd'} : '$'.$usr_macro.'$/test_servicecheck.pl --type=$ARG1$ --failchance='.$self->{'servicefailrate'}.'% --previous-state=$SERVICESTATE$ --state-duration=$SERVICEDURATIONSEC$ --total-critical-on-host=$TOTALHOSTSERVICESCRITICAL$ --total-warning-on-host=$TOTALHOSTSERVICESWARNING$ --hostname $HOSTNAME$ --servicedesc $SERVICEDESC$'),
    50          
    50          
774             };
775              
776 1         4 return($objects);
777             }
778              
779             ########################################
780             sub _set_timeperiods_cfg {
781 1     1   2 my $self = shift;
782 1         2 my $objects = shift;
783              
784 1 50       5 $objects->{'timeperiod'} = [] unless defined $objects->{'timeperiod'};
785 1         3 push @{$objects->{'timeperiod'}}, {
  1         17  
786             'timeperiod_name' => '24x7',
787             'alias' => '24 Hours A Day, 7 Days A Week',
788             'sunday' => '00:00-24:00',
789             'monday' => '00:00-24:00',
790             'tuesday' => '00:00-24:00',
791             'wednesday' => '00:00-24:00',
792             'thursday' => '00:00-24:00',
793             'friday' => '00:00-24:00',
794             'saturday' => '00:00-24:00',
795             };
796              
797 1         4 return($objects);
798             }
799              
800             ########################################
801             sub _get_main_cfg {
802 1     1   3 my $self = shift;
803              
804             my $main_cfg = {
805             'log_file' => $self->{'output_dir'}.'/var/'.$self->{'layout'}.'.log',
806             'cfg_dir' => $self->{'output_dir'}.'/etc/conf.d',
807             'object_cache_file' => $self->{'output_dir'}.'/var/objects.cache',
808             'precached_object_file' => $self->{'output_dir'}.'/var/objects.precache',
809             'resource_file' => $self->{'output_dir'}.'/etc/resource.cfg',
810             'status_file' => $self->{'output_dir'}.'/var/status.dat',
811             'status_update_interval' => 30,
812             $self->{'layout'}.'_user' => $self->{'user'},
813             $self->{'layout'}.'_group' => $self->{'group'},
814             'check_external_commands' => 1,
815             'command_check_interval' => -1,
816             'command_file' => $self->{'output_dir'}.'/var/'.$self->{'layout'}.'.cmd',
817             'external_command_buffer_slots' => 4096,
818             'lock_file' => $self->{'output_dir'}.'/var/'.$self->{'layout'}.'.pid',
819             'temp_file' => $self->{'output_dir'}.'/var/tmp/'.$self->{'layout'}.'.tmp',
820             'temp_path' => $self->{'output_dir'}.'/var/tmp',
821             'event_broker_options' =>-1,
822             'log_rotation_method' =>'d',
823             'log_archive_path' => $self->{'output_dir'}.'/archives',
824             'use_syslog' => 0,
825             'log_notifications' => 1,
826             'log_service_retries' => 1,
827             'log_host_retries' => 1,
828             'log_event_handlers' => 1,
829             'log_initial_states' => 0,
830             'log_external_commands' => 1,
831             'log_passive_checks' => 1,
832             'service_inter_check_delay_method' => 's',
833             'max_service_check_spread' => 30,
834             'service_interleave_factor' => 's',
835             'host_inter_check_delay_method' => 's',
836             'max_host_check_spread' => 30,
837             'max_concurrent_checks' => 0,
838             'check_result_reaper_frequency' => 10,
839             'max_check_result_reaper_time' => 30,
840             'check_result_path' => $self->{'output_dir'}.'/var/checkresults',
841             'max_check_result_file_age' => 3600,
842             'cached_host_check_horizon' => 15,
843             'cached_service_check_horizon' => 15,
844             'enable_predictive_host_dependency_checks' => 1,
845             'enable_predictive_service_dependency_checks' => 1,
846             'soft_state_dependencies' => 0,
847             'auto_reschedule_checks' => 0,
848             'auto_rescheduling_interval' => 30,
849             'auto_rescheduling_window' => 180,
850             'sleep_time' => 0.25,
851             'service_check_timeout' => 60,
852             'host_check_timeout' => 30,
853             'event_handler_timeout' => 30,
854             'notification_timeout' => 30,
855             'ocsp_timeout' => 5,
856             'perfdata_timeout' => 5,
857             'retain_state_information' => 1,
858             'state_retention_file' => $self->{'output_dir'}.'/var/retention.dat',
859             'retention_update_interval' => 60,
860             'use_retained_program_state' => 1,
861             'use_retained_scheduling_info' => 1,
862             'retained_host_attribute_mask' => 0,
863             'retained_service_attribute_mask' => 0,
864             'retained_process_host_attribute_mask' => 0,
865             'retained_process_service_attribute_mask' => 0,
866             'retained_contact_host_attribute_mask' => 0,
867             'retained_contact_service_attribute_mask' => 0,
868             'interval_length' => 60,
869             'use_aggressive_host_checking' => 0,
870             'execute_service_checks' => 1,
871             'accept_passive_service_checks' => 1,
872             'execute_host_checks' => 1,
873             'accept_passive_host_checks' => 1,
874             'enable_notifications' => 1,
875             'enable_event_handlers' => 1,
876             'process_performance_data' => 0,
877             'obsess_over_services' => 0,
878             'obsess_over_hosts' => 0,
879             'translate_passive_host_checks' => 0,
880             'passive_host_checks_are_soft' => 0,
881             'check_for_orphaned_services' => 1,
882             'check_for_orphaned_hosts' => 1,
883             'check_service_freshness' => 1,
884             'service_freshness_check_interval' => 60,
885             'check_host_freshness' => 0,
886             'host_freshness_check_interval' => 60,
887             'additional_freshness_latency' => 15,
888             'enable_flap_detection' => 1,
889             'low_service_flap_threshold' => 5.0,
890             'high_service_flap_threshold' => 20.0,
891             'low_host_flap_threshold' => 5.0,
892             'high_host_flap_threshold' => 20.0,
893             'date_format' => 'iso8601',
894             'p1_file' => $self->{'output_dir'}.'/plugins/p1.pl',
895             'enable_embedded_perl' => 1,
896             'use_embedded_perl_implicitly' => 1,
897             'illegal_object_name_chars' => '`~!\\$%^&*|\'"<>?,()=',
898             'illegal_macro_output_chars' => '`~\\$&|\'"<>',
899             'use_regexp_matching' => 0,
900             'use_true_regexp_matching' => 0,
901             'admin_email' => $self->{'user'}.'@localhost',
902             'admin_pager' => $self->{'user'}.'@localhost',
903             'daemon_dumps_core' => 0,
904             'use_large_installation_tweaks' => 0,
905             'enable_environment_macros' => 1,
906             'debug_level' => 0,
907             'debug_verbosity' => 1,
908 1         103 'debug_file' => $self->{'output_dir'}.'/var/'.$self->{'layout'}.'.debug',
909             'max_debug_file_size' => 1000000,
910             };
911              
912 1 50       7 $main_cfg->{'use_large_installation_tweaks'} = 1 if ($self->{'hostcount'} * $self->{'services_per_host'} > 2000);
913              
914 1         6 my $merged = $self->_merge_config_hashes($main_cfg, $self->{'main_cfg'});
915 1         5 my $confstring = $self->_config_hash_to_string($merged);
916 1         40 return($confstring);
917             }
918              
919             ########################################
920              
921             sub _merge_config_hashes {
922 121     121   2208 my $self = shift;
923 121         187 my $conf1 = shift;
924 121         169 my $conf2 = shift;
925 121         175 my $merged;
926              
927 121         172 for my $key (keys %{$conf1}) {
  121         422  
928 975         1872 $merged->{$key} = $conf1->{$key};
929             }
930 121         229 for my $key (keys %{$conf2}) {
  121         227  
931 4         10 $merged->{$key} = $conf2->{$key};
932             }
933              
934 121         350 return($merged);
935             }
936              
937              
938             ########################################
939             sub _config_hash_to_string {
940 2     2   721 my $self = shift;
941 2         6 my $conf = shift;
942 2         6 my $confstring;
943              
944 2         4 for my $key (sort keys %{$conf}) {
  2         52  
945 108         179 my $value = $conf->{$key};
946 108 100       172 if(ref($value) eq 'ARRAY') {
947 1         3 for my $newval (@{$value}) {
  1         3  
948 3         9 $confstring .= $key."=".$newval."\n";
949             }
950             } else {
951 107         270 $confstring .= $key."=".$value."\n";
952             }
953             }
954              
955 2         13 return($confstring);
956             }
957              
958              
959             ########################################
960             sub _create_object_conf {
961 9     9   22 my $self = shift;
962 9         18 my $type = shift;
963 9         12 my $objects = shift;
964              
965 9         17 my $cfg = "";
966 9 50       22 return $cfg unless defined $objects;
967              
968 9         13 for my $obj (@{$objects}) {
  9         21  
969 170         332 $cfg .= 'define '.$type."{\n";
970 170         237 for my $key (sort _sort_object_key keys %{$obj}) {
  170         760  
971 1062         1779 my $value = $obj->{$key};
972 1062         2710 $cfg .= ' '.sprintf("%-30s", $key)." ".$value."\n";
973             }
974 170         375 $cfg .= "}\n\n";
975             }
976              
977 9         169 return($cfg);
978             }
979              
980              
981             ########################################
982             sub _fisher_yates_shuffle {
983 3     3   7 my $self = shift;
984 3         5 my $array = shift;
985 3         5 my $i;
986 3         10 for ($i = @$array; --$i; ) {
987 116         194 my $j = int rand ($i+1);
988 116 100       204 next if $i == $j;
989 108         247 @$array[$i,$j] = @$array[$j,$i];
990             }
991 3         59 return($array);
992             }
993              
994              
995             ########################################
996             sub _get_types {
997 3     3   4 my $self = shift;
998 3         6 my $count = shift;
999 3         7 my $types = shift;
1000              
1001 3         4 my $total = 0;
1002 3         5 for my $type (keys %{$types}) {
  3         12  
1003 20         33 $total += $types->{$type};
1004             }
1005              
1006 3         5 my @types;
1007 3         5 for my $type (keys %{$types}) {
  3         9  
1008 20         30 my $perc = $types->{$type};
1009 20         61 for(1..ceil($count/$total*$perc)) {
1010 119         207 push @types, $type;
1011             }
1012             }
1013 3 50       9 if(scalar @types < $count) {
1014 0         0 warn("got only ".scalar @types." types, expected ".$count);
1015             }
1016 3         16 return(\@types);
1017             }
1018              
1019             ########################################
1020             sub _get_recreate_pl {
1021 1     1   3 my $self = shift;
1022 1   50     10 my $libs = shift || '';
1023 1         3 my $pl;
1024              
1025 1         4 $Data::Dumper::Sortkeys = 1;
1026 1         9 my $conf = Dumper($self);
1027 1         408 $conf =~ s/^\$VAR1\ =\ bless\(\ \{//mx;
1028 1         21 $conf =~ s/},\ 'Monitoring::Generator::TestConfig'\ \);$//mx;
1029              
1030 1         75 $pl = <
1031             #!$^X
1032             $libs
1033             use Monitoring::Generator::TestConfig;
1034             my \$ngt = Monitoring::Generator::TestConfig->new(
1035             $conf
1036             );
1037             \$ngt->create();
1038             EOT
1039              
1040 1         40 return $pl;
1041             }
1042              
1043              
1044             ########################################
1045             sub _sort_object_key {
1046 1923 100   1923   3489 return -7 if $a eq 'dependent_service_description';
1047 1896 100       3204 return -6 if $a eq 'dependent_host_name';
1048 1862 100       3146 return -5 if $a eq 'name';
1049 1857 100       3570 return -4 if $a =~ m/_name$/mx;
1050 1588 100       2854 return -3 if $a =~ m/_description$/mx;
1051 1380 100       2516 return -2 if $a eq 'use';
1052              
1053 1189 100       2012 return 7 if $b eq 'dependent_service_description';
1054 1171 100       1969 return 6 if $b eq 'dependent_host_name';
1055 1155 100       1944 return 5 if $b eq 'name';
1056 1151 100       2282 return 4 if $b =~ m/_name$/mx;
1057 1005 100       1846 return 3 if $b =~ m/_description$/mx;
1058 904 100       1542 return 2 if $b eq 'use';
1059              
1060 789         1291 return $a cmp $b;
1061             }
1062              
1063             ########################################
1064             sub _get_used_libs {
1065 1     1   4 my($self, $file) = @_;
1066 1         2 my $libs = '';
1067 1 50       38 return $libs unless -r $file;
1068 0 0         open(my $fh, '<', $file) or die('cannot read file '.$file.': '.$!);
1069 0           while(my $line = <$fh>) {
1070 0 0         next unless $line =~ m/^\s*use\s+lib\s+/gmx;
1071 0           $libs .= $line;
1072             }
1073 0           close($fh);
1074 0           return $libs;
1075             }
1076              
1077             1;
1078              
1079             __END__