perl-modules: increase timeouts for arm-build
[omd.git] / t / 20-package_nagvis.t
blob302896990cdb166867195e769ce78717b6ce02ab
1 #!/usr/bin/env perl
3 use warnings;
4 use strict;
5 use Test::More;
7 BEGIN {
8 use lib('t');
9 require TestUtils;
10 import TestUtils;
11 use FindBin;
12 use lib "$FindBin::Bin/lib/lib/perl5";
15 my $sitename = "testsite";
17 my $num_tests = 372;
18 if($ENV{NAGVIS_DEVEL}) {
19 $num_tests += 3;
22 my $has_thruk = 1;
23 if (! -e '/omd/sites/' . $sitename . '/etc/thruk') {
24 $num_tests -= 8;
25 $has_thruk = 0;
28 my $has_cmk = 1;
29 if (! -e '/omd/sites/' . $sitename . '/etc/check_mk') {
30 $num_tests -= 8;
31 $has_cmk = 0;
34 plan(tests => $num_tests);
36 ##################################################
37 # create our test site
38 my $response;
39 my $userId;
40 my $omd_bin = TestUtils::get_omd_bin();
41 my $site = TestUtils::create_test_site($sitename) or TestUtils::bail_out_clean("no further testing without site");
42 my $auth = 'OMD Monitoring Site '.$site.':omdadmin:omd';
43 my $orig_auth = $auth;
45 # create test host/service
46 TestUtils::prepare_obj_config('t/data/omd/testconf1', '/omd/sites/'.$site.'/etc/nagios/conf.d', $site);
48 # Developer test: Install NagVis into local hierarchy
49 if($ENV{NAGVIS_DEVEL}) {
50 TestUtils::test_command({ cmd => "/bin/bash -c 'cd /d1/lm/nagvis ; SITE=testsite bash .f12'" });
53 my $version = site_nagvis_version($site);
55 ##################################################
56 # Check installation paths
57 site_thing_exists($site, 'etc/nagvis/maps');
58 site_thing_exists($site, 'etc/nagvis/geomap');
59 site_thing_exists($site, 'etc/nagvis/conf.d');
60 site_thing_exists($site, 'etc/nagvis/conf.d/omd.ini.php');
61 site_thing_exists($site, 'etc/nagvis/conf.d/urls.ini.php');
62 site_thing_exists($site, 'etc/nagvis/conf.d/demo.ini.php');
63 site_thing_exists($site, 'etc/nagvis/nagvis.ini.php');
64 site_thing_exists($site, 'share/nagvis/htdocs');
65 site_thing_exists($site, 'share/nagvis/htdocs');
66 site_thing_exists($site, 'local/share/nagvis/htdocs');
68 ##################################################
69 # Check NAGVIS_URLS switcher
71 # Ensure the site is stopped, but don't care about the exit code here!
72 TestUtils::test_command({ cmd => $omd_bin." stop $site", exit => -1 });
74 #TestUtils::test_command({ cmd => $omd_bin." config $site set NAGVIS_URLS auto" });
75 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI welcome" });
76 # Now grep conf.d/urls.ini.php for lines matching
77 # a) hosturl="[htmlcgi]/status.cgi?host=[host_name]"
78 # b) htmlcgi="/nv/nagios/cgi-bin"
79 TestUtils::test_command({ cmd => "/bin/su - $site -c 'cat etc/nagvis/conf.d/urls.ini.php'",
80 like => [ '/hosturl="\[htmlcgi\]\/status.cgi\?host=\[host_name\]"/',
81 '/htmlcgi="\/'.$site.'\/nagios\/cgi-bin"/' ] });
83 if (-e '/omd/sites/' . $site . '/etc/nagios') {
84 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI nagios" });
85 TestUtils::test_command({ cmd => "/bin/su - $site -c 'cat etc/nagvis/conf.d/urls.ini.php'",
86 like => [ '/hosturl="\[htmlcgi\]\/status.cgi\?host=\[host_name\]"/',
87 '/htmlcgi="\/'.$site.'\/nagios\/cgi-bin"/' ] });
88 } else {
89 # dummy tests to statisfy number of tests (did not know how to decrease number of them)
90 TestUtils::test_command({ cmd => "/bin/echo skip nagios url test"});
91 TestUtils::test_command({ cmd => "/bin/echo skip nagios url test"});
94 if ($has_thruk) {
95 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI thruk" });
96 TestUtils::test_command({ cmd => "/bin/su - $site -c 'cat etc/nagvis/conf.d/urls.ini.php'",
97 like => [ '/hosturl="\[htmlcgi\]\/status.cgi\?host=\[host_name\]"/',
98 '/htmlcgi="\/'.$site.'\/thruk\/cgi-bin"/' ] });
101 if ($has_cmk) {
102 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI check_mk" });
103 TestUtils::test_command({ cmd => "/bin/su - $site -c 'cat etc/nagvis/conf.d/urls.ini.php'",
104 like => [ '/hosturl="\[htmlcgi\]\/view\.py\?view_name=host&site=&host=\[host_name\]"/',
105 '/htmlcgi="\/'.$site.'\/check_mk"/' ] });
108 ##################################################
109 # Prepare the site for testing...
111 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI nagvis" });
112 TestUtils::test_command({ cmd => $omd_bin." start $site" });
114 ##################################################
115 # Some checks to ensure the basic functionality
117 my $tests = [
118 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -u /$site/nagvis -e 401'",
119 like => '/HTTP OK:/' },
120 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis -e 301'",
121 like => '/HTTP OK:/' },
122 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/ -e 301'",
123 like => '/HTTP OK:/' },
124 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend/nagvis-js/index.php -e 200'",
125 like => '/HTTP OK:/' },
126 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend -e 301'",
127 like => '/HTTP OK:/' },
128 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend/ -e 301'",
129 like => '/HTTP OK:/' },
130 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend/wui -e 301'",
131 like => '/HTTP OK:/' },
132 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend/wui/ -e 301'",
133 like => '/HTTP OK:/' },
134 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend/wui/index.php -e 301'",
135 like => '/HTTP OK:/' },
136 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/index.php -e 301'",
137 like => '/HTTP OK:/' },
138 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/config.php -e 301'",
139 like => '/HTTP OK:/' },
142 for my $test (@{$tests}) {
143 TestUtils::test_command($test);
146 ##################################################
147 # User interface URL tests
149 my $urls = [
150 # OMD welcome page in NagVis mode
151 url({ url => "", like => '/<title>NagVis '.$version.'<\/title>/' }),
153 # default pages
154 url({ url => "/nagvis/frontend/nagvis-js/index.php",
155 like => '/<title>NagVis '.$version.'<\/title>/' }),
156 url({ url => "/nagvis/frontend/nagvis-js/index.php?mod=Info&lang=en_US",
157 like => '/NagVis Support Information<\/title>/' }),
158 url({ url => "/nagvis/frontend/nagvis-js/index.php?mod=Map&act=view&show=demo-germany",
159 like => '/, \'demo-germany\'/', 'skip_html_lint' => 1 }),
161 # Old redirects to maps
162 url({ url => "/nagvis/index.php?map=demo-germany",
163 like => '/, \'demo-germany\'/', 'skip_html_lint' => 1 }),
164 url({ url => "/nagvis/config.php?map=demo-germany",
165 like => '/, \'demo-germany\'/', 'skip_html_lint' => 1 }),
166 url({ url => "/nagvis/index.php?rotation=demo",
167 like => '/, \'demo-germany\'/', 'skip_html_lint' => 1 }),
169 # Ajax fetched dialogs
170 # FIXME: only valid when not using trusted auth:
171 #api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=ChangePassword&act=view',
172 # like => [ '/{"code":"/', '/changePasswordForm/' ]}),
173 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view&lang=en_US',
174 like => [ '/Create User/', '/"code":"/' ]}),
175 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=RoleMgmt&act=view&lang=en_US',
176 like => [ '/Create Role/', '/"code":"/' ]}),
178 # Language switch
179 url({ url => "/nagvis/frontend/nagvis-js/index.php?lang=de_DE",
180 like => '/Sprache w&auml;hlen/'}),
182 # Language switch back
183 url({ url => "/nagvis/frontend/nagvis-js/index.php?lang=en_US",
184 like => '/Choose Language/'}),
186 # Documentations
187 url({ url => "/nagvis/docs/de_DE/index.html",
188 like => '/NagVis (.*) Dokumentation/'}),
189 url({ url => "/nagvis/docs/en_US/index.html",
190 like => '/NagVis (.*) Documentation/'}),
193 # perform tests
194 for my $url ( @{$urls} ) {
195 TestUtils::test_url($url);
198 ##################################################
199 # Environment auth tests
201 # Create user "omduser" in omd site
202 # SLES11 does not have a "htpasswd" binary. Use the available htpasswd2 binary in that case.
203 #my $htpasswd = "htpasswd";
204 #if(system("which $htpasswd >/dev/null 2>&1") != 0) {
205 # $htpasswd = "htpasswd2";
207 #TestUtils::test_command({ cmd => "/bin/su - $site -c '$htpasswd -b /omd/sites/$site/etc/htpasswd omduser test 2>/dev/null'"});
208 TestUtils::test_command({ cmd => "/bin/sh -c \"echo 'omduser:bbTdyOM4g6r9Q' >> /omd/sites/".$site."/etc/htpasswd\""});
210 # Now try to auth with that user (environment auth)
211 $auth = 'OMD Monitoring Site '.$site.':omduser:test';
212 TestUtils::test_url(
213 url({ url => '/nagvis/frontend/nagvis-js/index.php',
214 like => '/Logged in: omduser/'})
216 $auth = $orig_auth;
218 # Fetch user management dialog
219 $response = TestUtils::test_url(
220 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view',
221 like => '/Create User/' })
224 # Get the id of the user
225 $userId = "";
226 if($response->{'content'} =~ m/<option value=\\\"([0-9]*)\\\">omduser<\\\/option>/g) {
227 $userId = $1;
229 ok($userId ne "", 'User-ID of omduser: '.$userId) or diag('Unable to gather the userid!');
231 # Check roles of the user
232 TestUtils::test_url(
233 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=getUserRoles&userId='.$userId,
234 like => '/^\[{"roleId":"3","name":"Guests"}\]$/'})
237 ##################################################
238 # User management tests
240 # 1. Create a user
241 # http://127.0.0.1/testsite/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=doAdd&_t=1322353697000
242 # password1 123
243 # password2 123
244 # username testuser
245 # submit Create User
246 TestUtils::test_url(
247 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=doAdd',
248 post => { password1 => '123', password2 => '123', username => 'testuser', 'submit' => 'Create User' },
249 like => '/^{"message":"The user has been created/' })
252 # 2. Fetch user management dialog
253 # http://127.0.0.1/testsite/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view&_t=1322354360000
254 $response = TestUtils::test_url(
255 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view',
256 like => '/Create User/' })
259 # 3. Get the id of the new user
260 $userId = "";
261 if($response->{'content'} =~ m/<option value=\\\"([0-9]*)\\\">testuser<\\\/option>/g) {
262 $userId = $1;
264 ok($userId ne "", 'User-ID of testuser: '.$userId) or diag('Unable to gather the userid!');
266 # 4. get all roles of the user
267 TestUtils::test_url(
268 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=getUserRoles&userId='.$userId,
269 like => '/^\[\]$/'})
272 # 5. add a role to the user
273 TestUtils::test_url(
274 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=doEdit',
275 post => { submit => "Modify User", userId => $userId, 'rolesSelected[]' => 1},
276 like => '/The roles for this user have been updated/'})
279 # 6. verify user roles
280 TestUtils::test_url(
281 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=getUserRoles&userId='.$userId,
282 like => '/^\[{"roleId":"1","name":"Administrators"}\]$/'})
287 # 8. Now try to delete this user again
288 TestUtils::test_url(
289 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=doDelete',
290 post => { userId => $userId, submit => 'Delete User' },
291 like => [ '/^{"message":"The user has been deleted/' ]})
294 TestUtils::test_url(
295 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view',
296 like => '/Create User/',
297 unlike => '/<option value=\\\"'.$userId.'\\\">testuser<\\\/option>/'})
300 ##################################################
301 # Logon dialog tests
303 # - Change the LogonModule to LogonDialog in nagvis
304 # - Disable the basic auth for /<site>/nagvis
305 site_write_file($site, 'etc/nagvis/conf.d/auth.ini.php', "[global]\nlogonmodule=\"LogonDialog\"");
306 site_write_file($site, 'etc/apache/conf.d/nagvis-auth.conf', "<Location \"/".$site."/nagvis\">\n"
307 ."Order allow,deny\n"
308 ."Allow from all\n"
309 ."Satisfy any\n"
310 ."</Location>\n");
311 TestUtils::test_command({ cmd => $omd_bin." restart $site apache" });
312 $auth = '';
314 TestUtils::test_url(
315 url({ url => '/nagvis/frontend/nagvis-js/index.php',
316 like => [ '/form name="loginform"/', '/name="_username"/', '/name="_password"/' ]})
319 # perform a random request which sould not be allowed to be requested by non logged in users
320 TestUtils::test_url(
321 url({ url => '/nagvis/server/core/ajax_handler.php?mod=General&act=getHoverTemplate&name[]=default',
322 like => [ '/{"message":"You are not authenticated"/' ]})
325 #diag('Test an invalid login');
326 TestUtils::test_url(
327 url({ url => '/nagvis/frontend/nagvis-js/index.php',
328 post => { _username => 'omdadmin', _password => 'XXX', submit => 'Login' },
329 like => [ '/form name="loginform"/', '/name="_username"/',
330 '/name="_password"/', '/Authentication failed/' ]})
333 #diag('Test logging in using the login dialog');
334 TestUtils::test_url(
335 url({ url => '/nagvis/frontend/nagvis-js/index.php',
336 post => { _username => 'omdadmin', _password => 'omd', submit => 'Login' },
337 like => [ '/<!-- Start header menu -->/', '/Logged in: omdadmin/' ]})
340 #diag('Test logging in using _GET vars');
341 TestUtils::test_url(
342 url({ url => '/nagvis/frontend/nagvis-js/index.php?_username=omdadmin&_password=omd',
343 like => [ '/<!-- Start header menu -->/', '/Logged in: omdadmin/' ]})
346 #diag('Test logging in at ajax API using _GET vars');
347 # Use random page to login by GET vars
348 TestUtils::test_url(
349 url({ url => '/nagvis/server/core/ajax_handler.php?mod=General&act=getHoverTemplate&name[]=default'
350 .'&_username=omdadmin&_password=omd',
351 like => [ '/"name":"default","css_file":/' ]})
354 # Disable dialog auth to use the environment auth for further testing
355 $auth = $orig_auth;
356 site_remove_file($site, 'etc/apache/conf.d/nagvis-auth.conf');
357 site_remove_file($site, 'etc/nagvis/conf.d/auth.ini.php');
358 TestUtils::test_command({ cmd => $omd_bin." restart $site apache" });
360 ##################################################
361 # AJAX API tests
363 # /nagvis/server/core/ajax_handler.php?mod=General&act=getHoverTemplate&name[]=default
364 # [{"name":"default","code":"<...>"}]
365 TestUtils::test_url(
366 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=General&act=getHoverTemplate&name[]=default',
367 like => [ '/"name":"default","css_file":/' ]})
370 # /nagvis/server/core/ajax_handler.php?mod=General&act=getContextTemplate&name[]=default
371 # [{"name":"default","code":"<...>"}]
372 TestUtils::test_url(
373 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=General&act=getContextTemplate&name[]=default',
374 like => [ '/"name":"default","css_file":/' ]})
377 # /nagvis/server/core/ajax_handler.php?mod=Map&act=getMapProperties&show=demo
378 # {"map_name":"demo","alias":"demo","background_image":"\/nagvis\/userfiles\/images\/maps\/nagvis-demo.png","background_color":"transparent","favicon_image":"\/nagvis\/frontend\/nagvis-js\/images\/internal\/favicon.png","page_title":"demo ([SUMMARY_STATE]) :: NagVis 1.5.7","event_background":"0","event_highlight":"1","event_highlight_interval":"500","event_highlight_duration":"10000","event_log":"0","event_log_level":"info","event_log_height":"100","event_log_hidden":1,"event_scroll":"1","event_sound":"1","in_maintenance":"0"}
379 TestUtils::test_url(
380 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=Map&act=getMapProperties&show=demo-germany',
381 like => [ '/"map_name":"demo-germany",/' ]})
384 # /nagvis/server/core/ajax_handler.php?mod=Map&act=getMapObjects&show=demo
385 # FIXME: Add sepecial tests for object states here using the test backend
386 TestUtils::test_url(
387 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Map&act=getMapObjects&show=demo-germany',
388 like => [ '/"alias":"Demo: 0 Overview Germany"/' ]})
391 # /nagvis/server/core/ajax_handler.php?mod=Map&act=getObjectStates&show=demo&ty=state&i[]=2&t[]=host&n1[]=host-down-hard&n2[]=
392 # FIXME: Add sepecial tests for object states here using the test backend
393 # 1. Match the object state
394 # 2. Match the member list
395 TestUtils::test_url(
396 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Map&act=getObjectStates&show=demo-germany&ty=state&i[]=d99295',
397 like => [ '/{"state":/', '/"members":\[{"/' ]})
400 ###############################################################################
401 # OVERVIEW
402 ###############################################################################
403 # /nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewProperties
404 # {"cellsperrow":4,"showautomaps":1,"showmaps":1,"showgeomap":0,"showmapthumbs":0,"showrotations":1,"page_title":"NagVis 1.5.7","favicon_image":"\/nagvis\/frontend\/nagvis-js\/images\/internal\/favicon.png","background_color":"#ffffff","lang_mapIndex":"Map Index","lang_automapIndex":"Automap Index","lang_rotationPools":"Rotation Pools","event_log":0,"event_log_level":"info","event_log_height":100,"event_log_hidden":1}
405 TestUtils::test_url(
406 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewProperties',
407 like => [ '/"showmaps":1,"showgeomap":0,"showmapthumbs":0,"showrotations":1/', ]})
410 # /nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewRotations
411 TestUtils::test_url(
412 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewRotations',
413 like => [ '/"name":"demo-germany",/', ]})
416 # /nagvis/server/core/ajax_handler.php?mod=General&act=getObjectStates&ty=state&i[]=automap-0&t[]=automap&n1[]=__automap&n2[]=
417 # http://127.0.0.1/nagvis/server/core/ajax_handler.php?mod=Overview&act=getObjectStates&ty=state&i[]=automap-__automap&_t=1298764833000
418 TestUtils::test_url(
419 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Overview&act=getObjectStates&ty=state&i[]=automap-__automap',
420 like => [ '/"state":"/', ]})
423 TestUtils::test_url(
424 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Overview&act=getObjectStates&ty=state&i[]=automap-notexisting',
425 like => [ '/"state":"ERROR/', '/Map Error: The path /' ]})
428 ###############################################################################
429 # Test user config
430 ###############################################################################
431 # Language switch
432 TestUtils::test_url(url({ url => "/nagvis/frontend/nagvis-js/index.php?lang=de_DE",
433 like => '/Sprache w&auml;hlen/'}));
435 # Check profile file
436 site_match_file($site, 'var/nagvis/profiles/omdadmin.profile', '/"language":"de_DE"/');
438 ##################################################
439 # cleanup test site
440 TestUtils::remove_test_site($site);
443 ##################################################
444 # HELPER FUNCTIONS
445 # maybe move some of them to the general code one day
447 sub url {
448 my $url = shift;
449 $url->{'url'} = "http://localhost/".$site.$url->{'url'};
450 $url->{'auth'} = $auth;
451 $url->{'unlike'} = [ '/internal server error/' ];
452 $url->{'skip_link_check'} = [ 'lang=' ];
453 return $url;
456 sub api_url {
457 my $url = url(shift);
458 my $obj_match = shift;
459 if(!defined $obj_match) {
460 $obj_match = '/^{.*}$/';
463 $url->{'no_html_lint'} = 1;
465 # Add default AJAX API tests
466 my $def_like = [ $obj_match ];
467 if(defined $url->{'like'}) {
468 if(ref $url->{'like'} ne 'ARRAY') {
469 $url->{'like'} = [ $url->{'like'} ];
471 push(@{$url->{'like'}}, @{$def_like});
472 } else {
473 $url->{'like'} = $def_like;
475 return $url;
478 sub api_url_list {
479 return api_url(shift, '/^\[.*\]$/')
482 sub get_maincfg_files {
483 my $site = shift;
484 my @files = ();
486 # Get all nagvis config files
487 opendir(my($dh), '/omd/sites/'.$site.'/etc/nagvis/conf.d') or die("Couldn't open dir conf.d dir: $!");
488 while(my $file = readdir($dh)) {
489 if($file =~ m/.*\.ini\.php/g) {
490 push(@files, 'conf.d/' . $file);
493 closedir($dh);
494 push(@files, 'nagvis.ini.php');
495 return @files;
498 sub site_nagvis_maincfg_mtime {
499 my $site = shift;
500 my $newest = 0;
501 my $age;
503 for my $file (get_maincfg_files($site)) {
504 $age = site_mtime($site, 'etc/nagvis/' . $file);
505 $newest = ($age > $newest ? $age : $newest);
507 return $newest;
510 sub site_remove_file {
511 unlink '/omd/sites/'.shift(@_).'/'.shift(@_);
514 sub site_write_file {
515 my $path = '/omd/sites/'.shift(@_).'/'.shift(@_);
516 open(FILE, '>'.$path) or fail("Could not open file ".$path.".");
517 print FILE shift(@_);
518 close(FILE);
521 =head2 site_touch
523 Touches a given site file to modify the last access and modification
524 time of a given file. The path is given relative to the sites base dir.
525 The functions returns the touch time as unix timestamp.
527 =cut
528 sub site_touch {
529 my $site = shift;
530 my $path = shift;
531 my $now = time;
532 utime $now, $now, '/omd/sites/'.$site.'/'.$path;
533 return $now;
536 =head2 site_mtime
538 Returns the mtime for a file in the given site.
539 The path is given as relative path to the sites base directory.
540 The time is returned as unix timestamp.
542 =cut
543 sub site_mtime {
544 my $site = shift;
545 my $path = shift;
546 return (stat '/omd/sites/'.$site.'/'.$path)[9];
549 =head2 site_match_file
551 Checks if the specified pattern can be found in the given file.
552 The path is given as relative path to the sites base directory.
553 This function returns 1 if the pattern could be found or 0 if
554 there was no match in the file.
556 =cut
557 sub site_match_file {
558 my $site = shift;
559 my $fpath = shift;
560 my $pattern = shift;
561 my $path = '/omd/sites/' . $site . '/' . $fpath;
563 #diag('Checking file contents of '.$path);
565 open FILE, '<'.$path or fail("Could not open file.");
566 my $content = do { local $/; <FILE> };
567 close(FILE);
569 like($content, $pattern, "content like ".$pattern) or diag('Contents: '.$content);
572 =head2 site_thing_exists
574 Checks if the specified directory/link/file exists. Fails if
575 the thing does not exist.
577 =cut
578 sub site_thing_exists {
579 my $site = shift;
580 my $fpath = shift;
581 my $path = '/omd/sites/' . $site . '/' . $fpath;
583 #diag('Checking file exists '.$path);
584 #TestUtils::test_command({ cmd => "[ -f '.$path.' ]" });
585 ok(-e $path, 'Checking file exists '.$path) or diag('File does not exist!');
588 =head2 site_nagvis_version
590 Returns version string for the sites NagVis version. It takes the
591 local/ path installations into account.
593 =cut
594 sub site_nagvis_version {
595 my $site = shift;
596 my $version = '';
597 my $path;
598 if(-e '/omd/sites/' . $site . '/local/share/nagvis/htdocs/server/core/defines/global.php') {
599 $path = '/omd/sites/' . $site . '/local/share/nagvis/htdocs/server/core/defines/global.php';
600 } else {
601 $path = '/omd/sites/' . $site . '/share/nagvis/htdocs/server/core/defines/global.php';
603 open FILE, $path or die("Could not open file.");
604 foreach my $line (<FILE>) {
605 if($line =~ m/^define\('CONST_VERSION', '([^']*)'/) {
606 $version = $1;
609 close(FILE);
610 return $version;