You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
temp.write(', "query": {"bool": {"must":[ {"range": {"@timestamp": {"gte": "'+args['dateTimeStart'] +'","lt": "'+args['dateTimeEnd'] +'"} } }], ') ##Establish Query with Time Range Requirements
@@ -143,9 +143,9 @@ def build_query(args):
143
143
if (("kube"inargnameor"level"inargnameor"mes"inargnameor"log"inargname) and (args[argname]) andargname.find('-ex')==-1): ##Looking for non-exclusion options that are not NoneType in args argsionary
query=" ".join([line.strip() forlineintemp]) ## Turns query into string
188
+
query=" ".join([line.strip() forlineintemp]) ## Turns query into string
189
189
temp.close()
190
190
tfile.close()
191
191
192
192
else: ##Open existing query
193
193
x=open(args['query-filename'], 'rt')
194
194
query=" ".join([line.strip() forlineinx]) ## Turn file into string, return.
195
195
x.close()
196
-
196
+
197
197
returnquery
198
198
199
199
defget_arguments():
200
200
"""List of valid arguments that are read from user as soon as program is run, nargs=+ indicates that argument takes multiple whitespace separated values. """
201
-
201
+
202
202
parser=argparse.ArgumentParser(prog='getLogs.py', usage='\n%(prog)s [options]', description="""This program generates OpenSearch DSL Queries from user specified parameters, and submits them to a database to retrieve logs. The flags below provide specifications for your Query, and can be placed in any order. \n
203
203
\033[1m NOTES: *All default values for username, password, host, and port, are derived from the ENV variables OSUSER, OSPASSWD, OSHOST, OSPORT in that respective order. '\033[0m' \n
204
204
\033[1m If you have default connections set in your environment variables, you can call this program without arguments and get the latest 10 logs from the target API in the default CSV format. \033[0m \n
205
205
Getlogs has a default set of fields that runs with every query (seen below). You can replace the default fields with your own space-separated set of fields using --fields. Ex: --fields kube.labels.sas_com/deployment properties.appname \n
206
-
*The NAMESPACE*, POD*, CONTAINER*, LOGSOURCE* and LEVEL* options accept multiple, space-separated, values (e.g. --level INFO NONE). Please refrain from passing single quotes ('') into arguments. \n
207
-
*All Generated files are placed in the current working directory.
206
+
*The NAMESPACE*, POD*, CONTAINER*, LOGSOURCE* and LEVEL* options accept multiple, space-separated, values (e.g. --level INFO NONE). Please refrain from passing single quotes ('') into arguments. \n
207
+
*All Generated files are placed in the current working directory.
208
208
Don't include https:\\ in the HOST connection setting \n\n\n\t\t\t\t QUERY SEARCH PARAMETERS: \n\n""", formatter_class=argparse.RawTextHelpFormatter)
209
-
209
+
210
210
##Search Params
211
211
parser.add_argument('-n', '--namespace', required=False, dest="kube.namespace", nargs='*', metavar="NAMESPACE", help="\nOne or more Viya deployments/Kubernetes Namespace for which logs are sought\n\n")
212
212
parser.add_argument('-nx', '--namespace-exclude', required=False, dest="kube.namespace-ex", nargs='*', metavar="NAMESPACE", help='\nOne or more namespaces for which logs should be excluded from the output\n\n')
@@ -231,7 +231,7 @@ def get_arguments():
231
231
parser.add_argument('-fi','--fields', required=False, dest="fields", nargs="*", metavar="FIELDS", default=['@timestamp', 'level', 'kube.pod', 'message'], help="\n Specify desired output columns from query. If a matching log is returned that does not have the specified field, a NULL value will be used as a placeholder. The _id field is always provided for every log message. \n Default fields: @timestamp level kube.pod message _id\n\n")
232
232
parser.add_argument('-st', '--start', required=False, dest="dateTimeStart", nargs='*', metavar="DATETIME", default=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.mktime(time.localtime()) -3600)), help="\nDatetime for start of period for which logs are sought (default: 1 hour ago). Correct format is Y-M-D H:M:S. Ex: 2023-02-16 10:00:00\n\n")
233
233
parser.add_argument('-en', '--end', required=False, dest="dateTimeEnd",nargs='*', metavar="DATETIME", default=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), help="\nDatetime for end of period for which logs are sought (default: now). \n\n\n\t\t\t CONNECTION SETTINGS: \n\n")
234
-
234
+
235
235
parser.add_argument('-i', '--index', required=False, dest="index", metavar="INDEX", default="viya_logs-*") ## help = "\nDetermine which index to perform the search in. Default: viya-logs-*\n\n
236
236
##Connection settings
237
237
parser.add_argument('-pf','--port-forward', required=False, dest="portforward", action='store_true', help="\n If this option is provided, getlogs will use the value in your KUBECONFIG (case-sensitive) environment variable to port-forward and connect to the OpenSearch API. This skips OSHOST and OSPORT, but OSUSER and OSPASSWD are stil required to authenticate and connect to the database. \n\n")
@@ -240,21 +240,45 @@ def get_arguments():
240
240
parser.add_argument('-ho', '--host', required=False, dest="host", default=os.environ.get("OSHOST"), help="\nHostname for connection to OpenSearch Please ensure that host does not contain 'https://' (default: $OSHOST)\n\n")
241
241
parser.add_argument('-po', '--port', required=False, dest="port", default=os.environ.get("OSPORT"), help="\nPort number for connection to OpenSearch (default: $OSPORT)\n\n")
242
242
parser.add_argument('-nossl', '--disable-ssl', required=False, dest="ssl", action="store_false", help="\n If this option is provided, SSL will not be used to connect to the database.\n\n")
243
+
244
+
# Add arguments for path-based ingress configuration
help="Specify if path-based ingress is used. Without a value, defaults to 'opensearch' prefix. "
248
+
"You can also specify a custom prefix, e.g. --path-based my-custom-prefix")
249
+
243
250
returnparser.parse_args().__dict__
244
251
245
252
defmain():
246
-
253
+
url_prefix=None
247
254
args=get_arguments() ##Creates "args" dictionary that contains all user submitted options. Print "args" to debug values. Note that the 'dest' value for each argument in argparser object is its key.
248
255
validate_input(args) ##Pass args dictionary for input validation
249
256
250
257
ifargs['portforward']: ##Modify connection settings if port forward is selected
251
258
args['host'] ='localhost'
252
259
args['port'] =open_port()
253
260
261
+
ifargs['path_based']:
262
+
# User specified path-based ingress - either default or custom
263
+
url_prefix=args['path_based'] # Will be "opensearch" or custom value
264
+
254
265
# Establish Client Using User Authorization and Connection Settings
266
+
267
+
# Create hosts configuration
268
+
host_config= {
269
+
'host': args['host'],
270
+
'port': args['port']
271
+
}
272
+
ifurl_prefix:
273
+
host_config['url_prefix'] =url_prefix
274
+
print(f"Using path-based ingress with prefix: {url_prefix}")
0 commit comments