-
Notifications
You must be signed in to change notification settings - Fork 1
/
oargridstat
executable file
·1034 lines (937 loc) · 39.9 KB
/
oargridstat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
use oargrid_lib;
use oargrid_conflib;
use Data::Dumper;
use Getopt::Long;
use strict;
use warnings;
use Time::Local;
#Try to load XML module
my $XMLenabled = 1;
unless (eval "use XML::Simple qw(XMLout);1"){
$XMLenabled = 0;
}
#Try to load YAML module
my $YAMLenabled = 1;
unless (eval "use YAML;1"){
$YAMLenabled = 0;
}
my $gridpremsProperty = "gridPrems";
# suitable Data::Dumper configuration for serialization
$Data::Dumper::Purity = 1;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
$Data::Dumper::Deepcopy = 1;
#Prototypes
sub usage();
sub print_reservation($$);
sub get_info_nodes($$);
sub get_info_resources($$);
sub get_resource_dead_range_date($$$);
sub get_jobs_range_dates($$$$);
sub get_jobs_gantt_scheduled($$$$);
sub get_current_job_types($$);
sub get_gantt_visu_date($);
sub sql_to_local($);
sub ymdhms_to_local($$$$$$);
sub sql_to_ymdhms($);
#$SIG{INT} = 'IGNORE';
my $timeoutMysql = 30;
# Get the user name
if (!defined($ENV{SUDO_UID})){
die("[OAR_GRISUBD] I can not get user id\n");
}
my $lusr= getpwuid($ENV{SUDO_UID});
# parse arguments
my $sos;
my $listNodes = 0;
my $listKeys = 0;
my @cluster;
my $jobId;
my $monitorMode;
my $gantt;
my $waitJobs;
my $sleepTimeRetry = 5;
my $maxPollingTime = 3600;
my $dumperMode;
my $XMLmode;
my $YAMLmode;
my $list_clusters;
my $list_aliases;
my $backward_compatible;
my $version;
Getopt::Long::Configure ("gnu_getopt");
GetOptions (
"help|h" => \$sos,
"list_nodes|l=i" => \$listNodes,
"list_keys|k=i" => \$listKeys,
"cluster|c=s" => \@cluster,
"job|j=i" => \$jobId,
"monitor" => \$monitorMode,
"gantt=s" => \$gantt,
"wait|w" => \$waitJobs,
"polling|p=i" => \$sleepTimeRetry,
"max_polling|m=i" => \$maxPollingTime,
"dumper|d" => \$dumperMode,
"DUMPER|D" => \$dumperMode,
"XML|X" => \$XMLmode,
"YAML|Y" => \$YAMLmode,
"list_clusters" => \$list_clusters,
"list_aliases" => \$list_aliases,
"backward_compatible" => \$backward_compatible,
"VERSION|V" => \$version
);
if (defined($version)){
print("OARGRID version : ".oargrid_lib::get_version()."\n");
exit(0);
}
if ((defined($sos)) || (defined($jobId) && !defined($cluster[0]))){
usage();
exit(1);
}
if ($sleepTimeRetry < 5){
warn("Polling time must be greater than 5 seconds so I set it to 5.\n");
$sleepTimeRetry = 5;
}
if ($listKeys > 0) {
$listNodes=$listKeys;
}
my $exit_value = 0;
# Initialize database connection
oargrid_conflib::init_conf(oargrid_lib::get_config_file_name());
my $DB_SERVER = oargrid_conflib::get_conf("DB_HOSTNAME");
my $DB_BASE_NAME= oargrid_conflib::get_conf("DB_BASE_NAME");
my $DB_BASE_LOGIN = oargrid_conflib::get_conf("DB_BASE_LOGIN");
my $DB_BASE_PASSWD = oargrid_conflib::get_conf("DB_BASE_PASSWD");
my $DB_OAR_LOGIN = oargrid_conflib::get_conf("DB_OAR__LOGIN");
my $DB_OAR_PASSWD = oargrid_conflib::get_conf("DB_OAR_PASSWD");
my $dbh = oargrid_lib::connect($DB_SERVER,$DB_BASE_NAME,$DB_BASE_LOGIN,$DB_BASE_PASSWD);
# -l option
if ($listNodes > 0 || $listKeys > 0){
my $priv_key;
my $pub_key;
my %readyNodes;
my %clusters = oargrid_lib::get_cluster_names($dbh);
my %resaInfo = oargrid_lib::get_reservation_informations($dbh,$listNodes);
my $alias_properties ="";
if (defined($cluster[0])) {
$alias_properties=oargrid_lib::get_cluster_alias_properties($dbh,$cluster[0]);
}
foreach my $i (keys(%{$resaInfo{clusterJobs}})){
if ((!defined($cluster[0])
|| ($cluster[0] eq $i
|| (defined($clusters{$cluster[0]}->{parent}) && $clusters{$cluster[0]}->{parent} eq $i) )
)
&& (defined($clusters{$i}))){
my $retry = 1;
my $nbRetry = 0;
while ($retry > 0){
my $jobEnded = 0;
my $oldout;
my $olderr;
open($oldout, ">&STDOUT");
open($olderr, ">&STDERR");
open(STDOUT, ">/dev/null");
open(STDERR, ">/dev/null");
eval{
my $remoteDbh;
$SIG{ALRM} = sub {oargrid_lib::disconnect($remoteDbh);die("alarm\n")};
alarm($timeoutMysql);
$remoteDbh = oargrid_lib::connectPg($clusters{$i}->{dbHostname},$clusters{$i}->{dbName}, $DB_OAR_LOGIN, $DB_OAR_PASSWD);
my $whereClause = "" ;
my $whereProps = "" ;
foreach my $j (values(%{$resaInfo{clusterJobs}->{$i}})){
if (!defined($jobId) || ($jobId == $j->{batchId})){
$whereClause .= "$j->{batchId},";
}
}
chop($whereClause);
# Test if jobs are in Running state
my %weights = ();
my $sth;
$sth = $remoteDbh->prepare("SELECT job_id,state
FROM jobs
WHERE
job_id IN ($whereClause)");
$sth->execute();
while (my @ref = $sth->fetchrow_array()) {
##$weights{$ref[0]} = $ref[2];
$weights{$ref[0]} = $resaInfo{clusterJobs}->{$i}->{$ref[0]}->{weight};
if (defined($waitJobs)){
if ($ref[1] ne "Running"){
# if the job is already finished
if (($ref[1] eq "Terminated") || ($ref[1] eq "Error")){
$jobEnded = $ref[0];
}
oargrid_lib::disconnect($remoteDbh);
alarm(0);
return;
}
}
}
$sth->finish();
$retry = 0;
if ($alias_properties ne "") {
$whereProps .= " AND ". $alias_properties;
}
$sth = $remoteDbh->prepare("SELECT resources.network_address,moldable_job_descriptions.moldable_job_id
FROM assigned_resources, moldable_job_descriptions, resources
WHERE
moldable_job_descriptions.moldable_job_id IN ($whereClause)
AND assigned_resources.moldable_job_id = moldable_job_descriptions.moldable_id
AND resources.resource_id = assigned_resources.resource_id
AND resources.network_address IS NOT NULL
AND resources.network_address != ''
$whereProps
ORDER BY resources.network_address");
$sth->execute();
while (my @ref = $sth->fetchrow_array()) {
##for (my $w = 0; $w < $weights{$ref[1]}; $w++){
if (defined($resaInfo{clusterJobs}->{$i}->{$ref[1]}->{name}) and ($resaInfo{clusterJobs}->{$i}->{$ref[1]}->{name} ne "")){
#push(@{$readyNodes{$i}{$resaInfo{clusterJobs}->{$i}->{$ref[1]}->{name}}{$ref[1]}}, $ref[0]);
$readyNodes{$i}{$ref[1]}{name}=$resaInfo{clusterJobs}->{$i}->{$ref[1]}->{name};
push(@{$readyNodes{$i}{$ref[1]}{nodes}}, $ref[0]);
}else{
#push(@{$readyNodes{$i}{$resaInfo{clusterJobs}->{$i}->{$ref[1]}->{batchId}}{$ref[1]}}, $ref[0]);
$readyNodes{$i}{$ref[1]}{name}="";
push(@{$readyNodes{$i}{$ref[1]}{nodes}}, $ref[0]);
}
##}
}
$sth->finish();
$sth = $remoteDbh->prepare("SELECT ssh_private_key,ssh_public_key from challenges where job_id IN ($whereClause)");
$sth->execute();
while (my @ref = $sth->fetchrow_array()) {
$priv_key=$ref[0];
$pub_key=$ref[1];
}
oargrid_lib::disconnect($remoteDbh);
alarm(0);
};
open(STDOUT, ">&", $oldout);
open(STDERR, ">&", $olderr);
if ($@){
$exit_value = 2;
$retry = 0;
warn("$i sql server timed out or was unreachable : $@\n");
}
if ($jobEnded > 0){
warn("ERROR: job $jobEnded is ended on cluster $i\n");
$exit_value = 4;
$retry = 0;
}
if (!defined($waitJobs)){
$retry = 0;
}elsif($nbRetry * $sleepTimeRetry >= $maxPollingTime){
warn("Max polling time reached\n");
exit(3);
}elsif($retry > 0){
warn("Not all jobs are running on $i, I wait $sleepTimeRetry seconds\n");
sleep($sleepTimeRetry);
$nbRetry++;
}
}
}
}
if (defined($dumperMode)){
print(Dumper(\%readyNodes));
}elsif(defined($XMLmode)){
if ($XMLenabled == 1){
#print(XMLout(\%readyNodes));
print("<opt>\n");
foreach my $xml_cluster (sort (keys(%readyNodes))) {
print("\t<cluster name=\"" . $xml_cluster . "\">\n");
foreach my $xml_name (sort (keys(%{$readyNodes{$xml_cluster}}))) {
print("\t\t<job name=\"" . $xml_name . "\">\n");
foreach my $xml_job (sort (keys(%{$readyNodes{$xml_cluster}{$xml_name}}))) {
print("\t\t<job jobid=\"" . $xml_job . "\">\n");
foreach my $xml_node (@{$readyNodes{$xml_cluster}{$xml_name}{$xml_job}}) {
print("\t\t\t<node>" . $xml_node . "</node>\n");
}
print("\t\t</job>\n");
}
}
print("\t</cluster>\n");
}
print("</opt>\n");
}else{
warn("[ERROR] Cannot load XML module.\n");
$exit_value = 5;
}
}elsif(defined($YAMLmode)){
if ($YAMLenabled == 1){
print(YAML::Dump(\%readyNodes));
}else{
warn("[ERROR] Cannot load YAML module.\n");
$exit_value = 6;
}
}else{
if ($listKeys > 0) {
my $key_path="/tmp/oargrid/oargrid_ssh_key_retrieved_$lusr"."_$listNodes";
open (FILE,">$key_path");
print FILE "$priv_key";
close(FILE);
open (FILE,">$key_path.pub");
print FILE "$pub_key";
close (FILE);
`chmod 600 $key_path`;
`sudo chown $lusr $key_path*`;
print "Key restored into $key_path\n";
}else {
foreach my $i (keys(%readyNodes)){
foreach my $j (keys(%{$readyNodes{$i}})){
foreach my $n (keys(%{$readyNodes{$i}{$j}})){
if ($n ne "name") {
foreach my $k (@{$readyNodes{$i}{$j}{$n}}){
#for (my $weight=0; $weight < $resaInfo{clusterJobs}->{$i}->{$j}->{weight};$weight++){
print("$k\n");
#}
}
}
}
}
}
print("\n");
}
}
# --list_clusters option
}elsif(defined($list_clusters)){
my %clusterProperties = oargrid_lib::get_cluster_properties($dbh);
if(defined($XMLmode)){
if ($XMLenabled == 1){
print(XMLout(\%clusterProperties));
}else{
warn("[ERROR] Cannot load XML module.\n");
$exit_value = 5;
}
}elsif(defined($YAMLmode)){
if ($YAMLenabled == 1){
print(YAML::Dump(\%clusterProperties));
}else{
warn("[ERROR] Cannot load YAML module.\n");
$exit_value = 6;
}
}else{
print(Dumper(\%clusterProperties));
}
# --list_aliases option
}elsif(defined($list_aliases)){
my %cluster_aliases = oargrid_lib::get_all_cluster_aliases($dbh);
if(defined($XMLmode)){
if ($XMLenabled == 1){
print(XMLout(\%cluster_aliases));
}else{
warn("[ERROR] Cannot load XML module.\n");
$exit_value = 5;
}
}elsif(defined($YAMLmode)){
if ($YAMLenabled == 1){
print(YAML::Dump(\%cluster_aliases));
}else{
warn("[ERROR] Cannot load YAML module.\n");
$exit_value = 6;
}
}else{
print(Dumper(\%cluster_aliases));
}
# --monitor or --gantt option
}elsif(defined($monitorMode) || defined($gantt)){
my %clusters;
if ($backward_compatible) {
%clusters = oargrid_lib::get_cluster_aliases($dbh);
}
else {
%clusters = oargrid_lib::get_cluster_names($dbh);
}
my %pipeClusterList;
my @clustersToQuery;
if (defined($cluster[0])){
@clustersToQuery = @cluster;
}else{
@clustersToQuery = keys(%clusters);
}
foreach my $i (@clustersToQuery){
#create PIPES for communication with childs
my $P1;
my $P2;
pipe($P1,$P2);
$pipeClusterList{$i} = $P1;
if (fork() == 0){
# Child
close($P1);
#open(STDOUT, ">/dev/null");
#open(STDERR, ">/dev/null");
# Test if the cluster exists!!
if (!defined($clusters{$i}->{dbHostname})){
warn("[ERROR] Unknown cluster $i\n");
close($P2);
exit(1);
}
#$SIG{ALRM} = sub {die("alarm\n")};
#alarm($timeoutMysql);
# Timeout
my $father = $$;
my $child = fork();
if ($child == 0){
sleep($timeoutMysql);
kill(9, $father);
exit();
}
my %results;
my $remoteDbh = oargrid_lib::connectPg($clusters{$i}->{dbHostname},$clusters{$i}->{dbName}, $DB_OAR_LOGIN, $DB_OAR_PASSWD);
if (defined($monitorMode)){
#monitor queries
my %resTmp = get_info_nodes($remoteDbh,$clusters{$i}->{properties});
$results{nodes} = \%resTmp;
# Get current job informations
my $sth;
my $clusterProperties=$clusters{$i}->{properties};
if (defined($clusterProperties) && $clusterProperties ne "") { $clusterProperties="AND $clusterProperties"; }
else { $clusterProperties = "";}
$sth = $remoteDbh->prepare(" SELECT *
FROM jobs j, assigned_resources p, resources r, moldable_job_descriptions m
WHERE (j.state=\'Waiting\'
OR j.state=\'toLaunch\'
OR j.state=\'Running\'
OR j.state=\'Launching\'
OR j.state=\'Suspended\'
OR j.state=\'Resuming\'
OR j.state=\'Hold\'
OR j.state=\'toError\'
OR j.state=\'toFinish\'
OR j.state=\'toAckReservation\')
AND p.moldable_job_id = j.assigned_moldable_job
AND r.resource_id=p.resource_id
AND m.moldable_id = j.assigned_moldable_job
$clusterProperties
");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
$results{jobs}{$ref->{job_id}}{"state"} = $ref->{state};
$results{jobs}{$ref->{job_id}}{"queueName"} = $ref->{queue_name};
$results{jobs}{$ref->{job_id}}{"reservation"} = $ref->{reservation};
$results{jobs}{$ref->{job_id}}{"submissionTime"} = $ref->{submission_time};
$results{jobs}{$ref->{job_id}}{"user"} = $ref->{job_user};
#if (!defined($results{jobs_id}{$ref->{idJob}}{"weight"})){
if (!defined($results{jobs}{$ref->{job_id}}{"weight"})){
$results{jobs}{$ref->{job_id}}{"weight"} = 1;
}else{
$results{jobs}{$ref->{job_id}}{"weight"} ++;
}
$results{jobs}{$ref->{job_id}}{"startTime"} = $ref->{start_time};
## $results{jobs}{$ref->{job_id}}{"nbNodes"} = $ref->{nbNodes};
$results{jobs}{$ref->{job_id}}{"command"} = $ref->{command};
$results{jobs}{$ref->{job_id}}{"jobType"} = $ref->{job_type};
$results{jobs}{$ref->{job_id}}{"message"} = $ref->{message};
$results{jobs}{$ref->{job_id}}{"properties"} = $ref->{properties};
## $results{jobs}{$ref->{job_id}}{"maxTime"} = $ref->{maxTime};
## $results{jobs}{$ref->{job_id}}{"idFile"} = $ref->{idFile};
$results{jobs}{$ref->{job_id}}{"stopTime"} = $ref->{stop_time};
$results{jobs}{$ref->{job_id}}{"launchingDirectory"} = $ref->{launching_directory};
$results{jobs}{$ref->{job_id}}{"Walltime"} = $ref->{moldable_walltime};
$results{nodes}{$ref->{network_address}}{weight} ++;
$results{stats}{busyNodes} ++;
push(@{$results{jobs}{$ref->{job_id}}{hostnames}}, $ref->{network_address});
}
$sth->finish();
# Get stats informations
## $sth = $remoteDbh->prepare("SELECT COUNT(*) FROM nodes WHERE weight = 0 AND state = \"Alive\"");
## $sth->execute();
## while (my @ref = $sth->fetchrow_array()) {
## $results{stats}{freeNodes} = $ref[0];
## }
## $sth->finish();
# Count busy resources
#$sth = $remoteDbh->prepare("SELECT COUNT(DISTINCT(resources.resource_id))
# FROM resources,assigned_resources
# WHERE
# resources.state = \"Alive\"
# AND resources.resource_id = assigned_resources.resource_id
# AND assigned_resources.assigned_resource_index = 'CURRENT'
# $clusterProperties
# ");
#$sth->execute();
#while (my @ref = $sth->fetchrow_array()) {
# $results{stats}{busyNodes} = $ref[0];
#}
#$sth->finish();
# Count all resources
$clusterProperties=$clusters{$i}->{properties};
if (defined($clusterProperties) && $clusterProperties ne "") {
$sth = $remoteDbh->prepare("SELECT COUNT(DISTINCT(resource_id)) FROM resources WHERE $clusterProperties");
} else {
$sth = $remoteDbh->prepare("SELECT COUNT(DISTINCT(resource_id)) FROM resources");
}
$sth->execute();
while (my @ref = $sth->fetchrow_array()) {
$results{stats}{allNodes} = $ref[0];
if (defined($results{stats}{busyNodes})) {
$results{stats}{freeNodes} = $ref[0] - $results{stats}{busyNodes};
} else { $results{stats}{freeNodes} = $ref[0]; }
}
$sth->finish();
# Count not Alive resources and substract them from free resources
$clusterProperties=$clusters{$i}->{properties};
if (defined($clusterProperties) && $clusterProperties ne "")
{ $clusterProperties="AND $clusterProperties"; }
else
{ $clusterProperties=""; }
$sth = $remoteDbh->prepare("SELECT COUNT(DISTINCT(resource_id)) FROM resources where state != 'Alive' $clusterProperties");
$sth->execute();
while (my @ref = $sth->fetchrow_array()) {
$results{stats}{freeNodes} -= $ref[0];
}
$sth->finish();
}else{
# gantt queries
my ($date_start,$date_stop) = split(/,/,$gantt);
if ($backward_compatible) {
my %resNodeTmp = get_info_nodes($remoteDbh,$clusters{$i}->{properties});
$results{nodes} = \%resNodeTmp;
}
else {
my %resNodeTmp = get_info_resources($remoteDbh,$clusters{$i}->{properties});
$results{resources} = \%resNodeTmp;
}
# Add futur jobs
my %jobGantt = get_jobs_gantt_scheduled($remoteDbh,sql_to_local($date_start),sql_to_local($date_stop),$clusters{$i}->{properties});
$results{jobs} = \%jobGantt;
# Add finished or running jobs
#print "start : $date_start, stop: $date_stop\n";
my %jobs_history = get_jobs_range_dates($remoteDbh,sql_to_local($date_start),sql_to_local($date_stop),$clusters{$i}->{properties});
foreach my $i (keys(%jobs_history)){
my $types = get_current_job_types($remoteDbh,$i);
if (!defined($jobGantt{$i}) || (defined($types->{besteffort}))){
if (($jobs_history{$i}->{state} eq "Running") ||
($jobs_history{$i}->{state} eq "toLaunch") ||
($jobs_history{$i}->{state} eq "Suspended") ||
($jobs_history{$i}->{state} eq "Resuming") ||
($jobs_history{$i}->{state} eq "Launching")){
if (defined($types->{besteffort})){
$jobs_history{$i}->{stop_time} = get_gantt_visu_date($remoteDbh);
}else{
#This job must be already printed by gantt
next;
}
}
$results{jobs}{$i} = $jobs_history{$i};
}
}
# Add Absent, Down and Suspected nodes
my %deadNodeDates = get_resource_dead_range_date($remoteDbh,sql_to_local($date_start),sql_to_local($date_stop));
$results{dead_nodes} = \%deadNodeDates;
}
oargrid_lib::disconnect($remoteDbh);
my $localDbh = oargrid_lib::connect($DB_SERVER,$DB_BASE_NAME,$DB_BASE_LOGIN,$DB_BASE_PASSWD);
# Look at if the job is a grid reservation
foreach my $j (keys(%{$results{jobs}})){
my $localSth = $localDbh->prepare(" SELECT clusterJobsReservationId
FROM clusterJobs,clusters
WHERE ( clusterJobsClusterName = \'$i\' OR clusterJobsClusterName = parent )
AND clusterJobsBatchId = $j
AND clusters.clusterName = \'$i\'
");
$localSth->execute();
my @res = $localSth->fetchrow_array();
if (@res){
$results{jobs}{$j}{"gridReservation"} = $res[0];
}else{
$results{jobs}{$j}{"gridReservation"} = 0;
}
$localSth->finish();
}
oargrid_lib::disconnect($localDbh);
#alarm(0);
kill(9, $child);
print($P2 Dumper(\%results));
close($P2);
exit(0);
}
}
#Get informations from all childs
my %clusterProperties = oargrid_lib::get_cluster_properties($dbh);
my %monitorResults;
foreach my $i (@clustersToQuery){
my $reader = $pipeClusterList{$i};
my $str = <$reader>;
if (defined($str)){
my $resultHash = eval($str);
$monitorResults{$i} = $resultHash;
#Add cluster informations
foreach my $p (keys(%{$clusterProperties{$i}})){
$monitorResults{$i}{info}{$p} = $clusterProperties{$i}{$p};
}
}else{
warn("/!\\ Retrieve informations problems from $i.\n");
}
close($reader);
delete($pipeClusterList{$i});
}
if(defined($XMLmode)){
if ($XMLenabled == 1){
print(XMLout(\%monitorResults));
}else{
warn("[ERROR] Cannot load XML module.\n");
$exit_value = 5;
}
}elsif(defined($YAMLmode)){
if ($YAMLenabled == 1){
print(YAML::Dump(\%monitorResults));
}else{
warn("[ERROR] Cannot load YAML module.\n");
$exit_value = 6;
}
}else{
print(Dumper(\%monitorResults));
}
# Print informations about 1 reservation
}elsif ((defined($ARGV[0])) && ($ARGV[0] =~ m/\d+/m)){
my %resaInfo = oargrid_lib::get_reservation_informations($dbh,$ARGV[0]);
if (defined($dumperMode)){
print(Dumper(\%resaInfo));
}elsif(defined($XMLmode)){
if ($XMLenabled == 1){
print(XMLout(\%resaInfo));
}else{
warn("[ERROR] Cannot load XML module.\n");
$exit_value = 5;
}
}elsif(defined($YAMLmode)){
if ($YAMLenabled == 1){
print(YAML::Dump(\%resaInfo));
}else{
warn("[ERROR] Cannot load YAML module.\n");
$exit_value = 6;
}
}else{
if (%resaInfo){
print_reservation(\%resaInfo,"");
}
}
# Print informations about all reservations
}else{
my %userInfo = oargrid_lib::get_user_informations($dbh,$lusr);
if (defined($dumperMode)){
print(Dumper(\%userInfo));
}elsif(defined($XMLmode)){
if ($XMLenabled == 1){
print(XMLout(\%userInfo));
}else{
warn("[ERROR] Cannot load XML module.\n");
$exit_value = 5;
}
}elsif(defined($YAMLmode)){
if ($YAMLenabled == 1){
print(YAML::Dump(\%userInfo));
}else{
warn("[ERROR] Cannot load YAML module.\n");
$exit_value = 6;
}
}else{
foreach my $i (sort({$a <=> $b} keys(%userInfo))){
print("Reservation # $i:\n");
print_reservation($userInfo{$i},"\t");
print("\n");
}
}
}
oargrid_lib::disconnect($dbh);
exit($exit_value);
###############
## Functions ##
###############
# Print help messages
sub usage(){
print <<EOU;
Usage oargridstat [reservation_number | -h | -l reservation_number [-X | -Y | -D] [-w [-p polling_time][-m max_polling_time]] [-c cluster_alias_name [-j job_batch_id]] | --monitor [-c cluster_alias_name]* | --gantt "dateStart,dateStop" [-c cluster_alias_name]* | --list_clusters | --list_aliases | -V [ --backward_compatible ]
-h show this help message and exit
-l list all current nodes of the given reservation number
-D gives the results in Dumper format
-X gives the results in an XML format
-Y gives the results in an YAML format
-c cluster alias name
-j job batch id
-w wait for jobs on clusters to be in Running state
-p set the polling time : time in second between each job state check (default 5)
-m maximum of polling time in second (default 3600)
--monitor gives informations about current jobs on clusters (you can specify them with several -c options). The Result is a hash in Dumper mode by default
--gantt gives informations about jobs on clusters (you can specify them with several -c options). The Result is a hash in Dumper mode by default (date exemple : "2005-05-09 15:00:00,2005-05-10 15:00:00")
--list_clusters print a hash table in Dumper format by default with all clusters registred and there properties
--list_aliases print a hash table in Dumper format by default with all aliases registred and some properties
--backward_compatible prints output in the form of OAR1 version
-V print oargrid version and exit
EOU
}
sub print_reservation($$){
my $resa = shift;
my $prefixStr = shift;
print($prefixStr."submission date : $resa->{reservationSubmissionDate}\n");
print($prefixStr."start date : $resa->{reservationStartDate}\n");
print($prefixStr."walltime : $resa->{reservationWallTime}\n");
print($prefixStr."program : $resa->{reservationProgram}\n");
print($prefixStr."directory : $resa->{reservationDirectory}\n");
print($prefixStr."user : $resa->{reservationUser}\n");
print($prefixStr."cmd : $resa->{reservationCommandLine}\n");
print($prefixStr."clusters with job id:\n");
foreach my $i (keys(%{$resa->{clusterJobs}})){
foreach my $j (values(%{$resa->{clusterJobs}->{$i}})){
#print($prefixStr."\t$i --> $j->{batchId} (name = \"$j->{name}\", nbNodes = $j->{nodes}, cpu = $j->{weight}, properties = \"$j->{properties}\", queue = $j->{queue}, environment = \"$j->{env}\", partition = \"$j->{part}\")\n");
#print($prefixStr."\t$i --> $j->{batchId} (name = \"$j->{name}\", properties = \"$j->{properties}\", queue = $j->{queue}, environment = \"$j->{env}\", partition = \"$j->{part}\",status = \"$j->{status}\")\n");
print($prefixStr."\t$i --> $j->{batchId} (name = \"$j->{name}\", resources = \"$j->{rdef}\", properties = \"$j->{properties}\", queue = $j->{queue}, environment = \"$j->{env}\", partition = \"$j->{part}\")\n");
}
}
}
#Get informations about all nodes and return a hash
sub get_info_nodes($$){
my $dbh = shift;
my $properties = shift;
my $sth;
# Get node informations
if (defined($properties) && $properties ne "") {
$sth = $dbh->prepare("SELECT * FROM resources WHERE $properties");
} else {
$sth = $dbh->prepare("SELECT * FROM resources");
}
$sth->execute();
my %res;
while (my $ref = $sth->fetchrow_hashref()) {
$res{$ref->{network_address}}{"state"} = $ref->{state};
$res{$ref->{network_address}}{"maxWeight"} += 1;
$res{$ref->{network_address}}{"weight"} = 0;
}
$sth->finish();
return(%res);
}
#Get informations about all resources and return a hash
sub get_info_resources($$){
my $dbh = shift;
my $properties = shift;
my $sth;
# Get node informations
if (defined($properties) && $properties ne "") {
$sth = $dbh->prepare("SELECT * FROM resources WHERE $properties");
} else {
$sth = $dbh->prepare("SELECT * FROM resources");
}
$sth->execute();
my %res;
while ( my $ref = $sth->fetchrow_hashref() )
{
foreach my $key (keys %$ref) {
$res{$ref->{resource_id}}{$key}=$ref->{$key};
}
}
$sth->finish();
return(%res);
}
# get all jobs in a range of date in the gantt
# args : base, start range, end range
sub get_jobs_gantt_scheduled($$$$){
my $dbh = shift;
my $date_start = shift;
my $date_end = shift;
my $properties = shift;
if (defined($properties) && $properties ne "") {
$properties = "AND $properties";
} else { $properties = "" }
my $req =
"SELECT jobs.job_id,jobs.job_type,jobs.state,jobs.job_user,jobs.command,jobs.queue_name,moldable_job_descriptions.moldable_walltime,jobs.properties,jobs.launching_directory,jobs.submission_time,gantt_jobs_predictions_visu.start_time,(gantt_jobs_predictions_visu.start_time + moldable_job_descriptions.moldable_walltime),gantt_jobs_resources_visu.resource_id, resources.network_address
FROM jobs, moldable_job_descriptions, gantt_jobs_resources_visu, gantt_jobs_predictions_visu, resources
WHERE
gantt_jobs_predictions_visu.moldable_job_id = gantt_jobs_resources_visu.moldable_job_id AND
gantt_jobs_predictions_visu.moldable_job_id = moldable_job_descriptions.moldable_id AND
jobs.job_id = moldable_job_descriptions.moldable_job_id AND
gantt_jobs_predictions_visu.start_time < $date_end AND
resources.resource_id = gantt_jobs_resources_visu.resource_id AND
gantt_jobs_predictions_visu.start_time + moldable_job_descriptions.moldable_walltime >= $date_start
$properties
ORDER BY jobs.job_id";
my $sth = $dbh->prepare($req);
$sth->execute();
my %results;
while (my @ref = $sth->fetchrow_array()) {
if (!defined($results{$ref[0]})){
$results{$ref[0]} = {
'job_type' => $ref[1],
'state' => $ref[2],
'user' => $ref[3],
'command' => $ref[4],
'queue_name' => $ref[5],
'walltime' => $ref[6],
'properties' => $ref[7],
'launching_directory' => $ref[8],
'submission_time' => $ref[9],
'start_time' => $ref[10],
'stop_time' => $ref[11],
'resources' => [ $ref[12] ],
}
}else{
push(@{$results{$ref[0]}->{resources}}, $ref[12]);
}
}
$sth->finish();
return %results;
}
# get all jobs in a range of date
# args : base, start range, end range
sub get_jobs_range_dates($$$$){
my $dbh = shift;
my $date_start = shift;
my $date_end = shift;
my $properties = shift;
if (defined($properties) && $properties ne "") {
$properties = "AND $properties";
} else { $properties = "" }
my $req =
"SELECT jobs.job_id,jobs.job_type,jobs.state,jobs.job_user,jobs.command,jobs.queue_name,moldable_job_descriptions.moldable_walltime,jobs.properties,jobs.launching_directory,jobs.submission_time,jobs.start_time,jobs.stop_time,assigned_resources.resource_id,resources.network_address,(jobs.start_time + moldable_job_descriptions.moldable_walltime)
FROM jobs, assigned_resources, moldable_job_descriptions, resources
WHERE
(
jobs.stop_time >= $date_start OR
(
jobs.stop_time = \'0\' AND
(jobs.state = \'Running\' OR
jobs.state = \'Suspended\' OR
jobs.state = \'Resuming\')
)
) AND
jobs.start_time < $date_end AND
jobs.assigned_moldable_job = assigned_resources.moldable_job_id AND
moldable_job_descriptions.moldable_job_id = jobs.job_id AND
resources.resource_id = assigned_resources.resource_id
$properties
ORDER BY jobs.job_id";
my $sth = $dbh->prepare($req);
$sth->execute();
my %results;
while (my @ref = $sth->fetchrow_array()) {
if (!defined($results{$ref[0]})){
$results{$ref[0]} = {
'job_type' => $ref[1],
'state' => $ref[2],
'user' => $ref[3],
'command' => $ref[4],
'queue_name' => $ref[5],
'walltime' => $ref[6],
'properties' => $ref[7],
'launching_directory' => $ref[8],
'submission_time' => $ref[9],
'start_time' => $ref[10],
'stop_time' => $ref[11],
'resources' => [ $ref[12] ],
'limit_stop_time' => $ref[14]
}
}else{
push(@{$results{$ref[0]}->{resources}}, $ref[12]);
}
}
$sth->finish();
return %results;
}
#get the range when nodes are dead between two dates
# arg : base, start date, end date
sub get_resource_dead_range_date($$$){
my $dbh = shift;
my $date_start = shift;
my $date_end = shift;
# get dead nodes between two dates
my $req = "SELECT resource_id, date_start, date_stop, value
FROM resource_logs
WHERE
attribute = \'state\' AND
(
value = \'Absent\' OR
value = \'Dead\' OR
value = \'Suspected\'
) AND
date_start <= $date_end AND
(
date_stop = 0 OR
date_stop >= $date_start
)
";
my $sth = $dbh->prepare($req);
$sth->execute();
my %results;
while (my @ref = $sth->fetchrow_array()) {
my $interval_stopDate = $ref[2];
if (!defined($interval_stopDate)){
$interval_stopDate = $date_end;
}
push(@{$results{$ref[0]}}, [$ref[1],$interval_stopDate,$ref[3]]);
}
$sth->finish();
return(%results);
}
# get_current_job_types
# return a hash table with all types for the given job ID
sub get_current_job_types($$){
my $dbh = shift;
my $jobId = shift;
my $sth = $dbh->prepare(" SELECT type
FROM job_types
WHERE
types_index = \'CURRENT\'
AND job_id = $jobId
");
$sth->execute();
my %res;
while (my $ref = $sth->fetchrow_hashref()) {
if ($ref->{type} =~ m/^\s*(\w+)\s*=\s*(.+)$/m){
$res{$1} = $2;
}else{
$res{$ref->{type}} = "true";
}
}
$sth->finish();
return(\%res);
}
# Return date of the gantt for visu
sub get_gantt_visu_date($){
my $dbh = shift;
my $sth = $dbh->prepare("SELECT start_time
FROM gantt_jobs_predictions_visu
WHERE
moldable_job_id = 0
");
$sth->execute();
my @res = $sth->fetchrow_array();
$sth->finish();
return($res[0]);
}
# sql_to_local
# converts a date specified in the format used by the sql database to an
# integer local time format
# parameters : date string
# return value : date integer
# side effects : /
sub sql_to_local($) {