| 1 | package HPCI::SystemLogger; | ||
| 2 | |||
| 3 | use namespace::autoclean; | ||
| 4 | |||
| 5 | use Moose::Role; | ||
| 6 | use Moose::Util::TypeConstraints; | ||
| 7 | |||
| 8 | use autodie; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | use feature qw(say); | ||
| 12 | |||
| 13 | use MooseX::Types::Path::Class qw(Dir File); | ||
| 14 | use Fcntl qw(:flock); | ||
| 15 | use FindBin; | ||
| 16 | use DateTime; | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | HPCI::SystemLogger - This role provides an optional system logger | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | Defines a systemlogger attribute which logs the return status of | ||
| 25 | the group exwecution. | ||
| 26 | |||
| 27 | Not provided by default, but it can usefully included with a HPCI::LocalConfig | ||
| 28 | to set a local system logging facility. The saved logs can be examined for | ||
| 29 | such purposes as scanning for recurring errors coming from individual hardware | ||
| 30 | components of the cluster. | ||
| 31 | |||
| 32 | =head1 Attributes | ||
| 33 | |||
| 34 | =over 4 | ||
| 35 | |||
| 36 | =item * systemlogger - a code reference | ||
| 37 | |||
| 38 | This code reference (if provided) is called at the completion of | ||
| 39 | group execution. It is called with two parameters: the group object, | ||
| 40 | and the return_status hash. | ||
| 41 | |||
| 42 | The provider of this attribute (usually the HPCI::LocalConfig module) | ||
| 43 | is responsible to providing additional code to determine the location | ||
| 44 | and format of the logged data, although the B<write_system_log> method | ||
| 45 | provided below can be used to write info in a useable format to a | ||
| 46 | provided file handle. | ||
| 47 | |||
| 48 | =item * write_system_log - method to write a result description to a file handle | ||
| 49 | |||
| 50 | Can be used by the systemlogger to do the actual log writing, after systemlogger | ||
| 51 | has opened a file handle to the proper destination. | ||
| 52 | |||
| 53 | =back | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | has 'systemlogger' => ( | ||
| 58 | is => 'ro', | ||
| 59 | isa => 'CodeRef', | ||
| 60 | predicate => '_has_systemlogger' | ||
| 61 | ); | ||
| 62 | |||
| 63 | sub write_system_log { | ||
| 64 | my $self = shift; | ||
| 65 | my $path = shift; | ||
| 66 | my $fh = shift; | ||
| 67 | my $ret = shift; | ||
| 68 | my $lock = shift; | ||
| 69 | my $unlock = shift; | ||
| 70 | _getlock( $self, $path, $fh ) if $lock; | ||
| 71 | _log_ret( $self, $path, $fh, $ret ); | ||
| 72 | _freelock( $self, $path, $fh ) if $unlock; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _getlock { | ||
| 76 | my $self = shift; | ||
| 77 | my $path = shift // 'UNKNOWN PATH'; | ||
| 78 | my $fh = shift; | ||
| 79 | my $lock_cnt = 0; | ||
| 80 | while (1) { | ||
| 81 | flock $fh, LOCK_EX and last; | ||
| 82 | $self->croak( "$0 [$$]: flock failed on $path: $!" ) if $lock_cnt > 30; | ||
| 83 | $self->info( "Waiting for lock on $path" ) unless $lock_cnt++; | ||
| 84 | sleep(2); | ||
| 85 | } | ||
| 86 | $self->info( "Acquired lock on $path" ); | ||
| 87 | seek $fh, 2, 0; # make sure we're still at the end now that it is locked | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _freelock { | ||
| 91 | my $self = shift; | ||
| 92 | my $path = shift; | ||
| 93 | my $fh = shift; | ||
| 94 | flock $fh, LOCK_UN; | ||
| 95 | $self->info( "Released lock on $path" ); | ||
| 96 | } | ||
| 97 | |||
| 98 | my $groupcnt = 0; | ||
| 99 | my $start = DateTime->from_epoch(epoch => time); | ||
| 100 | |||
| 101 | sub _log_ret { | ||
| 102 | my $self = shift; | ||
| 103 | my $path = shift; | ||
| 104 | my $fh = shift; | ||
| 105 | my $ret = shift; | ||
| 106 | say $fh ""; | ||
| 107 | say $fh "*"x40 for 1..2; | ||
| 108 | say $fh "Program\t$FindBin::Bin/$FindBin::Script"; | ||
| 109 | say $fh "ProcessID\t$$"; | ||
| 110 | say $fh "StartTime\t", $start; | ||
| 111 | say $fh "User\t", scalar(getpwuid $<); | ||
| 112 | my $gname = $self->name; | ||
| 113 | say $fh "GroupName\t$gname"; | ||
| 114 | say $fh "GroupCount\t", ++$groupcnt; | ||
| 115 | say $fh "GroupEndTime\t", DateTime->from_epoch(epoch => time); | ||
| 116 | for my $stage (sort keys %$ret) { | ||
| 117 | my $runs = $ret->{$stage}; | ||
| 118 | say $fh "StageName\t$gname\t$stage"; | ||
| 119 | say $fh "StageAttempts\t$gname\t$stage\t", scalar( @$runs ); | ||
| 120 | for my $i (0..$#$runs) { | ||
| 121 | my $pre = "\t$gname\t$stage\tRun$i\t"; | ||
| 122 | my $run = $runs->[$i]; | ||
| 123 | for my $k (sort keys %$run) { | ||
| 124 | my $v = $run->{$k}; | ||
| 125 | say $fh "Res$pre$k\t$v"; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; |