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