generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 9
/
usage2adw_setup.sh
1152 lines (978 loc) · 46.1 KB
/
usage2adw_setup.sh
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
#!/bin/bash
#############################################################################################################################
# Copyright (c) 2024, Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
#
# Usage2ADW main Setup script
#
# Written by Adi Zohar, October 2020, Amended Feb 2024
#
# Git Location = https://github.com/oracle-samples/usage-reports-to-adw
# Git Raw Location = https://raw.githubusercontent.com/oracle-samples/usage-reports-to-adw/main
#
# If script fail, please add the policies and re-run
#
# for Help - usage2adw_setup.sh -h
#
#############################################################################################################################
source ~/.bashrc > /dev/null 2>&1
# Application Variables
export VERSION=24.10.01
export APPDIR=/home/opc/usage_reports_to_adw
export CREDFILE=$APPDIR/config.user
export LOGDIR=$APPDIR/log
export SCRIPT=$APPDIR/usage2adw_setup.sh
export LOG=/home/opc/setup.log
export PYTHONUNBUFFERED=TRUE
export WALLET=$HOME/wallet.zip
export WALLET_FOLDER=$HOME/ADWCUSG
export DATE=`date '+%Y%m%d_%H%M%S'`
export GIT=https://raw.githubusercontent.com/oracle-samples/usage-reports-to-adw/main
# Env Variables for database connectivity
export CLIENT_HOME=/usr/lib/oracle/current/client64
export LD_LIBRARY_PATH=$CLIENT_HOME/lib
export PATH=$PATH:$CLIENT_HOME/bin
export TNS_ADMIN=$HOME/ADWCUSG
export PYTHONUNBUFFERED=TRUE
export OCI_PYTHON_SDK_LAZY_IMPORTS_DISABLED=true
###########################################
# Usage
###########################################
Usage()
{
echo "Usage: {"
echo " -h | Help"
echo " -policy_requirement | Help with Policy Requirements"
echo " |"
echo " -setup_app | Setup Usage2ADW Application"
echo " -upgrade_app | Upgrade Usage2ADW Application"
echo " -drop_tables | Drop Usage2ADW Tables"
echo " -truncate_tables | Truncate Usage2ADW Tables"
echo " -setup_credential | Setup Usage2ADW Credentials"
echo " -setup_ol8_packages | Setup Oracle Linux 8 Packages - for manual installation"
echo " -setup_full | Setup Oracle Linux 8 Packages + Setup Application"
echo " -check_passwords | Check config.user file"
echo " -download_wallet | Generate Wallet from ADB and Extract to ADWCUSG folder"
echo "}"
echo ""
}
###########################################
# PolicyRequirement
###########################################
PolicyRequirement()
{
echo "#########################################################################################################################"
echo "# Dynamic Group and Policy Requirements:"
echo "#"
echo "# 1. Create new Dynamic Group : UsageDownloadGroup "
echo "# Obtain Compute OCID and add rule - "
echo "# ALL {instance.id = 'ocid1.instance.oc1.xxxxxxxxxx'}"
echo "# or "
echo "# Obtain Compartment OCID and add rule - "
echo "# ALL {instance.compartment.id = 'ocid1.compartment.oc1.xxxxxxxxxx'}"
echo "#"
echo "# 2. Create new Policy: UsageDownloadPolicy with Statements: (For OC1 don't change ocid1.tenancy)"
echo "# define tenancy usage-report as ocid1.tenancy.oc1..aaaaaaaaned4fkpkisbwjlr56u7cj63lf3wffbilvqknstgtvzub7vhqkggq"
echo "# endorse dynamic-group UsageDownloadGroup to read objects in tenancy usage-report"
echo "# Allow dynamic-group UsageDownloadGroup to read autonomous-database in compartment XXXXXXXX"
echo "# Allow dynamic-group UsageDownloadGroup to read secret-bundles in compartment XXXXXXXX"
echo "# Allow dynamic-group UsageDownloadGroup to inspect compartments in tenancy"
echo "# Allow dynamic-group UsageDownloadGroup to inspect tenancies in tenancy"
echo "#########################################################################################################################"
echo ""
}
########################################################################################################
# GenerateWalletFromADB
########################################################################################################
GenerateWalletFromADB()
{
echo "#######################################" >> $LOG
echo "# GenerateWalletFromADB at `date`" >> $LOG
echo "#######################################" >> $LOG
number=$1
echo "" | tee -a $LOG
echo "${number}. Generate database wallet and extract." | tee -a $LOG
if [ -z "${database_id}" ]
then
echo "DATABASE_ID parameter does not exist in the $CREDFILE" | tee -a $LOG
echo "Please run $SCRIPT -setup_credential to set it up" | tee -a $LOG
echo "Abort." | tee -a $LOG
exit 1
fi
slog=$LOGDIR/generate_adb_wallet_${DATE}.log
echo " Internal LOG=$slog" | tee -a $LOG
python3 $APPDIR/usage2adw_download_adb_wallet.py -ip -dbid $database_id -folder $WALLET_FOLDER -zipfile $WALLET -secret $database_secret_id | tee -a $LOG | tee -a $slog
if (( `grep Error $slog | wc -l` > 0 )); then
echo " Error generating Autonomous Wallet, please check the log $slog" | tee -a $LOG
echo " Please check the documentation to have the dynamic group and policy correctted" | tee -a $LOG
echo " Once fixed you can rerun the script $SCRIPT" | tee -a $LOG
echo " Abort" | tee -a $LOG
exit 1
else
echo " Okay." | tee -a $LOG
fi
}
########################################################################################################
# Main Setup App
########################################################################################################
ReadVariablesFromCredfile()
{
echo "#######################################" >> $LOG
echo "# ReadVariablesFromCredfile at `date`" >> $LOG
echo "#######################################" >> $LOG
number=$1
echo "" | tee -a $LOG
echo "${number}. Read Variables from config.user file." | tee -a $LOG
export db_db_name=`grep "^DATABASE_NAME" $CREDFILE | sed -s 's/DATABASE_NAME=//'`
export extract_from_date=`grep "^EXTRACT_DATE" $CREDFILE | sed -s 's/EXTRACT_DATE=//'`
export extract_tag_special_key=`grep "^TAG_SPECIAL" $CREDFILE | sed -s 's/TAG_SPECIAL=//'`
export extract_tag2_special_key=`grep "^TAG2_SPECIAL" $CREDFILE | sed -s 's/TAG2_SPECIAL=//'`
export database_id=`grep "^DATABASE_ID" $CREDFILE | sed -s 's/DATABASE_ID=//'`
export database_secret_id=`grep "^DATABASE_SECRET_ID" $CREDFILE | sed -s 's/DATABASE_SECRET_ID=//'`
export database_secret_tenant=`grep "^DATABASE_SECRET_TENANT" $CREDFILE | sed -s 's/DATABASE_SECRET_TENANT=//'`
if [ -z "${database_secret_id}" ]
then
echo "Usage2ADW moved to Secret and DATABASE_SECRET_ID does not exist in config file ..." | tee -a $LOG
echo "Please add DATABASE_SECRET_ID=ocid1.vaultsecret.oc1.... to $CREDFILE and run again" | tee -a $LOG
echo "Abort." | tee -a $LOG
exit 1
fi
if [ -z "${DATABASE_SECRET_TENANT}" ]
then
export database_secret_tenant=local
fi
###################################################
# Retrieve Secret from KMS Vault
###################################################
log=/tmp/check_secret_$$.log
python3 ${APPDIR}/usage2adw_retrieve_secret.py -t $database_secret_tenant -secret $database_secret_id -check | tee -a $log
if (( `grep "Secret Okay" $log | wc -l` < 1 )); then
echo "Error retrieving Secret, Abort"
rm -f $log
exit 1
fi
rm -f $log
export db_app_password=`python3 ${APPDIR}/usage2adw_retrieve_secret.py -t $database_secret_tenant -secret $database_secret_id | grep "^Value=" | sed -s 's/Value=//'`
if [ -z "${db_app_password}" ]
then
echo "Error Retrieving Secret from KMS Vault Service. Abort." | tee -a $LOG
exit 1
fi
echo "Secret Retrieved from KMS Vault Service." | tee -a $LOG
echo "" | tee -a $LOG
}
########################################################################################################
# Setup Initial Credential used mainly for upgrade
########################################################################################################
SetupCredential()
{
echo "###########################################################################" >> $LOG
echo "# SetupCredential at `date`" >> $LOG
echo "###########################################################################" >> $LOG
if [ -f "$CREDFILE" ]; then
printf "$CREDFILE already exists. Would you like to overwrite it ? (y/n) ? "; read ANSWER
if [ "$ANSWER" = 'y' ]; then
echo ""
else
exit 0
fi
fi
echo "Setup Credentials..." | tee -a $LOG
echo "" | tee -a $LOG
printf "Please Enter Database Name : "; read DATABASE_NAME
printf "Please Enter Database id (ocid): "; read DATABASE_ID
printf "Please Enter ADB Application Secret Id from KMS Vault: "; read DATABASE_SECRET_ID
printf "Please Enter ADB Application Secret Tenant Profile - The Tenancy name in which the Secret Vault resides (For instance principle use 'local'):"; read DATABASE_SECRET_TENANT
printf "Please Enter Extract Start Date (Format YYYY-MM i.e. 2023-01): "; read EXTRACT_DATE
printf "Please Enter Tag Key 1 to extract as Special Tag (Oracle-Tags.CreatedBy): "; read TAG_SPECIAL
printf "Please Enter Tag Key 2 to extract as Special Tag (Oracle-Tags.Program): "; read TAG2_SPECIAL
if [ -z "$TAG_SPECIAL" ]; then
TAG_SPECIAL="Oracle-Tags.CreatedBy"
fi
echo "DATABASE_USER=USAGE" > $CREDFILE
echo "DATABASE_ID=${DATABASE_ID}" >> $CREDFILE
echo "DATABASE_NAME=${DATABASE_NAME}_low" >> $CREDFILE
echo "DATABASE_SECRET_ID=${DATABASE_SECRET_ID}" >> $CREDFILE
echo "DATABASE_SECRET_TENANT=${DATABASE_SECRET_TENANT}" >> $CREDFILE
echo "EXTRACT_DATE=${EXTRACT_DATE}" >> $CREDFILE
echo "TAG_SPECIAL=${TAG_SPECIAL}" >> $CREDFILE
echo "TAG2_SPECIAL=${TAG2_SPECIAL}" >> $CREDFILE
echo "" | tee -a $LOG
echo "Below Data written to $CREDFILE:" | tee -a $LOG
cat $CREDFILE | tee -a $LOG
echo "" | tee -a $LOG
echo "Setup Credentials Completed." | tee -a $LOG
echo "" | tee -a $LOG
}
########################################################################################################
# Enable APEX Application
########################################################################################################
EnableAPEXApplication()
{
echo "#######################################" >> $LOG
echo "# EnableAPEXApplication at `date`" >> $LOG
echo "#######################################" >> $LOG
number=$1
echo "" | tee -a $LOG
echo "${number}. Enable APEX Application." | tee -a $LOG
###########################################
# Enable APEX
###########################################
slog=$LOGDIR/enable_apex_application_${DATE}.log
echo " Internal LOG=$slog" | tee -a $LOG
echo "set echo on serveroutput on time on lines 199 trimsp on pages 1000 verify off
define pass=${db_app_password}
---------------------------------------------------
-- APEX Create user and import app
---------------------------------------------------
prompt Create APEX Workspace User
begin
apex_util.set_workspace(p_workspace => 'USAGE');
apex_util.create_user(
p_user_name => 'USAGE',
p_web_password => '&pass.',
p_developer_privs => 'ADMIN:CREATE:DATA_LOADER:EDIT:HELP:MONITOR:SQL',
p_email_address => '[email protected]',
p_default_schema => 'USAGE',
p_change_password_on_first_use => 'N' );
end;
/
prompt Remove Application 100
begin
apex_util.set_workspace(p_workspace => 'USAGE');
wwv_flow_api.remove_flow(100);
end;
/
prompt Install Application 100
declare
c_workspace constant apex_workspaces.workspace%type := 'USAGE';
c_app_id constant apex_applications.application_id%type := 100;
c_app_alias constant apex_applications.alias%type := 'USAGE2ADW';
l_workspace_id apex_workspaces.workspace_id%type;
begin
apex_application_install.clear_all;
select workspace_id into l_workspace_id from apex_workspaces where workspace = 'USAGE';
apex_application_install.set_workspace_id(l_workspace_id);
apex_application_install.set_application_id(c_app_id);
apex_application_install.set_application_alias(c_app_alias);
apex_application_install.generate_offset;
end;
/
-----------------------------
-- setup the application
-----------------------------
@/home/opc/usage_reports_to_adw/usage2adw_demo_apex_app.sql
" | sqlplus -s USAGE/${db_app_password}@${db_db_name} | tee -a $slog >> $LOG
if (( `egrep 'ORA-|SP2-' $slog | egrep -v 'ORA-00955|ORA-00001|ORA-06512' | wc -l` > 0 )); then
egrep 'ORA-|SP2-' $slog | egrep -v 'ORA-00955|ORA-00001|ORA-06512'
echo " Error enabling APEX Application, please check log $slog, aborting." | tee -a $LOG
exit 1
else
echo " Okay." | tee -a $LOG
fi
}
###########################################
# Download file from git
###########################################
DownloadFileFromGit()
{
base_dir=$1
dest_dir=$2
file=$3
if [ "${dest_dir}" = "." ]; then
file_dir=$file
else
file_dir=${dest_dir}/$file
fi
file_download=${file_dir}.download
git_dir=${GIT}/${file_dir}
file_log=$LOGDIR/${file}.download.log
echo "" | tee -a $LOG
echo " Download $file" | tee -a $LOG
wget $git_dir -O ${base_dir}/$file_download -o $file_log | tee -a $LOG
if cat $file_log | grep -q "ERROR"
then
echo " -------> Error Downloading $file_dir, Abort, log=$file_log" | tee -a $LOG
echo ""
exit 1
else
echo " -------> $file_download downloaded successfully" | tee -a $LOG
echo " -------> rename $file_download to $file_dir" | tee -a $LOG
mv -f ${base_dir}/$file_download ${base_dir}/$file_dir
if echo $file | grep -q ".sh"
then
chmod +x ${base_dir}/$file_dir
echo " -------> change executable permission to $file_dir" | tee -a $LOG
fi
fi
}
########################################################################################################
# Upgrade App
########################################################################################################
UpgradeApp()
{
echo "###########################################################################" >> $LOG
echo "# Upgrade Usage2ADW Application at `date`" >> $LOG
echo "###########################################################################" >> $LOG
echo "" | tee -a $LOG
echo "Upgrade Usage2ADW will upgrade the below:" | tee -a $LOG
echo "1. Usage2ADW Application" | tee -a $LOG
echo "2. APEX Application" | tee -a $LOG
echo "3. Shell Scripts" | tee -a $LOG
echo "" | tee -a $LOG
printf "Do you want to continue (y/n) ? "; read ANSWER
if [ "$ANSWER" = 'y' ]; then
echo "Answer Y" | tee -a $LOG
else
exit 0
fi
###################################################
# Check file usage2adw.py location
###################################################
cd $HOME
echo "1. Checking file usage2adw.py location before upgrade" | tee -a $LOG
if [ -f "/home/opc/usage_reports_to_adw/usage2adw.py" ]; then
echo " File usage2adw.py exist in app - /home/opc/usage_reports_to_adw/usage2adw.py " | tee -a $LOG
elif [ -f "/home/opc/oci-python-sdk/examples/usage_reports_to_adw/usage2adw.py" ]; then
echo " File usage2adw.py exist in oci-python-sdk location, /home/opc/oci-python-sdk/examples/usage_reports_to_adw/usage2adw.py" | tee -a $LOG
echo " Creating Symbolic Link: ln -s /home/opc/oci-python-sdk/examples/usage_reports_to_adw ." | tee -a $LOG
ln -s /home/opc/oci-python-sdk/examples/usage_reports_to_adw .
else
echo " File usage2adw.py could not find, cannot upgrade, abort " | tee -a $LOG
exit 1
fi
###########################################
# Check if Credential file exist
###########################################
echo "" | tee -a $LOG
echo "2. Check if Credential File Exist - $CREDFILE" | tee -a $LOG
if [ -f "$CREDFILE" ]; then
echo " File exists." | tee -a $LOG
else
echo " File does not exist, running credential module: SetupCredential" | tee -a $LOG
echo "" | tee -a $LOG
SetupCredential
fi
###########################################
# Read Cred from File
###########################################
ReadVariablesFromCredfile 3
###########################################
# Download Files from Git
###########################################
echo "4. Download Files from Git" | tee -a $LOG
DownloadFileFromGit ${APPDIR} . usage2adw.py
DownloadFileFromGit ${APPDIR} . usage2adw_showoci_csv2adw.py
DownloadFileFromGit ${APPDIR} . usage2adw_demo_apex_app.sql
DownloadFileFromGit ${APPDIR} . usage2adw_download_adb_wallet.py
DownloadFileFromGit ${APPDIR} . usage2adw_retrieve_secret.py
DownloadFileFromGit ${APPDIR} . usage2adw_check_connectivity.py
DownloadFileFromGit ${APPDIR} . usage2adw_setup.sh
echo " Download shell files from Git" | tee -a $LOG
mkdir -p ${APPDIR}/shell_scripts
DownloadFileFromGit ${APPDIR} shell_scripts run_daily_report.sh
DownloadFileFromGit ${APPDIR} shell_scripts run_gather_stats.sh
DownloadFileFromGit ${APPDIR} shell_scripts run_load_showoci_csv_to_adw.sh
DownloadFileFromGit ${APPDIR} shell_scripts run_multi_daily_usage2adw.sh
DownloadFileFromGit ${APPDIR} shell_scripts run_report_compart_service_daily_to_csv.sh
DownloadFileFromGit ${APPDIR} shell_scripts run_report_compart_service_sku_daily_to_csv.sh
DownloadFileFromGit ${APPDIR} shell_scripts run_sqlplus_usage.sh
DownloadFileFromGit ${APPDIR} shell_scripts run_table_size_info.sh
###########################################
# Delete APEX App and import New App
###########################################
echo "" | tee -a $LOG
EnableAPEXApplication 5
###########################################
# Completed
###########################################
echo "" | tee -a $LOG
echo "##################################################" | tee -a $LOG
echo "# Upgrade Completed at `date`" | tee -a $LOG
echo "##################################################" | tee -a $LOG
echo "Please run the application to upgrade schema:" | tee -a $LOG
echo "${APPDIR}/shell_scripts/run_multi_daily_usage2adw.sh" | tee -a $LOG
echo "" | tee -a $LOG
}
########################################################################################################
# Main Setup App
########################################################################################################
SetupApp()
{
echo "###########################################################################" >> $LOG
echo "# Setup Application started at `date`" >> $LOG
echo "###########################################################################" >> $LOG
ReadVariablesFromCredfile 1
GenerateWalletFromADB 2
###########################################
# Check OCI Connectivity
###########################################
echo "" | tee -a $LOG
echo "3. Checking OCI Connectivity using instance principles..." | tee -a $LOG
echo " Executed: python3 check_connectivity.sh" | tee -a $LOG
slog=$LOGDIR/check_connectivity_${DATE}.log
echo " Internal LOG=$slog" | tee -a $LOG
python3 $APPDIR/usage2adw_check_connectivity.py | tee -a $slog | tee -a $LOG
if (( `grep Error $slog | wc -l` > 0 )); then
echo " Error querying OCI, please check the log $slog" | tee -a $LOG
echo " Please check the documentation to have the dynamic group and policy correctted" | tee -a $LOG
echo " Once fixed you can rerun the script $SCRIPT" | tee -a $LOG
echo "" | tee -a $LOG
echo " Script will continue incase creating the database on different tenant or child tenant..." | tee -a $LOG
else
echo " Okay." | tee -a $LOG
fi
###########################################
# create application schema and enable APEX
###########################################
echo "" | tee -a $LOG
slog=$LOGDIR/db_creation_user_${DATE}.log
echo " Internal LOG=$slog" | tee -a $LOG
echo "4. Creating USAGE user on ADWC instance and enable APEX Workspace" | tee -a $LOG
echo " commands executed:" | tee -a $LOG
echo " sqlplus ADMIN/xxxxxxxx@${db_db_name}" | tee -a $LOG
echo " create user usage identified by xxxxxxxxx;" | tee -a $LOG
echo " grant create dimension, connect, resource, dwrole, unlimited tablespace to usage;" | tee -a $LOG
echo " exec apex_instance_admin.add_workspace(p_workspace => 'USAGE', p_primary_schema => 'USAGE');" | tee -a $LOG
echo "set lines 199 trimsp on pages 0 feed on
create user usage identified by ${db_app_password};
grant create dimension, connect, resource, dwrole, unlimited tablespace to usage;
exec apex_instance_admin.add_workspace(p_workspace => 'USAGE', p_primary_schema => 'USAGE');
" | sqlplus -s ADMIN/${db_app_password}@${db_db_name} | tee -a $slog >> $LOG
if (( `grep ORA- $slog | egrep -v 'ORA-01920|ORA-20987|06512'| wc -l` > 0 )); then
echo " Error creating USAGE user, please check log $slog, aborting." | tee -a $LOG
exit 1
else
echo " Okay." | tee -a $LOG
fi
###########################################
# create usage2adw tables
###########################################
echo "" | tee -a $LOG
slog=$LOGDIR/create_tables_${DATE}.log
echo "5. Create Usage2ADW Tables" | tee -a $LOG
echo " Internal LOG=$slog" | tee -a $LOG
echo "set echo on serveroutput on time on lines 199 trimsp on pages 1000 verify off
-------------------------------
-- OCI_TENANT
-------------------------------
prompt Creating Table OCI_INTERNAL_COST
create table OCI_INTERNAL_COST (
RESOURCE_NAME VARCHAR2(100) NOT NULL,
SERVICE_NAME VARCHAR2(100),
BILLED_USAGE_UNIT VARCHAR2(100),
CONSUMED_MEASURE VARCHAR2(100),
RESOURCE_UNITS VARCHAR2(100),
UNIT_COST NUMBER,
CONVERSION_FACTOR NUMBER,
EXIST_IN_FINANCE CHAR(1),
CONVERSION_NOTES VARCHAR2(500),
CONSTRAINT OCI_INTERNAL_COST_PK PRIMARY KEY (RESOURCE_NAME,BILLED_USAGE_UNIT) USING INDEX ENABLE
);
-------------------------------
-- OCI_TENANT
-------------------------------
prompt Creating Table OCI_TENANT
create table OCI_TENANT (
TENANT_ID VARCHAR2(100),
TENANT_NAME VARCHAR2(100),
ADMIN_EMAIL VARCHAR2(100),
INFORMATION VARCHAR2(1000),
CONSTRAINT OCI_TENANT_PK PRIMARY KEY (TENANT_ID) USING INDEX
);
-------------------------------
-- OCI_USAGE
-------------------------------
prompt Creating Table OCI_USAGE
create table OCI_USAGE (
TENANT_NAME VARCHAR2(100),
TENANT_ID VARCHAR2(100),
FILE_ID VARCHAR2(30),
USAGE_INTERVAL_START DATE,
USAGE_INTERVAL_END DATE,
PRD_SERVICE VARCHAR2(100),
PRD_RESOURCE VARCHAR2(100),
PRD_COMPARTMENT_ID VARCHAR2(100),
PRD_COMPARTMENT_NAME VARCHAR2(100),
PRD_COMPARTMENT_PATH VARCHAR2(1000),
PRD_REGION VARCHAR2(100),
PRD_AVAILABILITY_DOMAIN VARCHAR2(100),
USG_RESOURCE_ID VARCHAR2(1000),
USG_BILLED_QUANTITY NUMBER,
USG_CONSUMED_QUANTITY NUMBER,
USG_CONSUMED_UNITS VARCHAR2(100),
USG_CONSUMED_MEASURE VARCHAR2(100),
IS_CORRECTION VARCHAR2(10),
TAGS_DATA VARCHAR2(4000),
TAG_SPECIAL VARCHAR2(4000),
TAG_SPECIAL2 VARCHAR2(4000)
) COMPRESS;
CREATE INDEX OCI_USAGE_1IX ON OCI_USAGE(TENANT_NAME,USAGE_INTERVAL_START);
-------------------------------
-- OCI_USAGE_TAG_KEYS
-------------------------------
prompt Creating Table OCI_USAGE_TAG_KEYS
CREATE TABLE OCI_USAGE_TAG_KEYS (
TENANT_NAME VARCHAR2(100),
TAG_KEY VARCHAR2(1000),
CONSTRAINT OCI_USAGE_TAG_KEYS_PK PRIMARY KEY(TENANT_NAME,TAG_KEY)
);
-------------------------------
-- OCI_USAGE_TAG_KEYS
-------------------------------
prompt Creating Table OCI_USAGE_STATS
CREATE TABLE OCI_USAGE_STATS (
TENANT_NAME VARCHAR2(100),
FILE_ID VARCHAR2(30),
USAGE_INTERVAL_START DATE,
NUM_ROWS NUMBER,
UPDATE_DATE DATE,
AGENT_VERSION VARCHAR2(30),
CONSTRAINT OCI_USAGE_STATS_PK PRIMARY KEY (TENANT_NAME,FILE_ID,USAGE_INTERVAL_START)
);
-------------------------------
-- OCI_COST
-------------------------------
prompt Creating Table OCI_COST
create table OCI_COST (
TENANT_NAME VARCHAR2(100),
TENANT_ID VARCHAR2(100),
FILE_ID VARCHAR2(30),
USAGE_INTERVAL_START DATE,
USAGE_INTERVAL_END DATE,
PRD_SERVICE VARCHAR2(100),
PRD_RESOURCE VARCHAR2(100),
PRD_COMPARTMENT_ID VARCHAR2(100),
PRD_COMPARTMENT_NAME VARCHAR2(100),
PRD_COMPARTMENT_PATH VARCHAR2(1000),
PRD_REGION VARCHAR2(100),
PRD_AVAILABILITY_DOMAIN VARCHAR2(100),
USG_RESOURCE_ID VARCHAR2(1000),
USG_BILLED_QUANTITY NUMBER,
USG_BILLED_QUANTITY_OVERAGE NUMBER,
COST_SUBSCRIPTION_ID NUMBER,
COST_PRODUCT_SKU VARCHAR2(10),
PRD_DESCRIPTION VARCHAR2(1000),
COST_UNIT_PRICE NUMBER,
COST_UNIT_PRICE_OVERAGE NUMBER,
COST_MY_COST NUMBER,
COST_MY_COST_OVERAGE NUMBER,
COST_CURRENCY_CODE VARCHAR2(10),
COST_BILLING_UNIT VARCHAR2(1000),
COST_OVERAGE_FLAG VARCHAR2(10),
IS_CORRECTION VARCHAR2(10),
TAGS_DATA VARCHAR2(4000),
TAG_SPECIAL VARCHAR2(4000),
TAG_SPECIAL2 VARCHAR2(4000)
) COMPRESS;
CREATE INDEX OCI_COST_1IX ON OCI_COST(TENANT_NAME,USAGE_INTERVAL_START);
-------------------------------
-- OCI_COST_TAG_KEYS
-------------------------------
prompt Creating Table OCI_COST_TAG_KEYS
CREATE TABLE OCI_COST_TAG_KEYS (TENANT_NAME VARCHAR2(100), TAG_KEY VARCHAR2(1000),
CONSTRAINT OCI_COST_TAG_KEYS_PK PRIMARY KEY(TENANT_NAME,TAG_KEY)
);
-------------------------------
-- OCI_COST_STATS
-------------------------------
prompt Creating Table OCI_COST_STATS
CREATE TABLE OCI_COST_STATS (
TENANT_NAME VARCHAR2(100),
FILE_ID VARCHAR2(30),
USAGE_INTERVAL_START DATE,
NUM_ROWS NUMBER,
COST_MY_COST NUMBER,
COST_MY_COST_OVERAGE NUMBER,
COST_CURRENCY_CODE VARCHAR2(30),
UPDATE_DATE DATE,
AGENT_VERSION VARCHAR2(30),
CONSTRAINT OCI_COST_STATS_PK PRIMARY KEY (TENANT_NAME,FILE_ID,USAGE_INTERVAL_START)
);
-------------------------------
-- OCI_COST_REFERENCE
-------------------------------
prompt Creating Table OCI_COST_REFERENCE
CREATE TABLE OCI_COST_REFERENCE (
TENANT_NAME VARCHAR2(100),
REF_TYPE VARCHAR2(100),
REF_NAME VARCHAR2(1000),
CONSTRAINT OCI_REFERENCE_PK PRIMARY KEY (TENANT_NAME,REF_TYPE,REF_NAME)
) ;
-------------------------------
-- OCI_PRICE_LIST
-------------------------------
prompt Creating Table OCI_PRICE_LIST
create table OCI_PRICE_LIST (
TENANT_NAME VARCHAR2(100),
TENANT_ID VARCHAR2(100),
COST_PRODUCT_SKU VARCHAR2(10),
PRD_DESCRIPTION VARCHAR2(1000),
COST_CURRENCY_CODE VARCHAR2(10),
COST_UNIT_PRICE NUMBER,
COST_LAST_UPDATE DATE,
RATE_DESCRIPTION VARCHAR2(1000),
RATE_PAYGO_PRICE NUMBER,
RATE_MONTHLY_FLEX_PRICE NUMBER,
RATE_UPDATE_DATE DATE,
CONSTRAINT OCI_PRICE_LIST_PK PRIMARY KEY (TENANT_NAME,TENANT_ID,COST_PRODUCT_SKU)
);
-------------------------------
-- OCI_INTERNAL_COST
-------------------------------
prompt Creating Table OCI_INTERNAL_COST
create table OCI_INTERNAL_COST (
RESOURCE_NAME varchar2(100) NOT NULL,
SERVICE_NAME varchar2(100),
BILLED_USAGE_UNIT varchar2(100),
UNIT_COST NUMBER,
CONSTRAINT OCI_INTERNAL_COST_PK PRIMARY KEY (RESOURCE_NAME) USING INDEX ENABLE
);
-------------------------------
-- OCI_LOAD_STATUS
-------------------------------
prompt Creating Table OCI_LOAD_STATUS
create table OCI_LOAD_STATUS (
TENANT_NAME varchar2(100) NOT NULL,
FILE_TYPE varchar2(100) NOT NULL,
FILE_ID varchar2(1000) NOT NULL,
FILE_NAME varchar2(1000) NOT NULL,
FILE_DATE DATE,
FILE_SIZE number,
NUM_ROWS number,
LOAD_START_TIME DATE,
LOAD_END_TIME DATE,
AGENT_VERSION varchar2(100),
BATCH_ID number,
BATCH_TOTAL number,
CONSTRAINT OCI_LOAD_STATUS PRIMARY KEY (TENANT_NAME, FILE_NAME) USING INDEX ENABLE
);
-------------------------------
-- OCI_RESOURCES
-------------------------------
prompt Creating Table OCI_RESOURCES
create table OCI_RESOURCES (
RESOURCE_ID VARCHAR2(200) NOT NULL,
RESOURCE_NAME VARCHAR2(1000),
SOURCE_TENANT VARCHAR2(100),
SOURCE_TABLE VARCHAR2(100),
LAST_LOADED DATE,
CONSTRAINT OCI_RESOURCES_PK PRIMARY KEY (RESOURCE_ID) USING INDEX
);
" | sqlplus -s USAGE/${db_app_password}@${db_db_name} | tee -a $slog >> $LOG
if (( `egrep 'ORA-|SP2-' $slog | egrep -v 'ORA-00955|ORA-00001|ORA-06512' | wc -l` > 0 )); then
egrep 'ORA-|SP2-' $slog | egrep -v 'ORA-00955|ORA-00001|ORA-06512'
echo " Error creating Usage2ADW tables, please check log $slog, aborting." | tee -a $LOG
exit 1
else
echo " Okay." | tee -a $LOG
fi
###########################################
# Enable APEX
###########################################
EnableAPEXApplication 6
###########################################
# Setup Crontab
###########################################
echo "" | tee -a $LOG
echo "7. Setup Crontab to run every 4 hours and gather stats every week" | tee -a $LOG
echo "
###############################################################################
# Crontab to run every 4 hours
###############################################################################
0 */4 * * * timeout 6h /home/opc/usage_reports_to_adw/shell_scripts/run_multi_daily_usage2adw.sh > /home/opc/usage_reports_to_adw/log/run_multi_daily_usage2adw_crontab_run.txt 2>&1
###############################################################################
# Gather stats every weekend
###############################################################################
30 0 * * 0 timeout 6h /home/opc/usage_reports_to_adw/shell_scripts/run_gather_stats.sh > /home/opc/usage_reports_to_adw/log/run_gather_stats_run.txt 2>&1
" | crontab -
echo " Setup Crontab Completed" | tee -a $LOG
###########################################
# run initial usage2adw
###########################################
echo "" | tee -a $LOG
echo "###############################################################" | tee -a $LOG
echo "# Running Initial extract" | tee -a $LOG
echo "###############################################################" | tee -a $LOG
echo " Command line: " | tee -a $LOG
echo " $APPDIR/shell_scripts/run_multi_daily_usage2adw.sh" | tee -a $LOG
echo "" | tee -a $LOG | tee -a $LOG
$APPDIR/shell_scripts/run_multi_daily_usage2adw.sh | tee -a $LOG
echo "" | tee -a $LOG
echo "############################################################################################" | tee -a $LOG
echo "# If process complete successfuly, please continue and login to APEX" | tee -a $LOG
echo "############################################################################################" | tee -a $LOG
exit 0
}
########################################################################################################
# Drop Tables
########################################################################################################
DropTables()
{
echo "###########################################################################" >> $LOG
echo "# Drop Tables at `date`" >> $LOG
echo "###########################################################################" >> $LOG
ReadVariablesFromCredfile 1
printf "Are you sure you want to drop Usage2ADW Tables for USAGE/xxxxxx@${db_db_name} (y/n) ? "; read ANSWER
if [ "$ANSWER" = 'y' ]; then
echo ""
else
exit 0
fi
echo "" | tee -a $LOG
slog=$LOGDIR/drop_usage2adw_tables_${DATE}.log
echo "Dropping Usage2ADW Application Tables." | tee -a $LOG
echo "Internal LOG=$slog" | tee -a $LOG
echo "set echo on serveroutput on time on lines 199 trimsp on pages 1000 verify off
select to_char(sysdate,'YYYY-MM-DD HH24:MI') current_date from dual;
prompt Dropping Table OCI_USAGE
drop table OCI_USAGE ;
prompt Dropping Table OCI_INTERNAL_COST
drop table OCI_INTERNAL_COST ;
prompt Dropping Table OCI_TENANT
drop table OCI_TENANT ;
prompt Dropping Table OCI_USAGE_STATS
drop table OCI_USAGE_STATS ;
prompt Dropping Table OCI_USAGE_TAG_KEYS
drop table OCI_USAGE_TAG_KEYS ;
prompt Dropping Table OCI_COST
drop table OCI_COST;
prompt Dropping Table OCI_COST_STATS
drop table OCI_COST_STATS;
prompt Dropping Table OCI_COST_TAG_KEYS
drop table OCI_COST_TAG_KEYS ;
prompt Dropping Table OCI_COST_REFERENCE
drop table OCI_COST_REFERENCE;
prompt Dropping Table OCI_PRICE_LIST
drop table OCI_PRICE_LIST;
prompt Dropping Table OCI_LOAD_STATUS
drop table OCI_LOAD_STATUS;
prompt Dropping Table OCI_RESOURCES
drop table OCI_RESOURCES;
" | sqlplus -s USAGE/${db_app_password}@${db_db_name} | tee -a $slog | tee -a $LOG
if (( `egrep 'ORA-|SP2-' $slog | egrep -v 'ORA-00955|ORA-00001|ORA-06512' | wc -l` > 0 )); then
egrep 'ORA-|SP2-' $slog | egrep -v 'ORA-00955|ORA-00001|ORA-06512'
echo "Error dropping USAGE tables, please check log $slog ." | tee -a $LOG
else
echo "Completed." | tee -a $LOG
fi
exit 0
}
########################################################################################################
# Truncate Tables
########################################################################################################
TruncateTables()
{
echo "###########################################################################" >> $LOG
echo "# Truncate Tables at `date`" >> $LOG
echo "###########################################################################" >> $LOG
ReadVariablesFromCredfile 1
printf "Are you sure you want to truncate Usage2ADW Tables for USAGE/xxxxx@${db_db_name} (y/n) ? "; read ANSWER
if [ "$ANSWER" = 'y' ]; then
echo ""
else
exit 0
fi
echo "" | tee -a $LOG
slog=$LOGDIR/truncate_usage2adw_tables_${DATE}.log
echo "Truncating Usage2ADW Application Tables." | tee -a $LOG
echo "Internal LOG=$slog" | tee -a $LOG
echo "set echo on serveroutput on time on lines 199 trimsp on pages 1000 verify off
select to_char(sysdate,'YYYY-MM-DD HH24:MI') current_date from dual;
prompt Truncating Table OCI_TENANT
truncate table OCI_TENANT ;
prompt Truncating Table OCI_USAGE
truncate table OCI_USAGE ;
prompt Truncating Table OCI_USAGE_STATS
truncate table OCI_USAGE_STATS ;
prompt Truncating Table OCI_USAGE_TAG_KEYS
truncate table OCI_USAGE_TAG_KEYS ;
prompt Truncating Table OCI_COST
truncate table OCI_COST;
prompt Truncating Table OCI_COST_STATS
truncate table OCI_COST_STATS;
prompt Truncating Table OCI_COST_TAG_KEYS
truncate table OCI_COST_TAG_KEYS ;
prompt Truncating Table OCI_COST_REFERENCE
truncate table OCI_COST_REFERENCE;
prompt Truncating Table OCI_PRICE_LIST
truncate table OCI_PRICE_LIST;
prompt Truncating Table OCI_LOAD_STATUS
truncate table OCI_LOAD_STATUS;
prompt Truncating Table OCI_INTERNAL_COST
truncate table OCI_INTERNAL_COST;
prompt Truncating Table OCI_RESOURCES
truncate table OCI_RESOURCES;
" | sqlplus -s USAGE/${db_app_password}@${db_db_name} | tee -a $slog | tee -a $LOG
if (( `egrep 'ORA-|SP2-' $slog | egrep -v 'ORA-00955|ORA-00001|ORA-06512' | wc -l` > 0 )); then
egrep 'ORA-|SP2-' $slog | egrep -v 'ORA-00955|ORA-00001|ORA-06512'
echo "Error truncating USAGE tables, please check log $slog ." | tee -a $LOG
else
echo "Completed." | tee -a $LOG
fi
exit 0
}
########################################################################################################
# SetupOL8Packages
########################################################################################################
SetupOL8Packages()
{
cd $HOME
echo "###########################################################################" >> $LOG
echo "# Setup OL8 Packages at `date`" >> $LOG
echo "###########################################################################" >> $LOG
###########################################
# Install Python3, git and python packages
###########################################
echo "" | tee -a $LOG
echo "########################################################################" | tee -a $LOG
echo "# 1. Install Python3 and Python OCI Packages, Can take a moment." | tee -a $LOG
echo "########################################################################" | tee -a $LOG
sudo dnf -y module install python39 | tee -a $LOG
sudo alternatives --set python3 /usr/bin/python3.9 | tee -a $LOG
python3 -m pip install --upgrade pip | tee -a $LOG
python3 -m pip install --upgrade oci | tee -a $LOG
python3 -m pip install --upgrade oracledb | tee -a $LOG
python3 -m pip install --upgrade requests | tee -a $LOG
echo "Completed." | tee -a $LOG
###########################################
# Install Oracle Instant Client
###########################################
export RPM_BAS=oracle-instantclient19.23-basic-19.23.0.0.0-1.x86_64
export RPM_SQL=oracle-instantclient19.23-sqlplus-19.23.0.0.0-1.x86_64
export RPM_LNK=https://download.oracle.com/otn_software/linux/instantclient/1923000/
export RPM_LOC=/usr/lib/oracle/19.23
echo "" | tee -a $LOG
echo "########################################################################" | tee -a $LOG
echo "# 2. Install Oracle Instant Client 19c" | tee -a $LOG