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