32a501ad8cdb4ef088c7ba46cd96a09b3587c37a
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_init(&rrdold);
59 rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY);
60 if (rrd_file == NULL) {
61 rrd_free(&rrdold);
62 return (-1);
63 }
65 if (rrd_lock(rrd_file) != 0) {
66 rrd_set_error("could not lock original RRD");
67 rrd_free(&rrdold);
68 rrd_close(rrd_file);
69 return (-1);
70 }
73 if (target_rra >= rrdold.stat_head->rra_cnt) {
74 rrd_set_error("no such RRA in this RRD");
75 rrd_free(&rrdold);
76 rrd_close(rrd_file);
77 return (-1);
78 }
80 if (modify < 0)
81 if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
82 rrd_set_error("This RRA is not that big");
83 rrd_free(&rrdold);
84 rrd_close(rrd_file);
85 return (-1);
86 }
88 rrd_init(&rrdnew);
89 /* These need to be initialised before calling rrd_open() with
90 the RRD_CREATE flag */
91 if ((rrdnew.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
92 rrd_set_error("allocating stat_head for new RRD");
93 rrd_free(&rrdold);
94 rrd_close(rrd_file);
95 return (-1);
96 }
98 if ((rrdnew.rra_def = malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
99 rrd_set_error("allocating rra_def for new RRD");
100 rrd_free(&rrdnew);
101 rrd_free(&rrdold);
102 rrd_close(rrd_file);
103 rrd_close(rrd_out_file);
104 return (-1);
105 }
107 memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
108 memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);
110 /* Set this so that the file will be created with the correct size */
111 rrdnew.rra_def[target_rra].row_cnt += modify;
113 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
114 if (rrd_out_file == NULL) {
115 rrd_set_error("Can't create '%s': %s", outfilename,
116 rrd_strerror(errno));
117 rrd_free(&rrdnew);
118 return (-1);
119 }
120 if (rrd_lock(rrd_out_file) != 0) {
121 rrd_set_error("could not lock new RRD");
122 rrd_free(&rrdold);
123 rrd_close(rrd_file);
124 rrd_close(rrd_out_file);
125 return (-1);
126 }
127 /*XXX: do one write for those parts of header that are unchanged */
128 if ((rrdnew.rra_ptr = 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 /* Put this back the way it was so that the rest of the algorithm
138 below remains unchanged, it will be corrected later */
139 rrdnew.rra_def[target_rra].row_cnt -= modify;
141 rrdnew.ds_def = rrdold.ds_def;
142 rrdnew.live_head = rrdold.live_head;
143 rrdnew.pdp_prep = rrdold.pdp_prep;
144 rrdnew.cdp_prep = rrdold.cdp_prep;
145 memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
148 version = atoi(rrdold.stat_head->version);
149 switch (version) {
150 case 4:
151 break;
152 case 3:
153 break;
154 case 1:
155 rrdold.stat_head->version[3] = '3';
156 break;
157 default:
158 rrd_set_error("Do not know how to handle RRD version %s",
159 rrdold.stat_head->version);
160 rrd_free(&rrdnew);
161 rrd_free(&rrdold);
162 rrd_close(rrd_file);
163 rrd_close(rrd_out_file);
164 return (-1);
165 break;
166 }
168 /* XXX: Error checking? */
169 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
170 rrd_write(rrd_out_file, rrdnew.ds_def,
171 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
172 rrd_write(rrd_out_file, rrdnew.rra_def,
173 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
174 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
175 rrd_write(rrd_out_file, rrdnew.pdp_prep,
176 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
177 rrd_write(rrd_out_file, rrdnew.cdp_prep,
178 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
179 rrdnew.stat_head->rra_cnt);
180 rrd_write(rrd_out_file, rrdnew.rra_ptr,
181 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
183 /* Move the CDPs from the old to the new database.
184 ** This can be made (much) faster but isn't worth the effort. Clarity
185 ** is much more important.
186 */
188 /* Move data in unmodified RRAs
189 */
190 l = 0;
191 for (rra = 0; rra < target_rra; rra++) {
192 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
193 }
194 while (l > 0) {
195 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
196 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
197 l--;
198 }
199 /* Move data in this RRA, either removing or adding some rows
200 */
201 if (modify > 0) {
202 /* Adding extra rows; insert unknown values just after the
203 ** current row number.
204 */
205 l = rrdnew.stat_head->ds_cnt *
206 (rrdnew.rra_ptr[target_rra].cur_row + 1);
207 while (l > 0) {
208 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
209 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
210 l--;
211 }
212 #ifndef HAVE_MMAP
213 buffer = DNAN;
214 l = rrdnew.stat_head->ds_cnt * modify;
215 while (l > 0) {
216 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
217 l--;
218 }
219 #else
220 /* for the mmap case, we did already fill the whole new file with DNAN
221 * before we copied the old values, so nothing to do here. */
222 #endif
223 } else {
224 /* Removing rows. Normally this would be just after the cursor
225 ** however this may also mean that we wrap to the beginning of
226 ** the array.
227 */
228 signed long int remove_end = 0;
230 remove_end =
231 (rrdnew.rra_ptr[target_rra].cur_row -
232 modify) % rrdnew.rra_def[target_rra].row_cnt;
233 if (remove_end <=
234 (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
235 while (remove_end >= 0) {
236 rrd_seek(rrd_file,
237 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
238 SEEK_CUR);
239 rrdnew.rra_ptr[target_rra].cur_row--;
240 rrdnew.rra_def[target_rra].row_cnt--;
241 remove_end--;
242 modify++;
243 }
244 remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
245 }
246 for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
247 unsigned int tmp;
249 for (tmp = 0; tmp < rrdnew.stat_head->ds_cnt; tmp++) {
250 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
251 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
252 }
253 }
254 while (modify < 0) {
255 rrd_seek(rrd_file,
256 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
257 SEEK_CUR);
258 rrdnew.rra_def[target_rra].row_cnt--;
259 modify++;
260 }
261 }
262 /* Move the rest of the CDPs
263 */
264 while (1) {
265 if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
266 break;
267 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
268 }
269 rrdnew.rra_def[target_rra].row_cnt += modify;
270 rrd_seek(rrd_out_file,
271 sizeof(stat_head_t) +
272 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt, SEEK_SET);
273 rrd_write(rrd_out_file, rrdnew.rra_def,
274 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
275 rrd_seek(rrd_out_file, sizeof(live_head_t), SEEK_CUR);
276 rrd_seek(rrd_out_file, sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt,
277 SEEK_CUR);
278 rrd_seek(rrd_out_file,
279 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
280 rrdnew.stat_head->rra_cnt, SEEK_CUR);
281 rrd_write(rrd_out_file, rrdnew.rra_ptr,
282 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
283 rrd_close(rrd_file);
284 rrd_close(rrd_out_file);
285 rrd_free(&rrdold);
286 rrd_free(&rrdnew);
287 return (0);
288 }