1 /*****************************************************************************
2 * RRDtool 1.3.8 Copyright by Tobi Oetiker, 1997-2009
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 #ifdef WIN32
12 #include <stdlib.h>
13 #endif
15 int rrd_resize(
16 int argc,
17 char **argv)
18 {
19 char *infilename, outfilename[11] = "resize.rrd";
20 rrd_t rrdold, rrdnew;
21 rrd_value_t buffer;
22 int version;
23 unsigned long l, rra;
24 long modify;
25 unsigned long target_rra;
26 int grow = 0, shrink = 0;
27 char *endptr;
28 rrd_file_t *rrd_file, *rrd_out_file;
30 infilename = argv[1];
31 if (!strcmp(infilename, "resize.rrd")) {
32 rrd_set_error("resize.rrd is a reserved name");
33 return (-1);
34 }
35 if (argc != 5) {
36 rrd_set_error("wrong number of parameters");
37 return (-1);
38 }
40 target_rra = strtol(argv[2], &endptr, 0);
42 if (!strcmp(argv[3], "GROW"))
43 grow = 1;
44 else if (!strcmp(argv[3], "SHRINK"))
45 shrink = 1;
46 else {
47 rrd_set_error("I can only GROW or SHRINK");
48 return (-1);
49 }
51 modify = strtol(argv[4], &endptr, 0);
53 if ((modify < 1)) {
54 rrd_set_error("Please grow or shrink with at least 1 row");
55 return (-1);
56 }
58 if (shrink)
59 modify = -modify;
62 rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY);
63 if (rrd_file == NULL) {
64 rrd_free(&rrdold);
65 return (-1);
66 }
68 if (rrd_lock(rrd_file) != 0) {
69 rrd_set_error("could not lock original RRD");
70 rrd_free(&rrdold);
71 rrd_close(rrd_file);
72 return (-1);
73 }
76 if (target_rra >= rrdold.stat_head->rra_cnt) {
77 rrd_set_error("no such RRA in this RRD");
78 rrd_free(&rrdold);
79 rrd_close(rrd_file);
80 return (-1);
81 }
83 if (modify < 0)
84 if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
85 rrd_set_error("This RRA is not that big");
86 rrd_free(&rrdold);
87 rrd_close(rrd_file);
88 return (-1);
89 }
90 /* the size of the new file */
91 /* yes we are abusing the float cookie for this, aargh */
92 if ((rrdnew.stat_head = (stat_head_t*)calloc(1, sizeof(stat_head_t))) == NULL) {
93 rrd_set_error("allocating stat_head for new RRD");
94 rrd_free(&rrdold);
95 rrd_close(rrd_file);
96 return (-1);
97 }
98 rrdnew.stat_head->float_cookie = rrd_file->file_len +
99 (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
100 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
101 if (rrd_out_file == NULL) {
102 rrd_set_error("Can't create '%s': %s", outfilename,
103 rrd_strerror(errno));
104 rrd_free(&rrdnew);
105 rrd_free(&rrdold);
106 rrd_close(rrd_file);
107 rrd_close(rrd_out_file);
108 return (-1);
109 }
110 if (rrd_lock(rrd_out_file) != 0) {
111 rrd_set_error("could not lock new RRD");
112 rrd_free(&rrdnew);
113 rrd_free(&rrdold);
114 rrd_close(rrd_file);
115 rrd_close(rrd_out_file);
116 return (-1);
117 }
118 /*XXX: do one write for those parts of header that are unchanged */
119 if ((rrdnew.stat_head = (stat_head_t*)malloc(sizeof(stat_head_t))) == NULL) {
120 rrd_set_error("allocating stat_head for new RRD");
121 rrd_free(&rrdnew);
122 rrd_free(&rrdold);
123 rrd_close(rrd_file);
124 rrd_close(rrd_out_file);
125 return (-1);
126 }
128 if ((rrdnew.rra_ptr = (rra_ptr_t*)malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
129 rrd_set_error("allocating rra_ptr for new RRD");
130 rrd_free(&rrdnew);
131 rrd_free(&rrdold);
132 rrd_close(rrd_file);
133 rrd_close(rrd_out_file);
134 return (-1);
135 }
137 if ((rrdnew.rra_def = (rra_def_t*)malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
138 rrd_set_error("allocating rra_def for new RRD");
139 rrd_free(&rrdnew);
140 rrd_free(&rrdold);
141 rrd_close(rrd_file);
142 rrd_close(rrd_out_file);
143 return (-1);
144 }
146 #ifndef WIN32
147 memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
148 rrdnew.ds_def = rrdold.ds_def;
149 memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);
150 rrdnew.live_head = rrdold.live_head;
151 rrdnew.pdp_prep = rrdold.pdp_prep;
152 rrdnew.cdp_prep = rrdold.cdp_prep;
153 memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
154 #else // WIN32
155 /*
156 * For windows Other fields also should be allocated. Crashes otherwise
157 */
159 if ((rrdnew.ds_def = (ds_def_t*)malloc(sizeof(ds_def_t) * rrdold.stat_head->ds_cnt)) == NULL) {
160 rrd_set_error("allocating ds_def for new RRD");
161 rrd_free(&rrdnew);
162 rrd_free(&rrdold);
163 rrd_close(rrd_file);
164 rrd_close(rrd_out_file);
165 return (-1);
166 }
168 if ((rrdnew.live_head = (live_head_t*)malloc(sizeof(live_head_t))) == NULL) {
169 rrd_set_error("allocating live_head for new RRD");
170 rrd_free(&rrdnew);
171 rrd_free(&rrdold);
172 rrd_close(rrd_file);
173 rrd_close(rrd_out_file);
174 return (-1);
175 }
177 if ((rrdnew.pdp_prep = (pdp_prep_t*)malloc(sizeof(pdp_prep_t))) == NULL) {
178 rrd_set_error("allocating pdp_prep for new RRD");
179 rrd_free(&rrdnew);
180 rrd_free(&rrdold);
181 rrd_close(rrd_file);
182 rrd_close(rrd_out_file);
183 return (-1);
184 }
186 if ((rrdnew.cdp_prep = (cdp_prep_t*)malloc(sizeof(cdp_prep_t))) == NULL) {
187 rrd_set_error("allocating cdp_prep for new RRD");
188 rrd_free(&rrdnew);
189 rrd_free(&rrdold);
190 rrd_close(rrd_file);
191 rrd_close(rrd_out_file);
192 return (-1);
193 }
194 memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
195 memcpy(rrdnew.ds_def,rrdold.ds_def,sizeof(ds_def_t) * rrdold.stat_head->ds_cnt);
196 memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);
197 memcpy(rrdnew.live_head,rrdold.live_head,sizeof(live_head_t));
198 memcpy(rrdnew.pdp_prep,rrdold.pdp_prep,sizeof(pdp_prep_t));
199 memcpy(rrdnew.cdp_prep,rrdold.cdp_prep,sizeof(cdp_prep_t));
200 memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
201 #endif // WIN32
203 version = atoi(rrdold.stat_head->version);
204 switch (version) {
205 case 4:
206 break;
207 case 3:
208 break;
209 case 1:
210 rrdnew.stat_head->version[3] = '3';
211 break;
212 default:
213 rrd_set_error("Do not know how to handle RRD version %s",
214 rrdold.stat_head->version);
215 rrd_free(&rrdnew);
216 rrd_free(&rrdold);
217 rrd_close(rrd_file);
218 rrd_close(rrd_out_file);
219 return (-1);
220 break;
221 }
223 /* XXX: Error checking? */
224 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
225 rrd_write(rrd_out_file, rrdnew.ds_def,
226 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
227 rrd_write(rrd_out_file, rrdnew.rra_def,
228 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
229 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
230 rrd_write(rrd_out_file, rrdnew.pdp_prep,
231 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
232 rrd_write(rrd_out_file, rrdnew.cdp_prep,
233 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
234 rrdnew.stat_head->rra_cnt);
235 rrd_write(rrd_out_file, rrdnew.rra_ptr,
236 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
238 /* Move the CDPs from the old to the new database.
239 ** This can be made (much) faster but isn't worth the effort. Clarity
240 ** is much more important.
241 */
243 /* Move data in unmodified RRAs
244 */
245 l = 0;
246 for (rra = 0; rra < target_rra; rra++) {
247 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
248 }
249 while (l > 0) {
250 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
251 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
252 l--;
253 }
254 /* Move data in this RRA, either removing or adding some rows
255 */
256 if (modify > 0) {
257 /* Adding extra rows; insert unknown values just after the
258 ** current row number.
259 */
260 l = rrdnew.stat_head->ds_cnt *
261 (rrdnew.rra_ptr[target_rra].cur_row + 1);
262 while (l > 0) {
263 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
264 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
265 l--;
266 }
267 buffer = DNAN;
268 l = rrdnew.stat_head->ds_cnt * modify;
269 while (l > 0) {
270 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
271 l--;
272 }
273 } else {
274 /* Removing rows. Normally this would be just after the cursor
275 ** however this may also mean that we wrap to the beginning of
276 ** the array.
277 */
278 signed long int remove_end = 0;
280 remove_end =
281 (rrdnew.rra_ptr[target_rra].cur_row -
282 modify) % rrdnew.rra_def[target_rra].row_cnt;
283 if (remove_end <=
284 (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
285 while (remove_end >= 0) {
286 rrd_seek(rrd_file,
287 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
288 SEEK_CUR);
289 rrdnew.rra_ptr[target_rra].cur_row--;
290 rrdnew.rra_def[target_rra].row_cnt--;
291 remove_end--;
292 modify++;
293 }
294 remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
295 }
296 for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
297 unsigned int tmp;
299 for (tmp = 0; tmp < rrdnew.stat_head->ds_cnt; tmp++) {
300 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
301 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
302 }
303 }
304 while (modify < 0) {
305 rrd_seek(rrd_file,
306 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
307 SEEK_CUR);
308 rrdnew.rra_def[target_rra].row_cnt--;
309 modify++;
310 }
311 }
312 /* Move the rest of the CDPs
313 */
314 while (1) {
315 if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
316 break;
317 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
318 }
319 rrdnew.rra_def[target_rra].row_cnt += modify;
320 rrd_seek(rrd_out_file,
321 sizeof(stat_head_t) +
322 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt, SEEK_SET);
323 rrd_write(rrd_out_file, rrdnew.rra_def,
324 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
325 rrd_seek(rrd_out_file, sizeof(live_head_t), SEEK_CUR);
326 rrd_seek(rrd_out_file, sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt,
327 SEEK_CUR);
328 rrd_seek(rrd_out_file,
329 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
330 rrdnew.stat_head->rra_cnt, SEEK_CUR);
331 rrd_write(rrd_out_file, rrdnew.rra_ptr,
332 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
333 rrd_close(rrd_file);
334 rrd_close(rrd_out_file);
335 rrd_free(&rrdold);
336 rrd_free(&rrdnew);
337 return (0);
338 }