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");
59 rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY);
60 if (rrd_file == NULL) {
65 if (rrd_lock(rrd_file) != 0) {
66 rrd_set_error("could not lock original RRD");
73 if (target_rra >= rrdold.stat_head->rra_cnt) {
74 rrd_set_error("no such RRA in this RRD");
81 if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
82 rrd_set_error("This RRA is not that big");
89 /* These need to be initialised before calling rrd_open() with
90 the RRD_CREATE flag */
91 if ((rrdnew.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
92 rrd_set_error("allocating stat_head for new RRD");
98 if ((rrdnew.rra_def = malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
99 rrd_set_error("allocating rra_def for new RRD");
103 rrd_close(rrd_out_file);
107 memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
108 memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);
110 /* Set this so that the file will be created with the correct size */
111 rrdnew.rra_def[target_rra].row_cnt += modify;
113 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
114 if (rrd_out_file == NULL) {
115 rrd_set_error("Can't create '%s': %s", outfilename,
116 rrd_strerror(errno));
120 if (rrd_lock(rrd_out_file) != 0) {
121 rrd_set_error("could not lock new RRD");
124 rrd_close(rrd_out_file);
127 /*XXX: do one write for those parts of header that are unchanged */
128 if ((rrdnew.rra_ptr = malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
129 rrd_set_error("allocating rra_ptr for new RRD");
133 rrd_close(rrd_out_file);
137 /* Put this back the way it was so that the rest of the algorithm
138 below remains unchanged, it will be corrected later */
139 rrdnew.rra_def[target_rra].row_cnt -= modify;
141 rrdnew.ds_def = rrdold.ds_def;
142 rrdnew.live_head = rrdold.live_head;
143 rrdnew.pdp_prep = rrdold.pdp_prep;
144 rrdnew.cdp_prep = rrdold.cdp_prep;
145 memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
148 version = atoi(rrdold.stat_head->version);
155 rrdold.stat_head->version[3] = '3';
158 rrd_set_error("Do not know how to handle RRD version %s",
159 rrdold.stat_head->version);
163 rrd_close(rrd_out_file);
168 /* XXX: Error checking? */
169 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
170 rrd_write(rrd_out_file, rrdnew.ds_def,
171 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
172 rrd_write(rrd_out_file, rrdnew.rra_def,
173 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
174 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
175 rrd_write(rrd_out_file, rrdnew.pdp_prep,
176 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
177 rrd_write(rrd_out_file, rrdnew.cdp_prep,
178 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
179 rrdnew.stat_head->rra_cnt);
180 rrd_write(rrd_out_file, rrdnew.rra_ptr,
181 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
183 /* Move the CDPs from the old to the new database.
184 ** This can be made (much) faster but isn't worth the effort. Clarity
185 ** is much more important.
188 /* Move data in unmodified RRAs
191 for (rra = 0; rra < target_rra; rra++) {
192 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
195 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
196 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
199 /* Move data in this RRA, either removing or adding some rows
202 /* Adding extra rows; insert unknown values just after the
203 ** current row number.
205 l = rrdnew.stat_head->ds_cnt *
206 (rrdnew.rra_ptr[target_rra].cur_row + 1);
208 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
209 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
214 l = rrdnew.stat_head->ds_cnt * modify;
216 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
220 /* for the mmap case, we did already fill the whole new file with DNAN
221 * before we copied the old values, so nothing to do here. */
224 /* Removing rows. Normally this would be just after the cursor
225 ** however this may also mean that we wrap to the beginning of
228 signed long int remove_end = 0;
231 (rrdnew.rra_ptr[target_rra].cur_row -
232 modify) % rrdnew.rra_def[target_rra].row_cnt;
234 (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
235 while (remove_end >= 0) {
237 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
239 rrdnew.rra_ptr[target_rra].cur_row--;
240 rrdnew.rra_def[target_rra].row_cnt--;
244 remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
246 for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
249 for (tmp = 0; tmp < rrdnew.stat_head->ds_cnt; tmp++) {
250 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
251 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
256 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
258 rrdnew.rra_def[target_rra].row_cnt--;
262 /* Move the rest of the CDPs
265 if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
267 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
269 rrdnew.rra_def[target_rra].row_cnt += modify;
270 rrd_seek(rrd_out_file,
271 sizeof(stat_head_t) +
272 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt, SEEK_SET);
273 rrd_write(rrd_out_file, rrdnew.rra_def,
274 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
275 rrd_seek(rrd_out_file, sizeof(live_head_t), SEEK_CUR);
276 rrd_seek(rrd_out_file, sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt,
278 rrd_seek(rrd_out_file,
279 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
280 rrdnew.stat_head->rra_cnt, SEEK_CUR);
281 rrd_write(rrd_out_file, rrdnew.rra_ptr,
282 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
284 rrd_close(rrd_out_file);