Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
255 dpurdie 1
########################################################################
2
# Copyright ( C ) 2008 ERG Limited, All rights reserved
3
#
4
# Module name   : jats.sh
5
# Module type   : Makefile system
6
# Compiler(s)   : n/a
7
# Environment(s): jats
8
#
9
# Description   : Display Daemon Status information
10
#
11
# Usage         : See POD at end of file
12
#
13
#......................................................................#
14
 
15
require 5.6.1;
16
use strict;
17
use warnings;
18
use JatsError;
19
use JatsSystem;
20
use Getopt::Long;
21
use Pod::Usage;
22
use JatsRmApi;
23
use Term::ANSIColor qw(:constants);
24
use DBI;
25
 
26
my $VERSION = "1.0.0";                      # Update this
27
my $opt_verbose = 1;
28
my $opt_help = 0;
29
my $opt_repeat;
267 dpurdie 30
my $opt_short = 0;
255 dpurdie 31
my $opt_color;
32
my @opt_rtag;
33
my %opt_rtag;
34
my $opt_filter;
35
my $RM_DB;
36
my %pname;
37
my %rc;
38
my %rl;
39
my %rname;
261 dpurdie 40
my %official;
255 dpurdie 41
my $indefinite = 0;
42
my $ff_string = "\f";
271 dpurdie 43
my $dead_time = ( 5 * 60 ) * 2;
289 dpurdie 44
my %StateData = ( 0 => 'Undefined',
45
                  1 => 'Disk Full',
271 dpurdie 46
                  2 => 'Paused',
47
                  3 => 'Active',
48
                  4 => 'Idle' ,
49
                  5 => 'Waiting',
50
                  6 => 'Publishing',
51
                  );
255 dpurdie 52
 
53
#-------------------------------------------------------------------------------
54
# Function        : Main Entry
55
#
56
# Description     :
57
#
58
# Inputs          :
59
#
60
# Returns         :
61
#
62
my $result = GetOptions (
267 dpurdie 63
                "help:+"        => \$opt_help,          # flag, multiple use allowed
64
                "manual:3"      => \$opt_help,          # flag
271 dpurdie 65
                "verbose:+"     => \$opt_verbose,       # flag
255 dpurdie 66
                "repeat=i"      => \$opt_repeat,        # Integer
267 dpurdie 67
                "short:+"       => \$opt_short,         # Flag
255 dpurdie 68
                "color"         => \$opt_color,         # Flag
69
                "rtag=s"        => \@opt_rtag,          # Array
70
                "filter"        => \$opt_filter,        # Flag
71
                );
72
 
73
#
74
#   Process help and manual options
75
#
76
pod2usage(-verbose => 0, -message => "Version: $VERSION")  if ($opt_help == 1  || ! $result);
77
pod2usage(-verbose => 1)  if ($opt_help == 2 );
267 dpurdie 78
pod2usage(-verbose => 2)  if ($opt_help > 2);
255 dpurdie 79
 
80
ErrorConfig( 'name'    =>'DSTATUS' );
81
 
82
#
83
#   Native windows doesn't handle color
84
#   Dispable color by default
85
#
86
unless ( $opt_color )
87
{
88
    $ENV{ANSI_COLORS_DISABLED} = 1;
89
    $ff_string = "\n\n\n";
90
}
91
 
92
#
93
#   Convert array of RTAGS into a hash for simpler usage
94
#
95
 
96
$opt_rtag{$_} = 1 foreach ( split( /[,\s]+/, "@opt_rtag"));
97
 
98
#
99
#   Attempt to prevent users from repeating every second
100
#   If the user specified < 10, then set it to 10
101
#   Exports ( with internal knowledge) can set shorter times
102
#
103
$opt_repeat = 10 if ( $opt_repeat && $opt_repeat < 10 );
104
$opt_repeat -= 1000 if ( $opt_repeat && $opt_repeat > 1001 );
105
 
