Subversion Repositories DevTools

Rev

Rev 289 | Rev 361 | 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
        {
289 dpurdie 302
        Error("Execute failure: getGlobal" );
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
 
271 dpurdie 331
    my $m_sqlstr = "SELECT rc.RCON_ID, rc.RTAG_ID, rc.GBE_ID, rc.DAEMON_HOSTNAME, " .
332
                          "rc.DAEMON_MODE, rc.GBE_BUILDFILTER, rt.RTAG_NAME, " .
333
                          "p.PROJ_NAME, rt.OFFICIAL" .
289 dpurdie 334
                    " FROM release_manager.release_config rc, release_manager.RELEASE_TAGS rt, release_manager.PROJECTS p" .
271 dpurdie 335
                    " WHERE      rt.RTAG_ID = rc.RTAG_ID " .
336
                            "AND rt.PROJ_ID = p.PROJ_ID " .
337
                            "AND rt.OFFICIAL != 'A' " .
338
                            "AND rt.OFFICIAL != 'Y' "
339
                            ;
255 dpurdie 340
 
341
    my $sth = $RM_DB->prepare($m_sqlstr);
342
    if ( defined($sth) )
343
    {
344
        if ( $sth->execute( ) )
345
        {
346
            if ( $sth->rows )
347
            {
348
                while ( @row = $sth->fetchrow_array )
349
                {
350
                    my $rcon_id = $row[0];
351
                    my $rtag_id = $row[1];
352
                    my $gbe_id =  $row[2];
353
                    my $hostname = $row[3];
354
                    my $hostmode = $row[4];
355
                    my $filter = $row[5];
356
                    my $rname = $row[6];
357
                    my $pname = $row[7];
261 dpurdie 358
                    my $official = $row[8];
255 dpurdie 359
 
360
                    next unless ( $hostname );
361
                    if ( @opt_rtag )
362
                    {
363
                        next unless ( defined $opt_rtag{$rtag_id} );
364
                    }
365
 
366
                    my %data;
367
                    $data{rcon_id}  = $rcon_id;
368
                    $data{hostname} = $hostname;
369
                    $data{hostname} = $hostname;
370
                    $data{hostmode} = $hostmode;
371
                    $data{filter}   = $filter;
372
 
373
                    $rc{$rtag_id}{$rcon_id} = \%data;
374
 
375
                    $rname{$rtag_id} = "$pname: $rname";
376
 
377
                    $pname{$pname}{$rname} = $rtag_id;
261 dpurdie 378
 
379
                    $official{$rtag_id} = $official;
380
 
255 dpurdie 381
#                    print "@row\n";
382
                }
383
            }
384
            $sth->finish();
385
        }
386
        else
387
        {
289 dpurdie 388
        Error("Execute failure: GetConfigData" );
255 dpurdie 389
        }
390
    }
391
    else
392
    {
289 dpurdie 393
        Error("Prepare failure: GetConfigData" );
255 dpurdie 394
    }
395
}
396
 
397
#-------------------------------------------------------------------------------
398
# Function        : GetRunData
399
#
400
# Description     : Build up data for each daemon
401
#
402
# Inputs          : 
403
#
404
# Returns         : 
405
#
406
sub  GetRunData
407
{
408
    my $foundDetails = 0;
409
    my (@row);
410
 
411
    # if we are not or cannot connect then return 0 as we have not found anything
412
    connectRM( \$RM_DB) unless $RM_DB;
413
 
414
    # First get details from pv_id
415
 
271 dpurdie 416
    my $m_sqlstr = "SELECT rl.RCON_ID, rl.CURRENT_BUILD_FILES, rl.CURRENT_RUN_LEVEL, rl.PAUSE, " .
417
                            "rl.CURRENT_PKG_ID_BEING_BUILT, pkg.PKG_NAME, " .
418
                            "rl.KEEP_ALIVE, TRUNC (86400*(SYSDATE - rl.KEEP_ALIVE))" .
289 dpurdie 419
                    " FROM RELEASE_MANAGER.RUN_LEVEL rl, RELEASE_MANAGER.PACKAGES pkg" .
255 dpurdie 420
                    " WHERE pkg.PKG_ID (+)= rl.CURRENT_PKG_ID_BEING_BUILT";
421
 
422
 
423
    my $sth = $RM_DB->prepare($m_sqlstr);
424
    if ( defined($sth) )
425
    {
426
        if ( $sth->execute( ) )
427
        {
428
            if ( $sth->rows )
429
            {
430
                while ( @row = $sth->fetchrow_array )
431
                {
432
#                    print "@row\n";
433
                    my $rcon_id = $row[0] || '';
434
                    my $cbf     = $row[1] || '';
289 dpurdie 435
                    my $crl     = $row[2] || '0';
255 dpurdie 436
                    my $pause   = $row[3] || '0';
437
                    my $cpid    = $row[5] || '';
271 dpurdie 438
                    my $alive   = $row[6] || '';
439
                    my $deltat  = $row[7] || '0';
255 dpurdie 440
 
441
                    $rl{$rcon_id}{crl} = $crl;
442
                    $rl{$rcon_id}{pause} = $pause;
443
                    $rl{$rcon_id}{cpid} = $cpid;
271 dpurdie 444
                    $rl{$rcon_id}{alive} = $alive;
445
                    $rl{$rcon_id}{deltat} = $deltat;
446
#                    printf "%10s, %10s, %10s, %10s, %10s, %20s\n", $rcon_id, $cbf, $crl, $pause, $cpid, "$alive, $deltat";
255 dpurdie 447
                }
448
            }
449
            $sth->finish();
450
        }
451
        else
452
        {
453
        Error("Execute failure: GetRunData", $m_sqlstr );
454
        }
455
    }
456
    else
457
    {
458
        Error("Prepare failure" );
459
    }
460
}
461
 
