forked from fasrc/prometheus-slurm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsacct_push_metrics.py
217 lines (188 loc) · 9.25 KB
/
sacct_push_metrics.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
import csv
from typing import List, Tuple, Dict
import time
import os
from prometheus_client.core import GaugeMetricFamily, REGISTRY
from prometheus_client import start_http_server
def read_file_to_dict(file_path: str, include_index=False, start_index=1) -> Tuple[Dict[str, Dict[str, float]], int]:
"""
Reads data from a single file and stores it in a dictionary.
:param file_path: Path to the CSV file.
:param include_index: Whether to include an incrementing index (e.g., A1, A2, ...) in the dictionary.
:param start_index: The starting index for the 'A' labels.
:return: A tuple of (dictionary with data, next available index).
"""
data = {}
current_index = start_index
with open(file_path, 'r') as file:
reader = csv.reader(file)
for row in reader:
if len(row) >= 3: # Ensure there are enough columns
name_id = row[0].split('=')[1].strip()
gpu_hours = float(row[1].split('=')[1].strip())
gpu_tres_hours = float(row[2].split('=')[1].strip())
if include_index:
# Create an index label like A1, A2, ...
index_label = f"A{current_index}"
data[name_id] = {'index': index_label, 'gpu_hours': gpu_hours, 'gpu_tres_hours': gpu_tres_hours}
current_index += 1
else:
# Store without index
data[name_id] = {'gpu_hours': gpu_hours, 'gpu_tres_hours': gpu_tres_hours}
return data, current_index
def read_file_pairs(file_pairs: List[Tuple[str, str]]):
"""
Reads file pairs one at a time and stores data in six dictionaries.
:param file_pairs: List of tuples containing file paths for each category (partition, group, user).
:return: Six dictionaries for each data type and sum data.
"""
# Initialize separate dictionaries for each type and sum type
partition_dict = {}
partition_dict_sum = {}
group_dict = {}
group_dict_sum = {}
user_dict = {}
user_dict_sum = {}
# Initialize starting index for 'A' labels for each dictionary
partition_index = 1
partition_sum_index = 1
group_index = 1
group_sum_index = 1
user_index = 1
user_sum_index = 1
for file_sum, file_regular in file_pairs:
# Determine which dictionary to update based on the file path
if 'partition' in file_sum:
# Update partition sum and regular dictionaries with unique indices
partition_dict_sum_data, partition_sum_index = read_file_to_dict(file_sum, include_index=True, start_index=partition_sum_index)
partition_dict_data, partition_index = read_file_to_dict(file_regular, include_index=True, start_index=partition_index)
partition_dict_sum.update(partition_dict_sum_data)
partition_dict.update(partition_dict_data)
elif 'group' in file_sum:
# Update group sum and regular dictionaries with unique indices
group_dict_sum_data, group_sum_index = read_file_to_dict(file_sum, include_index=True, start_index=group_sum_index)
group_dict_data, group_index = read_file_to_dict(file_regular, include_index=True, start_index=group_index)
group_dict_sum.update(group_dict_sum_data)
group_dict.update(group_dict_data)
elif 'user' in file_sum:
# Update user sum and regular dictionaries with unique indices
user_dict_sum_data, user_sum_index = read_file_to_dict(file_sum, include_index=True, start_index=user_sum_index)
user_dict_data, user_index = read_file_to_dict(file_regular, include_index=True, start_index=user_index)
user_dict_sum.update(user_dict_sum_data)
user_dict.update(user_dict_data)
return partition_dict, partition_dict_sum, group_dict, group_dict_sum, user_dict, user_dict_sum
# Example usage
file_pairs = [
('/tmp/sacct_tmp_files/partition_dictionary_sum.csv', '/tmp/sacct_tmp_files/partition_dictionary.csv'),
('/tmp/sacct_tmp_files/group_dictionary_sum.csv', '/tmp/sacct_tmp_files/group_dictionary.csv'),
('/tmp/sacct_tmp_files/user_dictionary_sum.csv', '/tmp/sacct_tmp_files/user_dictionary.csv')
]
# Call the function and store the results in separate dictionaries
partition_dict, partition_dict_sum, group_dict, group_dict_sum, user_dict, user_dict_sum = read_file_pairs(file_pairs)
class SlurmKempnerSacctsCollector:
def collect(self):
# Create GaugeMetricFamily for gpu_hours and gpu_tres_hours with name_id and index labels
day_gpu_hours_part_metric = GaugeMetricFamily(
'day_gpu_part_hours',
'Total GPU hours for partition',
labels=['name_id', 'index']
)
day_gpu_tres_hours_part_metric = GaugeMetricFamily(
'day_gpu_tres_part_hours',
'Total GPU hours for partition',
labels=['name_id', 'index']
)
day_gpu_hours_group_metric = GaugeMetricFamily(
'day_gpu_group_hours',
'Total GPU hours for group',
labels=['name_id', 'index']
)
day_gpu_tres_hours_group_metric = GaugeMetricFamily(
'day_gpu_tres_group_hours',
'Total GPU hours for group',
labels=['name_id', 'index']
)
day_gpu_hours_user_metric = GaugeMetricFamily(
'day_gpu_user_hours',
'Total GPU hours for user',
labels=['name_id', 'index']
)
day_gpu_tres_hours_user_metric = GaugeMetricFamily(
'day_gpu_tres_user_hours',
'Total GPU hours for user',
labels=['name_id', 'index']
)
tot_gpu_hours_part_metric = GaugeMetricFamily(
'tot_gpu_part_hours',
'Total GPU hours for partition',
labels=['name_id', 'index']
)
tot_gpu_tres_hours_part_metric = GaugeMetricFamily(
'tot_gpu_tres_part_hours',
'Cumulative Total GPU hours for partition',
labels=['name_id', 'index']
)
tot_gpu_hours_group_metric = GaugeMetricFamily(
'tot_gpu_group_hours',
'Cumulative Total GPU hours for group',
labels=['name_id', 'index']
)
tot_gpu_tres_hours_group_metric = GaugeMetricFamily(
'tot_gpu_tres_group_hours',
'Cumulative Total GPU hours for group',
labels=['name_id', 'index']
)
tot_gpu_hours_user_metric = GaugeMetricFamily(
'tot_gpu_user_hours',
'Cumulative Total GPU hours for user',
labels=['name_id', 'index']
)
tot_gpu_tres_hours_user_metric = GaugeMetricFamily(
'tot_gpu_tres_user_hours',
'Cumulative Total GPU hours for user',
labels=['name_id', 'index']
)
# Add metrics from partition_dict, group_dict, user_dict
for name_id, metrics in partition_dict.items():
index = metrics['index']
day_gpu_hours_part_metric.add_metric([name_id, index], metrics['gpu_hours'])
day_gpu_tres_hours_part_metric.add_metric([name_id, index], metrics['gpu_tres_hours'])
for name_id, metrics in group_dict.items():
index = metrics['index']
day_gpu_hours_group_metric.add_metric([name_id, index], metrics['gpu_hours'])
day_gpu_tres_hours_group_metric.add_metric([name_id, index], metrics['gpu_tres_hours'])
for name_id, metrics in user_dict.items():
index = metrics['index']
day_gpu_hours_user_metric.add_metric([name_id, index], metrics['gpu_hours'])
day_gpu_tres_hours_user_metric.add_metric([name_id, index], metrics['gpu_tres_hours'])
# Add metrics from partition_dict_sum, group_dict_sum, user_dict_sum
for name_id, metrics in partition_dict_sum.items():
index = metrics['index']
tot_gpu_hours_part_metric.add_metric([name_id, index], metrics['gpu_hours'])
tot_gpu_tres_hours_part_metric.add_metric([name_id, index], metrics['gpu_tres_hours'])
for name_id, metrics in group_dict_sum.items():
index = metrics['index']
tot_gpu_hours_group_metric.add_metric([name_id, index], metrics['gpu_hours'])
tot_gpu_tres_hours_group_metric.add_metric([name_id, index], metrics['gpu_tres_hours'])
for name_id, metrics in user_dict_sum.items():
index = metrics['index']
tot_gpu_hours_user_metric.add_metric([name_id, index], metrics['gpu_hours'])
tot_gpu_tres_hours_user_metric.add_metric([name_id, index], metrics['gpu_tres_hours'])
# Yield metrics to Prometheus
yield day_gpu_hours_part_metric
yield day_gpu_tres_hours_part_metric
yield day_gpu_hours_group_metric
yield day_gpu_tres_hours_group_metric
yield day_gpu_hours_user_metric
yield day_gpu_tres_hours_user_metric
yield tot_gpu_hours_part_metric
yield tot_gpu_tres_hours_part_metric
yield tot_gpu_hours_group_metric
yield tot_gpu_tres_hours_group_metric
yield tot_gpu_hours_user_metric
yield tot_gpu_tres_hours_user_metric
if __name__ == "__main__":
start_http_server(10003)
REGISTRY.register(SlurmKempnerSacctsCollector())
while True:
time.sleep(160)