106
#
107
#   Repeat for ever
108
#
109
while (1)
110
{
111
    getGlobal();
112
    GetConfigData();
113
    GetRunData();
114
    DisplayInfo();
115
 
116
    last unless ( $opt_repeat );
117
    sleep ($opt_repeat );
267 dpurdie 118
 
119
    #
120
    #   Cleanout any data from previous run
121
    #
122
    %pname = ();
123
    %rc = ();
124
    %rl = ();
125
    %rname = ();
126
    %official = ();
127
 
255 dpurdie 128
}
129
 
130
disconnectRM(\$RM_DB);
131
exit;
132
 
133
#-------------------------------------------------------------------------------
134
# Function        : DisplayInfo
135
#
136
# Description     : Display collected information
137
#
138
# Inputs          : 
139
#
140
# Returns         : 
141
#
142
sub DisplayInfo
143
{
144
 
145
#DebugDumpData ("Data", \%rc);
146
 
147
    #
148
    #   Display a header
149
    #   Include a form feed if possible
150
    #
151
    if ( $opt_repeat )
152
    {
153
        print $ff_string, BOLD, "Build Daemon Status [" . localtime() . "]" , RESET, "\n";
154
    }
155
    if ( $opt_short && $indefinite )
156
    {
157
        print RED "*** Indefinite Pause", RESET, "\n";
158
    }
159
 
160
    #
161
    #  Sort by Project Name and Release Name
162
    #
163
    foreach my $pname ( sort keys %pname )
164
    {
165
        foreach my $rname ( sort keys %{$pname{$pname}} )
166
        {
167
            my $rtag_id = $pname{$pname}{$rname};
168
            my @orderm;
169
            my @orders;
170
            my $building = 0;
271 dpurdie 171
            my $dead = 0;
172
            my $paused = 0;
173
            my $in_error = 0;
174
 
255 dpurdie 175
 
176
            foreach my $rcon_id ( keys %{$rc{$rtag_id}}  )
177
            {
271 dpurdie 178
                next unless ( exists $rl{$rcon_id} );
255 dpurdie 179
 
180
                #
271 dpurdie 181
                #   Maintain alive info
182
                #
183
                if ( $rl{$rcon_id}{deltat} )
184
                {
185
                    if ( $rl{$rcon_id}{deltat} > $dead_time )
186
                    {
187
                        $dead = 1;
188
                    }
189
                }
190
 
191
                #
255 dpurdie 192
                #   Determine if any of the machines are building a package
193
                #
194
                $building = 1 if ( $rl{$rcon_id}{cpid} );
195
 
196
                #
197
                #   Sort into Master and Slaves
198
                #   Done so that we can print the master at the top of the list
199
                #
200
                if ($rc{$rtag_id}{$rcon_id}{hostmode} =~ m{M} )
201
                {
202
                    push @orderm, $rcon_id;
203
                }
204
                else
205
                {
206
                    push @orders, $rcon_id;
207
                }
271 dpurdie 208
 
209
                #
210
                #   Paused deamons don't maintain alive data
211
                #
212
                $paused += 1
213
                    if ( $rl{$rcon_id}{crl} == 2 );
214
 
215
                $dead = 0
216
                    if ( $paused );
217
 
218
                $in_error ++
219
                    if ( $rl{$rcon_id}{crl} == 1 );
220
 
255 dpurdie 221
            }
222
 
271 dpurdie 223
            if ( $in_error || $dead || $building || $opt_short < 2  )
255 dpurdie 224
            {
267 dpurdie 225
                print BOLD GREEN "[$rtag_id] $rname{$rtag_id} ($official{$rtag_id})", RESET, "\n";
226
            }
227
 
271 dpurdie 228
            if ( $in_error || $dead || $building || $opt_short < 1 )
267 dpurdie 229
            {
255 dpurdie 230
                foreach my $rcon_id ( @orderm, sort(@orders) )
231
                {
232
                    if ( $opt_filter )
233
                    {
275 dpurdie 234
                         printf "    %1.1s: %-20.20s %s\n",
255 dpurdie 235
                            $rc{$rtag_id}{$rcon_id}{hostmode},
236
                            $rc{$rtag_id}{$rcon_id}{hostname},
237
                            $rc{$rtag_id}{$rcon_id}{filter};
238
                    }
239
                    else
240
                    {
275 dpurdie 241
                             printf "    %1.1s: %-20.20s %s%10s%s(%1.1s) %-20s %-20s (%5s)\n",
255 dpurdie 242
                #                $rtag_id, $rcon_id,
243
                                $rc{$rtag_id}{$rcon_id}{hostmode},
244
                                $rc{$rtag_id}{$rcon_id}{hostname},
245
                                $indefinite ? RED : RESET,
246
                                $indefinite ? ('AllStop') : (getState($rl{$rcon_id}{crl})),
247
                                RESET,
248
                                defined ($rl{$rcon_id}{pause} ) ? $rl{$rcon_id}{pause} : '-',
271 dpurdie 249
                                $rl{$rcon_id}{cpid} || '-',
250
                                $rl{$rcon_id}{alive} || '-',
251
                                $rl{$rcon_id}{deltat} || '-',
255 dpurdie 252
                            ;
253
                    }
254
                }
255
                print "\n";
256
            }
257
        }
258
    }
259
}
260
 
