Subversion Repositories DevTools

Rev

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