1 /*****************************************************************************
2 * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008
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 (rrd_lock(rrd_file) != 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 /* yes we are abusing the float cookie for this, aargh */
86 if ((rrdnew.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
87 rrd_set_error("allocating stat_head for new RRD");
92 rrdnew.stat_head->float_cookie = rrd_file->file_len +
93 (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
94 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
95 if (rrd_out_file == NULL) {
96 rrd_set_error("Can't create '%s': %s", outfilename,
101 if (rrd_lock(rrd_out_file) != 0) {
102 rrd_set_error("could not lock new RRD");
105 rrd_close(rrd_out_file);
108 /*XXX: do one write for those parts of header that are unchanged */
109 rrdnew.stat_head = rrdold.stat_head;
110 rrdnew.ds_def = rrdold.ds_def;
111 rrdnew.rra_def = rrdold.rra_def;
112 rrdnew.live_head = rrdold.live_head;
113 rrdnew.pdp_prep = rrdold.pdp_prep;
114 rrdnew.cdp_prep = rrdold.cdp_prep;
115 rrdnew.rra_ptr = rrdold.rra_ptr;
117 version = atoi(rrdold.stat_head->version);
122 rrdold.stat_head->version[3] = '3';
125 rrd_set_error("Do not know how to handle RRD version %s",
126 rrdold.stat_head->version);
133 /* XXX: Error checking? */
134 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
135 rrd_write(rrd_out_file, rrdnew.ds_def,
136 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
137 rrd_write(rrd_out_file, rrdnew.rra_def,
138 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
139 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
140 rrd_write(rrd_out_file, rrdnew.pdp_prep,
141 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
142 rrd_write(rrd_out_file, rrdnew.cdp_prep,
143 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
144 rrdnew.stat_head->rra_cnt);
145 rrd_write(rrd_out_file, rrdnew.rra_ptr,
146 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
148 /* Move the CDPs from the old to the new database.
149 ** This can be made (much) faster but isn't worth the effort. Clarity
150 ** is much more important.
153 /* Move data in unmodified RRAs
156 for (rra = 0; rra < target_rra; rra++) {
157 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
160 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
161 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
164 /* Move data in this RRA, either removing or adding some rows
167 /* Adding extra rows; insert unknown values just after the
168 ** current row number.
170 l = rrdnew.stat_head->ds_cnt *
171 (rrdnew.rra_ptr[target_rra].cur_row + 1);
173 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
174 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
179 l = rrdnew.stat_head->ds_cnt * modify;
181 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
185 /* for the mmap case, we did already fill the whole new file with DNAN
186 * before we copied the old values, so nothing to do here. */
189 /* Removing rows. Normally this would be just after the cursor
190 ** however this may also mean that we wrap to the beginning of
193 signed long int remove_end = 0;
196 (rrdnew.rra_ptr[target_rra].cur_row -
197 modify) % rrdnew.rra_def[target_rra].row_cnt;
199 (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
200 while (remove_end >= 0) {
202 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
204 rrdnew.rra_ptr[target_rra].cur_row--;
205 rrdnew.rra_def[target_rra].row_cnt--;
209 remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
211 for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
214 for (tmp = 0; tmp < rrdnew.stat_head->ds_cnt; tmp++) {
215 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
216 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
221 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
223 rrdnew.rra_def[target_rra].row_cnt--;
227 /* Move the rest of the CDPs
230 if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
232 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
234 rrdnew.rra_def[target_rra].row_cnt += modify;
235 rrd_seek(rrd_out_file,
236 sizeof(stat_head_t) +
237 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt, SEEK_SET);
238 rrd_write(rrd_out_file, rrdnew.rra_def,
239 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
240 rrd_seek(rrd_out_file, sizeof(live_head_t), SEEK_CUR);
241 rrd_seek(rrd_out_file, sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt,
243 rrd_seek(rrd_out_file,
244 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
245 rrdnew.stat_head->rra_cnt, SEEK_CUR);
246 rrd_write(rrd_out_file, rrdnew.rra_ptr,
247 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
252 rrd_close(rrd_out_file);