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