1 /*****************************************************************************
2 * RRDtool 1.2rc4 Copyright by Tobi Oetiker, 1997-2005
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;
20 unsigned long target_rra;
25 if (!strcmp(infilename,"resize.rrd")) {
26 rrd_set_error("resize.rrd is a reserved name");
30 rrd_set_error("wrong number of parameters");
34 target_rra=strtol(argv[2],&endptr,0);
36 if (!strcmp(argv[3],"GROW")) grow=1;
37 else if (!strcmp(argv[3],"SHRINK")) shrink=1;
39 rrd_set_error("I can only GROW or SHRINK");
43 modify=strtol(argv[4],&endptr,0);
46 rrd_set_error("Please grow or shrink with at least 1 row");
50 if (shrink) modify = -modify;
53 if (rrd_open(infilename, &infile, &rrdold, RRD_READWRITE)==-1) {
54 rrd_set_error("could not open RRD");
57 if (LockRRD(infile) != 0) {
58 rrd_set_error("could not lock original RRD");
64 if (target_rra >= rrdold.stat_head->rra_cnt) {
65 rrd_set_error("no such RRA in this RRD");
72 if ((long)rrdold.rra_def[target_rra].row_cnt <= -modify) {
73 rrd_set_error("This RRA is not that big");
79 rrdnew.stat_head = rrdold.stat_head;
80 rrdnew.ds_def = rrdold.ds_def;
81 rrdnew.rra_def = rrdold.rra_def;
82 rrdnew.live_head = rrdold.live_head;
83 rrdnew.pdp_prep = rrdold.pdp_prep;
84 rrdnew.cdp_prep = rrdold.cdp_prep;
85 rrdnew.rra_ptr = rrdold.rra_ptr;
87 if ((outfile=fopen(outfilename,"wb"))==NULL) {
88 rrd_set_error("Can't create '%s'",outfilename);
91 if (LockRRD(outfile) != 0) {
92 rrd_set_error("could not lock new RRD");
98 fwrite(rrdnew.stat_head, sizeof(stat_head_t),1,outfile);
99 fwrite(rrdnew.ds_def,sizeof(ds_def_t),rrdnew.stat_head->ds_cnt,outfile);
100 fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt,outfile);
101 fwrite(rrdnew.live_head,sizeof(live_head_t),1,outfile);
102 fwrite(rrdnew.pdp_prep,sizeof(pdp_prep_t),rrdnew.stat_head->ds_cnt,outfile);
103 fwrite(rrdnew.cdp_prep,sizeof(cdp_prep_t),rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,outfile);
104 fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt,outfile);
106 /* Move the CDPs from the old to the new database.
107 ** This can be made (much) faster but isn't worth the effort. Clarity
108 ** is much more important.
111 /* Move data in unmodified RRAs
114 for (rra=0;rra<target_rra;rra++) {
115 l+=rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
118 fread(&buffer,sizeof(rrd_value_t),1,infile);
119 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
122 /* Move data in this RRA, either removing or adding some rows
125 /* Adding extra rows; insert unknown values just after the
126 ** current row number.
128 l = rrdnew.stat_head->ds_cnt * (rrdnew.rra_ptr[target_rra].cur_row+1);
130 fread(&buffer,sizeof(rrd_value_t),1,infile);
131 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
135 l=rrdnew.stat_head->ds_cnt * modify;
137 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
141 /* Removing rows. Normally this would be just after the cursor
142 ** however this may also mean that we wrap to the beginning of
145 signed long int remove_end=0;
147 remove_end=(rrdnew.rra_ptr[target_rra].cur_row-modify)%rrdnew.rra_def[target_rra].row_cnt;
148 if (remove_end <= (signed long int)rrdnew.rra_ptr[target_rra].cur_row) {
149 while (remove_end >= 0) {
150 fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
151 rrdnew.rra_ptr[target_rra].cur_row--;
152 rrdnew.rra_def[target_rra].row_cnt--;
156 remove_end=rrdnew.rra_def[target_rra].row_cnt-1;
158 for (l=0;l<=rrdnew.rra_ptr[target_rra].cur_row;l++) {
160 for (tmp=0;tmp<rrdnew.stat_head->ds_cnt;tmp++) {
161 fread(&buffer,sizeof(rrd_value_t),1,infile);
162 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
166 fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
167 rrdnew.rra_def[target_rra].row_cnt--;
171 /* Move the rest of the CDPs
174 fread(&buffer,sizeof(rrd_value_t),1,infile);
177 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
179 rrdnew.rra_def[target_rra].row_cnt += modify;
180 fseek(outfile,sizeof(stat_head_t)+sizeof(ds_def_t)*rrdnew.stat_head->ds_cnt,SEEK_SET);
181 fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt, outfile);
182 fseek(outfile,sizeof(live_head_t),SEEK_CUR);
183 fseek(outfile,sizeof(pdp_prep_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
184 fseek(outfile,sizeof(cdp_prep_t)*rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,SEEK_CUR);
185 fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt, outfile);