script/ecs_status.pl untested

filter: Statementstmt 0.0% 0 / 252 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 474 CC 50 SCAR 57.4
CC
50
Cov
0%
html_node_tbl
756.0
bgcolor
72.0
all_red
42.0
CRAP 312.4
1 #!/usr/local/bin/perl -w
2 #
3 # $Id: ecs_status.pl,v 1.39 2010/09/03 20:25:32 jschneid Exp $
4 #
5 ##########################################################################
6 # SCRIPT NAME: ecs_status.pl
7 # DESCRIPTION: CGI script to display ECS status via web
8 # PARAMETERS: optional you can start it with user=...
9 # e.g. localhost/ecs/ecs_status.pl?user=emdistest.
10 # additional, see $THIS_NODE, $NODE_TBL, and
11 # $NODE_TBL_LOCK, below
12 # OUTPUT: ECS status, in HTML format
13 #
14 # DATE WRITTEN: 2004-07-01
15 # WRITTEN BY: Joel Schneider
16 #
17 # REVISION HISTORY: please refer to CVS
18 #
19 # Copyright (C) 2004-2012 National Marrow Donor Program.
20 # All rights reserved.
21 ##########################################################################
22  
23 use CGI::Pretty;
24 use CGI::Carp 'fatalsToBrowser';
25 use Fcntl qw(:DEFAULT :flock);
26 use File::Basename qw(basename);
27 use File::Copy;
28 use File::Spec::Functions qw(catdir catfile);
29 use EMDIS::ECS::LockedHash;
30 use Env qw(ECS_BIN_DIR ECS_DAEMON_CHECK ECS_DAEMON_USER ECS_IMAGE_DIR
31 ECS_STATUS_CELLBG ECS_STATUS_RED ECS_STATUS_YELLOW ECS_ALL_RED
32 ECS_PAGE_HEADER NODE_TBL NODE_TBL_LOCK THIS_NODE TMPDIR);
33  
34 use strict;
35 use vars qw($node_tbl $content
36 $cvs_header $current_time $timenow $page_header);
37  
38 my( $err, $daemons_status, $ecs_scan_mail_status,
39 $ecs_chk_com_status );
40 $err = '';
41  
42 # use values from environment variables to compute configuration options
43 my $opt_ecs_bin_dir = compute_opt($ECS_BIN_DIR, '/usr/bin');
44 my $opt_daemon_check = compute_opt($ECS_DAEMON_CHECK, 'YES');
45 my $opt_user = compute_opt($ECS_DAEMON_USER, '');
46 my $opt_image_dir = compute_opt($ECS_IMAGE_DIR, '/ecs/images');
47 my $opt_status_cellbg = compute_opt($ECS_STATUS_CELLBG, 'NO');
48 my $opt_status_red = compute_opt($ECS_STATUS_RED, 50);
49 my $opt_status_yellow = compute_opt($ECS_STATUS_YELLOW, 5);
50 my $opt_all_red = compute_opt($ECS_ALL_RED, 60*60*4);
51 my $opt_page_header = compute_opt($ECS_PAGE_HEADER, '');
52 my $opt_node_tbl = compute_opt($NODE_TBL, '');
53 my $opt_node_tbl_lock = compute_opt($NODE_TBL_LOCK, '');
54 my $opt_this_node = compute_opt($THIS_NODE, '');
55 my $opt_tmpdir = compute_opt($TMPDIR, '.');
56  
57 die "THIS_NODE not specified."
58 unless $opt_this_node;
59 die "NODE_TBL not specified."
60 unless $opt_node_tbl;
61 die "NODE_TBL_LOCK not specified."
62 unless $opt_node_tbl_lock;
63  
64 my $result=new CGI;
65  
66 # "user" CGI parameter can override $opt_user setting
67 if( defined($result->param("user")) ){
68 $opt_user = $result->param("user");
69 }
70  
71 if($opt_daemon_check =~ /YES/i) {
72 if ( ! $opt_user ) {
73 $daemons_status = qx{$opt_ecs_bin_dir/ecs_pid_chk 2>&1};
74 }
75 else {
76 $daemons_status = qx{$opt_ecs_bin_dir/ecs_pid_chk --user=$opt_user 2>&1};
77 }
78  
79 if ( $daemons_status =~ /ERROR:\s*ecs_chk_com/ ){
80 $ecs_chk_com_status = 'red';
81 }
82 else {
83 $ecs_chk_com_status = 'green';
84 }
85 if ( $daemons_status =~ /ERROR:\s*ecs_scan_mail/){
86 $ecs_scan_mail_status = 'red';
87 }
88 else {
89 $ecs_scan_mail_status = 'green';
90 }
91 if ( $? && $? != 255*256 ) {
92 $err .= "DAEMON STATUS ERROR $?: $!\n";
93 }
94 }
95  
96 $cvs_header = '$Header: /usr/local/cvs/rposdir/codebase/emdis/ecs/src/nmdp/web_status/ecs_status.pl,v 1.39 2010/09/03 20:25:32 jschneid Exp $ ';
97 $current_time = time();
98 $timenow = format_datetime($current_time);
99 $node_tbl = node_tbl_snapshot($opt_node_tbl, $opt_node_tbl_lock, $opt_user);
100  
101 if (ref $node_tbl)
102 {
103 $content = html_node_tbl($node_tbl, $daemons_status);
104 }
105 else
106 {
107 $content = error_message($node_tbl);
108 }
109  
110 $page_header = '';
111 if($opt_page_header eq 'REFRESH_INFO')
112 {
113 $page_header = <<EOF;
114
115 <!--This section will force a refresh of web page.-->
116 <fieldset style="width: 630px; line-height: 22px; padding: 10px; font-family: arial; font-size: 14px;">
117
118 This page will automatically refresh every 3 minutes.<br>
119
120 To force a page refresh, <a href="javascript:void(0);" onClick="window.location.reload()" title="Click here to refresh the page.">click here</a>. <br>
121
122 <!--Option of displaying environment variables.-->
123 ECS Path: $opt_ecs_bin_dir
124
125 </fieldset>
126
127 EOF
128 }
129
130 # generate HTML output
131 print <<EOF;
132 Content-type: text/html; charset=utf-8
133  
134 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
135 "http://www.w3.org/TR/html4/loose.dtd">
136 <html>
137 <head>
138 <title>ECS Status $opt_user - $timenow</title>
139 <link rel="shortcut icon" href="$opt_image_dir/ecs_logo.ico" type="image/x-icon">
140 <meta http-equiv="refresh" content="180">
141 <style type="text/css">
142 <!--
143 /* document-level style sheet */
144 .bold { font-weight: bold; }
145 .data { font-family: monospace; }
146 .disabled { background-color: #eeeeee; }
147 .enabled { background-color: #ffffff; }
148 .flagimg { margin-bottom: 8px; border: 1px solid #dddddd; }
149 .header { font-size: x-large; font-weight: bold; }
150 .label { text-align: right; vertical-align: top; padding-right: 1ex; }
151 .leftimg { float:left; margin-right: 1em; border: 1px solid #dddddd; }
152 .tiny { font-size: x-small; }
153 .tblhdr1 { font-weight: bold; text-align: center;
154 background-color: #cccccc; }
155 .tblhdr2 { font-weight: bold; text-align: center; vertical-align: center;
156 background-color: #eeeeee; }
157 .top { vertical-align: top; }
158 .topdata { font-family: monospace; vertical-align: top; }
159 .urgent { font-weight: bold; text-decoration: underline; }
160 -->
161 </style>
162 </head>
163 <body>
164 $page_header
165 <p class="header">
166 <img class="leftimg" src="$opt_image_dir/60px/flag_$opt_this_node.png"
167 alt="$opt_this_node" title="$opt_this_node flag">
168 ECS Status $opt_user
169 </p>
170 $err
171 $content
172 </body>
173 </html>
174 EOF
175  
176 exit 0;
177  
178  
179 # ----------------------------------------------------------------------
180 # return snapshot of NODE_TBL
181 sub node_tbl_snapshot
182 {
183 my $NODE_TBL = shift;
184 my $NODE_TBL_LOCK = shift;
185 my $prefix = shift;
186 my $fh;
187 my $result = '';
188  
189 # work using copies of the node_tbl files, thereby avoiding the
190 # need for public RW access to the actual production files
191  
192 # first, obtain lock on production lock file
193 sysopen($fh, $NODE_TBL_LOCK, O_RDWR)
194 or return "Unable to access lock file $NODE_TBL_LOCK: $!";
195 lock($fh)
196 or return "Unable to obtain lock on file $NODE_TBL_LOCK";
197  
198 # copy node_tbl files
199 $prefix .= '_' if $prefix;
200 copy("$NODE_TBL.dir", "$opt_tmpdir/${prefix}node_tbl.dat.dir")
201 or return "Unable to perform file copy(): $!";
202 copy("$NODE_TBL.pag", "$opt_tmpdir/${prefix}node_tbl.dat.pag")
203 or return "Unable to perform file copy(): $!";
204  
205 my $ECS_NODE_TBL = new EMDIS::ECS::LockedHash("$opt_tmpdir/${prefix}node_tbl.dat",
206 "$opt_tmpdir/${prefix}node_tbl.lock");
207 if(!ref $ECS_NODE_TBL)
208 {
209 return "Unable to initialize LockedHash(" .
210 "$opt_tmpdir/${prefix}node_tbl.dat, $opt_tmpdir/${prefix}node_tbl.lock)";
211 }
212 elsif(!$ECS_NODE_TBL->lock())
213 {
214 return "Unable to lock NODE_TBL: " . $ECS_NODE_TBL->ERROR;
215 }
216 else
217 {
218 $result = {};
219 my @keys = $ECS_NODE_TBL->keys();
220 for my $node_id (@keys)
221 {
222 my $this_node = $ECS_NODE_TBL->read($node_id);
223 if(defined $this_node)
224 {
225 $result->{$node_id} = {(%$this_node)};
226 }
227 else
228 {
229 warn "Unable to read node '$node_id' from node_tbl!";
230 }
231 }
232 }
233  
234 # release lock on production lock file
235 flock($fh, LOCK_UN);
236  
237 return $result;
238 }
239  
240 # ----------------------------------------------------------------------
241 # format $node_tbl as HTML
242 sub html_node_tbl
243 {
244 my $node_tbl = shift;
245 my @header = ();
246 my @result = ();
247  
248 my @status_color = ('green', 'yellow', 'orange', 'red', 'grey');
249 my $max_status_color_idx = 0;
250 my $status_image;
251  
252 push @result, <<EOF;
253 <table border="1" cellpadding="5" cellspacing="0">
254 <colgroup>
255 <col width="70">
256 <col width="120">
257 <col width="120">
258 <col width="120">
259 <col width="140">
260 </colgroup>
261 <tr class="tblhdr1">
262 <th>Node</th>
263 <th>Outbound</th>
264 <th>Inbound</th>
265 <th>Queue</th>
266 <th>Info</th>
267 </tr>
268 EOF
269  
270 my @keys = sort keys %$node_tbl;
271 my @all_red = all_red ( \@keys );
272
273 for my $node_id (@keys)
274 {
275 next if $opt_this_node eq $node_id;
276  
277 my $node = $node_tbl->{$node_id};
278 my $in_seq_ack = bold_if_ne($node->{in_seq_ack}, $node->{in_seq});
279  
280 my $bgcolor_in = '';
281 my $bgcolor_out = '';
282 my $bgcolor_queue = '';
283 if($opt_status_cellbg !~ /NO/i)
284 {
285 $bgcolor_in = bgcolor( $node->{in_seq_ack}, $node->{in_seq}, 0);
286 $bgcolor_out = bgcolor( $node->{ack_seq}, $node->{out_seq},
287 $node->{last_out});
288 $bgcolor_queue = bgcolor( $node->{q_size}, 0 , 0);
289  
290 $bgcolor_in = 'bgcolor="red"' if ( $all_red[0] == 1 );
291 $bgcolor_out = 'bgcolor="red"' if ( $all_red[1] == 1 );
292 }
293  
294 my $ack_seq = bold_if_ne($node->{ack_seq}, $node->{out_seq});
295 my $last_in = format_timediff($current_time, $node->{last_in});
296 my $last_out = format_timediff($current_time, $node->{last_out});
297 my $q_size = $node->{q_size};
298  
299 $q_size = "<span class=\"bold\">$q_size</span>"
300 if $q_size > 499;
301 my $info = "";
302 my $node_disabled = '';
303 my $status_color_idx = 0;
304 my $trclass = 'enabled';
305 if($node->{node_disabled} =~ /^\s*(yes|true)\s*$/i)
306 {
307 $node_disabled = '<span class="tiny"><br/>node disabled</span>';
308 $trclass = 'disabled';
309 $info .= "Node disabled.<br/>";
310 $status_color_idx = 4 # grey
311 if $status_color_idx < 4;
312 }
313 if ( $current_time - $node->{last_in} > 24*60*60 ) {
314 $info .= "Node silent!<br/>";
315 $status_color_idx = 3 # red
316 if $status_color_idx < 3;
317 }
318 my $diff = 0;
319 $diff = $node->{q_min_seq} - $node->{in_seq} if $node->{q_min_seq};
320 if ( $diff > 1 )
321 {
322 $diff -= 1;
323 $info .= "$diff slow msg.<br/>";
324 $status_color_idx = 2 # orange
325 if $status_color_idx < 2;
326 }
327 $diff = $node->{in_seq} - $node->{in_seq_ack};
328 if ( $diff > 0 ) {
329 $info .= "Diff. In:&nbsp; $diff<br/>";
330 $status_color_idx = 2 # orange
331 if $status_color_idx < 2 and $diff >= $opt_status_red;
332 $status_color_idx = 1 # yellow
333 if $status_color_idx < 1 and $diff >= $opt_status_yellow;
334 }
335 $diff = $node->{out_seq} - $node->{ack_seq};
336 if ( $diff > 0 ) {
337 $info .= "Diff. Out: $diff<br/>";
338 $status_color_idx = 2 # orange
339 if $status_color_idx < 2 and $diff >= $opt_status_red;
340 $status_color_idx = 1 # yellow
341 if $status_color_idx < 1 and $diff >= $opt_status_yellow;
342 }
343 $status_image = $status_color[$status_color_idx];
344 $max_status_color_idx = $status_color_idx
345 if $max_status_color_idx < $status_color_idx
346 and $status_color_idx < 4;
347 $info = "Ok."
348 unless $info;
349  
350 push @result, <<EOF;
351 <tr class="$trclass">
352 <td class="tblhdr2">
353 <table align="right" border="0" cellspacing="0" cellpadding="0">
354 <tr>
355 <td align="center">
356 $node_id<br/>
357 <img class="flagimg" src="$opt_image_dir/22px/flag_$node_id.png"
358 title="$node_id flag" alt="$node_id">
359 </td>
360 <td width="11">&nbsp;</td>
361 <td>
362 <img src="$opt_image_dir/led_vert_$status_image.png"
363 alt="$status_image" title="$status_image status"
364 height="24" width="12">
365 </td>
366 <td width="5">&nbsp;</td>
367 </tr>
368 </table>
369 </td>
370 <td class="top" $bgcolor_out>
371 <table border="0" cellpadding="0" cellspacing="0">
372 <tr><td class="label">Seqnum:</td><td class="data">$node->{out_seq}</td></tr>
373 <tr><td class="label">Acked:</td><td class="data">$ack_seq</td></tr>
374 <tr><td class="label" nowrap>Lastcom:</td><td class="data">$last_out</td></tr>
375 </table>
376 </td>
377 <td class="top" $bgcolor_in>
378 <table border="0" cellpadding="0" cellspacing="0">
379 <tr><td class="label">Seqnum:</td><td class="data">$node->{in_seq}</td></tr>
380 <tr><td class="label">Acked:</td><td class="data">$in_seq_ack</td></tr>
381 <tr><td class="label" nowrap>Lastcom:</td><td class="data">$last_in</td></tr>
382 </table>
383 </td>
384 <td class="top" $bgcolor_queue>
385 <table border="0" cellpadding="0" cellspacing="0">
386 <tr><td class="label">Size:</td><td class="data">$q_size</td></tr>
387 EOF
388  
389 if($node->{q_size})
390 {
391 push @result, <<EOF;
392 <tr><td class="label" nowrap>Max:</td><td class="data">$node->{q_max_seq}</td></tr>
393 <tr><td class="label">Min:</td><td class="data">$node->{q_min_seq}</td></tr>
394 EOF
395 }
396  
397 push @result, <<EOF;
398 </table>
399 </td>
400 <td class="topdata">$info</td>
401 </tr>
402 EOF
403 }
404  
405 push @result, <<EOF;
406 </table>
407 EOF
408  
409 my ($proc_msg, $proc_file);
410 my $node = $node_tbl->{$opt_this_node};
411 if($node->{proc_node})
412 {
413 $proc_msg = "$node->{proc_node}:$node->{proc_seq}";
414 $proc_file = basename($node->{proc_file});
415 }
416 else
417 {
418 $proc_msg = 'Nothing';
419 $proc_file = '';
420 }
421 if(defined $daemons_status)
422 {
423 if($max_status_color_idx < 3)
424 {
425 if($ecs_chk_com_status eq 'red'
426 or $ecs_scan_mail_status eq 'red')
427 {
428 $max_status_color_idx = 3; # red
429 }
430 }
431 }
432 $status_image = $status_color[$max_status_color_idx];
433
434 push @header, <<EOF;
435 <table border="0" cellpadding="6" cellspacing="0">
436 <colgroup>
437 <col width="60">
438 <col width="320">
439 <col width="60">
440 <col width="60">
441 </colgroup>
442 <tr>
443 <td align="center">
444 <img src="$opt_image_dir/led_square_$status_image.png"
445 alt="$status_image" title="$status_image status"
446 height="24" width="24">
447 </td>
448 <td>
449 <span class="data">$timenow</span>
450 <br/><span class="bold">$proc_msg</span> is currently being processed.
451 <br/><span class="data">$proc_file</span>
452 </td>
453 EOF
454 if (defined $daemons_status) {
455 push @header, <<EOF;
456 <td align="center" valign="middle">
457 &nbsp;ecs_chk_com&nbsp;<br/>
458 &nbsp;<img src="$opt_image_dir/led_horiz_$ecs_chk_com_status.png"
459 alt="$ecs_chk_com_status" title="$ecs_chk_com_status status"
460 height="12" width="24">&nbsp;
461 </td>
462 <td align="center" valign="middle">
463 &nbsp;ecs_scan_mail&nbsp;<br/>
464 &nbsp;<img src="$opt_image_dir/led_horiz_$ecs_scan_mail_status.png"
465 alt="$ecs_scan_mail_status" title="$ecs_scan_mail_status status"
466 height="12" width="24">&nbsp;
467 </td>
468 EOF
469 }
470 push @header, <<EOF;
471 </tr>
472 </table>
473 <p class="tiny"></p>
474  
475 EOF
476  
477 return join('', @header, @result);
478 }
479  
480 # ----------------------------------------------------------------------
481 sub all_red
482 {
483 my $keys = shift;
484  
485 my $red_in = 0;
486 my $red_out = 0;
487 my @all_red = ( 0 , 0 );
488 # number of nodes in table without this_node
489 my $length = scalar(@$keys) - 1;
490  
491  
492 foreach my $node_id ( @$keys ){
493 next if $opt_this_node eq $node_id;
494 my $node = $node_tbl->{$node_id};
495  
496 $red_out++ if ( ( $current_time - $node->{last_out}) > $opt_all_red );
497 $red_in++ if ( ( $current_time - $node->{last_in}) > $opt_all_red );
498 }
499  
500 $all_red[0] = 1 if ( $red_in == $length );
501 $all_red[1] = 1 if ( $red_out == $length );
502  
503 return @all_red;
504 }
505 # ----------------------------------------------------------------------
506 sub bold_if_ne
507 {
508 my $value = shift;
509 my $testval = shift;
510 return $value if $value eq $testval;
511 return "<span class=\"urgent\">$value</span>"
512 if abs($value - $testval) > 499;
513 return "<span class=\"bold\">$value</span>";
514 }
515  
516 # ----------------------------------------------------------------------
517 sub bgcolor
518 {
519 my $value = shift;
520 my $testval = shift;
521 my $time_last_node = shift;
522  
523 my $y_level = $opt_status_yellow;
524 if ( ! defined $y_level || $y_level eq '' ) {
525 $y_level = 5;
526 }
527 my $r_level = $opt_status_red;
528 if ( ! defined $r_level || $r_level eq '' ) {
529 $r_level = 10;
530 }
531  
532 if( ( $time_last_node == 0 )
533 || ( $current_time - $time_last_node > $opt_all_red ) )
534 {
535 return 'bgcolor="palegreen"'
536 if abs( $value - $testval) < $y_level;
537 return 'bgcolor="yellow"'
538 if abs( $value - $testval) >= $y_level
539 && abs( $value - $testval) < $r_level;
540 return 'bgcolor="red"'
541 if abs( $value - $testval) >= $r_level;
542 }
543 elsif( $time_last_node != 0 )
544 {
545 return 'bgcolor="palegreen"';
546 }
547  
548 }
549  
550 # ----------------------------------------------------------------------
551 # Format a datetime value
552 sub format_datetime
553 {
554 my $datetime = shift;
555 my $format = '%04d-%02d-%02d %02d:%02d:%02d';
556 my ($seconds, $minutes, $hours, $mday, $month, $year, $wday, $yday,
557 $isdst) = localtime($datetime);
558 return sprintf($format, $year + 1900, $month + 1, $mday,
559 $hours, $minutes, $seconds);
560 }
561  
562 # ----------------------------------------------------------------------
563 # Format a time difference
564 sub format_timediff
565 {
566 my $time1 = shift;
567 my $time2 = shift;
568 my $timediff = $time1 - $time2;
569 my $seconds = $timediff % 60;
570 my $minutes = ($timediff/60) % 60;
571 my $hours = ($timediff/(60*60)) % 24;
572 my $days = int $timediff/(60*60*24);
573 my $result = sprintf "%dd %02d:%02d:%02d",
574 $days, $hours, $minutes, $seconds;
575 $result = "<span class=\"bold\">$result</span>"
576 if $days > 0;
577 return $result;
578 }
579  
580 # ----------------------------------------------------------------------
581 sub error_message
582 {
583 my $errmsg = shift;
584 return "<p>ERROR: $errmsg</p>";
585 }
586  
587 # ----------------------------------------------------------------------
588 sub compute_opt
589 {
590 my $input_value = shift;
591 my $default_value = shift;
592  
593 return $input_value
594 if defined $input_value and $input_value ne '';
595 return $default_value;
596 }
597  
598 # ----------------------------------------------------------------------
599 # lock file, with time limit
600 sub lock
601 {
602 my $fh = shift;
603 my $lock_timeout = 5;
604 my $lock_type = LOCK_EX;
605 my $result = 1;
606  
607 # set up "local" SIG_ALRM handler
608 # (Note: not using "local $SIG{PIPE}" because it ignores die())
609 my $oldsigalrm = $SIG{ALRM};
610 $SIG{ALRM} = sub {
611 die "timeout - $lock_timeout second time limit exceeded\n";
612 };
613  
614 # attempt to obtain lock, with time limit
615 eval {
616 alarm($lock_timeout); # set alarm
617 die "flock() failed: $!\n"
618 unless flock($fh, $lock_type);
619 alarm(0); # turn off alarm
620 };
621 if($@) {
622 alarm(0); # turn off alarm
623 $result = '';
624 }
625  
626 # restore previous SIG_ALRM handler
627 if(defined $oldsigalrm) { $SIG{ALRM} = $oldsigalrm; }
628 else { delete $SIG{ALRM}; }
629  
630 return $result;
631 }
632  
633 #= EOF =
634  
635 __END__