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 *****************************************************************************/
9 #include "rrd_tool.h"
11 int rrd_resize(
12 int argc,
13 char **argv)
14 {
15 char *infilename, outfilename[11] = "resize.rrd";
16 rrd_t rrdold, rrdnew;
17 rrd_value_t buffer;
18 int version;
19 unsigned long l, rra;
20 long modify;
21 unsigned long target_rra;
22 int grow = 0, shrink = 0;
23 char *endptr;
24 rrd_file_t *rrd_file, *rrd_out_file;
26 infilename = argv[1];
27 if (!strcmp(infilename, "resize.rrd")) {
28 rrd_set_error("resize.rrd is a reserved name");
29 return (-1);
30 }
31 if (argc != 5) {
32 rrd_set_error("wrong number of parameters");
33 return (-1);
34 }
36 target_rra = strtol(argv[2], &endptr, 0);
38 if (!strcmp(argv[3], "GROW"))
39 grow = 1;
40 else if (!strcmp(argv[3], "SHRINK"))
41 shrink = 1;
42 else {
43 rrd_set_error("I can only GROW or SHRINK");
44 return (-1);
45 }
47 modify = strtol(argv[4], &endptr, 0);
49 if ((modify < 1)) {
50 rrd_set_error("Please grow or shrink with at least 1 row");
51 return (-1);
52 }
54 if (shrink)
55 modify = -modify;
58 rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY);
59 if (rrd_file == NULL) {
60 rrd_free(&rrdold);
61 return (-1);
62 }
64 if (rrd_lock(rrd_file) != 0) {
65 rrd_set_error("could not lock original RRD");
66 rrd_free(&rrdold);
67 rrd_close(rrd_file);
68 return (-1);
69 }
72 if (target_rra >= rrdold.stat_head->rra_cnt) {
73 rrd_set_error("no such RRA in this RRD");
74 rrd_free(&rrdold);
75 rrd_close(rrd_file);
76 return (-1);
77 }
79 if (modify < 0)
80 if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
81 rrd_set_error("This RRA is not that big");
82 rrd_free(&rrdold);
83 rrd_close(rrd_file);
84 return (-1);
85 }
86 /* the size of the new file */
87 /* yes we are abusing the float cookie for this, aargh */
88 if ((rrdnew.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
89 rrd_set_error("allocating stat_head for new RRD");
90 rrd_free(&rrdold);
91 rrd_close(rrd_file);
92 return (-1);
93 }
94 rrdnew.stat_head->float_cookie = rrd_file->file_len +
95 (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
96 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
97 if (rrd_out_file == NULL) {
98 rrd_set_error("Can't create '%s': %s", outfilename,
99 rrd_strerror(errno));
100 rrd_free(&rrdnew);
101 return (-1);
102 }
103 if (rrd_lock(rrd_out_file) != 0) {
104 rrd_set_error("could not lock new RRD");
105 rrd_free(&rrdold);
106 rrd_close(rrd_file);
107 rrd_close(rrd_out_file);
108 return (-1);
109 }
110 /*XXX: do one write for those parts of header that are unchanged */
111 if ((rrdnew.stat_head = malloc(sizeof(stat_head_t))) == NULL) {
112 rrd_set_error("allocating stat_head for new RRD");
113 rrd_free(&rrdnew);
114 rrd_free(&rrdold);
115 rrd_close(rrd_file);
116 rrd_close(rrd_out_file);
117 return (-1);
118 }
120 if ((rrdnew.rra_ptr = malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
121 rrd_set_error("allocating rra_ptr for new RRD");
122 rrd_free(&rrdnew);
123 rrd_free(&rrdold);
124 rrd_close(rrd_file);
125 rrd_close(rrd_out_file);
126 return (-1);
127 }
129 if ((rrdnew.rra_def = malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
130 rrd_set_error("allocating rra_def for new RRD");
131 rrd_free(&rrdnew);
132 rrd_free(&rrdold);
133 rrd_close(rrd_file);
134 rrd_close(rrd_out_file);
135 return (-1);
136 }
138 memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
139 rrdnew.ds_def = rrdold.ds_def;
140 memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);
141 rrdnew.live_head = rrdold.live_head;
142 rrdnew.pdp_prep = rrdold.pdp_prep;
143 rrdnew.cdp_prep = rrdold.cdp_prep;
144 memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
147 version = atoi(rrdold.stat_head->version);
148 switch (version) {
149 case 4:
150 break;
151 case 3:
152 break;
153 case 1:
154 rrdold.stat_head->version[3] = '3';
155 break;
156 default:
157 rrd_set_error("Do not know how to handle RRD version %s",
158 rrdold.stat_head->version);
159 rrd_free(&rrdnew);
160 rrd_free(&rrdold);
161 rrd_close(rrd_file);
162 rrd_close(rrd_out_file);
163 return (-1);
164 break;
165 }
167 /* XXX: Error checking? */
168 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
169 rrd_write(rrd_out_file, rrdnew.ds_def,
170 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
171 rrd_write(rrd_out_file, rrdnew.rra_def,
172 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
173 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
174 rrd_write(rrd_out_file, rrdnew.pdp_prep,
175 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
176 rrd_write(rrd_out_file, rrdnew.cdp_prep,
177 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
178 rrdnew.stat_head->rra_cnt);
179 rrd_write(rrd_out_file, rrdnew.rra_ptr,
180 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
182 /* Move the CDPs from the old to the new database.
183 ** This can be made (much) faster but isn't worth the effort. Clarity
184 ** is much more important.
185 */
187 /* Move data in unmodified RRAs
188 */
189 l = 0;
190 for (rra = 0; rra < target_rra; rra++) {
191 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
192 }
193 while (l > 0) {
194 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
195 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
196 l--;
197 }
198 /* Move data in this RRA, either removing or adding some rows
199 */
200 if (modify > 0) {
201 /* Adding extra rows; insert unknown values just after the
202 ** current row number.
203 */
204 l = rrdnew.stat_head->ds_cnt *
205 (rrdnew.rra_ptr[target_rra].cur_row + 1);
206 while (l > 0) {
207 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
208 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
209 l--;
210 }
211 #ifndef HAVE_MMAP
212 buffer = DNAN;
213 l = rrdnew.stat_head->ds_cnt * modify;
214 while (l > 0) {
215 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
216 l--;
217 }
218 #else
219 /* for the mmap case, we did already fill the whole new file with DNAN
220 * before we copied the old values, so nothing to do here. */
221 #endif
222 } else {
223 /* Removing rows. Normally this would be just after the cursor
224 ** however this may also mean that we wrap to the beginning of
225 ** the array.
226 */
227 signed long int remove_end = 0;
229 remove_end =
230 (rrdnew.rra_ptr[target_rra].cur_row -
231 modify) % rrdnew.rra_def[target_rra].row_cnt;
232 if (remove_end <=
233 (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
234 while (remove_end >= 0) {
235 rrd_seek(rrd_file,
236 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
237 SEEK_CUR);
238 rrdnew.rra_ptr[target_rra].cur_row--;
239 rrdnew.rra_def[target_rra].row_cnt--;
240 remove_end--;
241 modify++;
242 }
243 remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
244 }
245 for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
246 unsigned int tmp;
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);
251 }
252 }
253 while (modify < 0) {
254 rrd_seek(rrd_file,
255 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
256 SEEK_CUR);
257 rrdnew.rra_def[target_rra].row_cnt--;
258 modify++;
259 }
260 }
261 /* Move the rest of the CDPs
262 */
263 while (1) {
264 if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
265 break;
266 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
267 }
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,
276 SEEK_CUR);
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);
282 rrd_close(rrd_file);
283 rrd_close(rrd_out_file);
284 rrd_free(&rrdold);
285 rrd_free(&rrdnew);
286 return (0);
287 }