261
#-------------------------------------------------------------------------------
262
# Function        : getGlobal
263
#
264
# Description     : Get global information
265
#
266
# Inputs          : 
267
#
268
# Returns         : 
269
#
270
 
271
sub getGlobal
272
{
273
    my (@row);
274
    $indefinite = 0;
275
 
276
    # if we are not or cannot connect then return 0 as we have not found anything
277
    connectRM( \$RM_DB) unless $RM_DB;
278
 
279
    # First get details from pv_id
280
 
281
    my $m_sqlstr = "SELECT SCHEDULED_PAUSE, SCHEDULED_RESUME, REPEAT, INDEFINITE_PAUSE" .
282
                   " FROM RELEASE_MANAGER.RUN_LEVEL_SCHEDULE";
283
 
284
    my $sth = $RM_DB->prepare($m_sqlstr);
285
    if ( defined($sth) )
286
    {
287
        if ( $sth->execute( ) )
288
        {
289
            if ( $sth->rows )
290
            {
291
                while ( @row = $sth->fetchrow_array )
292
                {
293
                    $indefinite = 1 if ( $row[3] )
294
#                    print "@row\n";
295
                }
296
            }
297
            $sth->finish();
298
        }
299
        else
300
        {
289 dpurdie 301
        Error("Execute failure: getGlobal" );
255 dpurdie 302
        }
303
    }
304
    else
305
    {
289 dpurdie 306
        Error("Prepare failure: getGlobal" );
255 dpurdie 307
    }
308
}
309
 
