1 /*****************************************************************************
2 * RRDtool 1.2.23 Copyright by Tobi Oetiker, 1997-2007
3 *****************************************************************************
4 * rrd_resize.c Alters size of an RRA
5 *****************************************************************************
6 * Initial version by Alex van den Bogaerdt
7 *****************************************************************************/
15 char *infilename, outfilename[11] = "resize.rrd";
21 unsigned long target_rra;
22 int grow = 0, shrink = 0;
24 rrd_file_t *rrd_file, *rrd_out_file;
27 if (!strcmp(infilename, "resize.rrd")) {
28 rrd_set_error("resize.rrd is a reserved name");
32 rrd_set_error("wrong number of parameters");
36 target_rra = strtol(argv[2], &endptr, 0);
38 if (!strcmp(argv[3], "GROW"))
40 else if (!strcmp(argv[3], "SHRINK"))
43 rrd_set_error("I can only GROW or SHRINK");
47 modify = strtol(argv[4], &endptr, 0);
50 rrd_set_error("Please grow or shrink with at least 1 row");
58 rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY);
59 if (rrd_file == NULL) {
63 if (LockRRD(rrd_file->fd) != 0) {
64 rrd_set_error("could not lock original RRD");
70 if (target_rra >= rrdold.stat_head->rra_cnt) {
71 rrd_set_error("no such RRA in this RRD");
78 if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
79 rrd_set_error("This RRA is not that big");
84 /* the size of the new file */
85 if ((rrdnew.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
86 rrd_set_error("allocating stat_head for new RRD");
91 rrdnew.stat_head->float_cookie = rrd_file->file_len +
92 (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
93 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
94 if (rrd_out_file == NULL) {
95 rrd_set_error("Can't create '%s': %s", outfilename,
100 if (LockRRD(rrd_out_file->fd) != 0) {
101 rrd_set_error("could not lock new RRD");
104 rrd_close(rrd_out_file);
107 /*XXX: do one write for those parts of header that are unchanged */
108 rrdnew.stat_head = rrdold.stat_head;
109 rrdnew.ds_def = rrdold.ds_def;
110 rrdnew.rra_def = rrdold.rra_def;
111 rrdnew.live_head = rrdold.live_head;
112 rrdnew.pdp_prep = rrdold.pdp_prep;
113 rrdnew.cdp_prep = rrdold.cdp_prep;
114 rrdnew.rra_ptr = rrdold.rra_ptr;
116 version = atoi(rrdold.stat_head->version);
121 rrdold.stat_head->version[3] = '3';
124 rrd_set_error("Do not know how to handle RRD version %s",
125 rrdold.stat_head->version);
132 /* XXX: Error checking? */
133 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
134 rrd_write(rrd_out_file, rrdnew.ds_def,
135 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
136 rrd_write(rrd_out_file, rrdnew.rra_def,
137 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
138 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
139 rrd_write(rrd_out_file, rrdnew.pdp_prep,
140 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
141 rrd_write(rrd_out_file, rrdnew.cdp_prep,
142 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
143 rrdnew.stat_head->rra_cnt);
144 rrd_write(rrd_out_file, rrdnew.rra_ptr,
145 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
147 /* Move the CDPs from the old to the new database.
148 ** This can be made (much) faster but isn't worth the effort. Clarity
149 ** is much more important.
152 /* Move data in unmodified RRAs
155 for (rra = 0; rra < target_rra; rra++) {
156 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
159 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
160 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
163 /* Move data in this RRA, either removing or adding some rows
166 /* Adding extra rows; insert unknown values just after the
167 ** current row number.
169 l = rrdnew.stat_head->ds_cnt *
170 (rrdnew.rra_ptr[target_rra].cur_row + 1);
172 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
173 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
178 l = rrdnew.stat_head->ds_cnt * modify;
180 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
184 /* for the mmap case, we did already fill the whole new file with DNAN
185 * before we copied the old values, so nothing to do here. */
188 /* Removing rows. Normally this would be just after the cursor
189 ** however this may also mean that we wrap to the beginning of
192 signed long int remove_end = 0;
195 (rrdnew.rra_ptr[target_rra].cur_row -
196 modify) % rrdnew.rra_def[target_rra].row_cnt;
198 (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
199 while (remove_end >= 0) {
201 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
203 rrdnew.rra_ptr[target_rra].cur_row--;
204 rrdnew.rra_def[target_rra].row_cnt--;
208 remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
210 for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
213 for (tmp = 0; tmp < rrdnew.stat_head->ds_cnt; tmp++) {
214 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
215 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
220 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
222 rrdnew.rra_def[target_rra].row_cnt--;
226 /* Move the rest of the CDPs
229 if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
231 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
233 rrdnew.rra_def[target_rra].row_cnt += modify;
234 rrd_seek(rrd_out_file,
235 sizeof(stat_head_t) +
236 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt, SEEK_SET);
237 rrd_write(rrd_out_file, rrdnew.rra_def,
238 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
239 rrd_seek(rrd_out_file, sizeof(live_head_t), SEEK_CUR);
240 rrd_seek(rrd_out_file, sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt,
242 rrd_seek(rrd_out_file,
243 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
244 rrdnew.stat_head->rra_cnt, SEEK_CUR);
245 rrd_write(rrd_out_file, rrdnew.rra_ptr,
246 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
251 rrd_close(rrd_out_file);