-
Notifications
You must be signed in to change notification settings - Fork 0
/
urbstat.js
1301 lines (1211 loc) · 56.3 KB
/
urbstat.js
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
import { colors } from '@cliffy/ansi/colors';
import { Command, EnumType } from '@cliffy/command';
import { load } from '@std/dotenv';
import { Secret } from '@cliffy/prompt';
import { Table } from '@cliffy/table';
import { UrbackupServer } from 'urbackup-server-api';
import ms from 'ms';
/**
* Hard-coded configuration values used as a fallback when not found in config file.
*/
const configFallback = {
URBSTAT_SERVER_URL: { defaultValue: 'http://127.0.0.1:55414' },
URBSTAT_SERVER_USERNAME: { defaultValue: 'admin' },
URBSTAT_SERVER_PASSWORD: { defaultValue: '' },
URBSTAT_THRESHOLD_STALE_FILE: { defaultValue: 7200 },
URBSTAT_THRESHOLD_STALE_IMAGE: { defaultValue: 7200 },
URBSTAT_THRESHOLD_VOID_CLIENT: { defaultValue: 10080 },
URBSTAT_LOCALE: { defaultValue: 'en' },
URBSTAT_CLIENTS_FORMAT: { defaultValue: 'table', acceptedValues: ['table', 'list', 'number', 'raw'] },
URBSTAT_CLIENTS_SORT: { defaultValue: 'name', acceptedValues: ['name', 'seen', 'file', 'image'] },
URBSTAT_ACTIVITIES_FORMAT: { defaultValue: 'table', acceptedValues: ['table', 'number', 'raw'] },
URBSTAT_ACTIVITIES_SORT_CURRENT: { defaultValue: 'client', acceptedValues: ['client', 'eta', 'progress', 'size'] },
URBSTAT_ACTIVITIES_SORT_LAST: { defaultValue: 'time', acceptedValues: ['client', 'time', 'duration', 'size'] },
URBSTAT_USAGE_FORMAT: { defaultValue: 'table', acceptedValues: ['table', 'raw'] },
URBSTAT_USAGE_SORT: { defaultValue: 'total', acceptedValues: ['name', 'file', 'image', 'total'] },
URBSTAT_CLIENT_FORMAT: { defaultValue: 'table', acceptedValues: ['table', 'raw'] },
};
/**
* Common font and style definitions.
*/
const cliTheme = { error: colors.bold.red, warning: colors.yellow, information: colors.blue };
/**
* Configuration data loaded from the configuration file.
*/
const configData = await load({ envPath: './urbstat.conf', export: false });
// TODO: convert to iife?
/**
* Get the configuration value for the specified key.
* @param {string} key - The configuration key.
* @returns {*} The configuration value.
*/
const getConfigValue = function (key) {
if (key in configFallback) {
return configData[key] ?? configFallback[key].defaultValue;
} else {
return null;
}
};
// NOTE: Conversion is needed as UrBackup/Python uses seconds for timestamps whereas Javascript uses milliseconds.
/**
* The current epoch time in seconds.
*/
const currentEpochTime = Math.round(new Date().getTime() / 1000.0);
/**
* The status response from the server.
*/
let statusResponse;
/**
* The activities response from the server.
*/
let activitiesResponse;
/**
* The usage response from the server.
*/
let usageResponse;
/**
* The all-clients response from the server.
*/
let allClientsResponse;
/**
* The ok-clients response from the server.
*/
let okClientsResponse;
/**
* The failed-clients response from the server.
*/
let failedClientsResponse;
/**
* The stale-clients response from the server.
*/
let staleClientsResponse;
/**
* The blano-clients response from the server.
*/
let blankClientsResponse;
/**
* The void-clients response from the server.
*/
let voidClientsResponse;
/**
* The online-clients response from the server.
*/
let onlineClientsResponse;
/**
* The offline-clients response from the server.
*/
let offlineClientsResponse;
/**
* The active-clients response from the server.
*/
let activeClientsResponse;
/**
* Make the required API calls to the UrBackup Server.
* @param {string[]} requiredCalls - The required API calls.
* @param {Object} commandOptions - The command options.
* @returns {Promise<void>}
*/
async function makeServerCalls(requiredCalls, commandOptions) {
const username = commandOptions?.user?.length > 0 ? commandOptions.user : getConfigValue('URBSTAT_SERVER_USERNAME');
const password = commandOptions?.askPass === true
? await Secret.prompt('Enter password')
: getConfigValue('URBSTAT_SERVER_PASSWORD');
const server = new UrbackupServer({
url: getConfigValue('URBSTAT_SERVER_URL'),
username: username,
password: password,
});
try {
statusResponse = requiredCalls.includes('status')
? await server.getStatus({
includeRemoved: true,
clientId: commandOptions?.id,
clientName: commandOptions?.name,
})
: null;
activitiesResponse = requiredCalls.includes('activities')
? await server.getActivities({
includeCurrent: true,
includeLast: true,
includePaused: commandOptions?.skipPaused !== true,
clientName: commandOptions?.client?.length > 0 ? commandOptions.client : undefined,
})
: null;
usageResponse = requiredCalls.includes('usage') ? await server.getUsage() : null;
allClientsResponse = requiredCalls.includes('all-clients')
? await server.getClients({
includeRemoved: true,
})
: null;
okClientsResponse = requiredCalls.includes('ok-clients')
? await server.getOkClients({
includeRemoved: false,
includeFileBackups: commandOptions?.skipFile !== true,
includeImageBackups: commandOptions?.skipImage !== true,
failOnFileIssues: commandOptions?.strict,
})
: null;
failedClientsResponse = requiredCalls.includes('failed-clients')
? await server.getFailedClients({
includeRemoved: false,
includeBlank: commandOptions?.skipBlank !== true,
includeFileBackups: commandOptions?.skipFile !== true,
includeImageBackups: commandOptions?.skipImage !== true,
failOnFileIssues: commandOptions?.strict,
})
: null;
staleClientsResponse = requiredCalls.includes('stale-clients')
? await server.getClients({
includeRemoved: false,
})
: null;
blankClientsResponse = requiredCalls.includes('blank-clients')
? await server.getBlankClients({
includeRemoved: false,
includeFileBackups: commandOptions?.skipFile !== true,
includeImageBackups: commandOptions?.skipImage !== true,
})
: null;
voidClientsResponse = requiredCalls.includes('void-clients')
? await server.getUnseenClients({
includeRemoved: false,
includeBlank: commandOptions?.skipBlank !== true,
timeThreshold: commandOptions?.threshold,
})
: null;
onlineClientsResponse = requiredCalls.includes('online-clients')
? await server.getOnlineClients({
includeRemoved: false,
includeBlank: commandOptions?.skipBlank !== true,
})
: null;
offlineClientsResponse = requiredCalls.includes('offline-clients')
? await server.getOfflineClients({
includeRemoved: false,
includeBlank: commandOptions?.skipBlank !== true,
})
: null;
activeClientsResponse = requiredCalls.includes('active-clients')
? await server.getActiveClients({
includeRemoved: false,
})
: null;
} catch (e) {
console.error(cliTheme.error(e.message));
Deno.exit(1);
}
}
/**
* Normalize client object for further use in the application.
* @param {Object} client - The client object to normalize.
* @param {string} format - The format to normalize the client object.
* @returns {Object} The normalized client object.
*/
const normalizeClient = function (client, format) {
if (format === 'raw') {
return client;
} else {
return (function (
{
id,
name,
file_ok,
file_disabled,
last_filebackup_issues,
lastbackup,
image_ok,
image_disabled,
last_imagebackup_issues,
lastbackup_image,
online,
lastseen,
status,
},
) {
// TODO: file_disabled, last_imagebackup_issues ??
if (file_disabled === true) {
file_ok = 'disabled';
} else if (file_ok === true) {
file_ok = last_filebackup_issues === 0 ? 'ok' : 'issues';
} else {
// TODO: no recent === failed?
file_ok = 'failed';
}
if (image_disabled === true) {
image_ok = 'disabled';
} else if (image_ok === true) {
image_ok = last_imagebackup_issues === 0 ? 'ok' : 'issues';
} else {
// TODO: no recent === failed?
image_ok = 'failed';
}
return ({
'Client Id': id,
'Client Name': name,
'File BUP Status': file_ok,
'Last File BUP': lastbackup,
'Image BUP Status': image_ok,
'Last Image BUP': lastbackup_image,
Online: online === true ? 'yes' : 'no',
'Last Seen': lastseen,
Activity: status,
});
})(client);
}
};
/**
* Normalize activity object for further use in the application.
* @param {Object} activity - The activity object to normalize.
* @param {boolean} last - Flag indicating if it's the last activity.
* @param {string} format - The format to normalize the activity object.
* @returns {Object} The normalized activity object.
*/
const normalizeActivity = function (activity, last, format) {
if (format === 'raw') {
return activity;
}
if (last === true) {
return (function ({ clientid, name, id, duration, size_bytes, backuptime }) {
return ({
'Activity Id': id,
'Client Id': clientid,
'Client Name': name,
Duration: duration,
Size: activity.del === true ? size_bytes * -1 : size_bytes,
'Starting Time': backuptime,
});
})(activity);
} else {
return (function ({ clientid, name, action, paused, pcdone, queue, done_bytes, total_bytes, eta_ms }) {
return ({
'Client Id': clientid,
'Client Name': name,
Action: action,
Paused: paused === true ? 'yes' : 'no',
Progress: pcdone,
Queue: queue,
// TODO:
'Bytes Done': activity.del === true ? done_bytes * -1 : done_bytes,
Size: total_bytes,
ETA: eta_ms,
});
})(activity);
}
};
/**
* Normalize usage object for further use in the application.
* @param {Object} element - The usage object to normalize.
* @param {string} format - The format to normalize the usage object.
* @returns {Object} The normalized usage object.
*/
const normalizeUsage = function (element, format) {
if (format === 'raw') {
return element;
} else {
return (function ({ files, images, name, used }) {
return ({
'Client Name': name,
'File Backups': files,
'Image Backups': images,
'Total': used,
});
})(element);
}
};
/**
* Sort clients. This function sorts the elements of an array in place.
* NOTE: Sorting must be done after normalization.
* @param {Array} clients - The array of client objects to sort.
* @param {string} format - The format used for normalization.
* @param {string} order - The sorting order (name, seen, file, image).
* @param {boolean} reverse - Flag indicating whether to sort in reverse order.
*/
const sortClients = function (clients, format, order, reverse) {
switch (order) {
case 'name':
clients.sort((a, b) =>
a['Client Name'].localeCompare(b['Client Name'], getConfigValue('URBSTAT_LOCALE'), { sensitivity: 'base' })
);
break;
case 'seen':
clients.sort((a, b) => a['Last Seen'] - b['Last Seen']);
break;
case 'file':
clients.sort((a, b) => a['Last File BUP'] - b['Last File BUP']);
break;
case 'image':
clients.sort((a, b) => a['Last Image BUP'] - b['Last Image BUP']);
break;
}
if (reverse === true && format !== 'number') {
clients.reverse();
}
};
/**
* Sort activities. This function sorts the elements of an array in place.
* NOTE: Sorting must be done after normalization.
* @param {Array} activities - The array of activity objects to sort.
* @param {boolean} last - Flag indicating if it's the last activity.
* @param {string} format - The format used for normalization.
* @param {string} order - The sorting order (eta, progress, size, client, time, duration).
* @param {boolean} reverse - Flag indicating whether to sort in reverse order.
*/
const sortActivities = function (activities, last, format, order, reverse) {
switch (order) {
case 'eta':
activities.sort((a, b) => a.ETA - b.ETA);
break;
case 'progress':
activities.sort((a, b) => a.Progress - b.Progress);
break;
case 'size':
activities.sort((a, b) => a.Size - b.Size);
break;
case 'client':
activities.sort((a, b) =>
a['Client Name'].localeCompare(b['Client Name'], getConfigValue('URBSTAT_LOCALE'), { sensitivity: 'base' })
);
break;
case 'time':
activities.sort((a, b) => a['Starting Time'] - b['Starting Time']);
break;
case 'duration':
activities.sort((a, b) => a.Duration - b.Duration);
break;
}
if (reverse === true && format !== 'number') {
activities.reverse();
}
};
/**
* Sort usage. This function sorts the elements of an array in place.
* NOTE: Sorting must be done after normalization.
* @param {Array} usages - The array of usage objects to sort.
* @param {string} format - The format used for normalization.
* @param {string} order - The sorting order (name, file, image, total).
* @param {boolean} reverse - Flag indicating whether to sort in reverse order.
*/
const sortUsage = function (usages, format, order, reverse) {
switch (order) {
case 'name':
usages.sort((a, b) =>
a['Client Name'].localeCompare(b['Client Name'], getConfigValue('URBSTAT_LOCALE'), { sensitivity: 'base' })
);
break;
case 'file':
usages.sort((a, b) => a['File Backups'] - b['File Backups']);
break;
case 'image':
usages.sort((a, b) => a['Image Backups'] - b['Image Backups']);
break;
case 'total':
usages.sort((a, b) => a.Total - b.Total);
break;
}
if (reverse === true && format !== 'number') {
usages.reverse();
}
};
/**
* Print output based on the specified format.
* @param {Array} data - The data to print.
* @param {string} format - The output format (table, list, number, raw).
*/
const printOutput = function (data, format) {
/**
* Format bytes to human-readable string.
* @param {number} bytes - The number of bytes.
* @param {number} [decimals=2] - The number of decimal places to round to.
* @returns {string} The formatted string.
*/
const formatBytes = function (bytes, decimals = 2) {
if (bytes === 0) {
return '0 Bytes';
}
const kilo = 1024;
const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const unitIndex = Math.floor(Math.log(Math.abs(bytes)) / Math.log(kilo));
return parseFloat((bytes / Math.pow(kilo, unitIndex)).toFixed(decimals < 0 ? 0 : decimals)) +
' ' + units[unitIndex];
};
// TODO: || 'raw'?
if (format !== 'number') {
for (const element in data) {
Object.keys(data[element]).forEach(function (key) {
switch (key) {
case 'Bytes Done':
/* falls through */
case 'File Backups':
/* falls through */
case 'Image Backups':
/* falls through */
case 'Total':
/* falls through */
case 'Size':
data[element][key] = formatBytes(data[element][key], 2);
break;
case 'Duration':
data[element][key] = ms(data[element][key] * 1000);
break;
case 'ETA':
data[element][key] = data[element][key] <= 0 ? 'n/a' : ms(data[element][key]);
break;
case 'Starting Time':
/* falls through */
case 'Last File BUP':
/* falls through */
case 'Last Image BUP':
/* falls through */
case 'Last Seen':
if (data[element][key] === 0) {
data[element][key] = 'never';
} else {
data[element][key] = new Date(data[element][key] * 1000).toLocaleString(getConfigValue('URBSTAT_LOCALE'));
}
break;
case 'Activity':
if (data[element][key] === 0) {
data[element][key] = 'none';
}
break;
case 'Progress':
data[element][key] = `${data[element][key]}%`;
break;
}
});
}
}
switch (format) {
case 'table':
if (data.length > 0) {
const table = new Table().padding(2).border(true).maxColWidth(11);
table.header(Object.keys(data[0]));
for (const element of data) {
table.push(Object.values(element));
}
table.render();
}
break;
case 'list':
if (data.length > 0) {
console.log(data);
}
break;
case 'number':
console.log(data.length);
break;
case 'raw':
console.log(data);
break;
}
};
/**
* Process matching data i.e. normalize, sort and limit. This function changes elements of an array in place.
*
* @param {Array} data - The array of data to be processed.
* @param {string} type - The type of data to be processed.
* @param {object} commandOptions - The options for the command.
*/
const processMatchingData = function (data, type, commandOptions) {
data.forEach((element, index) => {
switch (type) {
case 'clients':
data[index] = normalizeClient(element, commandOptions?.format);
break;
case 'currentActivities':
data[index] = normalizeActivity(element, false, commandOptions?.format);
break;
case 'lastActivities':
data[index] = normalizeActivity(element, true, commandOptions?.format);
break;
case 'usage':
data[index] = normalizeUsage(element, commandOptions?.format);
break;
default:
break;
}
});
if (commandOptions.format !== 'raw') {
switch (type) {
case 'clients':
sortClients(data, commandOptions?.format, commandOptions?.sort, commandOptions?.reverse);
break;
case 'currentActivities':
sortActivities(data, false, commandOptions?.format, commandOptions?.sort, commandOptions?.reverse);
break;
case 'lastActivities':
sortActivities(data, true, commandOptions?.format, commandOptions?.sort, commandOptions?.reverse);
break;
case 'usage':
sortUsage(data, commandOptions?.format, commandOptions?.sort, commandOptions?.reverse);
break;
default:
break;
}
}
data.splice(commandOptions?.max > 0 ? commandOptions.max : data.length);
data.forEach((element, index) => {
if (type === 'clients') {
switch (commandOptions?.format) {
case 'list':
/* falls through */
case 'number':
data[index] = element['Client Name'];
break;
}
}
if (type === 'currentActivities' || type === 'lastActivities') {
switch (commandOptions?.format) {
case 'number':
data[index] = element['Activity Id'];
break;
}
}
if (type === 'usage') {
switch (commandOptions?.format) {
case 'list':
data[index] = element['Client Name'];
break;
}
}
});
};
/**
* Main command.
*/
const cli = await new Command()
.name('urbstat')
.version('0.11.1')
.description(
'The Missing Command-line Tool for UrBackup Server.\nDefault options like server address and password are set in the urbstat.conf configuration file.',
)
.example('Get failed clients, use password from configuration file', 'urbstat failed-clients')
.example('Get failed clients, ask for password', 'urbstat failed-clients --ask-pass')
.example('Get options and detailed help for specific command', 'urbstat failed-clients --help')
.globalType(
'clientsFormatValues',
new EnumType(configFallback.URBSTAT_CLIENTS_FORMAT.acceptedValues),
)
.globalType(
'clientsSortValues',
new EnumType(configFallback.URBSTAT_CLIENTS_SORT.acceptedValues),
)
.globalType(
'activitiesFormatValues',
new EnumType(configFallback.URBSTAT_ACTIVITIES_FORMAT.acceptedValues),
)
.globalType(
'currentActivitiesSortValues',
new EnumType(configFallback.URBSTAT_ACTIVITIES_SORT_CURRENT.acceptedValues),
)
.globalType(
'lastActivitiesSortValues',
new EnumType(configFallback.URBSTAT_ACTIVITIES_SORT_LAST.acceptedValues),
)
.globalType(
'usageFormatValues',
new EnumType(configFallback.URBSTAT_USAGE_FORMAT.acceptedValues),
)
.globalType(
'usageSortValues',
new EnumType(configFallback.URBSTAT_USAGE_SORT.acceptedValues),
)
.globalType(
'clientFormatValues',
new EnumType(configFallback.URBSTAT_CLIENT_FORMAT.acceptedValues),
)
.globalOption('--user <name:string>', 'User name.')
.globalOption('--ask-pass', 'Ask for connection password.')
.action(() => {
cli.showHelp();
Deno.exit(0);
});
/**
* Get raw response of "status" API call. Matches all clients, including those marked for removal.
* Required rights: status(all).
* Raw responses cannot be sorted, filtered, etc. Property names and values are left unaltered.
*/
cli.command(
'raw-status',
'Get raw response of "status" API call. Matches all clients, including those marked for removal.\nRequired rights: status(all).\nRaw responses cannot be sorted, filtered, etc. Property names and values are left unaltered.',
)
.example('Get raw response', 'raw-status')
.action((commandOptions) => {
makeServerCalls(['status'], commandOptions).then(() => {
printOutput(statusResponse, 'raw');
});
});
/**
* Get raw response of "activities" API call. Matches all clients, including those marked for removal.
* Required rights: progress(all), lastacts(all).
* Raw responses cannot be sorted, filtered, etc. Property names and values are left unaltered.
*/
cli.command(
'raw-activities',
'Get raw response of "activities" API call. Matches all clients, including those marked for removal.\nRequired rights: progress(all), lastacts(all).\nRaw responses cannot be sorted, filtered, etc. Property names and values are left unaltered.',
)
.example('Get raw response', 'raw-activities')
.action((commandOptions) => {
makeServerCalls(['activities'], commandOptions).then(() => {
printOutput(activitiesResponse, 'raw');
});
});
/**
* Get raw response of "usage" API call. Matches all clients, including those marked for removal.
* Required rights: piegraph(all).
* Raw responses cannot be sorted, filtered, etc. Property names and values are left unaltered.
*/
cli.command(
'raw-usage',
'Get raw response of "usage" API call. Matches all clients, including those marked for removal.\nRequired rights: piegraph(all).\nRaw responses cannot be sorted, filtered, etc. Property names and values are left unaltered.',
)
.example('Get raw response', 'raw-usage')
.action((commandOptions) => {
makeServerCalls(['usage'], commandOptions).then(() => {
printOutput(usageResponse, 'raw');
});
});
/**
* Retrieves all clients, including those marked for removal.
* Required rights: status(all).
* If you specify "raw" format, the output cannot be sorted or filtered,
* and property names/values are left unaltered.
* Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.
*/
cli.command(
'all-clients',
'Retrieves all clients, including those marked for removal.\nRequired rights: status(all).\nIf you specify "raw" format, the output cannot be sorted or filtered, and property names/values are left unaltered.\nDefault options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.',
)
.example('Get all clients, use default options', 'all-clients')
.example('Get the total number of all clients', 'all-clients --format "number"')
.example('Get a sorted table', 'all-clients --format "table" --sort "file"')
.example('Get reversed list', 'all-clients --format "list" --sort "name" --reverse')
.example('Get names of three of the longest-unseen clients', 'all-clients --format "list" --sort "seen" --max 3')
.option('--format <format:clientsFormatValues>', 'Change the output format.', {
default: getConfigValue('URBSTAT_CLIENTS_FORMAT'),
})
.option('--sort <field:clientsSortValues>', "Change the sorting order. Ignored with 'raw' output format.", {
default: getConfigValue('URBSTAT_CLIENTS_SORT'),
})
.option('--reverse', "Reverse the sorting order. Ignored with 'raw' output format.")
.option('--max <number:integer>', 'Show only <number> of clients, 0 means no limit.', { default: 0 })
.action((commandOptions) => {
makeServerCalls(['all-clients'], commandOptions).then(() => {
processMatchingData(allClientsResponse, 'clients', commandOptions);
printOutput(allClientsResponse, commandOptions?.format);
});
});
/**
* Retrieves OK clients, i.e., clients with OK backup status. Excludes clients marked for removal.
* Backups finished with issues are treated as OK by default.
* Required rights: status(all).
* If you specify "raw" format, the output cannot be sorted or filtered,
* and property names/values are left unaltered.
* Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.
*/
cli
.command(
'ok-clients',
'Retrieves OK clients i.e. clients with OK backup status. Excludes clients marked for removal.\nBackups finished with issues are treated as OK by default.\nRequired rights: status(all).\nIf you specify "raw" format, the output cannot be sorted or filtered, and property names/values are left unaltered.\nDefault options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.',
)
.example('Get OK clients, use default options', 'ok-clients')
.example('Get the total number of OK clients', 'ok-clients --format "number"')
.example('Get a sorted table', 'ok-clients --format "table" --sort "file"')
.example('Get a sorted table, skip file backup problems', 'ok-clients --format "table" --sort "image" --skip-file')
.example('Get reversed list', 'ok-clients --format "list" --sort "name" --reverse')
.option('--format <format:clientsFormatValues>', 'Change the output format.', {
default: getConfigValue('URBSTAT_CLIENTS_FORMAT'),
})
.option('--sort <field:clientsSortValues>', "Change the sorting order. Ignored with 'raw' output format.", {
default: getConfigValue('URBSTAT_CLIENTS_SORT'),
})
.option('--reverse', "Reverse the sorting order. Ignored with 'raw' output format.")
.option('--max <number:integer>', 'Show only <number> of clients, 0 means no limit.', { default: 0 })
.option('--skip-file', 'Skip file backups when matching clients.', { conflicts: ['skip-image'] })
.option('--skip-image', 'Skip image backups when matching clients.')
.option('--strict', 'Do not treat backups finished with issues as being OK.')
.action((commandOptions) => {
makeServerCalls(['ok-clients'], commandOptions).then(() => {
processMatchingData(okClientsResponse, 'clients', commandOptions);
printOutput(okClientsResponse, commandOptions?.format);
});
});
/**
* Retrieves failed clients i.e. clients with failed backup status or without a recent backup as configured in UrBackup Server.
* Excludes clients marked for removal.
* Required rights: status(all).
* If you specify "raw" format then output can not be sorted or filtered and property names/values are left unaltered.
* Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.
*/
cli.command(
'failed-clients',
'Retrieves failed clients i.e. clients with failed backup status or without a recent backup as configured in UrBackup Server. Excludes clients marked for removal.\nRequired rights: status(all).\nIf you specify "raw" format then output can not be sorted or filtered and property names/values are left unaltered.\nDefault options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.',
)
.example('Get FAILED clients, use default options', 'failed-clients')
.example('Get the total number of FAILED clients', 'failed-clients --format "number"')
.example('Get a sorted table', 'failed-clients --format "table" --sort "file"')
.example(
'Get a sorted table, skip file backup problems',
'failed-clients --format "table" --sort "image" --skip-file',
)
.example('Get reversed list', 'failed-clients --format "list" --sort "name" --reverse')
.option('--format <format:clientsFormatValues>', 'Change the output format.', {
default: getConfigValue('URBSTAT_CLIENTS_FORMAT'),
})
.option('--sort <field:clientsSortValues>', "Change the sorting order. Ignored with 'raw' output format.", {
default: getConfigValue('URBSTAT_CLIENTS_SORT'),
})
.option('--reverse', "Reverse the sorting order. Ignored with 'raw' output format.")
.option('--max <number:integer>', 'Show only <number> of clients, 0 means no limit.', { default: 0 })
.option('--skip-file', 'Skip file backups when matching clients.', { conflicts: ['skip-image'] })
.option('--skip-image', 'Skip image backups when matching clients.')
.option('--skip-blank', 'Skip blank clients.')
.action((commandOptions) => {
makeServerCalls(['failed-clients'], commandOptions).then(() => {
processMatchingData(failedClientsResponse, 'clients', commandOptions);
printOutput(failedClientsResponse, commandOptions?.format);
});
});
/**
* Retrieves stale clients, i.e. clients without a recent backup as configured in urbstat. Excludes clients marked for removal.
* Required rights: status(all).
* If you specify "raw" format then output cannot be sorted or filtered and property names/values are left unaltered.
* Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE, URBSTAT_THRESHOLD_STALE_FILE, URBSTAT_THRESHOLD_STALE_IMAGE.
*/
cli.command(
'stale-clients',
'Retrieves stale clients i.e. clients without a recent backup as configured in urbstat. Excludes clients marked for removal.\nRequired rights: status(all).\nIf you specify "raw" format then output can not be sorted or filtered and property names/values are left unaltered.\nDefault options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE, URBSTAT_THRESHOLD_STALE_FILE, URBSTAT_THRESHOLD_STALE_IMAGE.',
)
.example('Get STALE clients, use default options', 'stale-clients')
.example('Get the total number of STALE clients', 'stale-clients --format "number"')
.example('Get a sorted table', 'stale-clients --format "table" --sort "name"')
.example('Get a sorted table, skip BLANK clients', 'stale-clients --format "table" --sort "name" --skip-blank')
.example('Get a sorted table, skip file backups', 'stale-clients --format "table" --sort "image" --skip-file')
.example('Get reversed list', 'stale-clients --format "list" --sort "name" --reverse')
.example(
'Get clients with file backup older than a day',
'stale-clients --format "table" --sort "name" --threshold-file 1440',
)
.example(
'Get number of clients with image backup older than 12hrs',
'stale-clients --format "number" --threshold-image 720 --skip-file',
)
.option('--format <format:clientsFormatValues>', 'Change the output format.', {
default: getConfigValue('URBSTAT_CLIENTS_FORMAT'),
})
.option('--sort <field:clientsSortValues>', "Change the sorting order. Ignored with 'raw' output format.", {
default: getConfigValue('URBSTAT_CLIENTS_SORT'),
})
.option('--reverse', "Reverse the sorting order. Ignored with 'raw' output format.")
.option('--max <number:integer>', 'Show only <number> of clients, 0 means no limit.', { default: 0 })
.option('--threshold-file <minutes:integer>', 'Set time threshold in minutes.', {
default: getConfigValue('URBSTAT_THRESHOLD_STALE_FILE'),
})
.option('--threshold-image <minutes:integer>', 'Set time threshold in minutes.', {
default: getConfigValue('URBSTAT_THRESHOLD_STALE_IMAGE'),
})
.option('--skip-file', 'Skip file backups when matching clients.', { conflicts: ['skip-image'] })
.option('--skip-image', 'Skip image backups when matching clients.')
.option('--skip-blank', 'Skip blank clients.')
.action((commandOptions) => {
makeServerCalls(['stale-clients'], commandOptions).then(() => {
const matchingClients = [];
for (const client of staleClientsResponse) {
if (commandOptions.skipFile !== true) {
const timestampDifference = Math.round((currentEpochTime - (client?.lastbackup ?? 0)) / 60);
if (
(commandOptions.skipBlank !== true || (commandOptions.skipBlank === true && client.lastbackup !== 0)) &&
client.file_disabled !== true &&
timestampDifference >= commandOptions.thresholdFile
) {
matchingClients.push(client);
continue;
}
}
if (commandOptions.skipImage !== true) {
const timestampDifference = Math.round((currentEpochTime - (client?.lastbackup_image ?? 0)) / 60);
if (
(commandOptions.skipBlank !== true ||
(commandOptions.skipBlank === true && client.lastbackup_image !== 0)) &&
client.image_disabled !== true &&
timestampDifference >= commandOptions.thresholdImage
) {
matchingClients.push(client);
continue;
}
}
}
processMatchingData(matchingClients, 'clients', commandOptions);
printOutput(matchingClients, commandOptions?.format);
});
});
/**
* Retrieves blank clients i.e. clients without any finished backups. Excludes clients marked for removal.
* Required rights: status(all).
* If you specify "raw" format then output can not be sorted or filtered and property names/values are left unaltered.
* Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.
*/
cli.command(
'blank-clients',
'Retrieves blank clients i.e. clients without any finished backups. Excludes clients marked for removal.\nRequired rights: status(all).\nIf you specify "raw" format then output can not be sorted or filtered and property names/values are left unaltered. Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.',
)
.example('Get BLANK clients, use default options', 'blank-clients')
.example('Get the total number of BLANK clients', 'blank-clients --format "number"')
.example('Get a sorted table', 'blank-clients --format "table" --sort "seen"')
.example('Get a sorted table, skip image backups', 'blank-clients --format "table" --sort "name" --skip-image')
.example('Get reversed list', 'blank-clients --format "list" --sort "name" --reverse')
.option('--format <format:clientsFormatValues>', 'Change the output format.', {
default: getConfigValue('URBSTAT_CLIENTS_FORMAT'),
})
.option('--sort <field:clientsSortValues>', "Change the sorting order. Ignored with 'raw' output format.", {
default: getConfigValue('URBSTAT_CLIENTS_SORT'),
})
.option('--reverse', "Reverse the sorting order. Ignored with 'raw' output format.")
.option('--max <number:integer>', 'Show only <number> of clients, 0 means no limit.', { default: 0 })
.option('--skip-file', 'Skip file backups when matching clients.', { conflicts: ['skip-image'] })
.option('--skip-image', 'Skip image backups when matching clients.')
.action((commandOptions) => {
makeServerCalls(['blank-clients'], commandOptions).then(() => {
processMatchingData(blankClientsResponse, 'clients', commandOptions);
printOutput(blankClientsResponse, commandOptions?.format);
});
});
/**
* Retrieves void clients i.e. clients not seen for a long time as configured in urbstat. Excludes clients marked for removal.
* Required rights: status(all).
* If you specify "raw" format then output can not be sorted or filtered and property names/values are left unaltered.
* Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE, URBSTAT_THRESHOLD_VOID_CLIENT.
*/
cli
.command(
'void-clients',
'Retrieves void clients i.e. clients not seen for a long time as configured in urbstat. Excludes clients marked for removal.\nRequired rights: status(all).\nIf you specify "raw" format then output can not be sorted or filtered and property names/values are left unaltered. Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE, URBSTAT_THRESHOLD_VOID_CLIENT.',
)
.example('Get VOID clients, use default options', 'void-clients')
.example('Get the total number of VOID clients', 'void-clients --format "number"')
.example('Get a sorted table', 'void-clients --format "table" --sort "name"')
.example('Get a sorted table, skip BLANK clients', 'void-clients --format "table" --sort "seen" --skip-blank')
.example('Get reversed list', 'void-clients --format "list" --sort "name" --reverse')
.example('Get clients not seen for more than 2 days', 'void-clients --format "table" --sort "name" --threshold 2880')
.example('Get number of clients not seen for more than 12hrs', 'void-clients --format "number" --threshold 720')
.option('--format <format:clientsFormatValues>', 'Change the output format.', {
default: getConfigValue('URBSTAT_CLIENTS_FORMAT'),
})
.option('--sort <field:clientsSortValues>', "Change the sorting order. Ignored with 'raw' output format.", {
default: getConfigValue('URBSTAT_CLIENTS_SORT'),
})
.option('--reverse', "Reverse the sorting order. Ignored with 'raw' output format.")
.option('--max <number:integer>', 'Show only <number> of clients, 0 means no limit.', { default: 0 })
.option('--threshold <minutes:integer>', 'Set time threshold in minutes.', {
default: getConfigValue('URBSTAT_THRESHOLD_VOID_CLIENT'),
})
.option('--skip-blank', 'Skip blank clients.')
.action((commandOptions) => {
makeServerCalls(['void-clients'], commandOptions).then(() => {
processMatchingData(voidClientsResponse, 'clients', commandOptions);
printOutput(voidClientsResponse, commandOptions?.format);
});
});
/**
* Retrieves online clients. Excludes clients marked for removal.
* Required rights: status(all).
* If you specify "raw" format then output can not be sorted or filtered and property names/values are left unaltered.
* Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.
*/
cli.command(
'online-clients',
'Retrieves online clients. Excludes clients marked for removal.\nRequired rights: status(all).\nIf you specify "raw" format then output can not be sorted or filtered and property names/values are left unaltered. Default options are configured with: URBSTAT_CLIENTS_FORMAT, URBSTAT_CLIENTS_SORT, URBSTAT_LOCALE.',
)
.example('Get ONLINE clients, use default options', 'online-clients')
.example('Get the total number of ONLINE clients', 'online-clients --format "number"')
.example('Get a sorted table', 'online-clients --format "table" --sort "name"')
.example('Get a sorted table, skip BLANK clients', 'online-clients --format "table" --sort "name" --skip-blank')
.example('Get reversed list', 'online-clients --format "list" --sort "name" --reverse')
.option('--format <format:clientsFormatValues>', 'Change the output format.', {
default: getConfigValue('URBSTAT_CLIENTS_FORMAT'),
})
.option('--sort <field:clientsSortValues>', "Change the sorting order. Ignored with 'raw' output format.", {
default: getConfigValue('URBSTAT_CLIENTS_SORT'),
})
.option('--reverse', "Reverse the sorting order. Ignored with 'raw' output format.")