1 /*****************************************************************************
2 * RRDtool 1.2.19 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";
15 FILE *infile,*outfile;
21 unsigned long target_rra;
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 if (rrd_open(infilename, &infile, &rrdold, RRD_READWRITE)==-1) {
55 rrd_set_error("could not open RRD");
58 if (LockRRD(infile) != 0) {
59 rrd_set_error("could not lock original RRD");
65 if (target_rra >= rrdold.stat_head->rra_cnt) {
66 rrd_set_error("no such RRA in this RRD");
73 if ((long)rrdold.rra_def[target_rra].row_cnt <= -modify) {
74 rrd_set_error("This RRA is not that big");
80 rrdnew.stat_head = rrdold.stat_head;
81 rrdnew.ds_def = rrdold.ds_def;
82 rrdnew.rra_def = rrdold.rra_def;
83 rrdnew.live_head = rrdold.live_head;
84 rrdnew.pdp_prep = rrdold.pdp_prep;
85 rrdnew.cdp_prep = rrdold.cdp_prep;
86 rrdnew.rra_ptr = rrdold.rra_ptr;
88 version = atoi(rrdold.stat_head->version);
91 case 1: rrdold.stat_head->version[3]='3';
94 rrd_set_error("Do not know how to handle RRD version %s",rrdold.stat_head->version);
101 if ((outfile=fopen(outfilename,"wb"))==NULL) {
102 rrd_set_error("Can't create '%s'",outfilename);
105 if (LockRRD(outfile) != 0) {
106 rrd_set_error("could not lock new RRD");
112 fwrite(rrdnew.stat_head, sizeof(stat_head_t),1,outfile);
113 fwrite(rrdnew.ds_def,sizeof(ds_def_t),rrdnew.stat_head->ds_cnt,outfile);
114 fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt,outfile);
115 fwrite(rrdnew.live_head,sizeof(live_head_t),1,outfile);
116 fwrite(rrdnew.pdp_prep,sizeof(pdp_prep_t),rrdnew.stat_head->ds_cnt,outfile);
117 fwrite(rrdnew.cdp_prep,sizeof(cdp_prep_t),rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,outfile);
118 fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt,outfile);
120 /* Move the CDPs from the old to the new database.
121 ** This can be made (much) faster but isn't worth the effort. Clarity
122 ** is much more important.
125 /* Move data in unmodified RRAs
128 for (rra=0;rra<target_rra;rra++) {
129 l+=rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
132 fread(&buffer,sizeof(rrd_value_t),1,infile);
133 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
136 /* Move data in this RRA, either removing or adding some rows
139 /* Adding extra rows; insert unknown values just after the
140 ** current row number.
142 l = rrdnew.stat_head->ds_cnt * (rrdnew.rra_ptr[target_rra].cur_row+1);
144 fread(&buffer,sizeof(rrd_value_t),1,infile);
145 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
149 l=rrdnew.stat_head->ds_cnt * modify;
151 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
155 /* Removing rows. Normally this would be just after the cursor
156 ** however this may also mean that we wrap to the beginning of
159 signed long int remove_end=0;
161 remove_end=(rrdnew.rra_ptr[target_rra].cur_row-modify)%rrdnew.rra_def[target_rra].row_cnt;
162 if (remove_end <= (signed long int)rrdnew.rra_ptr[target_rra].cur_row) {
163 while (remove_end >= 0) {
164 fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
165 rrdnew.rra_ptr[target_rra].cur_row--;
166 rrdnew.rra_def[target_rra].row_cnt--;
170 remove_end=rrdnew.rra_def[target_rra].row_cnt-1;
172 for (l=0;l<=rrdnew.rra_ptr[target_rra].cur_row;l++) {
174 for (tmp=0;tmp<rrdnew.stat_head->ds_cnt;tmp++) {
175 fread(&buffer,sizeof(rrd_value_t),1,infile);
176 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
180 fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
181 rrdnew.rra_def[target_rra].row_cnt--;
185 /* Move the rest of the CDPs
188 fread(&buffer,sizeof(rrd_value_t),1,infile);
191 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
193 rrdnew.rra_def[target_rra].row_cnt += modify;
194 fseek(outfile,sizeof(stat_head_t)+sizeof(ds_def_t)*rrdnew.stat_head->ds_cnt,SEEK_SET);
195 fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt, outfile);
196 fseek(outfile,sizeof(live_head_t),SEEK_CUR);
197 fseek(outfile,sizeof(pdp_prep_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
198 fseek(outfile,sizeof(cdp_prep_t)*rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,SEEK_CUR);
199 fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt, outfile);