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 *****************************************************************************/
12 rrd_resize(int argc, char **argv)
14 char *infilename,outfilename[11]="resize.rrd";
20 unsigned long target_rra;
23 rrd_file_t *rrd_file, *rrd_out_file;
26 if (!strcmp(infilename,"resize.rrd")) {
27 rrd_set_error("resize.rrd is a reserved name");
31 rrd_set_error("wrong number of parameters");
35 target_rra=strtol(argv[2],&endptr,0);
37 if (!strcmp(argv[3],"GROW")) grow=1;
38 else if (!strcmp(argv[3],"SHRINK")) shrink=1;
40 rrd_set_error("I can only GROW or SHRINK");
44 modify=strtol(argv[4],&endptr,0);
47 rrd_set_error("Please grow or shrink with at least 1 row");
51 if (shrink) modify = -modify;
54 rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE);
55 if (rrd_file == NULL) {
56 rrd_set_error("could not open RRD");
59 if (LockRRD(rrd_file->fd) != 0) {
60 rrd_set_error("could not lock original RRD");
66 if (target_rra >= rrdold.stat_head->rra_cnt) {
67 rrd_set_error("no such RRA in this RRD");
74 if ((long)rrdold.rra_def[target_rra].row_cnt <= -modify) {
75 rrd_set_error("This RRA is not that big");
81 rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_CREAT);
82 if (rrd_out_file == NULL) {
83 rrd_set_error("Can't create '%s': %s",outfilename, rrd_strerror(errno));
86 if (LockRRD(rrd_out_file->fd) != 0) {
87 rrd_set_error("could not lock new RRD");
90 close(rrd_out_file->fd);
93 /*XXX: do one write for those parts of header that are unchanged */
94 rrdnew.stat_head = rrdold.stat_head;
95 rrdnew.ds_def = rrdold.ds_def;
96 rrdnew.rra_def = rrdold.rra_def;
97 rrdnew.live_head = rrdold.live_head;
98 rrdnew.pdp_prep = rrdold.pdp_prep;
99 rrdnew.cdp_prep = rrdold.cdp_prep;
100 rrdnew.rra_ptr = rrdold.rra_ptr;
102 version = atoi(rrdold.stat_head->version);
105 case 1: rrdold.stat_head->version[3]='3';
108 rrd_set_error("Do not know how to handle RRD version %s",rrdold.stat_head->version);
116 /* XXX: Error checking? */
117 rrd_write(rrd_out_file,rrdnew.stat_head, sizeof(stat_head_t)*1);
118 rrd_write(rrd_out_file,rrdnew.ds_def,sizeof(ds_def_t)*rrdnew.stat_head->ds_cnt);
119 rrd_write(rrd_out_file,rrdnew.rra_def,sizeof(rra_def_t)*rrdnew.stat_head->rra_cnt);
120 rrd_write(rrd_out_file,rrdnew.live_head,sizeof(live_head_t)*1);
121 rrd_write(rrd_out_file,rrdnew.pdp_prep,sizeof(pdp_prep_t)*rrdnew.stat_head->ds_cnt);
122 rrd_write(rrd_out_file,rrdnew.cdp_prep,sizeof(cdp_prep_t)*rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt);
123 rrd_write(rrd_out_file,rrdnew.rra_ptr,sizeof(rra_ptr_t)*rrdnew.stat_head->rra_cnt);
125 /* Move the CDPs from the old to the new database.
126 ** This can be made (much) faster but isn't worth the effort. Clarity
127 ** is much more important.
130 /* Move data in unmodified RRAs
133 for (rra=0;rra<target_rra;rra++) {
134 l+=rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
137 rrd_read(rrd_file,&buffer,sizeof(rrd_value_t)*1);
138 rrd_write(rrd_out_file,&buffer,sizeof(rrd_value_t)*1);
141 /* Move data in this RRA, either removing or adding some rows
144 /* Adding extra rows; insert unknown values just after the
145 ** current row number.
147 l = rrdnew.stat_head->ds_cnt * (rrdnew.rra_ptr[target_rra].cur_row+1);
149 rrd_read(rrd_file,&buffer,sizeof(rrd_value_t)*1);
150 rrd_write(rrd_out_file,&buffer,sizeof(rrd_value_t)*1);
154 l=rrdnew.stat_head->ds_cnt * modify;
156 rrd_write(rrd_out_file,&buffer,sizeof(rrd_value_t)*1);
160 /* Removing rows. Normally this would be just after the cursor
161 ** however this may also mean that we wrap to the beginning of
164 signed long int remove_end=0;
166 remove_end=(rrdnew.rra_ptr[target_rra].cur_row-modify)%rrdnew.rra_def[target_rra].row_cnt;
167 if (remove_end <= (signed long int)rrdnew.rra_ptr[target_rra].cur_row) {
168 while (remove_end >= 0) {
169 rrd_seek(rrd_file,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
170 rrdnew.rra_ptr[target_rra].cur_row--;
171 rrdnew.rra_def[target_rra].row_cnt--;
175 remove_end=rrdnew.rra_def[target_rra].row_cnt-1;
177 for (l=0;l<=rrdnew.rra_ptr[target_rra].cur_row;l++) {
179 for (tmp=0;tmp<rrdnew.stat_head->ds_cnt;tmp++) {
180 rrd_read(rrd_file,&buffer,sizeof(rrd_value_t)*1);
181 rrd_write(rrd_out_file,&buffer,sizeof(rrd_value_t)*1);
185 rrd_seek(rrd_file,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
186 rrdnew.rra_def[target_rra].row_cnt--;
190 /* Move the rest of the CDPs
193 if (rrd_read(rrd_file,&buffer,sizeof(rrd_value_t)*1) <= 0)
195 rrd_write(rrd_out_file,&buffer,sizeof(rrd_value_t)*1);
197 rrdnew.rra_def[target_rra].row_cnt += modify;
198 rrd_seek(rrd_out_file,sizeof(stat_head_t)+sizeof(ds_def_t)*rrdnew.stat_head->ds_cnt,SEEK_SET);
199 rrd_write(rrd_out_file,rrdnew.rra_def,sizeof(rra_def_t)*rrdnew.stat_head->rra_cnt);
200 rrd_seek(rrd_out_file,sizeof(live_head_t),SEEK_CUR);
201 rrd_seek(rrd_out_file,sizeof(pdp_prep_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
202 rrd_seek(rrd_out_file,sizeof(cdp_prep_t)*rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,SEEK_CUR);
203 rrd_write(rrd_out_file,rrdnew.rra_ptr,sizeof(rra_ptr_t)*rrdnew.stat_head->rra_cnt);
205 close(rrd_out_file->fd);