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");
85 /* the size of the new file */
86 rrdnew.stat_head = rrd_file->file_len +
87 (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
89 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
90 if (rrd_out_file == NULL) {
91 rrd_set_error("Can't create '%s': %s", outfilename,
96 if (LockRRD(rrd_out_file->fd) != 0) {
97 rrd_set_error("could not lock new RRD");
100 rrd_close(rrd_out_file);
103 /*XXX: do one write for those parts of header that are unchanged */
104 rrdnew.stat_head = rrdold.stat_head;
105 rrdnew.ds_def = rrdold.ds_def;
106 rrdnew.rra_def = rrdold.rra_def;
107 rrdnew.live_head = rrdold.live_head;
108 rrdnew.pdp_prep = rrdold.pdp_prep;
109 rrdnew.cdp_prep = rrdold.cdp_prep;
110 rrdnew.rra_ptr = rrdold.rra_ptr;
112 version = atoi(rrdold.stat_head->version);
117 rrdold.stat_head->version[3] = '3';
120 rrd_set_error("Do not know how to handle RRD version %s",
121 rrdold.stat_head->version);
128 /* XXX: Error checking? */
129 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
130 rrd_write(rrd_out_file, rrdnew.ds_def,
131 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
132 rrd_write(rrd_out_file, rrdnew.rra_def,
133 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
134 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
135 rrd_write(rrd_out_file, rrdnew.pdp_prep,
136 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
137 rrd_write(rrd_out_file, rrdnew.cdp_prep,
138 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
139 rrdnew.stat_head->rra_cnt);
140 rrd_write(rrd_out_file, rrdnew.rra_ptr,
141 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
143 /* Move the CDPs from the old to the new database.
144 ** This can be made (much) faster but isn't worth the effort. Clarity
145 ** is much more important.
148 /* Move data in unmodified RRAs
151 for (rra = 0; rra < target_rra; rra++) {
152 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
155 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
156 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
159 /* Move data in this RRA, either removing or adding some rows
162 /* Adding extra rows; insert unknown values just after the
163 ** current row number.
165 l = rrdnew.stat_head->ds_cnt * (rrdnew.rra_ptr[target_rra].cur_row +
168 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
169 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
173 l = rrdnew.stat_head->ds_cnt * modify;
175 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
179 /* Removing rows. Normally this would be just after the cursor
180 ** however this may also mean that we wrap to the beginning of
183 signed long int remove_end = 0;
186 (rrdnew.rra_ptr[target_rra].cur_row -
187 modify) % rrdnew.rra_def[target_rra].row_cnt;
189 (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
190 while (remove_end >= 0) {
192 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
194 rrdnew.rra_ptr[target_rra].cur_row--;
195 rrdnew.rra_def[target_rra].row_cnt--;
199 remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
201 for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
204 for (tmp = 0; tmp < rrdnew.stat_head->ds_cnt; tmp++) {
205 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
206 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
210 rrd_seek(rrd_file, sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
212 rrdnew.rra_def[target_rra].row_cnt--;
216 /* Move the rest of the CDPs
219 if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
221 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
223 rrdnew.rra_def[target_rra].row_cnt += modify;
224 rrd_seek(rrd_out_file,
225 sizeof(stat_head_t) +
226 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt, SEEK_SET);
227 rrd_write(rrd_out_file, rrdnew.rra_def,
228 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
229 rrd_seek(rrd_out_file, sizeof(live_head_t), SEEK_CUR);
230 rrd_seek(rrd_out_file, sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt,
232 rrd_seek(rrd_out_file,
233 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
234 rrdnew.stat_head->rra_cnt, SEEK_CUR);
235 rrd_write(rrd_out_file, rrdnew.rra_ptr,
236 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
242 rrd_close(rrd_out_file);