1 /*****************************************************************************
2 * RRDtool 1.0.33 Copyright Tobias Oetiker, 1997 - 2000
3 *****************************************************************************
4 * change header parameters of an rrd
5 *****************************************************************************
6 * $Id$
7 * $Log$
8 * Revision 1.1 2001/02/25 22:25:06 oetiker
9 * Initial revision
10 *
11 *****************************************************************************/
13 #include "rrd_tool.h"
15 int
16 rrd_tune(int argc, char **argv)
17 {
18 rrd_t rrd;
19 FILE *rrd_file;
20 int matches;
21 int optcnt = 0;
22 long ds;
23 char ds_nam[DS_NAM_SIZE];
24 char ds_new[DS_NAM_SIZE];
25 long heartbeat;
26 double min;
27 double max;
28 char dst[DST_SIZE];
31 if(rrd_open(argv[1],&rrd_file,&rrd, RRD_READWRITE)==-1){
32 return -1;
33 }
36 while (1){
37 static struct option long_options[] =
38 {
39 {"heartbeat", required_argument, 0, 'h'},
40 {"minimum", required_argument, 0, 'i'},
41 {"maximum", required_argument, 0, 'a'},
42 {"data-source-type", required_argument, 0, 'd'},
43 {"data-source-rename", required_argument, 0, 'r'},
44 {0,0,0,0}
45 };
46 int option_index = 0;
47 int opt;
48 opt = getopt_long(argc, argv, "h:i:a:d:r:",
49 long_options, &option_index);
50 if (opt == EOF)
51 break;
53 optcnt++;
54 switch(opt) {
55 case 'h':
56 if ((matches = sscanf(optarg, DS_NAM_FMT ":%ld",ds_nam,&heartbeat)) != 2){
57 rrd_set_error("invalid arguments for heartbeat");
58 rrd_free(&rrd);
59 fclose(rrd_file);
60 return -1;
61 }
62 if ((ds=ds_match(&rrd,ds_nam))==-1){
63 rrd_free(&rrd);
64 fclose(rrd_file);
65 return -1;
66 }
67 rrd.ds_def[ds].par[DS_mrhb_cnt].u_cnt = heartbeat;
68 break;
70 case 'i':
71 if ((matches = sscanf(optarg,DS_NAM_FMT ":%lf",ds_nam,&min)) <1){
72 rrd_set_error("invalid arguments for minimum ds value");
73 rrd_free(&rrd);
74 fclose(rrd_file);
75 return -1;
76 }
77 if ((ds=ds_match(&rrd,ds_nam))==-1){
78 rrd_free(&rrd);
79 fclose(rrd_file);
80 return -1;
81 }
83 if(matches == 1)
84 min= DNAN;
85 rrd.ds_def[ds].par[DS_min_val].u_val = min;
86 break;
88 case 'a':
89 if ((matches = sscanf(optarg, DS_NAM_FMT ":%lf",ds_nam,&max)) <1){
90 rrd_set_error("invalid arguments for maximum ds value");
91 rrd_free(&rrd);
92 fclose(rrd_file);
93 return -1;
94 }
95 if ((ds=ds_match(&rrd,ds_nam))==-1){
96 rrd_free(&rrd);
97 fclose(rrd_file);
98 return -1;
99 }
100 if(matches == 1)
101 max= DNAN;
102 rrd.ds_def[ds].par[DS_max_val].u_val = max;
103 break;
105 case 'd':
106 if ((matches = sscanf(optarg, DS_NAM_FMT ":" DST_FMT ,ds_nam,dst)) != 2){
107 rrd_set_error("invalid arguments for data source type");
108 rrd_free(&rrd);
109 fclose(rrd_file);
110 return -1;
111 }
112 if ((ds=ds_match(&rrd,ds_nam))==-1){
113 rrd_free(&rrd);
114 fclose(rrd_file);
115 return -1;
116 }
117 if (dst_conv(dst) == -1){
118 rrd_free(&rrd);
119 fclose(rrd_file);
120 return -1;
121 }
122 strncpy(rrd.ds_def[ds].dst,dst,DST_SIZE-1);
123 rrd.ds_def[ds].dst[DST_SIZE-1]='\0';
125 rrd.pdp_prep[ds].last_ds[0] = 'U';
126 rrd.pdp_prep[ds].last_ds[1] = 'N';
127 rrd.pdp_prep[ds].last_ds[2] = 'K';
128 rrd.pdp_prep[ds].last_ds[3] = 'N';
129 rrd.pdp_prep[ds].last_ds[4] = '\0';
131 break;
132 case 'r':
133 if ((matches =
134 sscanf(optarg,DS_NAM_FMT ":" DS_NAM_FMT , ds_nam,ds_new)) != 2){
135 rrd_set_error("invalid arguments for data source type");
136 rrd_free(&rrd);
137 fclose(rrd_file);
138 return -1;
139 }
140 if ((ds=ds_match(&rrd,ds_nam))==-1){
141 rrd_free(&rrd);
142 fclose(rrd_file);
143 return -1;
144 }
145 strncpy(rrd.ds_def[ds].ds_nam,ds_new,DS_NAM_SIZE-1);
146 rrd.ds_def[ds].ds_nam[DS_NAM_SIZE-1]='\0';
147 break;
148 case '?':
149 if (optopt != 0)
150 rrd_set_error("unknown option '%c'", optopt);
151 else
152 rrd_set_error("unknown option '%s'",argv[optind-1]);
153 rrd_free(&rrd);
154 fclose(rrd_file);
155 return -1;
156 }
157 }
158 if(optcnt>0){
160 fseek(rrd_file,0,SEEK_SET);
161 fwrite(rrd.stat_head,
162 sizeof(stat_head_t),1, rrd_file);
163 fwrite(rrd.ds_def,
164 sizeof(ds_def_t), rrd.stat_head->ds_cnt, rrd_file);
165 } else {
166 int i;
167 for(i=0;i< rrd.stat_head->ds_cnt;i++)
168 printf("DS[%s] typ: %s\thbt: %ld\tmin: %1.4f\tmax: %1.4f\n",
169 rrd.ds_def[i].ds_nam,
170 rrd.ds_def[i].dst,
171 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt,
172 rrd.ds_def[i].par[DS_min_val].u_val,
173 rrd.ds_def[i].par[DS_max_val].u_val);
174 }
175 fclose(rrd_file);
176 rrd_free(&rrd);
177 return 0;
178 }