1 /*****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
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("you must have at least one row in the RRA");
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");
70 if ((rrdold.rra_def[target_rra].row_cnt+modify)<0) {
71 rrd_set_error("This RRA is not that big");
77 rrdnew.stat_head = rrdold.stat_head;
78 rrdnew.ds_def = rrdold.ds_def;
79 rrdnew.rra_def = rrdold.rra_def;
80 rrdnew.live_head = rrdold.live_head;
81 rrdnew.pdp_prep = rrdold.pdp_prep;
82 rrdnew.cdp_prep = rrdold.cdp_prep;
83 rrdnew.rra_ptr = rrdold.rra_ptr;
85 if ((outfile=fopen(outfilename,"wb"))==NULL) {
86 rrd_set_error("Can't create '%s'",outfilename);
89 if (LockRRD(outfile) != 0) {
90 rrd_set_error("could not lock new RRD");
96 fwrite(rrdnew.stat_head, sizeof(stat_head_t),1,outfile);
97 fwrite(rrdnew.ds_def,sizeof(ds_def_t),rrdnew.stat_head->ds_cnt,outfile);
98 fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt,outfile);
99 fwrite(rrdnew.live_head,sizeof(live_head_t),1,outfile);
100 fwrite(rrdnew.pdp_prep,sizeof(pdp_prep_t),rrdnew.stat_head->ds_cnt,outfile);
101 fwrite(rrdnew.cdp_prep,sizeof(cdp_prep_t),rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,outfile);
102 fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt,outfile);
104 /* Move the CDPs from the old to the new database.
105 ** This can be made (much) faster but isn't worth the efford. Clarity
106 ** is much more important.
109 /* Move data in unmodified RRAs
112 for (rra=0;rra<target_rra;rra++) {
113 l+=rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
116 fread(&buffer,sizeof(rrd_value_t),1,infile);
117 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
120 /* Move data in this RRA, either removing or adding some rows
123 /* Adding extra rows; insert unknown values just after the
124 ** current row number.
126 l = rrdnew.stat_head->ds_cnt * (rrdnew.rra_ptr[target_rra].cur_row+1);
128 fread(&buffer,sizeof(rrd_value_t),1,infile);
129 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
133 l=rrdnew.stat_head->ds_cnt * modify;
135 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
139 /* Removing rows. Normally this would be just after the cursor
140 ** however this may also mean that we wrap to the beginning of
143 signed long int remove_end=0;
145 remove_end=(rrdnew.rra_ptr[target_rra].cur_row-modify)%rrdnew.rra_def[target_rra].row_cnt;
146 if (remove_end <= rrdnew.rra_ptr[target_rra].cur_row) {
147 while (remove_end >= 0) {
148 fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
149 rrdnew.rra_ptr[target_rra].cur_row--;
150 rrdnew.rra_def[target_rra].row_cnt--;
154 remove_end=rrdnew.rra_def[target_rra].row_cnt-1;
156 for (l=0;l<=rrdnew.rra_ptr[target_rra].cur_row;l++) {
158 for (tmp=0;tmp<rrdnew.stat_head->ds_cnt;tmp++) {
159 fread(&buffer,sizeof(rrd_value_t),1,infile);
160 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
164 fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
165 rrdnew.rra_def[target_rra].row_cnt--;
169 /* Move the rest of the CDPs
171 while (!(feof(infile))) {
172 fread(&buffer,sizeof(rrd_value_t),1,infile);
173 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
175 rrdnew.rra_def[target_rra].row_cnt += modify;
176 fseek(outfile,sizeof(stat_head_t)+sizeof(ds_def_t)*rrdnew.stat_head->ds_cnt,SEEK_SET);
177 fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt, outfile);
178 fseek(outfile,sizeof(live_head_t),SEEK_CUR);
179 fseek(outfile,sizeof(pdp_prep_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
180 fseek(outfile,sizeof(cdp_prep_t)*rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,SEEK_CUR);
181 fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt, outfile);