1 /*****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 *****************************************************************************
4 * rrd_resize.c Alters size of an RRA
5 *****************************************************************************
6 * Initial version by Alex van den Bogaerdt
7 *****************************************************************************/
17 char *infilename, outfilename[11] = "resize.rrd";
23 unsigned long target_rra;
24 int grow = 0, shrink = 0;
26 rrd_file_t *rrd_file, *rrd_out_file;
29 if (!strcmp(infilename, "resize.rrd")) {
30 rrd_set_error("resize.rrd is a reserved name");
34 rrd_set_error("wrong number of parameters");
38 target_rra = strtol(argv[2], &endptr, 0);
40 if (!strcmp(argv[3], "GROW"))
42 else if (!strcmp(argv[3], "SHRINK"))
45 rrd_set_error("I can only GROW or SHRINK");
49 modify = strtol(argv[4], &endptr, 0);
52 rrd_set_error("Please grow or shrink with at least 1 row");
61 rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY);
62 if (rrd_file == NULL) {
67 if (rrd_lock(rrd_file) != 0) {
68 rrd_set_error("could not lock original RRD");
75 if (target_rra >= rrdold.stat_head->rra_cnt) {
76 rrd_set_error("no such RRA in this RRD");
83 if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
84 rrd_set_error("This RRA is not that big");
91 /* These need to be initialised before calling rrd_open() with
92 the RRD_CREATE flag */
94 if ((rrdnew.stat_head = (stat_head_t*)calloc(1, sizeof(stat_head_t))) == NULL) {
95 rrd_set_error("allocating stat_head for new RRD");
100 memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
102 if ((rrdnew.rra_def = (rra_def_t *)malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
103 rrd_set_error("allocating rra_def for new RRD");
109 memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);
111 /* Set this so that the file will be created with the correct size */
112 rrdnew.rra_def[target_rra].row_cnt += modify;
114 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
115 if (rrd_out_file == NULL) {
116 rrd_set_error("Can't create '%s': %s", outfilename,
117 rrd_strerror(errno));
123 if (rrd_lock(rrd_out_file) != 0) {
124 rrd_set_error("could not lock new RRD");
128 rrd_close(rrd_out_file);
131 /*XXX: do one write for those parts of header that are unchanged */
132 if ((rrdnew.rra_ptr = (rra_ptr_t *)malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
133 rrd_set_error("allocating rra_ptr for new RRD");
137 rrd_close(rrd_out_file);
141 /* Put this back the way it was so that the rest of the algorithm
142 below remains unchanged, it will be corrected later */
143 rrdnew.rra_def[target_rra].row_cnt -= modify;
145 rrdnew.ds_def = rrdold.ds_def;
146 rrdnew.live_head = rrdold.live_head;
147 rrdnew.pdp_prep = rrdold.pdp_prep;
148 rrdnew.cdp_prep = rrdold.cdp_prep;
149 memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
152 version = atoi(rrdold.stat_head->version);
159 rrdnew.stat_head->version[3] = '3';
162 rrd_set_error("Do not know how to handle RRD version %s",
163 rrdold.stat_head->version);
167 rrd_close(rrd_out_file);
172 /* XXX: Error checking? */
173 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
174 rrd_write(rrd_out_file, rrdnew.ds_def,
175 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
176 rrd_write(rrd_out_file, rrdnew.rra_def,
177 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
178 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
179 rrd_write(rrd_out_file, rrdnew.pdp_prep,
180 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
181 rrd_write(rrd_out_file, rrdnew.cdp_prep,
182 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
183 rrdnew.stat_head->rra_cnt);
184 rrd_write(rrd_out_file, rrdnew.rra_ptr,
185 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
187 /* Move the CDPs from the old to the new database.
188 ** This can be made (much) faster but isn't worth the effort. Clarity
189 ** is much more important.
192 /* Move data in unmodified RRAs
195 for (rra = 0; rra < target_rra; rra++) {
196 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
199 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
200 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
203 /* Move data in this RRA, either removing or adding some rows
206 /* Adding extra rows; insert unknown values just after the
207 ** current row number.
209 l = rrdnew.stat_head->ds_cnt *
210 (rrdnew.rra_ptr[target_rra].cur_row + 1);
212 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
213 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
217 l = rrdnew.stat_head->ds_cnt * modify;
219 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
223 /* Removing rows. Normally this would be just after the cursor
224 ** however this may also mean that we wrap to the beginning of
227 signed long int remove_end = 0;
230 (rrdnew.rra_ptr[target_rra].cur_row -
231 modify) % rrdnew.rra_def[target_rra].row_cnt;
233 (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
234 while (remove_end >= 0) {
236 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
238 rrdnew.rra_ptr[target_rra].cur_row--;
239 rrdnew.rra_def[target_rra].row_cnt--;
243 remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
245 for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
248 for (tmp = 0; tmp < rrdnew.stat_head->ds_cnt; tmp++) {
249 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
250 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
255 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
257 rrdnew.rra_def[target_rra].row_cnt--;
261 /* Move the rest of the CDPs
264 if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
266 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
268 rrdnew.rra_def[target_rra].row_cnt += modify;
269 rrd_seek(rrd_out_file,
270 sizeof(stat_head_t) +
271 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt, SEEK_SET);
272 rrd_write(rrd_out_file, rrdnew.rra_def,
273 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
274 rrd_seek(rrd_out_file, sizeof(live_head_t), SEEK_CUR);
275 rrd_seek(rrd_out_file, sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt,
277 rrd_seek(rrd_out_file,
278 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
279 rrdnew.stat_head->rra_cnt, SEEK_CUR);
280 rrd_write(rrd_out_file, rrdnew.rra_ptr,
281 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
283 rrd_close(rrd_out_file);