-
Notifications
You must be signed in to change notification settings - Fork 2
/
loadDlaBooks.py
1462 lines (1097 loc) · 51.3 KB
/
loadDlaBooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ../../bin/jython_coll_253
############### Begin Standard Header - Do not add comments here ###############
#
# File: %W%
# Version: %I%
# Modified: %G% %U%
# Build: %R% %L%
#
# Licensed Materials - Property of IBM
#
# Restricted Materials of IBM
#
# 5724-N55
#
# (C) COPYRIGHT IBM CORP. 2007. All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
############################# End Standard Header ##############################
#
# It is assumed that this script is located in a directory
# which is not part of the standard TADDM directory structure.
# This minimizes the risk of removing the script inadvertantly as part of an upgrade.
#
# To launch the TADDM jython environment, the first line should point to the
# jython_coll script in the $COLLATION_HOME/bin directory.
# Use the relative location of the directory to initialize the environment.
#
# If for example, the script is stored in $COLLATION_HOME/usr/bin
# use the following path to launch the jython interpreter and initiate the TADDM environment
# ../../dist/bin/jython_coll_253
#
# If the script is stored in $COLLATION_HOME/bin
# use the following path to launch the jython interpreter and initiate the TADDM environment
# ./jython_coll_253
#
import sys
import traceback
import getopt
import os
import platform
import inspect
import socket
import string
from decimal import Decimal
#import csv
#import math
import datetime
from datetime import *
import time
from time import strftime, localtime
from math import *
import re
#import random
#from random import *
#import threading
import hashlib
import array
from symbol import exec_stmt
from sensorhelper import coll_home
current_milli_time = lambda: int(round(time.time() * 1000))
import ftplib
from ftplib import *
import subprocess
from subprocess import Popen, PIPE
from getopt import GetoptError
from org.apache.log4j import *
from org.apache.log4j.xml import *
import java.lang
from java.lang import System
from java.io import File
from java.io import FileInputStream
from java.util import Properties
#from java.sql import DriverManager
#from java.sql import Statement
#from java.sql import SQLException
from java.security import *
'''
from com.collation.platform.model.topology import enums as enums
from com.collation.platform.model.topology.enums import *
'''
from random import *
from com.ibm.cdb.platform.ip import IPv4Utils
import com.collation.platform.model.discovery
from com.collation.platform.model.discovery import *
from com.collation.platform.model.discovery.scope import *
from com.ibm.cdb.topomgr import TopologyManagerFactory
from com.ibm.cdb.topomgr import TopomgrProps
from com.collation.proxy.api.util import ModelObjectFactory
from com.collation.proxy.api.util import ModelObjectFactory
from com.collation.platform.util import *
from java.lang import System
from java.lang import Class
from java.util import HashMap
from java.lang import Boolean
from java.lang import Long
from java.io import File
from java.io import FileInputStream
from java.util import Properties
from com.collation.proxy.api.client import *
from com.collation.proxy.api.client.ApiException import *
from com.collation.proxy.api.util import * ##ModelObjectFactory
from com.ibm.cdb.api import * ##ApiFactory
from com.collation.platform.model import *
from com.collation.platform.model.topology.meta import *
from com.collation.platform.model.discovery.agent import SensorConfiguration
from com.collation.platform.model.discovery.template import Template
from com.collation.platform.model.topology.sys import *
from com.collation.platform.model.discovery.profile import *
from com.collation.platform.model.topology.service import *
from com.collation.platform.model.util.openid import OpenId
from com.collation.platform.model.util.einst import ExtendedInstanceData
from com.collation.platform.model.util.ea import ExtendedAttributesData
from com.collation.platform.model.topology.customCollection import *
from com.collation.platform.model.topology.simple import *
from com.collation.platform.model.discovery.auth import *
from com.collation.discover.manager import *
######################################################################################################
# enumerations
######################################################################################################
from com.collation.platform.model.topology.enums import IpAddressTypeEnum
#collHome = System.getProperty("com.collation.home")
#newJythonHome = collHome + "osgi/plugins/com.ibm.cdb.core.jython253_2.5.3"
#if os.path.isdir(newJythonHome):
# System.setProperty("jython.home",newJythonHome)
# System.setProperty("python.home",newJythonHome)
# jython_home = System.getProperty("jython.home")
# sys.path.append(jython_home + "/Lib")
from com.ibm.cdb.platform.ip import IPv4Utils
from com.ibm.cdb.platform.ip import IPUtils
from com.ibm.cdb.util import IPAddressType
from com.ibm.cdb.util import IPFormatException
#######################################################################################################
#######################################################################################################
class Usage(Exception):
def __init__(self, msg):
if not msg == "help":
log.error("\nINVOCATION ERROR:\t" + msg)
print "\n" + 150*"!"
print "\t" + msg
print 150*"!" + "\n"
show_help()
raise SystemExit(0)
#######################################################################################################
#######################################################################################################
class Fatal(Exception):
def __init__(self,msg=None):
print 150*"*"
print "AN ERROR OCCURED:"
print msg
print 150*"*"
self.rc = 8
self.msg = msg
raise Quit(self.rc)
#######################################################################################################
#######################################################################################################
class Quit(Exception):
def __init__(self,rc=0,msg=None):
self.rc = rc
self.msg = msg
#######################################################################################################
#######################################################################################################
class Exit(Exception):
def __init__(self, rc=0,msg=None):
self.msg = msg
self.rc = rc
def leave(self):
raise SystemExit()
#######################################################################################################
#######################################################################################################
class SystemExit(Exception):
def __init__(self, rc=0,msg=None):
self.msg = msg
self.rc = rc
sys.exit()
# def leave(self):
# sys.exit()
##--------------------------------------------------------------------------------------------------------------
##--------------------------------------------------------------------------------------------------------------
##--------------------------------------------------------------------------------------------------------------
##--------------------------------------------------------------------------------------------------------------
###########################################################################
###########################################################################
def show_help():
if "WINDOWS" in java.lang.System.getProperty("os.name").upper():
ext1 = "bat"
else:
ext1 = ext
delim = 3*" "
l0 = delim + "%-s"
l1 = 10*" " + "%-30s%s"
l2 = 15*" " + "%-50s%s"
l3 = 20*" " + "%-30s%s"
l4 = 70*" " + "%-10s%s"
print l0 % ("The " + prog + " utility downloads DLA books from an ftp site, and loads component information represented in the books into the TADDM database.")
print l0 % ("Custom properties can be specified in a properties file or provided as invocation arguments to control the behavior.")
print ""
print l0 % ("If generation of delta DLA books is enabled, the last processed DLA books are stored locally in the 'current' subdirectory ")
print l0 % ("of the directory specified by the targetDirectory argument.")
print ""
print l0 % ("Messages from the utility are logged in the " + coll_home + os.sep + "log" + os.sep + prog + ".log file")
print ""
print l0 % ("INVOCATION:")
print ""
print l1 % (prog + "." + ext1,"[ACTION OPTIONS] [FTP OPTIONS] [PROCESSING OPTIONS] [MISC OPTIONS]")
print ""
print l1 % ("[ACTION OPTIONS]","-" + inputOpts["action="][0] + "|--action <action> -" + inputOpts["runType="][0] + "|--runType <runType>" )
print ""
print l2 % ("-" + inputOpts["action="][0] + ", --action <action>", "action to perform. Valid actions are:")
print l4 % ("load", "Downloads and processes DLA books, and loads the information into the TADDM database.")
print l4 % ("list", "Lists the files hosted on the FTP site.")
print l2 % ("-" + inputOpts["runType="][0] + ", --runType <runType>", "Specifies whether to test or execute the operation. Valid runTypes are:")
print l4 % ("test", "Performs all actions - except loading resource confifuration and relationship information into the TADDM database.")
print l4 % ("doIt", "Performs all actions, including loading DLA book information into the TADDM database.")
print " "
print l1 % ("[FTP OPTIONS]","-" + inputOpts["ftpHost="][0] + "|--ftpHost <hostname> -" + inputOpts["ftpPort="][0] + "|--ftpPort <portnumber> -" + inputOpts["ftpUser="][0] + "|--ftpUser <username> -" + inputOpts["ftpPassword="][0] + "|--ftpPassword <password> ")
print l1 % ("","-" + inputOpts["ftpSourceDirectory="][0] + "|--ftpSourceDirectory <directory name> -" + inputOpts["ftpDebugLevel="][0] + "|--ftpDebugLevel <debugLevel> -" + inputOpts["removeSourceFiles"][0] + "|--removeSourceFiles " )
print ""
print l2 % ("-" + inputOpts["ftpHost="][0] + ", --ftpHost <hostname>", "Hostname or IP Address of the FTP server where the DLA books are hosted")
print l2 % ("-" + inputOpts["ftpPort="][0] + ", --ftpPort <portNumber>", "Port number used to connect to the FTP server where the DLA books are hosted")
print l2 % ("-" + inputOpts["ftpUser="][0] + ", --ftpUser <hostname>", "User name used to authenticate with the FTP server")
print l2 % ("-" + inputOpts["ftpPassword="][0] + ", --ftpPassword <password>", "Password for the user used to access the FTP server")
print l2 % ("-" + inputOpts["ftpSourceDirectory="][0] + ", --ftpSourceDirectory <directory name>", "Path on the FTP server, relative to the user's home directory, from which DLA books are downloaded")
print l2 % ("-" + inputOpts["ftpRetries="][0] + ", --ftpRetries <number-of-retries>", "Integer, used to control the amount of times the utility will attempt to connect to the FTP server")
print l2 % ("-" + inputOpts["ftpWaitTime="][0] + ", --ftpWaitTime <number of seconds>", "Integer, number of seconds the utitility will wait between attempts to connect to the FTP server")
print l2 % ("-" + inputOpts["ftpDebugLevel="][0] + ", --ftpDebugLevel <debugLevel>", "Integer, 0,1, or 2, used to control the level of logging information from the ftp transactions")
print l2 % ("-" + inputOpts["removeSourceFiles"][0] + ", --removeSourceFiles True|False", "Controls whether source files on the FTP server are removed after they have been downloaded")
print " "
targetFilenameFilterRegex
print l1 % ("[PROCESSING OPTIONS]","-" + inputOpts["targetDirectory="][0] + "|--targetDirectory <directory name> -" + inputOpts["targetFilenameFilterRegex="][0] + "|--targetFilenameFilterRegex <regEx> -" + inputOpts["targetFileMaxAgeDays="][0] + "|--targetFileMaxAgeDays <days> ")
print l1 % ("","-" + inputOpts["enableBookDeltaProcessing"][0] + "|--enableBookDeltaProcessing -" + inputOpts["deltaBookScriptLocation="][0] + "|--deltaBookScriptLocation <directory name>")
print l1 % ("","-" + inputOpts["loadidmlOptions="][0] + "|--loadidmlOptions <options> -" + inputOpts["locationTag="][0] + "|--locationTag <locatioNTag> -" + inputOpts["keepWorkFiles"][0] + "|--keepWorkFiles -")
print ""
print l2 % ("-" + inputOpts["targetDirectory="][0] + ", --targetDirectory <directory name>", "Name of local directory to which DLA files are downloaded and processed")
print l2 % ("-" + inputOpts["targetFilenameFilterRegex="][0] + ", --targetFilenameFilterRegex <regEx>", "Regular MATCHING expression to filter files to be downloaded and processed")
print l2 % ("-" + inputOpts["targetFileMaxAgeDays="][0] + ", --targetFileMaxAgeDays <integer>", "Maximim age of DLA books to process (in days)")
print l2 % ("-" + inputOpts["enableBookDeltaProcessing"][0] + ", --enableBookDeltaProcessing", "Enables delta processing of DLA books prior to updating CI information in the TADDM database")
print l2 % ("-" + inputOpts["deltaBookScriptLocation="][0] + ", --deltaBookScriptLocation <directory name>", "User name used to authenticate with the FTP server")
print l2 % ("-" + inputOpts["loadidmlOptions="][0] + ", --loadidmlOptions <options>", "Options, except for --locationTag/(-l), to be parsed to the loadidml utility")
print l2 % ("-" + inputOpts["locationTag="][0] + ", --locationTag <string>", "LocationTag to be assigned to all resources created by during loading of DLA books")
print l2 % ("-" + inputOpts["keepWorkFiles"][0] + ", --keepWorkFiles <options>", "Instructs the " + prog + " utility to remove downloaded and generated DLA books after processing")
print " "
print l1 % ("[MISC OPTIONS]","[-h|--help] [-d|--debug]")
print ""
print l2 % ("-" + inputOpts["debug"][0] + ", --debug","Echos debug messages to the console.")
print l2 % ("-" + inputOpts["showInvocationOptions"][0] + ", --showInvocationOptions","Displays all invocation options in the console.")
print l2 % ("-" + inputOpts["help"][0] + ", --help","Displays this help message.")
print l2 % ("-" + inputOpts["quiet"][0] + ", --quiet","Suppress all output, except for help information, to the console.")
print l2 % ("-" + inputOpts["suppressWarnings"][0] + ", --suppressWarnings","Prevents warning messages to be displayed on the console.")
print " "
print l0 % ("** Additional information regarding the supported arguments can be found in the associated properties file: ../etc/" + prog + ".properties")
print " "
print " "
print l0 % ("EXAMPLES:")
print ""
print l1 % ("To assign default values for arguments that should be configured only once for your environment, update the relevant values in the ../etc/ " + prog + ".properties file","")
print l3 % ("ftpHost = myFtpServer.mydomain.com","")
print l3 % ("ftpPort = 21","")
print l3 % ("ftpUser = taddmusr","")
print l3 % ("ftpPassword = myPassword","")
print l3 % ("enableBookDeltaProcessing = True","")
print l3 % ("loadidmlOptions = -g -e","")
print l3 % ("locationTag = MANAGEMENT","")
print l3 % ("removeSourceFiles = True","")
print l3 % ("keepWorkFiles = False","")
print l2 % ("** Note that all boolean controls have a default value of False. ","")
print l2 % ("** If the value set to True in the properties file, they cannot be overwritten by a command line argument","")
print ""
print l1 % ("To download DLA books to the default directory, populate the TADDM database, and remove the files on both the ftp site and locally:"," ")
print l2 % (prog + "." + ext + " -" + inputOpts["action="][0] + " load -" + inputOpts["runType="][0] + " doit -" + inputOpts["removeSourceFiles"][0] + " -" + inputOpts["keepWorkFiles"][0],"")
print ""
print l1 % ("To download DLA books to the '../dla Books' directory, and generate delta books WITHOUT populating the TADDM database:","")
print l2 % (prog + "." + ext + " -" + inputOpts["action="][0] + " load -" + inputOpts["targetDirectory="][0] + " \"../dla Books\" -" + inputOpts["enableBookDeltaProcessing"][0] + " -" + inputOpts["deltaBookScriptLocation="][0] + " ../../tools/deltabooks -" + inputOpts["runType="][0] + " test","")
print ""
print l1 % ("To download DLA books to default directory, and populate the TADDM database while assigning a locationTag of MAINFRAME:","")
print l2 % (prog + "." + ext + " -" + inputOpts["action="][0] + " load -" + inputOpts["locationTag="][0] + " MANAGEMENT -" + inputOpts["runType="][0] + " doit","")
print ""
print l1 % ("To process only DLA books with a name that contains 'MQ':","")
print l2 % (prog + "." + ext + " -" + inputOpts["action="][0] + " load -" + inputOpts["targetFilenameFilterRegex="][0] + " \"(.*MQ.*)\" -" + inputOpts["runType="][0] + " doit","")
raise SystemExit()
#################################################
################################################################################################
def init(trace=False,debug=False):
global log, logfileName, prog, ext
coll_home = getCollHome()
prog,ext = getProgramName()
#Load properties file in java.util.Properties
propFileName = coll_home+"/etc/collation.properties"
inStream = FileInputStream(propFileName)
propFile = Properties()
propFile.load(inStream)
log, logfileName = setupLog4jLogging(trace,debug)
return log, logfileName, prog
#############################################################################
def getProgramName():
global prog
dirname, progname = os.path.split(sys.argv[0])
prog,ext = progname.split(".",1)
return prog,ext
#############################################################################
def getCollHome():
global coll_home
coll_home = System.getProperty("com.collation.home")
if coll_home[0] == "/" and "WINDOWS" in java.lang.System.getProperty('os.name').upper():
coll_home = coll_home[1:]
##coll_home = os.environ["COLLATION_HOME"]
return coll_home
#############################################################################
def logit (level, message):
#print level.upper() + "logLevel:" + logLevel.upper() + " debug:" + str(debug) + " quiet:" + str(quiet) + " trace:" + str(trace) + ": \t" + message
#print "???? " + level.upper() + " " + logLevel
if runType == "test":
message = "TESTING: " + message
if level.upper() == "INFO":
#if string.upper(logLevel) in ["INFO","DEBUG","TRACE"]:
log.info(message)
if not quiet:
print level.upper() + ":\t" + message
elif level.upper() in ["WARNING"]:
if logLevel.upper() in ["INFO", "WARNING","ERROR","DEBUG","TRACE"]:
log.info(message)
if not quiet and not suppressWarnings:
print level.upper() + ":\t" + message
elif level.upper() == "ERROR":
if logLevel.upper() in ["INFO","WARNING","ERROR","DEBUG","TRACE"]:
log.error(message)
if not quiet:
print level.upper() + ":\t" + message
elif level.upper() == "DEBUG":
if logLevel.upper() in ["INFO", "WARNING","ERROR","DEBUG","TRACE"]:
log.debug(message)
if not quiet and debug:
print level.upper() + ": \t" + message
else:
print "Illegal log level '" + level.upper() + "'"
raise Fatal
return
#############################################################################
def connectToTaddm(host,port,user,password):
global log
if host == None:
host = "localhost"
if port == None:
port = -1
if user == None:
user = "administrator"
#############################
# connect to TADDM
#############################
log.debug("Connecting to: " + str(host) + " on port " + str(port) )
conn = ApiFactory.getInstance().getApiConnection(host, port, None, False)
log.debug("Establishing session for user: " + str(user))
sess = ApiFactory.getInstance().getSession(conn, user, password, ApiSession.DEFAULT_VERSION);
api = sess.createCMDBApi()
log.debug("Created CMDB api: " + str(api))
#control = sess.createControlApi()
#log.debug("Created Control api: " + str(api))
return api
#######################################################################
def setupLog4jLogging(trace=False,debug=False):
global log, logLevel
#coll_home = getCollHome()
#prog,ext = getProgramName()
#Load properties file in java.util.Properties
propFileName = coll_home+"/etc/collation.properties"
inStream = FileInputStream(propFileName)
propFile = Properties()
propFile.load(inStream)
if debug == True:
logLevel = "DEBUG"
else:
logLevel = System.getProperty("com.collation.log.level")
if logLevel == None:
logLevel = "Info"
# set properties for using the default TADDM log4j.xml file for logging
if System.getProperty("com.collation.log.level") == None:
System.setProperty("com.collation.log.level",propFile.getProperty("com.collation.log.level"))
if System.getProperty("com.collation.log.filesize") == None:
System.setProperty("com.collation.log.filesize",propFile.getProperty("com.collation.log.filesize"))
if System.getProperty("com.collation.log.filecount") == None:
System.setProperty("com.collation.log.filecount",propFile.getProperty("com.collation.log.filecount"))
if System.getProperty("com.collation.log4j.servicename") == None:
System.setProperty("com.collation.log4j.servicename","-" + prog)
#Start logging
# is a dedicated log4j.xml file provided (name is <prog>.xml
log4jFile = []
log4jFile.append("./"+prog+".xml")
log4jFile.append(coll_home+"/etc/"+prog+".xml")
log4jFile.append(coll_home+"/etc/log4j.xml")
for logF in log4jFile:
if os.path.isfile(logF):
log4j = logF
break
DOMConfigurator.configure(logF)
log = Logger.getLogger("com.ibm.cdb.TivoliStdMsgLogger")
layout = PatternLayout("%d{ISO8601} %X{service} [%t] %x %p %c{2} - %m\n")
if logLevel == "INFO":
log.setLevel(Level.INFO)
elif logLevel == "ERROR":
log.setLevel(Level.ERROR)
elif logLevel == "DEBUG":
log.setLevel(Level.DEBUG)
elif logLevel == "TRACE":
log.setLevel(Level.TRACE)
logfile = File(coll_home+"/log/"+prog+".log")
fileAppender = FileAppender(layout, logfile.getAbsolutePath(), True);
log.addAppender(fileAppender);
if trace == True:
consoleAppender = ConsoleAppender(layout,"System.out")
log.addAppender(consoleAppender);
return log,logfile.getAbsolutePath()
###############################################################################
def executeCommandLineCommand(cmd):
logit("DEBUG","executing:\t" + cmd)
##rc, output = commands.getstatusoutput(cmd + " " + args)
p = Popen(cmd, shell=True, stdout=PIPE, stderr=subprocess.STDOUT)
stdout, stderr = p.communicate()
rc = p.returncode
logit("DEBUG",str(rc) + " returned from command :\t" + str(cmd))
logit("DEBUG","command output:\t" + str(stdout))
if rc > 0:
logit("WARNING","command out :\t" + str(stdout))
logit("WARNING","command err :\t" + str(stderr))
else:
logit("DEBUG","command error :\t" + str(stderr))
return rc, stdout, stderr
###############################################################################
def trueOrFalse(val):
ret = False
if val.upper().strip() == "TRUE":
ret = True
return ret
###############################################################################
def debugDetails(obj):
print 100*"^"
print str(obj.__class__.__name__)
print str(obj)
print 100*"^"
###############################################################################
def getObjectMethods(obj):
print 100*"^"
print str(obj.__class__.__name__)
methods = inspect.getmembers(obj,predicate=inspect.ismethod)
debugDetails(methods)
for m in methods:
print "\t" + str(m)
###############################################################################
def getmostRecentDLAFiles(files):
currentFiles = getDLABookTimestamp(files)
workFiles = {}
for f in currentFiles.keys():
mssName, newTS = currentFiles[f]
if workFiles.has_key(mssName):
fileName, oldTS = workFiles[mssName]
if int(newTS) > int(oldTS):
workFiles[mssName] = [ f, newTS]
else:
workFiles[mssName] = [ f, newTS]
recentFiles = {}
for m in workFiles:
fileName, timestamp = workFiles[m]
recentFiles[fileName] = [m, timestamp]
return recentFiles
###############################################################################
def getDLABookTimestamp(files):
# extract timestamp for each file
mssNameRegex = "(.*)([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9].[0-9][0-9].[0-9][0-9])Z.*"
bookFiles = {}
for f in files:
r = re.findall(mssNameRegex, f)
if r != None:
for h in r:
mssName=h[0]
t = h[1]
timestamp = str(t[0:4]) + str(t[5:7]) + str(t[8:10]) + str(t[11:13]) + str(t[14:16]) + str(t[17:19])
bookFiles[f] = [mssName, timestamp]
return bookFiles
###############################################################################
def getFilesFromFtpServer(action, ftp, sourceDirectory, targetDirectory):
if ftpDebugLevel > 0:
logit("DEBUG","Setting ftp-debug level to " + str(ftpDebugLevel))
ftp.set_debuglevel(ftpDebugLevel)
logit("DEBUG","Connecting to ftp server " + ftpHost + " on port " + ftpPort)
conn = ftp.connect(ftpHost, ftpPort)
#Open ftp connection
logit("DEBUG","Logging in to ftp server " + ftpHost + " as user " + ftpUser + " using password " + ftpPassword)
ftp.login(ftpUser, ftpPassword)
code,home = ftp.sendcmd("pwd").split(" ",1)
home = home.strip("\"")
logit("DEBUG","Home directory for user '" + ftpUser + "' is '" + str(home))
#Change to source directory
logit("DEBUG","Changing to ftpSourceDirectory '" + ftpSourceDirectory + "' on the ftp server")
ftp.cwd(ftpSourceDirectory)
#List the files from the ftp source directory
logit("DEBUG","Getting files in '" + ftpSourceDirectory + "' on the ftp server")
#bookFiles = ftp.nlst("*.xml")
bookFiles = ftp.nlst("*")
if targetFileMaxAgeDays != None:
age = timedelta(targetFileMaxAgeDays)
#local_tz = get_localzone() # get local timezone
utcNow = datetime.utcnow() # get timezone-aware datetime object
days_ago = utcNow - age
#naive = now.replace(tzinfo=None) - age # same time
#yesterday = local_tz.localize(naive, is_dst=None) # but elapsed hours may differ
currentTime = int(utcNow.strftime("%Y%m%d%H%M%S"))
logit("DEBUG", "Execution UTC timestamp is : " + str(currentTime))
cutoffTime = int(days_ago.strftime("%Y%m%d%H%M%S"))
logit("DEBUG", "File age timestamp is : " + str(cutoffTime))#Get the file info
bookFiles.sort()
files = []
workFiles = getDLABookTimestamp(bookFiles)
for book in workFiles.keys():
if targetFilenameFilterRegex != None:
r = re.match(targetFilenameFilterRegex, book)
if r != None:
logit("DEBUG","DLA book: " + str(book) + " matches: " + str(targetFilenameFilterRegex))
mssName, fileTime = workFiles[book]
if targetFileMaxAgeDays != None and int(fileTime) < cutoffTime :
logit("DEBUG","DLA book: " + str(book) + " omitted. Older than : " + str(targetFileMaxAgeDays) + " days")
continue
files.append(book)
else:
logit("DEBUG", "DLA book EXCLUDED: " + str(book) + " DOES NOT MATCH " + str(targetFilenameFilterRegex))
continue
dlaFiles = getDLABookTimestamp(files)
keys = dlaFiles.keys()
keys.sort()
for book in keys:
if action == "list":
print str(book)
else:
try:
# move to the target directory
if not os.path.exists(targetDirectory):
logit("DEBUG","Creating ftp target directory: '" + targetDirectory + "'")
os.mkdir(targetDirectory)
os.chdir(targetDirectory)
#if runType != "test":
retFile = open(book, "wb")
r = ftp.retrbinary("RETR " + book, retFile.write)
retFile.close()
logit("DEBUG","Retrieved book '" + book + "' into " + os.getcwd())
## delete the source file
if removeSourceFiles:
logit("DEBUG","Deleting source book '" + book + "' from " + ftpSourceDirectory)
if runType != "test":
a = ftp.delete(book)
except ftplib.error_perm, ex:
ex_type, ex, tb = sys.exc_info()
msg = "Exception retrieving '" + book + "':\t" + str(ex_type) + "\tErrorType:" + str(ex)
#msg = "\n\texception class:\t" + str(ex_type) + "\n\tErrorType:\t" + str(ex) + "\n\t" + str(traceback.format_tb(tb)[0])
#for i in xrange(len(traceback.format_tb(tb))-1):
# msg = msg + "\n\t" + str(traceback.format_tb(tb)[i+1])
logit("WARNING", msg)
bookFiles.remove(book)
raise
except IOError, ex:
ex_type, ex, tb = sys.exc_info()
msg = "Exception retrieving '" + book + "':\t" + str(ex_type) + "\tErrorType:\t" + str(ex)
#for i in xrange(len(traceback.format_tb(tb))-1):
# msg = msg + "\n\t" + str(traceback.format_tb(tb)[i+1])
logit("WARNING", msg)
bookFiles.remove(book)
raise
ftp.quit()
return dlaFiles
###############################################################################
### MAIN
###############################################################################
try:
runType = "test"
sStart = time.time()
global log,logfileName,prog,ext,dirname,progname,quiet,ext, numberOfRecords, debug, trace, actionVerb
log,logfileName,prog = init()
### set defaults
action = "help"
actionVerb = None
debug = False
removeSourceFiles = False
keepWorkFiles = False
deltaBookScriptLocation = None
deltaBookScriptName = "deltabooks"
if "WINDOWS" in java.lang.System.getProperty('os.name').upper():
deltaBookScriptName = deltaBookScriptName + ".cmd"
else:
deltaBookScriptName = deltaBookScriptName + ".sh"
loadIdmlScriptName = "loadidml"
if "WINDOWS" in java.lang.System.getProperty('os.name').upper():
loadIdmlScriptName = loadIdmlScriptName + ".cmd"
else:
loadIdmlScriptName = loadIdmlScriptName + ".sh"
#dlaRootDir = None
enableBookDeltaProcessing = False
help = ""
propertiesFile = str(os.sep).join(["..","etc",prog+".properties"])
quiet = False
now = time.time()
#localtime = time.gmttime(now)
milliseconds = '%03d' % int((now - int(now)) * 1000)
showInvocationOptions = False
suppressWarnings = False
targetFilenameFilterRegex = None
targetFileMaxAgeDays = None
#taddmServerHost = "localhost"
#taddmServerPassword = "collation"
#taddmServerPort = 9433
#taddmServerUser = "administrator"
ftpSourceDirectory = None
targetDirectory = None
locationTag = None
loadidmlOptions = None
ftpDebugLevel = 0
ftpRetries = 5
ftpWaitTime = 30
ftpHost = None
ftpPassword = None
ftpPort = 21
ftpUser = None
#locationScopeSelectionWhereClause = None
#operatingSystemScopeSelectionWhereClause = None
#discoveryProtocolScopeSelectionWhereClause = None
trace = False
subSystemNameRegex = "(.*)[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9].[0-9][0-9].[0-9][0-9]Z.*"
runTypes = ["test","doit"]
actions = ["help", "list", "load" ]
#expandedlocationTagPriorities = ["scope", "scopegroup"]
#locationTagPriorities = ["assigned", "scope", "scopegroup", "nameregex", "locationtagregex","default","static"]
#host = taddmServerHost
#port = taddmServerPort
#user = taddmServerUser
#password = taddmServerPassword
inputOpts = {}
inputOpts["action="] = "a:"
inputOpts["debug"] = "d"
inputOpts["removeSourceFiles"] = "R"
inputOpts["keepWorkFiles"] = "K"
#inputOpts["dlaRootDir"] = "S"
inputOpts["deltaBookScriptLocation="] = "s:"
inputOpts["enableBookDeltaProcessing"] = "D"
inputOpts["targetFilenameFilterRegex="] = "F:"
inputOpts["targetFileMaxAgeDays="] = "A:"
inputOpts["ftpSourceDirectory="] = "S:"
inputOpts["targetDirectory="] = "T:"
inputOpts["locationTag="] = "l:"
inputOpts["loadidmlOptions="] = "o:"
inputOpts["ftpDebugLevel="] = "f:"
inputOpts["ftpRetries="] = "n:"
inputOpts["ftpWaitTime="] = "w:"
inputOpts["ftpHost="] = "H:"
inputOpts["ftpPassword="] = "p:"
inputOpts["ftpPort="] = "P:"
inputOpts["ftpUser="] = "u:"
inputOpts["propertiesFile="] = "I:"
inputOpts["quiet"] = "q"
inputOpts["runType="] = "r:"
inputOpts["help"] = "h"
inputOpts["showInvocationOptions"] = "O"
inputOpts["suppressWarnings"] = "W"
inputOpts["trace"] = "t"
#inputOpts["host="] = "H:"
#inputOpts["password="] = "p:"
#inputOpts["port="] = "P:"
#inputOpts["user="] = "u:"
shortOpts = ",".join(inputOpts.values())
longOpts = inputOpts.keys()
#try:
# properties file name
try:
sys.argv.index("--propertiesFile")
prog_propsFileName = sys.argv[sys.argv.index("--propertiesFile") + 1]
except:
try:
sys.argv.index("-I")
prog_propsFileName = sys.argv[sys.argv.index("-"+ inputOpts["propertiesFile="][0]) + 1]
except:
prog_propsFileName = ".." + os.sep + "etc" + os.sep + prog + ".properties"
### READ THE PROPERTIES FILE
if not os.path.exists(prog_propsFileName):
pFileName = ".." + os.sep + "etc" + os.sep + prog_propsFileName
if os.path.exists(pFileName):
prog_propsFileName = pFileName
else:
name, ext = os.path.splitext(pFileName)
if ext in [None,""] :
prog_propsFileName = pFileName + ".properties"
if not os.path.exists(prog_propsFileName):
raise Fatal, "Properties file " + prog_propsFileName + " does not exist"
progProps = {}
logit("DEBUG", "reading properties file: " + prog_propsFileName)
inStream = FileInputStream(prog_propsFileName)
prog_props = Properties()
prog_props.load(inStream)
validProperties = inputOpts.keys()
for key in prog_props.propertyNames():
val = prog_props.getProperty(key).strip()
progProps[key] = val.strip()
if key.lower() == "action".lower():
action = progProps[key]
elif key.lower() == "debug".lower():
debug = trueOrFalse(progProps[key])
elif key.lower() == "targetFilenameFilterRegex".lower():
targetFilenameFilterRegex = progProps[key]
elif key.lower() == "targetFileMaxAgeDays".lower():
targetFileMaxAgeDays = int(progProps[key])
elif key.lower() == "removeSourceFiles".lower():
removeSourceFiles = trueOrFalse(progProps[key])
elif key.lower() == "keepWorkFiles".lower():
keepWorkFiles = trueOrFalse(progProps[key])
elif key.lower() == "enableBookDeltaProcessing".lower():
enableBookDeltaProcessing = trueOrFalse(progProps[key])
elif key.lower() == "deltaBookScriptLocation".lower():
deltaBookScriptLocation = progProps[key]
elif key.lower() == "targetDirectory".lower():
targetDirectory = progProps[key]
elif key.lower() == "ftpSourceDirectory".lower():
ftpSourceDirectory = progProps[key]
elif key.lower() == "ftpUser".lower():
ftpUser = progProps[key]
elif key.lower() == "ftpPassword".lower():
ftpPassword = progProps[key]
elif key.lower() == "ftpDebugLevel".lower():
ftpDebugLevel = int(progProps[key])
elif key.lower() == "ftpRetries".lower():
ftpRetries = int(progProps[key])
elif key.lower() == "ftpWaitTime".lower():
ftpWaitTime = int(progProps[key])
elif key.lower() == "ftpHost".lower():
ftpHost = progProps[key]
elif key.lower() == "ftpPort".lower():
ftpPort = progProps[key]
elif key.lower() == "locationTag".lower():
locationTag = progProps[key]
elif key.lower() == "loadidmlOptions".lower():
loadidmlOptions = progProps[key]
elif key.lower() == "quiet".lower():
quiet = trueOrFalse(progProps[key])
elif key.lower() == "runType".lower():
runType = progProps[key]
elif key.lower() == "suppressWarnings".lower():
suppressWarnings = trueOrFalse(progProps[key])
elif key.lower() == "showInvocationOptions".lower():
showInvocationOptions = trueOrFalse(progProps[key])
'''
elif key.lower() == "taddmServerUser".lower():
taddmServerUser = progProps[key]
elif key.lower() == "taddmServerPassword".lower():
taddmServerPassword = progProps[key]
elif key.lower() == "taddmServerHost".lower():
taddmServerHost = progProps[key]
elif key.lower() == "taddmServerPort".lower():
taddmServerPort = int(progProps[key])
'''
elif key.lower() == "trace".lower():
trace = trueOrFalse(progProps[key])
else:
raise Fatal, "Unknown property '" + key + "' specified in properties file " + prog_propsFileName
####################################################################
# get arguments
####################################################################
if len(sys.argv) == 1:
raise Usage("help")
if len(sys.argv) > 1:
if sys.argv[1].upper() in ["-H", "HELP", "--HELP"]:
raise Usage("help")
elif not (sys.argv[1][0] == "-"):
action = sys.argv[1]
argv = sys.argv[2:]
else:
argv = sys.argv[1:]
#shortOpts = "a:c:de:hH:iI:l:Op:P:n:qr:s:x:w:Wt:u:?:"
#longOpts = ["action=","concurrentDiscoveries=","debug","runNameEyeCatcher=","help","host=","inline","propertiesFile=","locationTag=","showInvocationOptions","password=","port=","profileName=","quiet","action=","scope=","whereClause=","suppressWarnings","threads=","user=","unknownValue"]
opts = {}
args = {}