Skip to content

Commit ec9dbd4

Browse files
authored
Merge pull request syslog-ng#4875 from MrAnno/diskq-compact
dqtool: add option for truncating (compacting) abandoned disk-buffers
2 parents 77d3bf4 + 7557049 commit ec9dbd4

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

modules/diskq/dqtool.c

+57-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ gchar *assign_persist_name;
5050
gboolean relocate_all;
5151
gboolean display_version;
5252
gboolean assign_help;
53+
gboolean truncate_confirm;
5354

5455
static GOptionEntry cat_options[] =
5556
{
@@ -65,6 +66,12 @@ static GOptionEntry info_options[] =
6566
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL }
6667
};
6768

69+
static GOptionEntry truncate_options[] =
70+
{
71+
{ "force", 'f', 0, G_OPTION_ARG_NONE, &truncate_confirm },
72+
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL }
73+
};
74+
6875
static GOptionEntry relocate_options[] =
6976
{
7077
{
@@ -100,9 +107,9 @@ static GOptionEntry assign_options[] =
100107
};
101108

102109
static gboolean
103-
open_queue(char *filename, LogQueue **lq, DiskQueueOptions *options)
110+
open_queue(char *filename, LogQueue **lq, DiskQueueOptions *options, gboolean read_only)
104111
{
105-
options->read_only = TRUE;
112+
options->read_only = read_only;
106113
options->reliable = FALSE;
107114
FILE *f = fopen(filename, "rb");
108115
if (f)
@@ -142,6 +149,51 @@ open_queue(char *filename, LogQueue **lq, DiskQueueOptions *options)
142149
return TRUE;
143150
}
144151

152+
static inline off_t
153+
get_file_size(const char *path)
154+
{
155+
struct stat s;
156+
if (stat(path, &s) != 0)
157+
return 0;
158+
159+
return s.st_size;
160+
}
161+
162+
static gint
163+
dqtool_truncate(int argc, char *argv[])
164+
{
165+
if (!truncate_confirm)
166+
{
167+
printf("Truncating disk-buffer files is not recommended in case they are actively used by syslog-ng.\n"
168+
"Repeat this command with the --force flag if you are sure you want to truncate them.\n");
169+
return 1;
170+
}
171+
172+
for (gint i = optind; i < argc; i++)
173+
{
174+
LogQueue *lq;
175+
DiskQueueOptions options = {0};
176+
disk_queue_options_set_default_options(&options);
177+
178+
options.truncate_size_ratio = 0;
179+
off_t orig_size = get_file_size(argv[i]);
180+
181+
if (!open_queue(argv[i], &lq, &options, FALSE))
182+
continue;
183+
184+
gboolean persistent;
185+
log_queue_disk_stop(lq, &persistent);
186+
log_queue_unref(lq);
187+
188+
off_t truncated_size = get_file_size(argv[i]);
189+
190+
double reclaimed_gib = (orig_size - truncated_size) / 1024.0 / 1024.0 / 1024.0;
191+
printf("Disk-buffer %s has been truncated, reclaimed space: %f GiB\n", argv[i], reclaimed_gib);
192+
}
193+
194+
return 0;
195+
}
196+
145197
static gint
146198
dqtool_cat(int argc, char *argv[])
147199
{
@@ -178,7 +230,7 @@ dqtool_cat(int argc, char *argv[])
178230
LogPathOptions local_options = LOG_PATH_OPTIONS_INIT;
179231
LogQueue *lq;
180232

181-
if (!open_queue(argv[i], &lq, &options))
233+
if (!open_queue(argv[i], &lq, &options, TRUE))
182234
continue;
183235

184236
log_queue_rewind_backlog_all(lq);
@@ -200,7 +252,6 @@ dqtool_cat(int argc, char *argv[])
200252
}
201253
g_string_free(msg, TRUE);
202254
return 0;
203-
204255
}
205256

206257
static gint
@@ -213,7 +264,7 @@ dqtool_info(int argc, char *argv[])
213264
DiskQueueOptions options = {0};
214265
disk_queue_options_set_default_options(&options);
215266

216-
if (!open_queue(argv[i], &lq, &options))
267+
if (!open_queue(argv[i], &lq, &options, TRUE))
217268
continue;
218269

219270
gboolean persistent;
@@ -629,6 +680,7 @@ static struct
629680
{ "info", info_options, "Print infos about the given disk queue file", dqtool_info },
630681
{ "relocate", relocate_options, "Relocate(rename) diskq file. Note that this option modifies the persist file.", dqtool_relocate },
631682
{ "assign", assign_options, "Assign diskq file to the given persist file with the given persist name.", dqtool_assign },
683+
{ "truncate", truncate_options, "Truncate unused space in abandoned disk queues", dqtool_truncate },
632684
{ NULL, NULL },
633685
};
634686

0 commit comments

Comments
 (0)