-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlustre-disk-space-usage-collect.py
executable file
·114 lines (81 loc) · 3.34 KB
/
lustre-disk-space-usage-collect.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
#!/usr/bin/env python3
#
# -*- coding: utf-8 -*-
#
# © Copyright 2023 GSI Helmholtzzentrum für Schwerionenforschung
#
# This software is distributed under
# the terms of the GNU General Public Licence version 3 (GPL Version 3),
# copied verbatim in the file "LICENCE".
import configparser
import logging
import argparse
import time
import sys
import os
import database.disk_space_usage_collect as dsuc
import dataset.lfs_dataset_handler as ldh
def main():
RUN_MODE = 'collect'
parser = argparse.ArgumentParser(description='')
parser.add_argument('--create-table', dest='create_table',
required=False, action='store_true',
help='Creates the group quota history table.')
parser.add_argument('-f', '--config-file', dest='config_file', type=str,
required=True, help='Path of the config file.')
parser.add_argument('-i', '--input-file', dest='input_file',
type=str, required=False, help='Path of the input file.')
parser.add_argument('-m', '--run-mode', dest='run_mode', type=str,
default=RUN_MODE, required=False,
help="Specifies the run mode: 'print' or 'collect' - Default: %s" %
RUN_MODE)
parser.add_argument('-D', '--enable-debug', dest='enable_debug',
required=False, action='store_true',
help='Enables logging of debug messages.')
args = parser.parse_args()
if not os.path.isfile(args.config_file):
raise IOError("The config file does not exist or is not a file: %s"
% args.config_file)
logging_level = logging.INFO
if args.enable_debug:
logging_level = logging.DEBUG
logging.basicConfig(level=logging_level,
format='%(asctime)s - %(levelname)s: %(message)s')
if not (args.run_mode == 'print' or args.run_mode == 'collect'):
raise RuntimeError("Invalid run mode: %s" % args.run_mode)
if not args.create_table and not os.path.isfile(args.input_file):
raise IOError("The input file does not exist or is not a file: %s" % args.input_file)
input_data = None
try:
logging.info('START')
date_today = time.strftime('%Y-%m-%d')
config = configparser.ConfigParser()
config.read(args.config_file)
if args.create_table:
dsuc.create_disk_space_usage_table(config)
logging.info('END')
sys.exit(0)
fs = config.get('lustre', 'file_system')
storage_info_list = None
if args.input_file:
storage_info_list = ldh.create_storage_info(fs, args.input_file).values()
else:
storage_info_list = ldh.create_storage_info(fs).values()
if args.run_mode == 'print':
for item in storage_info_list:
logging.info("Date: %s - Mounted on: %s - Total: %s - Free: %s - Used: %s - Usage Percentage: %s" \
% (date_today,
item.mount_point,
item.ost.total,
item.ost.free,
item.ost.used,
item.ost.used_percentage()))
if args.run_mode == 'collect':
dsuc.store_disk_space_usage(config, date_today, storage_info_list)
logging.info('END')
sys.exit(0)
except Exception:
logging.exception('Caught exception in main')
sys.exit(1)
if __name__ == '__main__':
main()