310
#-------------------------------------------------------------------------------
311
# Function        : GetConfigData
312
#
313
# Description     : Build up a list of all releases that have daemons
314
#                   configured
315
#
316
# Inputs          : 
317
#
318
# Returns         : 
319
#
320
sub  GetConfigData
321
{
322
    my $foundDetails = 0;
323
    my (@row);
324
 
325
    # if we are not or cannot connect then return 0 as we have not found anything
326
    connectRM( \$RM_DB) unless $RM_DB;
327
 
328
    # First get details from pv_id
329
 
271 dpurdie 330
    my $m_sqlstr = "SELECT rc.RCON_ID, rc.RTAG_ID, rc.GBE_ID, rc.DAEMON_HOSTNAME, " .
331
                          "rc.DAEMON_MODE, rc.GBE_BUILDFILTER, rt.RTAG_NAME, " .
332
                          "p.PROJ_NAME, rt.OFFICIAL" .
289 dpurdie 333
                    " FROM release_manager.release_config rc, release_manager.RELEASE_TAGS rt, release_manager.PROJECTS p" .
271 dpurdie 334
                    " WHERE      rt.RTAG_ID = rc.RTAG_ID " .
335
                            "AND rt.PROJ_ID = p.PROJ_ID " .
336
                            "AND rt.OFFICIAL != 'A' " .
337
                            "AND rt.OFFICIAL != 'Y' "
338
                            ;
255 dpurdie 339
 
340
    my $sth = $RM_DB->prepare($m_sqlstr);
341
    if ( defined($sth) )
342
    {
343
        if ( $sth->execute( ) )
344
        {
345
            if ( $sth->rows )
346
            {
347
                while ( @row = $sth->fetchrow_array )
348
                {
349
                    my $rcon_id = $row[0];
350
                    my $rtag_id = $row[1];
351
                    my $gbe_id =  $row[2];
352
                    my $hostname = $row[3];
353
                    my $hostmode = $row[4];
354
                    my $filter = $row[5];
355
                    my $rname = $row[6];
356
                    my $pname = $row[7];
261 dpurdie 357
                    my $official = $row[8];
255 dpurdie 358
 
359
                    next unless ( $hostname );
360
                    if ( @opt_rtag )
361
                    {
362
                        next unless ( defined $opt_rtag{$rtag_id} );
363
                    }
364
 
365
                    my %data;
366
                    $data{rcon_id}  = $rcon_id;
367
                    $data{hostname} = $hostname;
368
                    $data{hostname} = $hostname;
369
                    $data{hostmode} = $hostmode;
370
                    $data{filter}   = $filter;
371
 
372
                    $rc{$rtag_id}{$rcon_id} = \%data;
373
 
374
                    $rname{$rtag_id} = "$pname: $rname";
375
 
376
                    $pname{$pname}{$rname} = $rtag_id;
261 dpurdie 377
 
378
                    $official{$rtag_id} = $official;
379
 
255 dpurdie 380
#                    print "@row\n";
381
                }
382
            }
383
            $sth->finish();
384
        }
385
        else
386
        {
289 dpurdie 387
        Error("Execute failure: GetConfigData" );
255 dpurdie 388
        }
389
    }
390
    else
391
    {
289 dpurdie 392
        Error("Prepare failure: GetConfigData" );
255 dpurdie 393
    }
394
}
395
 
396
#-------------------------------------------------------------------------------
397
# Function        : GetRunData
398
#
399
# Description     : Build up data for each daemon
400
#
401
# Inputs          : 
402
#
403
# Returns         : 
404
#
405
sub  GetRunData
406
{
407
    my $foundDetails = 0;
408
    my (@row);
409
 
410
    # if we are not or cannot connect then return 0 as we have not found anything
411
    connectRM( \$RM_DB) unless $RM_DB;
412
 
413
    # First get details from pv_id
414
 
271 dpurdie 415
    my $m_sqlstr = "SELECT rl.RCON_ID, rl.CURRENT_BUILD_FILES, rl.CURRENT_RUN_LEVEL, rl.PAUSE, " .
416
                            "rl.CURRENT_PKG_ID_BEING_BUILT, pkg.PKG_NAME, " .
417
                            "rl.KEEP_ALIVE, TRUNC (86400*(SYSDATE - rl.KEEP_ALIVE))" .
289 dpurdie 418
                    " FROM RELEASE_MANAGER.RUN_LEVEL rl, RELEASE_MANAGER.PACKAGES pkg" .
255 dpurdie 419
                    " WHERE pkg.PKG_ID (+)= rl.CURRENT_PKG_ID_BEING_BUILT";
420
 
421
 
422
    my $sth = $RM_DB->prepare($m_sqlstr);
423
    if ( defined($sth) )
424
    {
425
        if ( $sth->execute( ) )
426
        {
427
            if ( $sth->rows )
428
            {
429
                while ( @row = $sth->fetchrow_array )
430
                {
431
#                    print "@row\n";
432
                    my $rcon_id = $row[0] || '';
433
                    my $cbf     = $row[1] || '';
289 dpurdie 434
                    my $crl     = $row[2] || '0';
255 dpurdie 435
                    my $pause   = $row[3] || '0';
436
                    my $cpid    = $row[5] || '';
271 dpurdie 437
                    my $alive   = $row[6] || '';
438
                    my $deltat  = $row[7] || '0';
255 dpurdie 439
 
440
                    $rl{$rcon_id}{crl} = $crl;
441
                    $rl{$rcon_id}{pause} = $pause;
442
                    $rl{$rcon_id}{cpid} = $cpid;
271 dpurdie 443
                    $rl{$rcon_id}{alive} = $alive;
444
                    $rl{$rcon_id}{deltat} = $deltat;
445
#                    printf "%10s, %10s, %10s, %10s, %10s, %20s\n", $rcon_id, $cbf, $crl, $pause, $cpid, "$alive, $deltat";
255 dpurdie 446
                }
447
            }
448
            $sth->finish();
449
        }
450
        else
451
        {
452
        Error("Execute failure: GetRunData", $m_sqlstr );
453
        }
454
    }
455
    else
456
    {
457
        Error("Prepare failure" );
458
    }
459
}
460
 
