1 /*****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
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 */
94 if ((rrdnew.stat_head = (stat_head_t*)calloc(1, sizeof(stat_head_t))) == NULL) {
95 rrd_set_error("allocating stat_head for new RRD");
96 rrd_free(&rrdold);
97 rrd_close(rrd_file);
98 return (-1);
99 }
100 memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
102 if ((rrdnew.rra_def = (rra_def_t *)malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
103 rrd_set_error("allocating rra_def for new RRD");
104 rrd_free(&rrdnew);
105 rrd_free(&rrdold);
106 rrd_close(rrd_file);
107 return (-1);
108 }
109 memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);
111 /* Set this so that the file will be created with the correct size */
112 rrdnew.rra_def[target_rra].row_cnt += modify;
114 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
115 if (rrd_out_file == NULL) {
116 rrd_set_error("Can't create '%s': %s", outfilename,
117 rrd_strerror(errno));
118 rrd_free(&rrdnew);
119 rrd_free(&rrdold);
120 rrd_close(rrd_file);
121 rrd_close(rrd_out_file);
122 return (-1);
123 }
124 if (rrd_lock(rrd_out_file) != 0) {
125 rrd_set_error("could not lock new RRD");
126 rrd_free(&rrdnew);
127 rrd_free(&rrdold);
128 rrd_close(rrd_file);
129 rrd_close(rrd_out_file);
130 return (-1);
131 }
132 /*XXX: do one write for those parts of header that are unchanged */
133 if ((rrdnew.rra_ptr = (rra_ptr_t *)malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
134 rrd_set_error("allocating rra_ptr for new RRD");
135 rrd_free(&rrdnew);
136 rrd_free(&rrdold);
137 rrd_close(rrd_file);
138 rrd_close(rrd_out_file);
139 return (-1);
140 }
142 /* Put this back the way it was so that the rest of the algorithm
143 below remains unchanged, it will be corrected later */
144 rrdnew.rra_def[target_rra].row_cnt -= modify;
146 rrdnew.ds_def = rrdold.ds_def;
147 rrdnew.live_head = rrdold.live_head;
148 rrdnew.pdp_prep = rrdold.pdp_prep;
149 rrdnew.cdp_prep = rrdold.cdp_prep;
150 memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
153 version = atoi(rrdold.stat_head->version);
154 switch (version) {
155 case 4:
156 break;
157 case 3:
158 break;
159 case 1:
160 rrdnew.stat_head->version[3] = '3';
161 break;
162 default:
163 rrd_set_error("Do not know how to handle RRD version %s",
164 rrdold.stat_head->version);
165 rrd_free(&rrdnew);
166 rrd_free(&rrdold);
167 rrd_close(rrd_file);
168 rrd_close(rrd_out_file);
169 return (-1);
170 break;
171 }
173 /* XXX: Error checking? */
174 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
175 rrd_write(rrd_out_file, rrdnew.ds_def,
176 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
177 rrd_write(rrd_out_file, rrdnew.rra_def,
178 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
179 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
180 rrd_write(rrd_out_file, rrdnew.pdp_prep,
181 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
182 rrd_write(rrd_out_file, rrdnew.cdp_prep,
183 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
184 rrdnew.stat_head->rra_cnt);
185 rrd_write(rrd_out_file, rrdnew.rra_ptr,
186 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
188 /* Move the CDPs from the old to the new database.
189 ** This can be made (much) faster but isn't worth the effort. Clarity
190 ** is much more important.
191 */
193 /* Move data in unmodified RRAs
194 */
195 l = 0;
196 for (rra = 0; rra < target_rra; rra++) {
197 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
198 }
199 while (l > 0) {
200 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
201 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
202 l--;
203 }
204 /* Move data in this RRA, either removing or adding some rows
205 */
206 if (modify > 0) {
207 /* Adding extra rows; insert unknown values just after the
208 ** current row number.
209 */
210 l = rrdnew.stat_head->ds_cnt *
211 (rrdnew.rra_ptr[target_rra].cur_row + 1);
212 while (l > 0) {
213 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
214 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
215 l--;
216 }
217 buffer = DNAN;
218 l = rrdnew.stat_head->ds_cnt * modify;
219 while (l > 0) {
220 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
221 l--;
222 }
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 }