File Coverage

blib/lib/Monitoring/Generator/TestConfig.pm
Criterion Covered Total %
statement 409 460 88.9
branch 133 202 65.8
condition 23 50 46.0
subroutine 32 32 100.0
pod 2 2 100.0
total 599 746 80.2


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