e6b17c2f9f71266ca973fa7406ac8014aff9550e
1 /*****************************************************************************
2 * RRDtool 1.3rc2 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 }
63 if (LockRRD(rrd_file->fd) != 0) {
64 rrd_set_error("could not lock original RRD");
65 rrd_free(&rrdold);
66 rrd_close(rrd_file);
67 return (-1);
68 }
70 if (target_rra >= rrdold.stat_head->rra_cnt) {
71 rrd_set_error("no such RRA in this RRD");
72 rrd_free(&rrdold);
73 rrd_close(rrd_file);
74 return (-1);
75 }
77 if (modify < 0)
78 if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
79 rrd_set_error("This RRA is not that big");
80 rrd_free(&rrdold);
81 rrd_close(rrd_file);
82 return (-1);
83 }
84 /* the size of the new file */
85 /* yes we are abusing the float cookie for this, aargh */
86 if ((rrdnew.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
87 rrd_set_error("allocating stat_head for new RRD");
88 rrd_free(&rrdold);
89 rrd_close(rrd_file);
90 return (-1);
91 }
92 rrdnew.stat_head->float_cookie = rrd_file->file_len +
93 (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
94 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
95 if (rrd_out_file == NULL) {
96 rrd_set_error("Can't create '%s': %s", outfilename,
97 rrd_strerror(errno));
98 rrd_free(&rrdnew);
99 return (-1);
100 }
101 if (LockRRD(rrd_out_file->fd) != 0) {
102 rrd_set_error("could not lock new RRD");
103 rrd_free(&rrdold);
104 rrd_close(rrd_file);
105 rrd_close(rrd_out_file);
106 return (-1);
107 }
108 /*XXX: do one write for those parts of header that are unchanged */
109 rrdnew.stat_head = rrdold.stat_head;
110 rrdnew.ds_def = rrdold.ds_def;
111 rrdnew.rra_def = rrdold.rra_def;
112 rrdnew.live_head = rrdold.live_head;
113 rrdnew.pdp_prep = rrdold.pdp_prep;
114 rrdnew.cdp_prep = rrdold.cdp_prep;
115 rrdnew.rra_ptr = rrdold.rra_ptr;
117 version = atoi(rrdold.stat_head->version);
118 switch (version) {
119 case 3:
120 break;
121 case 1:
122 rrdold.stat_head->version[3] = '3';
123 break;
124 default:
125 rrd_set_error("Do not know how to handle RRD version %s",
126 rrdold.stat_head->version);
127 rrd_close(rrd_file);
128 rrd_free(&rrdold);
129 return (-1);
130 break;
131 }
133 /* XXX: Error checking? */
134 rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
135 rrd_write(rrd_out_file, rrdnew.ds_def,
136 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
137 rrd_write(rrd_out_file, rrdnew.rra_def,
138 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
139 rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
140 rrd_write(rrd_out_file, rrdnew.pdp_prep,
141 sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
142 rrd_write(rrd_out_file, rrdnew.cdp_prep,
143 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
144 rrdnew.stat_head->rra_cnt);
145 rrd_write(rrd_out_file, rrdnew.rra_ptr,
146 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
148 /* Move the CDPs from the old to the new database.
149 ** This can be made (much) faster but isn't worth the effort. Clarity
150 ** is much more important.
151 */
153 /* Move data in unmodified RRAs
154 */
155 l = 0;
156 for (rra = 0; rra < target_rra; rra++) {
157 l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
158 }
159 while (l > 0) {
160 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
161 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
162 l--;
163 }
164 /* Move data in this RRA, either removing or adding some rows
165 */
166 if (modify > 0) {
167 /* Adding extra rows; insert unknown values just after the
168 ** current row number.
169 */
170 l = rrdnew.stat_head->ds_cnt *
171 (rrdnew.rra_ptr[target_rra].cur_row + 1);
172 while (l > 0) {
173 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
174 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
175 l--;
176 }
177 #ifndef HAVE_MMAP
178 buffer = DNAN;
179 l = rrdnew.stat_head->ds_cnt * modify;
180 while (l > 0) {
181 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
182 l--;
183 }
184 #else
185 /* for the mmap case, we did already fill the whole new file with DNAN
186 * before we copied the old values, so nothing to do here. */
187 #endif
188 } else {
189 /* Removing rows. Normally this would be just after the cursor
190 ** however this may also mean that we wrap to the beginning of
191 ** the array.
192 */
193 signed long int remove_end = 0;
195 remove_end =
196 (rrdnew.rra_ptr[target_rra].cur_row -
197 modify) % rrdnew.rra_def[target_rra].row_cnt;
198 if (remove_end <=
199 (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
200 while (remove_end >= 0) {
201 rrd_seek(rrd_file,
202 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
203 SEEK_CUR);
204 rrdnew.rra_ptr[target_rra].cur_row--;
205 rrdnew.rra_def[target_rra].row_cnt--;
206 remove_end--;
207 modify++;
208 }
209 remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
210 }
211 for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
212 unsigned int tmp;
214 for (tmp = 0; tmp < rrdnew.stat_head->ds_cnt; tmp++) {
215 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
216 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
217 }
218 }
219 while (modify < 0) {
220 rrd_seek(rrd_file,
221 sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
222 SEEK_CUR);
223 rrdnew.rra_def[target_rra].row_cnt--;
224 modify++;
225 }
226 }
227 /* Move the rest of the CDPs
228 */
229 while (1) {
230 if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
231 break;
232 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
233 }
234 rrdnew.rra_def[target_rra].row_cnt += modify;
235 rrd_seek(rrd_out_file,
236 sizeof(stat_head_t) +
237 sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt, SEEK_SET);
238 rrd_write(rrd_out_file, rrdnew.rra_def,
239 sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
240 rrd_seek(rrd_out_file, sizeof(live_head_t), SEEK_CUR);
241 rrd_seek(rrd_out_file, sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt,
242 SEEK_CUR);
243 rrd_seek(rrd_out_file,
244 sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
245 rrdnew.stat_head->rra_cnt, SEEK_CUR);
246 rrd_write(rrd_out_file, rrdnew.rra_ptr,
247 sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
249 rrd_free(&rrdold);
250 rrd_close(rrd_file);
252 rrd_close(rrd_out_file);
254 return (0);
255 }