462
#-------------------------------------------------------------------------------
463
# Function        : getState
464
#
465
# Description     : Convert number into a nice state string
466
#
467
# Inputs          : 
468
#
469
# Returns         : 
470
#
471
sub getState
472
{
473
    my ($num) = @_;
474
    return "Undefined" unless ( defined $num );
475
    if ( exists ($StateData{$num}) )
476
    {
477
        return $StateData{$num};
478
    }
479
    return "Unknown:$num";
480
}
481
 
482
#-------------------------------------------------------------------------------
483
#   Documentation
484
#
485
 
486
=pod
487
 
488
=head1 NAME
489
 
490
dstatus - Display Daemon Status
491
 
492
=head1 SYNOPSIS
493
 
261 dpurdie 494
  jats dstatus [options]
255 dpurdie 495
 
496
 Options:
497
    -help              - brief help message
498
    -help -help        - Detailed help message
499
    -man               - Full documentation
500
    -repeat=num        - Repeat display every n seconds
501
    -short             - Fold up inactive releases
267 dpurdie 502
    -short -short      - Supress inactive releases
255 dpurdie 503
    -color             - Pretty color display
504
    -rtag=num[,num]    - List of RTAG Ids to display
505
    -filter            - Display GBE_BUILDFILTER
317 dpurdie 506
    -dead=num          - Set DeadTime Threshold
255 dpurdie 507
 
508
=head1 OPTIONS
509
 
510
=over 8
511
 
512
=item B<-help>
513
 
514
Print a brief help message and exits.
515
 
516
=item B<-help -help>
517
 
518
Print a detailed help message with an explanation for each option.
519
 
520
=item B<-man>
521
 
522
Prints the manual page and exits.
523
 
524
=item B<-repeat=num>
525
 
526
This option will cause the program to re-display the status page every B<num>
527
seconds. The default option is to display the status once and exit.
528
 
529
If the user specifies a repeat time of less than 10 seconds, then it will be set
530
to 10 seconds - to avoid heavy loads on the Release Manager database.
531
 
532
This option works best with -color on a non-windows console as the screen
533
will be cleared before the display is refreshed.
534
 
535
=item B<-short>
536
 
537
This option will generate a short display. The body of inactive releases will
538
not be displayed. This option is useful in conjunction with a repeating display
539
to limit the display space used.
540
 
267 dpurdie 541
If B<-short> is used twice then the header for inactive releases will also
542
be supressed.
543
 
255 dpurdie 544
=item B<-color>
545
 
546
This option will enable a colored display. This will not work on Windoes.
547
The default is for a non-colored display.
548
 
549
=item B<-rtag=num[,num]>
550
 
551
This option will limit the display to the specified rtag Id's (Numeric Release ID).
552
This option can be used multiple times to specify a list, or the ID's can be comma seperated.
553
 
554
The default option is to display all Releases.
555
 
556
=item B<-filter>
557
 
558
This optionwill alter the display such that the GBE_BUILDFILTER for each platform
559
is displayed instead of the normal status information. This may result in a very
560
wide display.
561
 
317 dpurdie 562
=item B<-dead=num>
563
 
564
This option alters the B<Set DeadTime Threshold>. If a deamon has not
565
communicated with the Release Manager Database within this time, then the daemon
566
may be dead and will be flagged in the shortened displays.
567
 
568
The default value is 600 seconds.
569
 
255 dpurdie 570
=back
571
 
572
=head1 DESCRIPTION
573
 
574
This program will display the status of the build daemons on all daemon enabled
575
releases.
576
 
577
=cut
578