@@ -46,7 +46,7 @@ def save_dashboard(config, args, base_path, dashboard_name, dashboard, action):
46
46
try :
47
47
output = open (output_file , "w" )
48
48
except OSError as e :
49
- print ("File {0} error: {1}." .format (output_file , e .strerror ))
49
+ logger . error ("File {0} error: {1}." .format (output_file , e .strerror ))
50
50
sys .exit (2 )
51
51
52
52
content = None
@@ -56,7 +56,7 @@ def save_dashboard(config, args, base_path, dashboard_name, dashboard, action):
56
56
content = json .dumps (dashboard ["dashboard" ])
57
57
output .write (content )
58
58
output .close ()
59
- print (f"OK: Dashboard '{ dashboard_name } ' { action } to: { output_file } " )
59
+ logger . info (f"OK: Dashboard '{ dashboard_name } ' { action } to: { output_file } " )
60
60
61
61
62
62
class myArgs :
@@ -212,7 +212,7 @@ def main():
212
212
if args .action == "exporter" and (
213
213
"dashboard_name" not in config ["general" ] or config ["general" ]["dashboard_name" ] is None
214
214
):
215
- print ("ERROR: no dashboard has been specified." )
215
+ logger . error ("ERROR: no dashboard has been specified." )
216
216
sys .exit (1 )
217
217
218
218
config ["check_folder" ] = False
@@ -238,29 +238,46 @@ def main():
238
238
try :
239
239
grafana_api = Grafana .Grafana (** params )
240
240
except Exception as e :
241
- print (f"ERROR: { e } " )
241
+ logger . error (f"ERROR: { e } " )
242
242
sys .exit (1 )
243
243
244
244
# Import
245
245
if args .action == "import" :
246
246
if args .dashboard_file is None :
247
- print ("ERROR: no file to import provided!" )
247
+ logger . error ("ERROR: no file to import provided!" )
248
248
sys .exit (1 )
249
249
250
250
# Compute effective input file path.
251
251
import_path = ""
252
252
import_file = args .dashboard_file
253
+ import_files = []
254
+
253
255
if not re .search (r"^(?:(?:/)|(?:\.?\./))" , import_file ):
254
256
import_path = base_path
255
- if "imports_path" in config ["general" ]:
256
- import_path = os .path .join (import_path , config ["general" ]["imports_path" ])
257
- import_file = os .path .join (import_path , import_file )
258
-
259
- def process_dashboard ():
257
+ if "import_path" in config ["general" ]:
258
+ import_path = os .path .join (import_path , config ["general" ]["import_path" ])
259
+ import_file = os .path .join (import_path , import_file )
260
+ import_files .append (import_file )
261
+ else :
262
+ if os .path .isfile (import_file ):
263
+ logger .info (f"The path is a file: '{ import_file } '" )
264
+ import_file = os .path .join (import_path , import_file )
265
+ import_files .append (import_file )
266
+
267
+ if os .path .isdir (import_file ):
268
+ logger .info (f"The path is a directory: '{ import_file } '" )
269
+ import_files = [
270
+ os .path .join (import_file , f )
271
+ for f in os .listdir (import_file )
272
+ if os .path .isfile (os .path .join (import_file , f ))
273
+ ]
274
+ logger .info (f"Found the following files: '{ import_files } ' in dir '{ import_file } '" )
275
+
276
+ def process_dashboard (file_path ):
260
277
try :
261
- dash = read_dashboard_file (import_file )
278
+ dash = read_dashboard_file (file_path )
262
279
except Exception as ex :
263
- msg = f"Failed to load dashboard from: { import_file } . Reason: { ex } "
280
+ msg = f"Failed to load dashboard from: { file_path } . Reason: { ex } "
264
281
logger .exception (msg )
265
282
raise IOError (msg ) from ex
266
283
@@ -280,13 +297,17 @@ def process_dashboard():
280
297
logger .error (msg )
281
298
raise IOError (msg )
282
299
283
- try :
284
- process_dashboard ()
285
- except Exception :
286
- sys .exit (1 )
300
+ for file in import_files :
301
+ print (f"Processing file: { file } " )
302
+ try :
303
+ process_dashboard (file )
304
+ except Exception as e :
305
+ logger .error (f"Failed to process file { file } . Reason: { str (e )} " )
306
+ continue
287
307
288
308
if args .reload :
289
- watchdog_service (import_file , process_dashboard )
309
+ for file in import_files :
310
+ watchdog_service (import_file , process_dashboard (file ))
290
311
291
312
sys .exit (0 )
292
313
@@ -295,19 +316,19 @@ def process_dashboard():
295
316
dashboard_name = config ["general" ]["dashboard_name" ]
296
317
try :
297
318
grafana_api .remove_dashboard (dashboard_name )
298
- print (f"OK: Dashboard removed: { dashboard_name } " )
319
+ logger . info (f"OK: Dashboard removed: { dashboard_name } " )
299
320
sys .exit (0 )
300
321
except Grafana .GrafanaDashboardNotFoundError as exp :
301
- print (f"KO: Dashboard not found in folder '{ exp .folder } ': { exp .dashboard } " )
322
+ logger . info (f"KO: Dashboard not found in folder '{ exp .folder } ': { exp .dashboard } " )
302
323
sys .exit (1 )
303
324
except Grafana .GrafanaFolderNotFoundError as exp :
304
- print (f"KO: Folder not found: { exp .folder } " )
325
+ logger . info (f"KO: Folder not found: { exp .folder } " )
305
326
sys .exit (1 )
306
327
except GrafanaApi .GrafanaBadInputError as exp :
307
- print (f"KO: Removing dashboard failed: { dashboard_name } . Reason: { exp } " )
328
+ logger . info (f"KO: Removing dashboard failed: { dashboard_name } . Reason: { exp } " )
308
329
sys .exit (1 )
309
330
except Exception :
310
- print ("ERROR: Dashboard '{0}' remove exception '{1}'" .format (dashboard_name , traceback .format_exc ()))
331
+ logger . info ("ERROR: Dashboard '{0}' remove exception '{1}'" .format (dashboard_name , traceback .format_exc ()))
311
332
sys .exit (1 )
312
333
313
334
# Export
@@ -316,10 +337,10 @@ def process_dashboard():
316
337
try :
317
338
dash = grafana_api .export_dashboard (dashboard_name )
318
339
except (Grafana .GrafanaFolderNotFoundError , Grafana .GrafanaDashboardNotFoundError ):
319
- print ("KO: Dashboard name not found: {0}" .format (dashboard_name ))
340
+ logger . info ("KO: Dashboard name not found: {0}" .format (dashboard_name ))
320
341
sys .exit (1 )
321
342
except Exception :
322
- print ("ERROR: Dashboard '{0}' export exception '{1}'" .format (dashboard_name , traceback .format_exc ()))
343
+ logger . info ("ERROR: Dashboard '{0}' export exception '{1}'" .format (dashboard_name , traceback .format_exc ()))
323
344
sys .exit (1 )
324
345
325
346
if dash is not None :
0 commit comments