461
#-------------------------------------------------------------------------------
462
# Function        : getState
463
#
464
# Description     : Convert number into a nice state string
465
#
466
# Inputs          : 
467
#
468
# Returns         : 
469
#
470
sub getState
471
{
472
    my ($num) = @_;
473
    return "Undefined" unless ( defined $num );
474
    if ( exists ($StateData{$num}) )
475
    {
476
        return $StateData{$num};
477
    }
478
    return "Unknown:$num";
479
}
480
 
481
#-------------------------------------------------------------------------------
482
#   Documentation
483
#
484
 
485
=pod
486
 
487
=head1 NAME
488
 
489
dstatus - Display Daemon Status
490
 
491
=head1 SYNOPSIS
492
 
261 dpurdie 493
  jats dstatus [options]
255 dpurdie 494
 
495
 Options:
496
    -help              - brief help message
497
    -help -help        - Detailed help message
498
    -man               - Full documentation
499
    -repeat=num        - Repeat display every n seconds
500
    -short             - Fold up inactive releases
267 dpurdie 501
    -short -short      - Supress inactive releases
255 dpurdie 502
    -color             - Pretty color display
503
    -rtag=num[,num]    - List of RTAG Ids to display
504
    -filter            - Display GBE_BUILDFILTER
505
 
506
=head1 OPTIONS
507
 
508
=over 8
509
 
510
=item B<-help>
511
 
512
Print a brief help message and exits.
513
 
514
=item B<-help -help>
515
 
516
Print a detailed help message with an explanation for each option.
517
 
518
=item B<-man>
519
 
520
Prints the manual page and exits.
521
 
522
=item B<-repeat=num>
523
 
524
This option will cause the program to re-display the status page every B<num>
525
seconds. The default option is to display the status once and exit.
526
 
527
If the user specifies a repeat time of less than 10 seconds, then it will be set
528
to 10 seconds - to avoid heavy loads on the Release Manager database.
529
 
530
This option works best with -color on a non-windows console as the screen
531
will be cleared before the display is refreshed.
532
 
533
=item B<-short>
534
 
535
This option will generate a short display. The body of inactive releases will
536
not be displayed. This option is useful in conjunction with a repeating display
537
to limit the display space used.
538
 
267 dpurdie 539
If B<-short> is used twice then the header for inactive releases will also
540
be supressed.
541
 
255 dpurdie 542
=item B<-color>
543
 
544
This option will enable a colored display. This will not work on Windoes.
545
The default is for a non-colored display.
546
 
547
=item B<-rtag=num[,num]>
548
 
549
This option will limit the display to the specified rtag Id's (Numeric Release ID).
550
This option can be used multiple times to specify a list, or the ID's can be comma seperated.
551
 
552
The default option is to display all Releases.
553
 
554
=item B<-filter>
555
 
556
This optionwill alter the display such that the GBE_BUILDFILTER for each platform
557
is displayed instead of the normal status information. This may result in a very
558
wide display.
559
 
560
=back
561
 
562
=head1 DESCRIPTION
563
 
564
This program will display the status of the build daemons on all daemon enabled
565
releases.
566
 
567
=cut
568