-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretain_app.sh
executable file
·1075 lines (727 loc) · 20.7 KB
/
retain_app.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
# -----------------------------------------------------------------------------
# retain_app.sh - command line tool for retain administrators
# -----------------------------------------------------------------------------
# Copyright 2014, Philipp Metzler [email protected]
# License: Beerware
#
# "THE BEER-WARE LICENSE" (Revision 42):
# <[email protected]> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
# -----------------------------------------------------------------------------
# Usage: retain_app.sh
# -----------------------------------------------------------------------------
# version
version=0.9.95
# CONFIGURATION
###############################################################################
asconfig=/opt/beginfinite/retain/RetainServer/WEB-INF/cfg/ASConfig.cfg
retain_log_dir=/var/log/retain-tomcat7
# temp data
tmpdir=/tmp
tmp_file_installed_schema=$tmpdir/ra_installed_schema.tmp
tmp_file_fresh_schema=$tmpdir/ra_fresh_schema.tmp
schema_download_url="https://raw.githubusercontent.com/inetrade/retain_app/master/retain_keys_35.dat"
# indexer monitor files
indexer_monitor_init_timestamp=$tmpdir/indexer_monitor_init_timestamp
indexer_monitor_absolute_data=$tmpdir/indexer_monitor_absolute_data
indexer_monitor_tmp_file=$tmpdir/indexer_monitor_tmp_file
indexer_monitor_data_file=$tmpdir/indexer_monitor_data
indexer_status_file=/srv/www/htdocs/indexer_status.txt
cronjob='-* * * * * root /root/retain_app.sh -m'
# ENVIRONMENT
###############################################################################
app_url='https://raw.githubusercontent.com/inetrade/csv_mysql_importer/master/retain_app.sh'
latest_file=$(curl "$app_url" 2>/dev/null)
current_version=$(grep -m1 'version=' $0)
latest_version=$(grep -m1 'version=' <<<"$latest_file")
# get a timestamp
timestamp=$(date)
# formatting
retain_app_head="retain_app ($version) - "\
"command line utility for retain administrators"
topic_wrapper="#########################################"\
"######################################"
# program messages
msg_new_version_available="NOTE: New version available!"
msg_offer_update="An update for this script is available. "\
"Use latest version? [y/n]: "
msg_processing_request="Processing request..."
msg_configured_jobs="Configured jobs:"
msg_run_jobs="Jobs run at least once:"
msg_up_to_date="You have the latest version. Nothing to do."
msg_known_users="Users known by the system (logged in at least once):"
error_no_filter="No Filter specified. Aborting."
error_no_job_id="No Job-ID specified. Aborting."
error_no_query="No query specified. Aborting."
error_no_log_dir="Log directory does not exist, "\
"please check your configuration. Aborting."
error_no_db_access="No database connection."
error_offline="You need online access to do that, check internet connetcion / proxy."
error_tmp_not_writable="TMP directory not writable."
# commands
cronrestart="/etc/init.d/cron restart"
# retain log keywords
str_job_start="registering Job with name:"
str_job_end="unregistering Job with name:"
# filters
declare -a log_filters_default
log_filters_default+=('LOGGING IN AS')
log_filters_default+=('- reportError: ArchiveEmail')
log_filters_default+=('Total mails archived for this ')
#log_filters_default+=('Exceeded maximum errors')
#log_filters_default+=('- ESE')
#log_filters_default+=('Code: ')
#log_filters_default+=('Total mails archived for this job: ')
#log_filters_default+=('RetainDredger - Job ended gracefully.')
#log_filters_default+=('DateFlagManager')
#log_filters_default+=('MailboxArchivingStats')
#log_filters_default+=('registering Job with')
#log_filters_default+=('Exceeded maximum errors')
#log_filters_default+=('Total mails archived for this ')
#log_filters_default+=('RetainDredger - Job ended gracefully.')
grep_arg_default="$(for i in "${log_filters_default[@]}"; do echo -n \
"${i}\\|"; done | rev | cut -c 4- | rev)"
# SQL queries
###############################################################################
query_show_tables='
SHOW TABLES;
'
query_get_users='
SELECT
f_first,
f_last,
f_mailbox
FROM
t_abook;
'
query_configured_jobs='
SELECT DISTINCT
a.f_name AS JOB_ID,
b.f_propertyvalue AS JOB_NAME
FROM
t_jobs a,
t_joboptions b
WHERE
a.job_id = b.job_id
AND
b.f_propertyname = '"'"'friendlyname'"'"';
'
query_indexed_items_count='
SELECT
COUNT(*)
FROM
t_message
WHERE
f_indexed = 1
'
query_message_count='
SELECT
COUNT(message_id)
FROM t_message'
query_avg_file_size='
SELECT
AVG(f_size)
FROM
t_dsref
'
query_file_count='
SELECT
COUNT(ds_reference_id)
FROM
t_dsref
'
query_items_count='
SELECT
COUNT(*)
FROM
t_message;
'
query_db_schema_version='
SELECT
value
FROM
t_dbinfo
WHERE
name = '"'"'DBSchemaVer'"'"';
'
query_db_mig_version='
SELECT
value
FROM
t_dbinfo
WHERE
name = '"'"'DBMigrateVer'"'"';
'
query_stored_mime_count='
SELECT
COUNT(*)
FROM
t_message_attachments
WHERE f_name = '"'"'Mime.822'"'"' AND f_size > 0;
'
query_attachments_all='
SELECT
COUNT(DISTINCT document_id)
FROM
t_message_attachments;
'
query_attachments_size='
SELECT
COUNT(DISTINCT document_id)
FROM
t_message_attachments
WHERE
f_size > GT_SIZE
AND f_size < LT_SIZE;
'
query_get_keys="
SELECT DISTINCT
TABLE_NAME,
INDEX_NAME
FROM
INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'retain'
"
# INITIALISATION
###############################################################################
initialize () {
# check if tmp directory is writable
tmp_is_writable=false
if [[ -w $tmpdir ]]; then
tmp_is_writable=true
fi
# check if we are online
online_access=false
wget -O /dev/null -q --tries=5 --timeout=10 http://google.com
if [[ $? -eq 0 ]]; then
online_access=true
else
online_access=false
fi
# check if log directory exists
log_dir_exists=false
if [ -d "$retain_log_dir" ]; then
log_dir_exists=true
fi
# check if ASConfig exists
asconfig_exists=false
if [ -f "$asconfig" ]; then
asconfig_exists=true
fi
# system status variables
system_memory=""
java_memory=""
# Retain variables
server_id=""
archive_path=""
index_path=""
# DB variables
db_server_type=""
db_url=""
db_user=""
db_password=""
# get database type
db_server_type=$(parse_asconfig '<DBDriver>')
#jdbc:mysql://localhost/retain
#jdbc:oracle:thin:@gerslesora.ger.int.seele.com:1521/ORCL_SERVICE.ger.int.seele.com
# DB credentials
db_url=$(parse_asconfig '<DBURL>')
db_host=$(parse_asconfig '<DBURL>' | cut -d \/ -f 3 )
db_name=retain
db_user=$(parse_asconfig '<DBUser>')
db_password=$(parse_asconfig '<DBPass>')
# check if DB connection can be established
case $db_server_type in
com.mysql.jdbc.Driver )
sql_command="mysql --raw \
--silent \
--skip-column-names \
--host=$db_host \
--user=$db_user \
--password=$db_password $db_name \
--execute "
sql_command_formatted="mysql --host=$db_host \
--user=$db_user \
--password=$db_password \
--table $db_name \
--execute "
sql_dump_command="mysqldump \
--host=$db_host \
--user=$db_user \
--password=$db_password $db_name"
check_db_conn
;;
oracle.jdbc.OracleDriver )
check_db_conn
;;
* )
db_access=false
esac
}
# SELF UPDATE
###############################################################################
update_retain_app () {
if [[ $online_access = true ]]; then
if [[ "$current_version" != "$latest_version" && "$online_access" = true ]]; then
offer_update
else
echo $msg_up_to_date
fi
else
echo $error_offline
fi
}
do_update() {
new_tmp=$(mktemp)
chmod +x "$new_tmp"
cat <<<"$latest_file" > "$new_tmp"
( mv "$new_tmp" "$0" )
exit 0
}
offer_update() {
while IFS= read -n1 -r -p 'An update for this script is available. Use latest version? [y/n]: '; do
case $REPLY in
[Yy] )
printf '\n%s\n' 'Updating now.'
do_update
break
;;
[Nn] )
printf '\n%s\n' 'Update was refused.'
break
;;
* )
printf '\r'
esac
done
}
# HELPER FUNCTIONS
###############################################################################
job_filter () {
if [ "$job_id" == "" ]; then
tee
return 0
else
sed -n "/ $str_job_start $job_id/, /$str_job_end $job_id/p" $1
fi
}
get_log_files () {
find $retain_log_dir/ -maxdepth 1 -name "RetainWorker*" -type f |
grep "$filter" |
sort
}
parse_asconfig () {
if [[ $asconfig_exists = true ]]; then
grep $1 $asconfig | sed -e 's/<[^>]*>//g' | sed 's/ *//g'
fi
}
check_db_conn () {
$sql_command "$query_show_tables" >/dev/null 2>&1
if [ $? -eq 0 ]; then
db_access=true
else
db_access=false
fi
}
get_install_keys () {
$sql_command "$query_get_keys" | sed 's/ \+/ /g' | sed 's/\t/ /g' | sort > $tmp_file_installed_schema
}
get_fresh_keys () {
curl --silent "$schema_download_url" | sort > $tmp_file_fresh_schema
}
# FUNCTIONS
###############################################################################
print_system_info () {
# OS info
system_memory=$(cat /proc/meminfo | grep MemTotal |
awk '{MB=$2/1024; print MB}')
# retain info
server_id=$(parse_asconfig '<serverID>')
archive_path=$(parse_asconfig '<archivePath>')
index_path=$(parse_asconfig '<indexPath>')
echo "System memory: $system_memory"
}
print_users () {
if [[ $db_access = true ]]; then
echo $msg_known_users
echo "$topic_wrapper"
$sql_command_formatted "$query_get_users"
echo
else
echo $error_no_db_access
fi
}
print_jobs () {
if [[ "$db_access" = true ]]; then
echo $msg_configured_jobs
echo "$topic_wrapper"
$sql_command "$query_configured_jobs"
echo
else
echo $error_no_db_access
fi
}
print_jobs_from_worker_log () {
echo $msg_run_jobs
echo "$topic_wrapper"
zgrep 'RUNNING' $retain_log_dir/RetainWorker* |
sed -n 's/^.*RetainArchiveJob - Job \(.*\) is in a RUNNING state$/\1/p' |
sed 's/[()]//g' |
sort |
uniq
echo
}
indexer_monitor () {
if [[ "$db_access" = true ]]; then
local timestamp=$(date +%s)
local indexed_items_count=$($sql_command "$query_indexed_items_count")
# if init timestamp file does not exist create it and install cronjob
if [ ! -f $indexer_monitor_init_timestamp ]; then
echo ${timestamp};${indexed_items_count} > $indexer_monitor_init_file
echo "$cronjob" >> /etc/crontab
echo "not enough data, come back in a minute." > $indexer_status_file
$cronrestart
fi
# if we already collected data ...
if [ -f "$indexer_monitor_init_file" ]; then
# write timestamp and absolute number of indexed messages
echo ${timestamp};${indexed_items_count} >> $indexer_monitor_absolute_data
init_indexed_items_count=$(cat $indexer_monitor_init_timestamp | cut -f2 -d\;)
init_timestamp=$(cat $indexer_monitor_init_timestamp | cut -f1 -d\;)
let absolute_diff=$indexed_items_count-$init_indexed_items_count
let time_diff=$timestamp-$init_timestamp
items_per_second=$(echo "scale=0; $absolute_diff/$time_diff" | bc)
items_per_minute=$(echo "scale=0; $items_per_second*60" | bc)
items_per_hour=$(echo "scale=0; $items_per_minute*60" | bc)
items_per_day=$(echo "scale=0; $items_per_hour*24" | bc)
init_timestamp_human=$(date -d @${init_timestamp})
# write values to status file
echo "indexer monitor started at: " $init_timestamp_human
echo "initial indexed items count: " $init_indexed_items_count
echo "=========================================" >> $indexer_status_file
echo "$indexed_items_count" indexed so far > $indexer_status_file
echo "$items_per_minute" per minute >> $indexer_status_file
echo "$items_per_hour" per hour >> $indexer_status_file
echo "$items_per_day" per day >> $indexer_status_file
# check if we are still indexing stuff
last_value_1=$(tail -n1 $indexer_monitor_absolute_data | cut -f2 -d\;)
last_value_2=$(tail -n1 $indexer_monitor_absolute_data | cut -f2 -d\;)
if [ $last_value_1 -eq $last_value_2 ]; then
# disable cronjob
sed -i '/retain_app/d' /etc/crontab
$cronrestart
# create directory to store collected performance data
indexer_monitor_dir=/root/indexer_$timestamp
mkdir $indexer_monitor_dir
mv /tmp/indexer_* $indexer_monitor_dir
cp $indexer_status_file $indexer_monitor_dir
else
echo "indexer is active"
fi
else
# write initial number of indexed messages
indexed_items_initial=$($sql_command "$query_indexed_items_count")
echo $indexed_items_initial > $indexer_monitor_tmp_file
fi
else
echo $error_no_db_access
fi
}
db_info () {
if [[ "$db_access" = true ]]; then
indexed_items_count=$($sql_command "$query_indexed_items_count")
items_count=$($sql_command "$query_items_count")
db_schema_version=$($sql_command "$query_db_schema_version")
db_mig_version=$($sql_command "$query_db_mig_version")
index_space_used="$(du -sch $index_path | grep total | cut -f1)"
stored_mime_count=$($sql_command "$query_stored_mime_count")
attachments_all=$($sql_command "$query_attachments_all")
#attachments_gt_size=$($sql_command "$query_attachments_size")
echo "Version:"
echo "$topic_wrapper"
echo "db schema version: $db_schema_version"
echo "db migrate version: $db_mig_version"
echo
echo "Messages:"
echo "$topic_wrapper"
echo "total messages: $items_count"
echo "count of stored Mime.822: $stored_mime_count"
echo
echo "Index:"
echo "$topic_wrapper"
echo "indexed items (messages): $indexed_items_count"
echo "Index Size: $index_space_used"
echo
echo "Attachments:"
echo "$topic_wrapper"
echo "All: $attachments_all"
else
echo $error_no_db_access
fi
}
key_check () {
if [[ $online_access = false ]]; then
echo $error_offline
else
if [[ $tmp_is_writable = false ]]; then
echo $error_tmp_not_writable
else
if [[ "$db_access" = true ]]; then
valid_key_data=""
current_key_data=""
get_install_keys
get_fresh_keys
schema_diff=$(comm -3 $tmp_file_installed_schema \
$tmp_file_fresh_schema)
if [[ $schema_diff != "" ]]; then
echo "first column: only in local DB"
echo "second column: missing in local DB"
echo "$schema_diff"
deprecated_tables=$(echo $schema_diff | grep 'Email\|Document')
if [[ $deprecated_tables != "" ]]; then
echo
echo "You still have retain 2.x tables in you database. See docs how to fix:"
echo 'http://support.gwava.com/kb/?View=entry&EntryID=2208'
fi
else
echo "seems OK."
fi
# clean up
rm $tmp_file_installed_schema $tmp_file_fresh_schema
else
echo $error_no_db_access
fi
fi
fi
}
get_errors () {
if [[ $log_dir_exists = false ]]; then
echo $error_no_log_dir
else
for i in $(get_log_files); do
echo "$topic_wrapper"
echo "$i"
echo ""
zcat -f $i | job_filter | egrep -o 'Code: [a-zA-Z0-9]{4}' | sort | uniq
echo ""
done
fi
}
dump_schema () {
if [[ "$db_access" = true ]]; then
$sql_dump_command "--no-data"
else
echo $error_no_db_access
fi
}
get_summary () {
for i in $(get_log_files); do
output=$(zcat -f $i | job_filter | grep "$grep_arg_default" | uniq)
if [ -n "$output" ]; then
echo "$topic_wrapper"
echo "$i"
echo "${output}"
echo ""
fi
done
}
show_help () {
echo
echo "Usage: retain_app.sh [ filters ] [ options ]"
echo
echo " -f | --filter <pattern> : filter files to process with grep-like " \
"expression, e.g. '-04-\\ | -05-'"
echo " -I | --id <Job-ID> : filters job ID"
echo
echo " -J | --runjobs : shows jobs IDs that did run at least once jobs"
echo " -d | --dbstats : shows statistics of retain db"
echo " -m | --monitor : monitor indexer performance"
echo " -K | --keycheck : checks retain db for missing keys / indexes"
echo " -e | --errors : shows all GW error codes"
echo " -j | --jobs : shows CONFIGURED retain jobs"
echo " -s | --summary : show archive job summary WITH errors"
echo " -S | --dump-schema : dump db schema"
echo " -i | --info : print system information"
echo " -u | --users : shows known retain users"
echo
echo " -q | -query <option>: execute query, use without argument for a listing"
echo
echo " -U | --update : update retain_app.sh"
echo " -h | --help : prints this help"
echo
exit 0
}
show_queries () {
echo
echo 'Use one of the following queries:'
echo 'e.g.: ./retain_app.sh -q 4'
echo
echo ' 1: query_show_tables'
echo ' 2: query_get_users'
echo ' 3: query_configured_jobs'
echo ' 4: query_indexed_items_count'
echo ' 5: query_message_count'
echo ' 6: query_avg_file_size'
echo ' 7: query_file_count'
echo ' 8: query_items_count'
echo ' 9: query_db_schema_version'
echo ' 10: query_db_mig_version'
echo ' 11: query_stored_mime_count'
echo ' 12: query_attachments_all'
echo ' 13: query_attachments_size'
echo ' 14: query_get_keys'
echo
exit 0
}
# PROGRAMM
###############################################################################
# if no argument is given, show help
if [[ $# -eq 0 ]] ; then
echo -e $retain_app_head
show_help
exit 0
fi
# initialize retain_app.sh
initialize
# print application header
echo -e $retain_app_head
while :
do
case $1 in
-h | --help | -\?)
show_help
exit 0
;;
-f | --filter)
if [ ! $2 ]; then
echo $error_no_filter
exit 1
fi
filter=$2
shift 2
;;
-I | --id)
if [ ! $2 ]; then
echo $error_no_job_id
exit 1
fi
job_id=$2
shift 2
;;
-q | --query)
if [ ! $2 ]; then
show_queries
exit 1
fi
case $2 in
1)
$sql_command_formatted "$query_show_tables"
;;
2)
$sql_command_formatted "$query_get_users"
;;
3)
$sql_command_formatted "$query_configured_jobs"
;;
4)
$sql_command_formatted "$query_indexed_items_count"
;;
5)
$sql_command_formatted "$query_message_count"
;;
6)
$sql_command_formatted "$query_avg_file_size"
;;
7)
$sql_command_formatted "$query_file_count"
;;
8)
$sql_command_formatted "$query_items_count"
;;
9)
$sql_command_formatted "$query_db_schema_version"
;;
10)
$sql_command_formatted "$query_db_mig_version"
;;
11)
$sql_command_formatted "$query_stored_mime_count"
;;
12)
$sql_command_formatted "$query_attachments_all"
;;
13)
$sql_command_formatted "$query_attachments_size"
;;
14)
$sql_command_formatted "$query_get_keys"
;;
esac
shift 2
;;
-m | --monitor)
indexer_monitor
shift 1
;;
-d | --dbstats)
db_info
shift 1
;;
-K | --keycheck)
key_check
shift 1
;;
-u | --users)
print_users