Subversion Repositories DevTools

Rev

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