forked from hepix-virtualisation/vmcatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmcatcher_eventHndlExpl
108 lines (93 loc) · 3.4 KB
/
vmcatcher_eventHndlExpl
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
#!/usr/bin/env python
import sys
if sys.version_info < (2, 4):
print "Your python interpreter is too old. Please consider upgrading."
sys.exit(1)
import os
import logging
import optparse
import hashlib
import datetime
try:
import simplejson as json
except:
import json
import time
import datetime
time_format_definition = "%Y-%m-%dT%H:%M:%SZ"
EnvEvent = set(['VMCATCHER_EVENT_TYPE',
'VMCATCHER_EVENT_DC_DESCRIPTION',
'VMCATCHER_EVENT_DC_IDENTIFIER',
'VMCATCHER_EVENT_DC_TITLE',
'VMCATCHER_EVENT_HV_HYPERVISOR',
'VMCATCHER_EVENT_HV_SIZE',
'VMCATCHER_EVENT_HV_URI',
'VMCATCHER_EVENT_SL_ARCH',
'VMCATCHER_EVENT_SL_CHECKSUM_SHA512',
'VMCATCHER_EVENT_SL_COMMENTS',
'VMCATCHER_EVENT_SL_OS',
'VMCATCHER_EVENT_SL_OSVERSION',
'VMCATCHER_EVENT_TYPE',
'VMCATCHER_EVENT_FILENAME',
'VMCATCHER_EVENT_IL_DC_IDENTIFIER',
'VMCATCHER_EVENT_HV_FORMAT'])
EnvConf = set(['VMCATCHER_RDBMS',
'VMCATCHER_CACHE_EVENT',
'VMCATCHER_LOG_CONF',
'VMCATCHER_DIR_CERT',
'VMCATCHER_CACHE_DIR_CACHE',
'VMCATCHER_CACHE_DIR_DOWNLOAD',
'VMCATCHER_CACHE_DIR_EXPIRE',
'VMCATCHER_CACHE_ACTION_DOWNLOAD',
'VMCATCHER_CACHE_ACTION_CHECK',
'VMCATCHER_CACHE_ACTION_EXPIRE'])
def generateEnvDict(enviromentDict = os.environ):
FoundEnviroment = {}
EnvConfInteresting = EnvEvent.union(EnvConf)
for key in EnvConfInteresting:
if key in enviromentDict.keys():
FoundEnviroment[key] = enviromentDict[key]
return FoundEnviroment
def nullEnvDict(enviromentDict = generateEnvDict(),NullValue = ""):
FoundEnviroment = enviromentDict.copy()
enviromentKeysSet = set(FoundEnviroment.keys())
EnvConfInteresting = EnvEvent.union(EnvConf)
missing = EnvConfInteresting.difference(enviromentKeysSet)
for item in missing:
FoundEnviroment[item] = NullValue
return FoundEnviroment
def main():
reload(sys)
sys.setdefaultencoding('utf-8')
log = logging.getLogger("main")
p = optparse.OptionParser(version = "%prog " + "0.0.1")
p.add_option('--output_file', action ='store',help='Output file to log event.', metavar='OUTPUT_LOGFILE')
p.add_option('--generator', action ='store',help='Set the dictionary Generator.', metavar='minium')
p.add_option('--datetime', action ='store_true',help='Add a datetime stamp.')
p.add_option('--logcfg', action ='store',help='Logfile configuration file.', metavar='LOGCFGFILE')
options, arguments = p.parse_args()
if options.logcfg:
if os.path.isfile(str(options.logfile)):
logging.config.fileConfig(options.logfile)
else:
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("main")
log.error("Logfile configuration file '%s' was not found." % (options.logfile))
sys.exit(1)
else:
logging.basicConfig(level=logging.INFO)
outputDict = None
generator = nullEnvDict()
if options.generator:
if options.generator == "minium":
generator = generateEnvDict()
if options.datetime:
generator['DATETIME'] = datetime.datetime.now().strftime(time_format_definition)
if options.output_file:
with open(options.output_file, "a") as myfile:
json.dump(generator, myfile, sort_keys=True)
myfile.write('\n')
else:
log.info("'--help' for options.")
if __name__ == "__main__":
main()