Newer
Older
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
{
"cells": [
{
"cell_type": "markdown",
"id": "073ef418",
"metadata": {},
"source": [
"# Convert raw policy lists into pickles\n",
"\n",
"Having the raw list-policy output data converted to pickels reduces storage space, centralized and speeds later processing and reporting.\n",
"\n",
"The script reads files that match the `glob_pattern` from the provided `dirname` and writes identical file names in pickled format to the `pickledir`, optionally filtering lines by the `line_regex_filter`. If the default parameters aren't changed no files are read or written.\n",
"\n",
"Some parsing progress is available via the `verbose` flag.\n",
"\n",
"This converter assumes a policy show format defined in the [list-paths-external policy](https://code.rc.uab.edu/rc/gpfs-policy/-/blob/main/policy/list-path-external):\n",
"```\n",
" SHOW ('|size=' || varchar(FILE_SIZE) ||\n",
" '|kballoc='|| varchar(KB_ALLOCATED) ||\n",
" '|access=' || varchar(ACCESS_TIME) ||\n",
" '|create=' || varchar(CREATION_TIME) ||\n",
" '|modify=' || varchar(MODIFICATION_TIME) ||\n",
" '|uid=' || varchar(USER_ID) ||\n",
" '|gid=' || varchar(GROUP_ID) ||\n",
" '|heat=' || varchar(FILE_HEAT) ||\n",
" '|pool=' || varchar(POOL_NAME) ||\n",
" '|mode=' || varchar(MODE) ||\n",
" '|misc=' || varchar(MISC_ATTRIBUTES) ||\n",
" '|'\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af015950",
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from urllib.parse import unquote\n",
"import sys\n",
"import os\n",
"import pathlib\n",
"import re"
]
},
{
"cell_type": "markdown",
"id": "3781a0d6",
"metadata": {},
"source": [
"## input vars"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "932707e6",
"metadata": {
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"dirname=\"data/list-20191520.list.gather-info.d\" # directory to fine files to pickle\n",
"glob_pattern = \"*.gz\" # file name glob pattern to match, can be file name for individual file\n",
"line_regex_filter = \".*\" # regex to match lines of interest in file\n",
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
"\n",
"verbose = True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "833be559",
"metadata": {},
"outputs": [],
"source": [
"pickledir=f\"{dirname}/pickles\""
]
},
{
"cell_type": "markdown",
"id": "47ea1d93",
"metadata": {},
"source": [
"dirname=\"data/list-17404604.list.gather-info.d/\" # directory to fine files to pickle\n",
"glob_pattern = \"list-*.gz\" # file name glob pattern to match, can be file name for individual file\n",
"line_regex_filter = \".*\" # regex to match lines of interest in file\n",
"pickledir=f\"{dirname}/pickles\"\n",
"\n",
"verbose = True"
]
},
{
"cell_type": "markdown",
"id": "07ef745a",
"metadata": {},
"source": [
"dirname=\"data/list-16144464.list.gather-info.d/\" # directory to fine files to pickle\n",
"glob_pattern = \"list-*\" # file name glob pattern to match, can be file name for individual file\n",
"line_regex_filter = \".*\" # regex to match lines of interest in file\n",
"pickledir=f\"{dirname}/pickles\"\n",
"\n",
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
218
219
220
221
222
223
224
225
226
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5599e260",
"metadata": {},
"outputs": [],
"source": [
"# parse files with read_csv optionally filtering specific lines via regex\n",
"\n",
"def parse_file(filename, pattern=\".*\"):\n",
" \n",
" gen = pd.read_csv(filename, sep='\\n', header=None, iterator=True)\n",
" df = pd.concat((x[x[0].str.contains(pattern, regex=True)] for x in gen), ignore_index=True)\n",
"\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6542cb23",
"metadata": {},
"outputs": [],
"source": [
"# parse rows according to the list-policy-external format\n",
"\n",
"def parse_rows(df):\n",
" # split content on white space\n",
" df=df.rename(columns={0:\"details\"})\n",
" new=df[\"details\"].str.split(expand=True)\n",
" \n",
" # create a new dataframe and populate with parsed data\n",
" df = pd.DataFrame()\n",
"\n",
" # split attribuignoring filename= prefix\n",
" df[\"showattr\"] = new[3].map(lambda x: re.sub(\"\\w+=\", \"\", unquote(x)))\n",
" df[[\"ignore1\", \"size\", \"kballoc\", \"access\", \"create\", \"modify\", \n",
" \"uid\", \"gid\", \"heat\", \"pool\", \"mode\", \"misc\", \"ignore2\"]] = df[\"showattr\"].str.split(\"|\", expand=True)\n",
" df[\"path\"] = new[5].map(lambda x: unquote(x))\n",
"\n",
" # drop temp columns\n",
" df = df.drop([\"showattr\", \"ignore1\", \"ignore2\"], axis=1)\n",
"\n",
" df.reset_index(drop=True, inplace=True)\n",
"\n",
" df = set_types(df)\n",
"\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9730f207",
"metadata": {},
"outputs": [],
"source": [
"# convert data to native pandas types\n",
"def set_types(df):\n",
" df[\"size\"] = df[\"size\"].astype('int64')\n",
" df[\"kballoc\"] = df[\"kballoc\"].astype('int64')\n",
" df[\"uid\"] = df[\"uid\"].astype('int64')\n",
" df[\"gid\"] = df[\"gid\"].astype('int64')\n",
" df[\"access\"] = df[\"access\"].astype('datetime64')\n",
" df[\"create\"] = df[\"create\"].astype('datetime64')\n",
" df[\"modify\"] = df[\"modify\"].astype('datetime64')\n",
" \n",
" return df"
]
},
{
"cell_type": "markdown",
"id": "2ed6bdc8",
"metadata": {},
"source": [
"## Gather the files according to glob_pattern"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7297f0d2",
"metadata": {},
"outputs": [],
"source": [
"dirpath = pathlib.Path(dirname)\n",
"\n",
"files = list()\n",
"for file in list(dirpath.glob(glob_pattern)):\n",
" files.append(str(file))"
]
},
{
"cell_type": "markdown",
"id": "e4929a0f",
"metadata": {},
"source": [
"## Read, parse and pickle files"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2ab7f7f5",
"metadata": {},
"outputs": [],
"source": [
"for file in files:\n",
" if (verbose): print(f\"parse: {file}\")\n",
" filename=os.path.basename(file)\n",
" df = parse_rows(parse_file(file))\n",
"\n",
" ## Write the pickled data\n",
"\n",
" # only create dir if there is data to pickle\n",
" if (not os.path.isdir(pickledir)):\n",
" os.mkdir(pickledir)\n",
"\n",
" if (verbose): print(f\"pickling: {filename}\")\n",
" df.to_pickle(f\"{pickledir}/{filename}\")"