/
ua_status.cc
1755 lines (1607 loc) · 52.7 KB
/
ua_status.cc
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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2001-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2019 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Kern Sibbald, August MMI
*/
/**
* @file
* User Agent Status Command
*/
#include "include/bareos.h"
#include "dird.h"
#include "dird/jcr_private.h"
#include "dird/dird_globals.h"
#include "dird/fd_cmds.h"
#include "dird/job.h"
#include "dird/ndmp_dma_generic.h"
#include "dird/next_vol.h"
#include "dird/sd_cmds.h"
#include "dird/scheduler.h"
#include "dird/storage.h"
#include "cats/sql_pooling.h"
#include "dird/ua_db.h"
#include "dird/ua_output.h"
#include "dird/ua_select.h"
#include "dird/ua_status.h"
#include "include/auth_protocol_types.h"
#include "lib/edit.h"
#include "lib/recent_job_results_list.h"
#include "lib/parse_conf.h"
#include "lib/util.h"
#define DEFAULT_STATUS_SCHED_DAYS 7
namespace directordaemon {
static void ListScheduledJobs(UaContext* ua);
static void ListRunningJobs(UaContext* ua);
static void ListTerminatedJobs(UaContext* ua);
static void ListConnectedClients(UaContext* ua);
static void DoDirectorStatus(UaContext* ua);
static void DoSchedulerStatus(UaContext* ua);
static bool DoSubscriptionStatus(UaContext* ua);
static void DoAllStatus(UaContext* ua);
static void StatusSlots(UaContext* ua, StorageResource* store);
static void StatusContentApi(UaContext* ua, StorageResource* store);
static void StatusContentJson(UaContext* ua, StorageResource* store);
static char OKdotstatus[] = "1000 OK .status\n";
static char DotStatusJob[] = "JobId=%s JobStatus=%c JobErrors=%d\n";
static void ClientStatus(UaContext* ua, ClientResource* client, char* cmd)
{
switch (client->Protocol) {
case APT_NATIVE:
DoNativeClientStatus(ua, client, cmd);
break;
case APT_NDMPV2:
case APT_NDMPV3:
case APT_NDMPV4:
DoNdmpClientStatus(ua, client, cmd);
break;
default:
break;
}
}
/**
* .status command
*/
bool DotStatusCmd(UaContext* ua, const char* cmd)
{
StorageResource* store;
ClientResource* client;
JobControlRecord* njcr = NULL;
char ed1[50];
char* statuscmd = NULL;
Dmsg2(20, "status=\"%s\" argc=%d\n", cmd, ua->argc);
if (ua->argc < 2) {
ua->SendMsg("1900 Bad .status command, missing arguments.\n");
return false;
}
if (Bstrcasecmp(ua->argk[1], "dir")) {
if (Bstrcasecmp(ua->argk[2], "current")) {
ua->SendMsg(OKdotstatus, ua->argk[2]);
foreach_jcr (njcr) {
if (njcr->JobId != 0 &&
ua->AclAccessOk(Job_ACL, njcr->impl->res.job->resource_name_)) {
ua->SendMsg(DotStatusJob, edit_int64(njcr->JobId, ed1),
njcr->JobStatus, njcr->JobErrors);
}
}
endeach_jcr(njcr);
} else if (Bstrcasecmp(ua->argk[2], "last")) {
ua->SendMsg(OKdotstatus, ua->argk[2]);
if (RecentJobResultsList::Count() > 0) {
RecentJobResultsList::JobResult job =
RecentJobResultsList::GetMostRecentJobResult();
if (ua->AclAccessOk(Job_ACL, job.Job)) {
ua->SendMsg(DotStatusJob, edit_int64(job.JobId, ed1), job.JobStatus,
job.Errors);
}
}
} else if (Bstrcasecmp(ua->argk[2], "header")) {
ListDirStatusHeader(ua);
} else if (Bstrcasecmp(ua->argk[2], "scheduled")) {
ListScheduledJobs(ua);
} else if (Bstrcasecmp(ua->argk[2], "running")) {
ListRunningJobs(ua);
} else if (Bstrcasecmp(ua->argk[2], "terminated")) {
ListTerminatedJobs(ua);
} else {
ua->SendMsg("1900 Bad .status command, wrong argument.\n");
return false;
}
} else if (Bstrcasecmp(ua->argk[1], "client")) {
client = get_client_resource(ua);
if (client) {
if (ua->argc == 3) { statuscmd = ua->argk[2]; }
Dmsg2(200, "Client=%s arg=%s\n", client->resource_name_, NPRT(statuscmd));
ClientStatus(ua, client, statuscmd);
}
} else if (Bstrcasecmp(ua->argk[1], "storage")) {
store = get_storage_resource(ua);
if (store) {
if (ua->argc == 3) { statuscmd = ua->argk[2]; }
StorageStatus(ua, store, statuscmd);
}
} else {
ua->SendMsg("1900 Bad .status command, wrong argument.\n");
return false;
}
return true;
}
/**
* status command
*/
bool StatusCmd(UaContext* ua, const char* cmd)
{
StorageResource* store;
ClientResource* client;
int item, i;
bool autochangers_only;
Dmsg1(20, "status:%s:\n", cmd);
for (i = 1; i < ua->argc; i++) {
if (Bstrcasecmp(ua->argk[i], NT_("all"))) {
DoAllStatus(ua);
return true;
} else if (bstrncasecmp(ua->argk[i], NT_("dir"), 3)) {
DoDirectorStatus(ua);
return true;
} else if (Bstrcasecmp(ua->argk[i], NT_("client"))) {
client = get_client_resource(ua);
if (client) { ClientStatus(ua, client, NULL); }
return true;
} else if (bstrncasecmp(ua->argk[i], NT_("sched"), 5)) {
DoSchedulerStatus(ua);
return true;
} else if (bstrncasecmp(ua->argk[i], NT_("sub"), 3)) {
if (DoSubscriptionStatus(ua)) {
return true;
} else {
return false;
}
} else {
/*
* limit storages to autochangers if slots is given
*/
autochangers_only = (FindArg(ua, NT_("slots")) > 0);
store = get_storage_resource(ua, false, autochangers_only);
if (store) {
if (FindArg(ua, NT_("slots")) > 0) {
switch (ua->api) {
case API_MODE_OFF:
StatusSlots(ua, store);
break;
case API_MODE_ON:
StatusContentApi(ua, store);
break;
case API_MODE_JSON:
StatusContentJson(ua, store);
break;
}
} else {
StorageStatus(ua, store, NULL);
}
}
return true;
}
}
/* If no args, ask for status type */
if (ua->argc == 1) {
char prmt[MAX_NAME_LENGTH];
StartPrompt(ua, _("Status available for:\n"));
AddPrompt(ua, NT_("Director"));
AddPrompt(ua, NT_("Storage"));
AddPrompt(ua, NT_("Client"));
AddPrompt(ua, NT_("Scheduler"));
AddPrompt(ua, NT_("All"));
Dmsg0(20, "DoPrompt: select daemon\n");
if ((item = DoPrompt(ua, "", _("Select daemon type for status"), prmt,
sizeof(prmt))) < 0) {
return true;
}
Dmsg1(20, "item=%d\n", item);
switch (item) {
case 0: /* Director */
DoDirectorStatus(ua);
break;
case 1:
store = select_storage_resource(ua);
if (store) { StorageStatus(ua, store, NULL); }
break;
case 2:
client = select_client_resource(ua);
if (client) { ClientStatus(ua, client, NULL); }
break;
case 3:
DoSchedulerStatus(ua);
break;
case 4:
DoAllStatus(ua);
break;
default:
break;
}
}
return true;
}
static void DoAllStatus(UaContext* ua)
{
StorageResource *store, **unique_store;
ClientResource *client, **unique_client;
int i, j;
bool found;
int32_t previous_JobStatus = 0;
DoDirectorStatus(ua);
/* Count Storage items */
LockRes(my_config);
i = 0;
foreach_res (store, R_STORAGE) {
i++;
}
unique_store = (StorageResource**)malloc(i * sizeof(StorageResource));
/* Find Unique Storage address/port */
i = 0;
foreach_res (store, R_STORAGE) {
found = false;
if (!ua->AclAccessOk(Storage_ACL, store->resource_name_)) { continue; }
for (j = 0; j < i; j++) {
if (bstrcmp(unique_store[j]->address, store->address) &&
unique_store[j]->SDport == store->SDport) {
found = true;
break;
}
}
if (!found) {
unique_store[i++] = store;
Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
}
}
UnlockRes(my_config);
previous_JobStatus = ua->jcr->JobStatus;
/* Call each unique Storage daemon */
for (j = 0; j < i; j++) {
StorageStatus(ua, unique_store[j], NULL);
ua->jcr->JobStatus = previous_JobStatus;
}
free(unique_store);
/* Count Client items */
LockRes(my_config);
i = 0;
foreach_res (client, R_CLIENT) {
i++;
}
unique_client = (ClientResource**)malloc(i * sizeof(ClientResource));
/* Find Unique Client address/port */
i = 0;
foreach_res (client, R_CLIENT) {
found = false;
if (!ua->AclAccessOk(Client_ACL, client->resource_name_)) { continue; }
for (j = 0; j < i; j++) {
if (bstrcmp(unique_client[j]->address, client->address) &&
unique_client[j]->FDport == client->FDport) {
found = true;
break;
}
}
if (!found) {
unique_client[i++] = client;
Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
}
}
UnlockRes(my_config);
previous_JobStatus = ua->jcr->JobStatus;
/* Call each unique File daemon */
for (j = 0; j < i; j++) {
ClientStatus(ua, unique_client[j], NULL);
ua->jcr->JobStatus = previous_JobStatus;
}
free(unique_client);
}
void ListDirStatusHeader(UaContext* ua)
{
int len, cnt;
CatalogResource* catalog;
char dt[MAX_TIME_LENGTH];
PoolMem msg(PM_FNAME), dbdrivers(PM_FNAME);
cnt = 0;
foreach_res (catalog, R_CATALOG) {
if (cnt) { dbdrivers.strcat(" "); }
dbdrivers.strcat(catalog->db_driver);
cnt++;
}
ua->SendMsg(_("%s Version: %s (%s) %s %s %s\n"), my_name, VERSION, BDATE,
HOST_OS, DISTNAME, DISTVER);
bstrftime_nc(dt, sizeof(dt), daemon_start_time);
ua->SendMsg(
_("Daemon started %s. Jobs: run=%d, running=%d db:%s, %s binary\n"), dt,
num_jobs_run, JobCount(), dbdrivers.c_str(), BAREOS_BINARY_INFO);
if (me->secure_erase_cmdline) {
ua->SendMsg(_(" secure erase command='%s'\n"), me->secure_erase_cmdline);
}
len = ListDirPlugins(msg);
if (len > 0) { ua->SendMsg("%s\n", msg.c_str()); }
}
static bool show_scheduled_preview(UaContext* ua,
ScheduleResource* sched,
PoolMem& overview,
int* max_date_len,
struct tm tm,
time_t time_to_check)
{
int date_len, hour, mday, wday, month, wom, woy, yday;
bool is_last_week = false; /* Are we in the last week of a month? */
char dt[MAX_TIME_LENGTH];
time_t runtime;
RunResource* run;
PoolMem temp(PM_NAME);
hour = tm.tm_hour;
mday = tm.tm_mday - 1;
wday = tm.tm_wday;
month = tm.tm_mon;
wom = mday / 7;
woy = TmWoy(time_to_check); /* Get week of year */
yday = tm.tm_yday; /* Get day of year */
is_last_week = IsDoyInLastWeek(tm.tm_year + 1900, yday);
for (run = sched->run; run; run = run->next) {
bool run_now;
int cnt = 0;
run_now = BitIsSet(hour, run->hour) && BitIsSet(mday, run->mday) &&
BitIsSet(wday, run->wday) && BitIsSet(month, run->month) &&
(BitIsSet(wom, run->wom) || (run->last_set && is_last_week)) &&
BitIsSet(woy, run->woy);
if (run_now) {
/*
* Find time (time_t) job is to be run
*/
Blocaltime(&time_to_check, &tm); /* Reset tm structure */
tm.tm_min = run->minute; /* Set run minute */
tm.tm_sec = 0; /* Zero secs */
/*
* Convert the time into a user parsable string.
* As we use locale specific strings for weekday and month we
* need to keep track of the longest data string used.
*/
runtime = mktime(&tm);
bstrftime_wd(dt, sizeof(dt), runtime);
date_len = strlen(dt);
if (date_len > *max_date_len) {
if (*max_date_len == 0) {
/*
* When the datelen changes during the loop the locale generates a
* date string that is variable. Only thing we can do about that is
* start from scratch again. We invoke this by return false from this
* function.
*/
*max_date_len = date_len;
PmStrcpy(overview, "");
return false;
} else {
/*
* This is the first determined length we use this until we are proven
* wrong.
*/
*max_date_len = date_len;
}
}
Mmsg(temp, "%-*s %-22.22s ", *max_date_len, dt, sched->resource_name_);
PmStrcat(overview, temp.c_str());
if (run->level) {
if (cnt++ > 0) { PmStrcat(overview, " "); }
Mmsg(temp, "Level=%s", JobLevelToString(run->level));
PmStrcat(overview, temp.c_str());
}
if (run->Priority) {
if (cnt++ > 0) { PmStrcat(overview, " "); }
Mmsg(temp, "Priority=%d", run->Priority);
PmStrcat(overview, temp.c_str());
}
if (run->spool_data_set) {
if (cnt++ > 0) { PmStrcat(overview, " "); }
Mmsg(temp, "Spool Data=%d", run->spool_data);
PmStrcat(overview, temp.c_str());
}
if (run->accurate_set) {
if (cnt++ > 0) { PmStrcat(overview, " "); }
Mmsg(temp, "Accurate=%d", run->accurate);
PmStrcat(overview, temp.c_str());
}
if (run->pool) {
if (cnt++ > 0) { PmStrcat(overview, " "); }
Mmsg(temp, "Pool=%s", run->pool->resource_name_);
PmStrcat(overview, temp.c_str());
}
if (run->storage) {
if (cnt++ > 0) { PmStrcat(overview, " "); }
Mmsg(temp, "Storage=%s", run->storage->resource_name_);
PmStrcat(overview, temp.c_str());
}
if (run->msgs) {
if (cnt++ > 0) { PmStrcat(overview, " "); }
Mmsg(temp, "Messages=%s", run->msgs->resource_name_);
PmStrcat(overview, temp.c_str());
}
PmStrcat(overview, "\n");
}
}
/*
* If we make it till here the length of the datefield is constant or didn't
* change.
*/
return true;
}
/**
* Check the number of clients in the DB against the configured number of
* subscriptions
*
* Return true if (number of clients < number of subscriptions), else
* return false
*/
static bool DoSubscriptionStatus(UaContext* ua)
{
int available;
bool retval = false;
/*
* See if we need to check.
*/
if (me->subscriptions == 0) {
ua->SendMsg(_("No subscriptions configured in director.\n"));
retval = true;
goto bail_out;
}
if (me->subscriptions_used <= 0) {
ua->ErrorMsg(_("No clients defined.\n"));
goto bail_out;
} else {
available = me->subscriptions - me->subscriptions_used;
if (available < 0) {
ua->SendMsg(
_("Warning! No available subscriptions: %d (%d/%d) (used/total)\n"),
available, me->subscriptions_used, me->subscriptions);
} else {
ua->SendMsg(_("Ok: available subscriptions: %d (%d/%d) (used/total)\n"),
available, me->subscriptions_used, me->subscriptions);
retval = true;
}
}
bail_out:
return retval;
}
static void DoSchedulerStatus(UaContext* ua)
{
int i;
int max_date_len = 0;
int days = DEFAULT_STATUS_SCHED_DAYS; /* Default days for preview */
bool schedulegiven = false;
time_t time_to_check, now, start, stop;
char schedulename[MAX_NAME_LENGTH];
const int seconds_per_day = 86400; /* Number of seconds in one day */
const int seconds_per_hour = 3600; /* Number of seconds in one hour */
struct tm tm;
ClientResource* client = NULL;
JobResource* job = NULL;
ScheduleResource* sched;
PoolMem overview(PM_MESSAGE);
now = time(NULL); /* Initialize to now */
time_to_check = now;
i = FindArgWithValue(ua, NT_("days"));
if (i >= 0) {
days = atoi(ua->argv[i]);
if (((days < -366) || (days > 366)) && !ua->api) {
ua->SendMsg(_(
"Ignoring invalid value for days. Allowed is -366 < days < 366.\n"));
days = DEFAULT_STATUS_SCHED_DAYS;
}
}
/*
* Schedule given ?
*/
i = FindArgWithValue(ua, NT_("schedule"));
if (i >= 0) {
bstrncpy(schedulename, ua->argv[i], sizeof(schedulename));
schedulegiven = true;
}
/*
* Client given ?
*/
i = FindArgWithValue(ua, NT_("client"));
if (i >= 0) { client = get_client_resource(ua); }
/*
* Jobname given ?
*/
i = FindArgWithValue(ua, NT_("job"));
if (i >= 0) {
job = ua->GetJobResWithName(ua->argv[i]);
/*
* If a bogus jobname was given ask for it interactively.
*/
if (!job) { job = select_job_resource(ua); }
}
ua->SendMsg("Scheduler Jobs:\n\n");
ua->SendMsg("Schedule Jobs Triggered\n");
ua->SendMsg("===========================================================\n");
LockRes(my_config);
foreach_res (sched, R_SCHEDULE) {
int cnt = 0;
if (!schedulegiven && !sched->enabled) { continue; }
if (!ua->AclAccessOk(Schedule_ACL, sched->resource_name_)) { continue; }
if (schedulegiven) {
if (!bstrcmp(sched->resource_name_, schedulename)) { continue; }
}
if (job) {
if (job->schedule &&
bstrcmp(sched->resource_name_, job->schedule->resource_name_)) {
if (cnt++ == 0) { ua->SendMsg("%s\n", sched->resource_name_); }
ua->SendMsg(" %s\n", job->resource_name_);
}
} else {
foreach_res (job, R_JOB) {
if (!ua->AclAccessOk(Job_ACL, job->resource_name_)) { continue; }
if (client && job->client != client) { continue; }
if (job->schedule &&
bstrcmp(sched->resource_name_, job->schedule->resource_name_)) {
if (cnt++ == 0) { ua->SendMsg("%s\n", sched->resource_name_); }
if (job->enabled && (!job->client || job->client->enabled)) {
ua->SendMsg(" %s\n", job->resource_name_);
} else {
ua->SendMsg(" %s (disabled)\n",
job->resource_name_);
}
}
}
}
if (cnt > 0) { ua->SendMsg("\n"); }
}
UnlockRes(my_config);
/*
* Build an overview.
*/
if (days > 0) { /* future */
start = now;
stop = now + (days * seconds_per_day);
} else { /* past */
start = now + (days * seconds_per_day);
stop = now;
}
start_again:
time_to_check = start;
while (time_to_check < stop) {
Blocaltime(&time_to_check, &tm);
if (client || job) {
/*
* List specific schedule.
*/
if (job) {
if (job->schedule) {
if (!show_scheduled_preview(ua, job->schedule, overview,
&max_date_len, tm, time_to_check)) {
goto start_again;
}
}
} else {
LockRes(my_config);
foreach_res (job, R_JOB) {
if (!ua->AclAccessOk(Job_ACL, job->resource_name_)) { continue; }
if (job->schedule && job->client == client) {
if (!show_scheduled_preview(ua, job->schedule, overview,
&max_date_len, tm, time_to_check)) {
job = NULL;
UnlockRes(my_config);
goto start_again;
}
}
}
UnlockRes(my_config);
job = NULL;
}
} else {
/*
* List all schedules.
*/
LockRes(my_config);
foreach_res (sched, R_SCHEDULE) {
if (!schedulegiven && !sched->enabled) { continue; }
if (!ua->AclAccessOk(Schedule_ACL, sched->resource_name_)) { continue; }
if (schedulegiven) {
if (!bstrcmp(sched->resource_name_, schedulename)) { continue; }
}
if (!show_scheduled_preview(ua, sched, overview, &max_date_len, tm,
time_to_check)) {
UnlockRes(my_config);
goto start_again;
}
}
UnlockRes(my_config);
}
time_to_check += seconds_per_hour; /* next hour */
}
ua->SendMsg("====\n\n");
ua->SendMsg("Scheduler Preview for %d days:\n\n", days);
ua->SendMsg("%-*s %-22s %s\n", max_date_len, _("Date"), _("Schedule"),
_("Overrides"));
ua->SendMsg(
"==============================================================\n");
ua->SendMsg(overview.c_str());
ua->SendMsg("====\n");
}
static void DoDirectorStatus(UaContext* ua)
{
ListDirStatusHeader(ua);
/*
* List scheduled Jobs
*/
ListScheduledJobs(ua);
/*
* List running jobs
*/
ListRunningJobs(ua);
/*
* List terminated jobs
*/
ListTerminatedJobs(ua);
ListConnectedClients(ua);
ua->SendMsg("====\n");
}
static void PrtRunhdr(UaContext* ua)
{
if (!ua->api) {
ua->SendMsg(_("\nScheduled Jobs:\n"));
ua->SendMsg(
_("Level Type Pri Scheduled Name "
"Volume\n"));
ua->SendMsg(
_("===================================================================="
"===============\n"));
}
}
/* Scheduling packet */
struct sched_pkt {
dlink link; /* keep this as first item!!! */
JobResource* job;
int level;
int priority;
utime_t runtime;
PoolResource* pool;
StorageResource* store;
};
static void PrtRuntime(UaContext* ua, sched_pkt* sp)
{
char dt[MAX_TIME_LENGTH];
const char* level_ptr;
bool ok = false;
bool CloseDb = false;
JobControlRecord* jcr = ua->jcr;
MediaDbRecord mr;
int orig_jobtype;
orig_jobtype = jcr->getJobType();
if (sp->job->JobType == JT_BACKUP) {
jcr->db = NULL;
ok = CompleteJcrForJob(jcr, sp->job, sp->pool);
Dmsg1(250, "Using pool=%s\n", jcr->impl->res.pool->resource_name_);
if (jcr->db) { CloseDb = true; /* new db opened, remember to close it */ }
if (ok) {
mr.PoolId = jcr->impl->jr.PoolId;
jcr->impl->res.write_storage = sp->store;
SetStorageidInMr(jcr->impl->res.write_storage, &mr);
Dmsg0(250, "call FindNextVolumeForAppend\n");
/* no need to set ScratchPoolId, since we use fnv_no_create_vol */
ok = FindNextVolumeForAppend(jcr, &mr, 1, NULL, fnv_no_create_vol,
fnv_no_prune);
}
if (!ok) { bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName)); }
}
bstrftime_nc(dt, sizeof(dt), sp->runtime);
switch (sp->job->JobType) {
case JT_ADMIN:
case JT_ARCHIVE:
case JT_RESTORE:
level_ptr = " ";
break;
default:
level_ptr = JobLevelToString(sp->level);
break;
}
if (ua->api) {
ua->SendMsg(_("%-14s\t%-8s\t%3d\t%-18s\t%-18s\t%s\n"), level_ptr,
job_type_to_str(sp->job->JobType), sp->priority, dt,
sp->job->resource_name_, mr.VolumeName);
} else {
ua->SendMsg(_("%-14s %-8s %3d %-18s %-18s %s\n"), level_ptr,
job_type_to_str(sp->job->JobType), sp->priority, dt,
sp->job->resource_name_, mr.VolumeName);
}
if (CloseDb) { DbSqlClosePooledConnection(jcr, jcr->db); }
jcr->db = ua->db; /* restore ua db to jcr */
jcr->setJobType(orig_jobtype);
}
/**
* Sort items by runtime, priority
*/
static int CompareByRuntimePriority(void* item1, void* item2)
{
sched_pkt* p1 = (sched_pkt*)item1;
sched_pkt* p2 = (sched_pkt*)item2;
if (p1->runtime < p2->runtime) {
return -1;
} else if (p1->runtime > p2->runtime) {
return 1;
}
if (p1->priority < p2->priority) {
return -1;
} else if (p1->priority > p2->priority) {
return 1;
}
return 0;
}
/**
* Find all jobs to be run in roughly the next 24 hours.
*/
static void ListScheduledJobs(UaContext* ua)
{
utime_t runtime;
RunResource* run;
JobResource* job;
int level, num_jobs = 0;
int priority;
bool hdr_printed = false;
dlist sched;
sched_pkt* sp;
int days, i;
Dmsg0(200, "enter list_sched_jobs()\n");
days = 1;
i = FindArgWithValue(ua, NT_("days"));
if (i >= 0) {
days = atoi(ua->argv[i]);
if (((days < 0) || (days > 500)) && !ua->api) {
ua->SendMsg(_("Ignoring invalid value for days. Max is 500.\n"));
days = 1;
}
}
/*
* Loop through all jobs
*/
LockRes(my_config);
foreach_res (job, R_JOB) {
if (!ua->AclAccessOk(Job_ACL, job->resource_name_) || !job->enabled ||
(job->client && !job->client->enabled)) {
continue;
}
for (run = NULL; (run = find_next_run(run, job, runtime, days));) {
UnifiedStorageResource store;
level = job->JobLevel;
if (run->level) { level = run->level; }
priority = job->Priority;
if (run->Priority) { priority = run->Priority; }
if (!hdr_printed) {
PrtRunhdr(ua);
hdr_printed = true;
}
sp = (sched_pkt*)malloc(sizeof(sched_pkt));
sp->job = job;
sp->level = level;
sp->priority = priority;
sp->runtime = runtime;
sp->pool = run->pool;
GetJobStorage(&store, job, run);
sp->store = store.store;
if (sp->store) {
Dmsg3(250, "job=%s storage=%s MediaType=%s\n", job->resource_name_,
sp->store->resource_name_, sp->store->media_type);
} else {
Dmsg1(250, "job=%s could not get job storage\n", job->resource_name_);
}
sched.BinaryInsertMultiple(sp, CompareByRuntimePriority);
num_jobs++;
}
} /* end for loop over resources */
UnlockRes(my_config);
foreach_dlist (sp, &sched) {
PrtRuntime(ua, sp);
}
if (num_jobs == 0 && !ua->api) { ua->SendMsg(_("No Scheduled Jobs.\n")); }
if (!ua->api) ua->SendMsg("====\n");
Dmsg0(200, "Leave list_sched_jobs_runs()\n");
}
static void ListRunningJobs(UaContext* ua)
{
JobControlRecord* jcr;
int njobs = 0;
const char* msg;
char* emsg; /* edited message */
char dt[MAX_TIME_LENGTH];
char level[10];
bool pool_mem = false;
Dmsg0(200, "enter list_run_jobs()\n");
if (!ua->api) ua->SendMsg(_("\nRunning Jobs:\n"));
foreach_jcr (jcr) {
if (jcr->JobId == 0) { /* this is us */
/* this is a console or other control job. We only show console
* jobs in the status output.
*/
if (jcr->is_JobType(JT_CONSOLE) && !ua->api) {
bstrftime_nc(dt, sizeof(dt), jcr->start_time);
ua->SendMsg(_("Console connected at %s\n"), dt);
}
continue;
}
njobs++;
}
endeach_jcr(jcr);
if (njobs == 0) {
/* Note the following message is used in regress -- don't change */
if (!ua->api) ua->SendMsg(_("No Jobs running.\n====\n"));
Dmsg0(200, "leave list_run_jobs()\n");
return;
}
njobs = 0;
if (!ua->api) {
ua->SendMsg(_(" JobId Level Name Status\n"));
ua->SendMsg(
_("===================================================================="
"==\n"));
}
foreach_jcr (jcr) {
if (jcr->JobId == 0 ||
!ua->AclAccessOk(Job_ACL, jcr->impl->res.job->resource_name_)) {
continue;
}
njobs++;
switch (jcr->JobStatus) {
case JS_Created:
msg = _("is waiting execution");
break;
case JS_Running:
msg = _("is running");
break;
case JS_Blocked:
msg = _("is blocked");
break;
case JS_Terminated:
msg = _("has terminated");
break;
case JS_Warnings:
msg = _("has terminated with warnings");
break;
case JS_ErrorTerminated:
msg = _("has erred");
break;
case JS_Error:
msg = _("has errors");
break;
case JS_FatalError:
msg = _("has a fatal error");
break;
case JS_Differences:
msg = _("has verify differences");
break;
case JS_Canceled:
msg = _("has been canceled");
break;
case JS_WaitFD:
emsg = (char*)GetPoolMemory(PM_FNAME);
if (!jcr->impl->res.client) {
Mmsg(emsg, _("is waiting on Client"));
} else {
Mmsg(emsg, _("is waiting on Client %s"),
jcr->impl->res.client->resource_name_);
}
pool_mem = true;