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