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