check_oracle_health: update to 1.9.2
[omd.git] / t / 20-package_nagvis.t
blob64f1e9b5b67d8cb7ba41c2d0024d675cbf2df981
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 $num_tests = 382;
16 if($ENV{NAGVIS_DEVEL}) {
17 $num_tests += 3;
20 plan(tests => $num_tests);
22 ##################################################
23 # create our test site
24 my $response;
25 my $userId;
26 my $omd_bin = TestUtils::get_omd_bin();
27 my $site = TestUtils::create_test_site() or TestUtils::bail_out_clean("no further testing without site");
28 my $auth = 'OMD Monitoring Site '.$site.':omdadmin:omd';
29 my $orig_auth = $auth;
31 # create test host/service
32 TestUtils::prepare_obj_config('t/data/omd/testconf1', '/omd/sites/'.$site.'/etc/nagios/conf.d', $site);
34 # Developer test: Install NagVis into local hierarchy
35 if($ENV{NAGVIS_DEVEL}) {
36 TestUtils::test_command({ cmd => "/bin/bash -c 'cd /d1/lm/nagvis ; SITE=testsite bash .f12'" });
39 my $version = site_nagvis_version($site);
41 ##################################################
42 # Check installation paths
43 site_thing_exists($site, 'etc/nagvis/maps');
44 site_thing_exists($site, 'etc/nagvis/geomap');
45 site_thing_exists($site, 'etc/nagvis/conf.d');
46 site_thing_exists($site, 'etc/nagvis/conf.d/omd.ini.php');
47 site_thing_exists($site, 'etc/nagvis/conf.d/urls.ini.php');
48 site_thing_exists($site, 'etc/nagvis/conf.d/demo.ini.php');
49 site_thing_exists($site, 'etc/nagvis/nagvis.ini.php');
50 site_thing_exists($site, 'share/nagvis/htdocs');
51 site_thing_exists($site, 'share/nagvis/htdocs');
52 site_thing_exists($site, 'local/share/nagvis/htdocs');
54 ##################################################
55 # Check NAGVIS_URLS switcher
57 # Ensure the site is stopped, but don't care about the exit code here!
58 TestUtils::test_command({ cmd => $omd_bin." stop $site", exit => -1 });
60 TestUtils::test_command({ cmd => $omd_bin." config $site set NAGVIS_URLS auto" });
61 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI welcome" });
62 # Now grep conf.d/urls.ini.php for lines matching
63 # a) hosturl="[htmlcgi]/status.cgi?host=[host_name]"
64 # b) htmlcgi="/nv/nagios/cgi-bin"
65 TestUtils::test_command({ cmd => "/bin/su - $site -c 'cat etc/nagvis/conf.d/urls.ini.php'",
66 like => [ '/hosturl="\[htmlcgi\]\/status.cgi\?host=\[host_name\]"/',
67 '/htmlcgi="\/'.$site.'\/nagios\/cgi-bin"/' ] }),
69 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI nagios" });
70 TestUtils::test_command({ cmd => "/bin/su - $site -c 'cat etc/nagvis/conf.d/urls.ini.php'",
71 like => [ '/hosturl="\[htmlcgi\]\/status.cgi\?host=\[host_name\]"/',
72 '/htmlcgi="\/'.$site.'\/nagios\/cgi-bin"/' ] }),
74 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI thruk" });
75 TestUtils::test_command({ cmd => "/bin/su - $site -c 'cat etc/nagvis/conf.d/urls.ini.php'",
76 like => [ '/hosturl="\[htmlcgi\]\/status.cgi\?host=\[host_name\]"/',
77 '/htmlcgi="\/'.$site.'\/thruk\/cgi-bin"/' ] }),
79 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI check_mk" });
80 TestUtils::test_command({ cmd => "/bin/su - $site -c 'cat etc/nagvis/conf.d/urls.ini.php'",
81 like => [ '/hosturl="\[htmlcgi\]\/view\.py\?view_name=host&site=&host=\[host_name\]"/',
82 '/htmlcgi="\/'.$site.'\/check_mk"/' ] }),
85 ##################################################
86 # Prepare the site for testing...
88 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI nagvis" });
89 TestUtils::test_command({ cmd => $omd_bin." start $site" });
91 ##################################################
92 # Some checks to ensure the basic functionality
94 my $tests = [
95 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -u /$site/nagvis -e 401'",
96 like => '/HTTP OK:/' },
97 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis -e 301'",
98 like => '/HTTP OK:/' },
99 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/ -e 301'",
100 like => '/HTTP OK:/' },
101 { 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'",
102 like => '/HTTP OK:/' },
103 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend -e 301'",
104 like => '/HTTP OK:/' },
105 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend/ -e 301'",
106 like => '/HTTP OK:/' },
107 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend/wui -e 301'",
108 like => '/HTTP OK:/' },
109 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/frontend/wui/ -e 301'",
110 like => '/HTTP OK:/' },
111 { 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'",
112 like => '/HTTP OK:/' },
113 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/index.php -e 301'",
114 like => '/HTTP OK:/' },
115 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 30 -H localhost -a omdadmin:omd -u /$site/nagvis/config.php -e 301'",
116 like => '/HTTP OK:/' },
119 for my $test (@{$tests}) {
120 TestUtils::test_command($test);
123 ##################################################
124 # User interface URL tests
126 my $urls = [
127 # OMD welcome page in NagVis mode
128 url({ url => "", like => '/<title>NagVis '.$version.'<\/title>/' }),
130 # default pages
131 url({ url => "/nagvis/frontend/nagvis-js/index.php",
132 like => '/<title>NagVis '.$version.'<\/title>/' }),
133 url({ url => "/nagvis/frontend/nagvis-js/index.php?mod=Info&lang=en_US",
134 like => '/NagVis Support Information<\/title>/' }),
135 url({ url => "/nagvis/frontend/nagvis-js/index.php?mod=Map&act=view&show=demo-germany",
136 like => '/, \'demo-germany\'/', 'skip_html_lint' => 1 }),
138 # Old redirects to maps
139 url({ url => "/nagvis/index.php?map=demo-germany",
140 like => '/, \'demo-germany\'/', 'skip_html_lint' => 1 }),
141 url({ url => "/nagvis/config.php?map=demo-germany",
142 like => '/, \'demo-germany\'/', 'skip_html_lint' => 1 }),
143 url({ url => "/nagvis/index.php?rotation=demo",
144 like => '/, \'demo-germany\'/', 'skip_html_lint' => 1 }),
146 # Ajax fetched dialogs
147 # FIXME: only valid when not using trusted auth:
148 #api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=ChangePassword&act=view',
149 # like => [ '/{"code":"/', '/changePasswordForm/' ]}),
150 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view&lang=en_US',
151 like => [ '/Create User/', '/"code":"/' ]}),
152 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=RoleMgmt&act=view&lang=en_US',
153 like => [ '/Create Role/', '/"code":"/' ]}),
155 # Language switch
156 url({ url => "/nagvis/frontend/nagvis-js/index.php?lang=de_DE",
157 like => '/Sprache w&auml;hlen/'}),
159 # Language switch back
160 url({ url => "/nagvis/frontend/nagvis-js/index.php?lang=en_US",
161 like => '/Choose Language/'}),
163 # Documentations
164 url({ url => "/nagvis/docs/de_DE/index.html",
165 like => '/NagVis (.*) Dokumentation/'}),
166 url({ url => "/nagvis/docs/en_US/index.html",
167 like => '/NagVis (.*) Documentation/'}),
170 # perform tests
171 for my $url ( @{$urls} ) {
172 TestUtils::test_url($url);
175 ##################################################
176 # Environment auth tests
178 # Create user "omduser" in omd site
179 # SLES11 does not have a "htpasswd" binary. Use the available htpasswd2 binary in that case.
180 #my $htpasswd = "htpasswd";
181 #if(system("which $htpasswd >/dev/null 2>&1") != 0) {
182 # $htpasswd = "htpasswd2";
184 #TestUtils::test_command({ cmd => "/bin/su - $site -c '$htpasswd -b /omd/sites/$site/etc/htpasswd omduser test 2>/dev/null'"});
185 TestUtils::test_command({ cmd => "/bin/sh -c \"echo 'omduser:bbTdyOM4g6r9Q' >> /omd/sites/".$site."/etc/htpasswd\""});
187 # Now try to auth with that user (environment auth)
188 $auth = 'OMD Monitoring Site '.$site.':omduser:test';
189 TestUtils::test_url(
190 url({ url => '/nagvis/frontend/nagvis-js/index.php',
191 like => '/Logged in: omduser/'})
193 $auth = $orig_auth;
195 # Fetch user management dialog
196 $response = TestUtils::test_url(
197 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view',
198 like => '/Create User/' })
201 # Get the id of the user
202 $userId = "";
203 if($response->{'content'} =~ m/<option value=\\\"([0-9]*)\\\">omduser<\\\/option>/g) {
204 $userId = $1;
206 ok($userId ne "", 'User-ID of omduser: '.$userId) or diag('Unable to gather the userid!');
208 # Check roles of the user
209 TestUtils::test_url(
210 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=getUserRoles&userId='.$userId,
211 like => '/^\[{"roleId":"3","name":"Guests"}\]$/'})
214 ##################################################
215 # User management tests
217 # 1. Create a user
218 # http://127.0.0.1/testsite/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=doAdd&_t=1322353697000
219 # password1 123
220 # password2 123
221 # username testuser
222 # submit Create User
223 TestUtils::test_url(
224 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=doAdd',
225 post => { password1 => '123', password2 => '123', username => 'testuser', 'submit' => 'Create User' },
226 like => '/^{"message":"The user has been created/' })
229 # 2. Fetch user management dialog
230 # http://127.0.0.1/testsite/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view&_t=1322354360000
231 $response = TestUtils::test_url(
232 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view',
233 like => '/Create User/' })
236 # 3. Get the id of the new user
237 $userId = "";
238 if($response->{'content'} =~ m/<option value=\\\"([0-9]*)\\\">testuser<\\\/option>/g) {
239 $userId = $1;
241 ok($userId ne "", 'User-ID of testuser: '.$userId) or diag('Unable to gather the userid!');
243 # 4. get all roles of the user
244 TestUtils::test_url(
245 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=getUserRoles&userId='.$userId,
246 like => '/^\[\]$/'})
249 # 5. add a role to the user
250 TestUtils::test_url(
251 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=doEdit',
252 post => { submit => "Modify User", userId => $userId, 'rolesSelected[]' => 1},
253 like => '/The roles for this user have been updated/'})
256 # 6. verify user roles
257 TestUtils::test_url(
258 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=getUserRoles&userId='.$userId,
259 like => '/^\[{"roleId":"1","name":"Administrators"}\]$/'})
264 # 8. Now try to delete this user again
265 TestUtils::test_url(
266 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=doDelete',
267 post => { userId => $userId, submit => 'Delete User' },
268 like => [ '/^{"message":"The user has been deleted/' ]})
271 TestUtils::test_url(
272 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=UserMgmt&act=view',
273 like => '/Create User/',
274 unlike => '/<option value=\\\"'.$userId.'\\\">testuser<\\\/option>/'})
277 ##################################################
278 # Logon dialog tests
280 # - Change the LogonModule to LogonDialog in nagvis
281 # - Disable the basic auth for /<site>/nagvis
282 site_write_file($site, 'etc/nagvis/conf.d/auth.ini.php', "[global]\nlogonmodule=\"LogonDialog\"");
283 site_write_file($site, 'etc/apache/conf.d/nagvis-auth.conf', "<Location \"/".$site."/nagvis\">\n"
284 ."Order allow,deny\n"
285 ."Allow from all\n"
286 ."Satisfy any\n"
287 ."</Location>\n");
288 TestUtils::test_command({ cmd => $omd_bin." restart $site apache" });
289 $auth = '';
291 TestUtils::test_url(
292 url({ url => '/nagvis/frontend/nagvis-js/index.php',
293 like => [ '/form name="loginform"/', '/name="_username"/', '/name="_password"/' ]})
296 # perform a random request which sould not be allowed to be requested by non logged in users
297 TestUtils::test_url(
298 url({ url => '/nagvis/server/core/ajax_handler.php?mod=General&act=getHoverTemplate&name[]=default',
299 like => [ '/{"message":"You are not authenticated"/' ]})
302 #diag('Test an invalid login');
303 TestUtils::test_url(
304 url({ url => '/nagvis/frontend/nagvis-js/index.php',
305 post => { _username => 'omdadmin', _password => 'XXX', submit => 'Login' },
306 like => [ '/form name="loginform"/', '/name="_username"/',
307 '/name="_password"/', '/Authentication failed/' ]})
310 #diag('Test logging in using the login dialog');
311 TestUtils::test_url(
312 url({ url => '/nagvis/frontend/nagvis-js/index.php',
313 post => { _username => 'omdadmin', _password => 'omd', submit => 'Login' },
314 like => [ '/<!-- Start header menu -->/', '/Logged in: omdadmin/' ]})
317 #diag('Test logging in using _GET vars');
318 TestUtils::test_url(
319 url({ url => '/nagvis/frontend/nagvis-js/index.php?_username=omdadmin&_password=omd',
320 like => [ '/<!-- Start header menu -->/', '/Logged in: omdadmin/' ]})
323 #diag('Test logging in at ajax API using _GET vars');
324 # Use random page to login by GET vars
325 TestUtils::test_url(
326 url({ url => '/nagvis/server/core/ajax_handler.php?mod=General&act=getHoverTemplate&name[]=default'
327 .'&_username=omdadmin&_password=omd',
328 like => [ '/"name":"default","css_file":/' ]})
331 # Disable dialog auth to use the environment auth for further testing
332 $auth = $orig_auth;
333 site_remove_file($site, 'etc/apache/conf.d/nagvis-auth.conf');
334 site_remove_file($site, 'etc/nagvis/conf.d/auth.ini.php');
335 TestUtils::test_command({ cmd => $omd_bin." restart $site apache" });
337 ##################################################
338 # AJAX API tests
340 # /nagvis/server/core/ajax_handler.php?mod=General&act=getHoverTemplate&name[]=default
341 # [{"name":"default","code":"<...>"}]
342 TestUtils::test_url(
343 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=General&act=getHoverTemplate&name[]=default',
344 like => [ '/"name":"default","css_file":/' ]})
347 # /nagvis/server/core/ajax_handler.php?mod=General&act=getContextTemplate&name[]=default
348 # [{"name":"default","code":"<...>"}]
349 TestUtils::test_url(
350 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=General&act=getContextTemplate&name[]=default',
351 like => [ '/"name":"default","css_file":/' ]})
354 # /nagvis/server/core/ajax_handler.php?mod=Map&act=getMapProperties&show=demo
355 # {"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"}
356 TestUtils::test_url(
357 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=Map&act=getMapProperties&show=demo-germany',
358 like => [ '/"map_name":"demo-germany",/' ]})
361 # /nagvis/server/core/ajax_handler.php?mod=Map&act=getMapObjects&show=demo
362 # FIXME: Add sepecial tests for object states here using the test backend
363 TestUtils::test_url(
364 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Map&act=getMapObjects&show=demo-germany',
365 like => [ '/"alias":"Demo: 0 Overview Germany"/' ]})
368 # /nagvis/server/core/ajax_handler.php?mod=Map&act=getObjectStates&show=demo&ty=state&i[]=2&t[]=host&n1[]=host-down-hard&n2[]=
369 # FIXME: Add sepecial tests for object states here using the test backend
370 # 1. Match the object state
371 # 2. Match the member list
372 TestUtils::test_url(
373 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Map&act=getObjectStates&show=demo-germany&ty=state&i[]=d99295',
374 like => [ '/{"state":/', '/"members":\[{"/' ]})
377 ###############################################################################
378 # OVERVIEW
379 ###############################################################################
380 # /nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewProperties
381 # {"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}
382 TestUtils::test_url(
383 api_url({ url => '/nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewProperties',
384 like => [ '/"showmaps":1,"showgeomap":0,"showmapthumbs":0,"showrotations":1/', ]})
387 # /nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewMaps
388 TestUtils::test_url(
389 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewMaps',
390 like => [ '/"alias":/', ]})
393 # /nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewRotations
394 TestUtils::test_url(
395 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Overview&act=getOverviewRotations',
396 like => [ '/"name":"demo-germany",/', ]})
399 # /nagvis/server/core/ajax_handler.php?mod=General&act=getObjectStates&ty=state&i[]=automap-0&t[]=automap&n1[]=__automap&n2[]=
400 # http://127.0.0.1/nagvis/server/core/ajax_handler.php?mod=Overview&act=getObjectStates&ty=state&i[]=automap-__automap&_t=1298764833000
401 TestUtils::test_url(
402 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Overview&act=getObjectStates&ty=state&i[]=automap-__automap',
403 like => [ '/"state":"/', ]})
406 TestUtils::test_url(
407 api_url_list({ url => '/nagvis/server/core/ajax_handler.php?mod=Overview&act=getObjectStates&ty=state&i[]=automap-notexisting',
408 like => [ '/"state":"ERROR/', '/Map Error: The path /' ]})
411 ###############################################################################
412 # Test user config
413 ###############################################################################
414 # Language switch
415 TestUtils::test_url(url({ url => "/nagvis/frontend/nagvis-js/index.php?lang=de_DE",
416 like => '/Sprache w&auml;hlen/'}));
418 # Check profile file
419 site_match_file($site, 'var/nagvis/profiles/omdadmin.profile', '/"language":"de_DE"/');
421 ##################################################
422 # cleanup test site
423 TestUtils::remove_test_site($site);
426 ##################################################
427 # HELPER FUNCTIONS
428 # maybe move some of them to the general code one day
430 sub url {
431 my $url = shift;
432 $url->{'url'} = "http://localhost/".$site.$url->{'url'};
433 $url->{'auth'} = $auth;
434 $url->{'unlike'} = [ '/internal server error/' ];
435 $url->{'skip_link_check'} = [ 'lang=' ];
436 return $url;
439 sub api_url {
440 my $url = url(shift);
441 my $obj_match = shift;
442 if(!defined $obj_match) {
443 $obj_match = '/^{.*}$/';
446 $url->{'no_html_lint'} = 1;
448 # Add default AJAX API tests
449 my $def_like = [ $obj_match ];
450 if(defined $url->{'like'}) {
451 if(ref $url->{'like'} ne 'ARRAY') {
452 $url->{'like'} = [ $url->{'like'} ];
454 push(@{$url->{'like'}}, @{$def_like});
455 } else {
456 $url->{'like'} = $def_like;
458 return $url;
461 sub api_url_list {
462 return api_url(shift, '/^\[.*\]$/')
465 sub get_maincfg_files {
466 my $site = shift;
467 my @files = ();
469 # Get all nagvis config files
470 opendir(my($dh), '/omd/sites/'.$site.'/etc/nagvis/conf.d') or die("Couldn't open dir conf.d dir: $!");
471 while(my $file = readdir($dh)) {
472 if($file =~ m/.*\.ini\.php/g) {
473 push(@files, 'conf.d/' . $file);
476 closedir($dh);
477 push(@files, 'nagvis.ini.php');
478 return @files;
481 sub site_nagvis_maincfg_mtime {
482 my $site = shift;
483 my $newest = 0;
484 my $age;
486 for my $file (get_maincfg_files($site)) {
487 $age = site_mtime($site, 'etc/nagvis/' . $file);
488 $newest = ($age > $newest ? $age : $newest);
490 return $newest;
493 sub site_remove_file {
494 unlink '/omd/sites/'.shift(@_).'/'.shift(@_);
497 sub site_write_file {
498 my $path = '/omd/sites/'.shift(@_).'/'.shift(@_);
499 open(FILE, '>'.$path) or fail("Could not open file ".$path.".");
500 print FILE shift(@_);
501 close(FILE);
504 =head2 site_touch
506 Touches a given site file to modify the last access and modification
507 time of a given file. The path is given relative to the sites base dir.
508 The functions returns the touch time as unix timestamp.
510 =cut
511 sub site_touch {
512 my $site = shift;
513 my $path = shift;
514 my $now = time;
515 utime $now, $now, '/omd/sites/'.$site.'/'.$path;
516 return $now;
519 =head2 site_mtime
521 Returns the mtime for a file in the given site.
522 The path is given as relative path to the sites base directory.
523 The time is returned as unix timestamp.
525 =cut
526 sub site_mtime {
527 my $site = shift;
528 my $path = shift;
529 return (stat '/omd/sites/'.$site.'/'.$path)[9];
532 =head2 site_match_file
534 Checks if the specified pattern can be found in the given file.
535 The path is given as relative path to the sites base directory.
536 This function returns 1 if the pattern could be found or 0 if
537 there was no match in the file.
539 =cut
540 sub site_match_file {
541 my $site = shift;
542 my $fpath = shift;
543 my $pattern = shift;
544 my $path = '/omd/sites/' . $site . '/' . $fpath;
546 #diag('Checking file contents of '.$path);
548 open FILE, '<'.$path or fail("Could not open file.");
549 my $content = do { local $/; <FILE> };
550 close(FILE);
552 like($content, $pattern, "content like ".$pattern) or diag('Contents: '.$content);
555 =head2 site_thing_exists
557 Checks if the specified directory/link/file exists. Fails if
558 the thing does not exist.
560 =cut
561 sub site_thing_exists {
562 my $site = shift;
563 my $fpath = shift;
564 my $path = '/omd/sites/' . $site . '/' . $fpath;
566 #diag('Checking file exists '.$path);
567 #TestUtils::test_command({ cmd => "[ -f '.$path.' ]" });
568 ok(-e $path, 'Checking file exists '.$path) or diag('File does not exist!');
571 =head2 site_nagvis_version
573 Returns version string for the sites NagVis version. It takes the
574 local/ path installations into account.
576 =cut
577 sub site_nagvis_version {
578 my $site = shift;
579 my $version = '';
580 my $path;
581 if(-e '/omd/sites/' . $site . '/local/share/nagvis/htdocs/server/core/defines/global.php') {
582 $path = '/omd/sites/' . $site . '/local/share/nagvis/htdocs/server/core/defines/global.php';
583 } else {
584 $path = '/omd/sites/' . $site . '/share/nagvis/htdocs/server/core/defines/global.php';
586 open FILE, $path or die("Could not open file.");
587 foreach my $line (<FILE>) {
588 if($line =~ m/^define\('CONST_VERSION', '([^']*)'/) {
589 $version = $1;
592 close(FILE);
593 return $version;