root/trunk/src/renetcolAgg.c @ 59

Revision 59, 23.2 KB (checked in by andreu, 15 years ago)

new aggregation method, based on SNMP index. New parameters in renetcolParam.h and new compilation options in configure.in.

  • Property svn:eol-style set to native
Line 
1/*
2 * File: renetcolAgg.c
3 *
4 * Authors: ANDREU Francois-Xavier
5 *
6 * Copyright (C) 2007 GIP RENATER
7 */
8
9/*  This file is part of renetcol.
10 *
11 *  renetcol is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  renetcol is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License
22 *  along with renetcol; if not, write to the Free Software
23 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 */
25
26/*
27 * In this file, I use directly the C rrd librairy to create and update
28 * the rrd files which contain the statistique for each subnet.
29 * When your system is too fast and the number of your subnet too big
30 * (> 1024), the create and update process can be too fast and before the
31 * creation end the maximum number of files which can be opened for one process
32 * in the same time is reached. So we must temporize.
33 */
34
35#include "renetcolAgg.h"
36
37struct SHMForAgg *shmForAgg;
38
39int
40main(int argc, char *argv[])
41{
42  int shmid;
43  int i, j, k = 0;
44  int index = 1;
45  int rrd_update_er_ct = 0;
46  int rrd_update_ok_ct = 0;
47  int rrd_already_created_ct = 0;
48  int rrd_create_er_ct = 0;
49  int rrd_create_ok_ct = 0;
50  int isFirstUpdate = 1;
51  key_t key;
52  static char buf[2048];
53  static char name[2048];
54  static char ipStr[24];
55  static char indexStr[24];
56  static char createstr[2048];
57  char *opt[27];
58  char *optUpdate[3];
59  FILE *fp;
60  static time_t now, now2;
61 
62  key = 8765;
63  if ((shmid = shmget(key, SHMSIZE, 0666)) < 0) {
64    perror("shmget");
65    exit(1);
66  }
67  if ((shmForAgg = (struct SHMForAgg *)shmat(shmid, (void *)0, 0)) == (void *) -1) {
68    perror("shmat");
69    exit(1);
70  }
71 
72  fprintf(stderr, "renetcolAgg: I become a deamon, next messages via syslogd. By.\n");
73  if (fork () != 0)
74    exit (0);
75  if (setsid() == -1){
76    exit(4);
77  }
78 
79  do {
80    if (shmForAgg->readed == 1) {
81      shmForAgg->readed = 0;
82      if (shmForAgg->currentTable == 0) {
83        index = 1;
84      }else{
85        index = 0;
86      }
87      now = time((time_t *)NULL);
88      if (isFirstUpdate == 0) {
89        /* RRD ACCOUNTING for prefixV4Tab */
90        for (i=0; i<shmForAgg->v4PrefixNb; i++){
91          /* HERE create or update RRD FILE */
92          if (shmForAgg->prefixV4Tab[index][i].sampling != 0) {
93            for (j=0; j<strlen(name); j++) {name[j] = '\0';}
94            for (j=0; j<strlen(createstr); j++) {createstr[j] = '\0';}
95            for (j=0; j<strlen(ipStr); j++) {ipStr[j] = '\0';}
96            for (j=0; j<strlen(buf); j++) {buf[j] = '\0';}
97            strcat(name, PREFIX_RRD_LOCATION);
98            sprintf(ipStr, "%lu_%lu_%lu_%lu_%hu_%lu",
99                    shmForAgg->prefixV4Tab[index][i].beginning>>24,
100                    shmForAgg->prefixV4Tab[index][i].beginning<<8>>24,
101                    shmForAgg->prefixV4Tab[index][i].beginning<<16>>24,
102                    shmForAgg->prefixV4Tab[index][i].beginning<<24>>24,
103                    shmForAgg->prefixV4Tab[index][i].mask,
104                    shmForAgg->prefixV4Tab[index][i].sampling
105                    );
106            strcat(name, ipStr);
107            strcat(name, RRD_EXTENSION);
108            if ( (fp=fopen(name,"r")) == NULL ){
109              opt[0]= (char *) malloc((strlen(RRD_PARAM_PREFIX_0) + 1) * sizeof(char));
110              strcpy(opt[0], RRD_PARAM_PREFIX_0);
111              opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
112              strcpy(opt[1], name);
113              opt[2]= (char *) malloc((strlen(RRD_PARAM_PREFIX_1) + 1) * sizeof(char));
114              strcpy(opt[2], RRD_PARAM_PREFIX_1);
115              opt[3]= (char *) malloc((strlen(RRD_PARAM_PREFIX_2) + 1) * sizeof(char));
116              strcpy(opt[3], RRD_PARAM_PREFIX_2);
117              opt[4]= (char *) malloc((strlen(RRD_PARAM_PREFIX_3) + 1) * sizeof(char));
118              strcpy(opt[4], RRD_PARAM_PREFIX_3);
119              opt[5]= (char *) malloc((strlen(RRD_PARAM_PREFIX_4) + 1) * sizeof(char));
120              strcpy(opt[5], RRD_PARAM_PREFIX_4);
121              opt[6]= (char *) malloc((strlen(RRD_PARAM_PREFIX_5) + 1) * sizeof(char));
122              strcpy(opt[6], RRD_PARAM_PREFIX_5);
123              opt[7]= (char *) malloc((strlen(RRD_PARAM_PREFIX_6) + 1) * sizeof(char));
124              strcpy(opt[7], RRD_PARAM_PREFIX_6);
125              opt[8]= (char *) malloc((strlen(RRD_PARAM_PREFIX_7) + 1) * sizeof(char));
126              strcpy(opt[8], RRD_PARAM_PREFIX_7);
127              opt[9]= (char *) malloc((strlen(RRD_PARAM_PREFIX_8) + 1) * sizeof(char));
128              strcpy(opt[9], RRD_PARAM_PREFIX_8);
129              opt[10]= (char *) malloc((strlen(RRD_PARAM_PREFIX_9) + 1) * sizeof(char));
130              strcpy(opt[10], RRD_PARAM_PREFIX_9);
131              opt[11]= (char *) malloc((strlen(RRD_PARAM_PREFIX_10) + 1) * sizeof(char));
132              strcpy(opt[11], RRD_PARAM_PREFIX_10);
133              opt[12]= (char *) malloc((strlen(RRD_PARAM_PREFIX_11) + 1) * sizeof(char));
134              strcpy(opt[12], RRD_PARAM_PREFIX_11);
135              opt[13]= (char *) malloc((strlen(RRD_PARAM_PREFIX_12) + 1) * sizeof(char));
136              strcpy(opt[13], RRD_PARAM_PREFIX_12);
137              opt[14]= (char *) malloc((strlen(RRD_PARAM_PREFIX_13) + 1) * sizeof(char));
138              strcpy(opt[14], RRD_PARAM_PREFIX_13);
139              opt[15]= (char *) malloc((strlen(RRD_PARAM_PREFIX_14) + 1) * sizeof(char));
140              strcpy(opt[15], RRD_PARAM_PREFIX_14);
141              opt[16]= (char *) malloc((strlen(RRD_PARAM_PREFIX_15) + 1) * sizeof(char));
142              strcpy(opt[16], RRD_PARAM_PREFIX_15);
143              opt[17]= (char *) malloc((strlen(RRD_PARAM_PREFIX_16) + 1) * sizeof(char));
144              strcpy(opt[17], RRD_PARAM_PREFIX_16);
145              opt[18]= (char *) malloc((strlen(RRD_PARAM_PREFIX_17) + 1) * sizeof(char));
146              strcpy(opt[18], RRD_PARAM_PREFIX_17);
147              opt[19]= (char *) malloc((strlen(RRD_PARAM_PREFIX_18) + 1) * sizeof(char));
148              strcpy(opt[19], RRD_PARAM_PREFIX_18);
149              opt[20]= (char *) malloc((strlen(RRD_PARAM_PREFIX_19) + 1) * sizeof(char));
150              strcpy(opt[20], RRD_PARAM_PREFIX_19);
151              opt[21]= (char *) malloc((strlen(RRD_PARAM_PREFIX_20) + 1) * sizeof(char));
152              strcpy(opt[21], RRD_PARAM_PREFIX_20);
153              opt[22]= (char *) malloc((strlen(RRD_PARAM_PREFIX_21) + 1) * sizeof(char));
154              strcpy(opt[22], RRD_PARAM_PREFIX_21);
155              opt[23]= (char *) malloc((strlen(RRD_PARAM_PREFIX_22) + 1) * sizeof(char));
156              strcpy(opt[23], RRD_PARAM_PREFIX_22);
157              opt[24]= (char *) malloc((strlen(RRD_PARAM_PREFIX_23) + 1) * sizeof(char));
158              strcpy(opt[24], RRD_PARAM_PREFIX_23);
159              opt[25]= (char *) malloc((strlen(RRD_PARAM_PREFIX_24) + 1) * sizeof(char));
160              strcpy(opt[25], RRD_PARAM_PREFIX_24);
161              opt[26]= (char *) malloc((strlen(RRD_PARAM_PREFIX_25) + 1) * sizeof(char));
162              strcpy(opt[26], RRD_PARAM_PREFIX_25);
163              optind = opterr = 0;
164              rrd_clear_error();
165              if ( rrd_create(27,opt) < 0) {
166                syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
167                rrd_create_er_ct++;
168              } else {
169                rrd_create_ok_ct++;
170              }
171              for ( j=0; j<27; j++) {
172                free(opt[j]);
173                opt[j] = NULL;
174              }
175            }else{
176              fclose(fp);
177              rrd_already_created_ct++;
178              snprintf(buf,
179                       2048,
180                       "%lu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu"
181                       now-300,  /* or ctime(&now) with %s */
182                       shmForAgg->prefixV4Tab[index][i].flowNbIN,  /* 1 */
183                       shmForAgg->prefixV4Tab[index][i].flowNbOUT,
184                       shmForAgg->prefixV4Tab[index][i].bytesNbIN,
185                       shmForAgg->prefixV4Tab[index][i].bytesNbOUT,
186                       shmForAgg->prefixV4Tab[index][i].pktsNbIN,  /* 5 */
187                       shmForAgg->prefixV4Tab[index][i].pktsNbOUT,
188                       shmForAgg->prefixV4Tab[index][i].firstCoSIN,
189                       shmForAgg->prefixV4Tab[index][i].firstCoSOUT,
190                       shmForAgg->prefixV4Tab[index][i].secondCoSIN,
191                       shmForAgg->prefixV4Tab[index][i].secondCoSOUT,
192                       shmForAgg->prefixV4Tab[index][i].thirdCoSIN,
193                       shmForAgg->prefixV4Tab[index][i].thirdCoSOUT,
194                       shmForAgg->prefixV4Tab[index][i].fourthCoSIN,
195                       shmForAgg->prefixV4Tab[index][i].fourthCoSOUT
196                       );
197              optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
198              strcpy(optUpdate[0], RRD_UPDATE_0);
199              optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
200              strcpy(optUpdate[1], name);
201              optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
202              strcpy(optUpdate[2], buf);
203              optind = opterr = 0;
204              rrd_clear_error();
205              if ( rrd_update(3, optUpdate) < 0 ) {
206                syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
207                rrd_update_er_ct++;
208              } else {
209                rrd_update_ok_ct++;
210              }
211              for ( j=0; j<3; j++) {
212                free(optUpdate[j]);
213                optUpdate[j] = NULL;
214              }
215            }
216            /* Reinit the shared tables */
217            shmForAgg->prefixV4Tab[index][i].flowNbIN = 0;
218            shmForAgg->prefixV4Tab[index][i].bytesNbIN = 0;
219            shmForAgg->prefixV4Tab[index][i].pktsNbIN = 0;
220            shmForAgg->prefixV4Tab[index][i].flowNbOUT = 0;
221            shmForAgg->prefixV4Tab[index][i].bytesNbOUT = 0;
222            shmForAgg->prefixV4Tab[index][i].pktsNbOUT = 0;
223            shmForAgg->prefixV4Tab[index][i].firstCoSIN = 0;
224            shmForAgg->prefixV4Tab[index][i].secondCoSIN = 0;
225            shmForAgg->prefixV4Tab[index][i].thirdCoSIN = 0;
226            shmForAgg->prefixV4Tab[index][i].fourthCoSIN = 0;
227            shmForAgg->prefixV4Tab[index][i].firstCoSOUT = 0;
228            shmForAgg->prefixV4Tab[index][i].secondCoSOUT = 0;
229            shmForAgg->prefixV4Tab[index][i].thirdCoSOUT = 0;
230            shmForAgg->prefixV4Tab[index][i].fourthCoSOUT = 0;
231          }
232        }
233        /* ACCOUNTING for prefixV4SubnetTab */
234        for (i=0; i<shmForAgg->v4PrefixNb; i++){
235          /* HERE create or update RRD FILE */
236          if (shmForAgg->prefixV4SubnetTab[index][i].sampling != 0) {
237            for (j=0; j<strlen(name); j++) {name[j] = '\0';}
238            for (j=0; j<strlen(createstr); j++) {createstr[j] = '\0';}
239            for (j=0; j<strlen(ipStr); j++) {ipStr[j] = '\0';}
240            for (j=0; j<strlen(buf); j++) {buf[j] = '\0';}
241            strcat(name, PREFIX_RRD_LOCATION);
242            sprintf(ipStr, "%lu_%lu_%lu_%lu_%hu_%lu",
243                    shmForAgg->prefixV4SubnetTab[index][i].beginning>>24,
244                    shmForAgg->prefixV4SubnetTab[index][i].beginning<<8>>24,
245                    shmForAgg->prefixV4SubnetTab[index][i].beginning<<16>>24,
246                    shmForAgg->prefixV4SubnetTab[index][i].beginning<<24>>24,
247                    shmForAgg->prefixV4SubnetTab[index][i].mask,
248                    shmForAgg->prefixV4SubnetTab[index][i].sampling
249                    );
250            strcat(name, ipStr);
251            strcat(name, RRD_EXTENSION);
252            if ( (fp=fopen(name,"r")) == NULL ){
253              opt[0]= (char *) malloc((strlen(RRD_PARAM_PREFIX_0) + 1) * sizeof(char));
254              strcpy(opt[0], RRD_PARAM_PREFIX_0);
255              opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
256              strcpy(opt[1], name);
257              opt[2]= (char *) malloc((strlen(RRD_PARAM_PREFIX_1) + 1) * sizeof(char));
258              strcpy(opt[2], RRD_PARAM_PREFIX_1);
259              opt[3]= (char *) malloc((strlen(RRD_PARAM_PREFIX_2) + 1) * sizeof(char));
260              strcpy(opt[3], RRD_PARAM_PREFIX_2);
261              opt[4]= (char *) malloc((strlen(RRD_PARAM_PREFIX_3) + 1) * sizeof(char));
262              strcpy(opt[4], RRD_PARAM_PREFIX_3);
263              opt[5]= (char *) malloc((strlen(RRD_PARAM_PREFIX_4) + 1) * sizeof(char));
264              strcpy(opt[5], RRD_PARAM_PREFIX_4);
265              opt[6]= (char *) malloc((strlen(RRD_PARAM_PREFIX_5) + 1) * sizeof(char));
266              strcpy(opt[6], RRD_PARAM_PREFIX_5);
267              opt[7]= (char *) malloc((strlen(RRD_PARAM_PREFIX_6) + 1) * sizeof(char));
268              strcpy(opt[7], RRD_PARAM_PREFIX_6);
269              opt[8]= (char *) malloc((strlen(RRD_PARAM_PREFIX_7) + 1) * sizeof(char));
270              strcpy(opt[8], RRD_PARAM_PREFIX_7);
271              opt[9]= (char *) malloc((strlen(RRD_PARAM_PREFIX_8) + 1) * sizeof(char));
272              strcpy(opt[9], RRD_PARAM_PREFIX_8);
273              opt[10]= (char *) malloc((strlen(RRD_PARAM_PREFIX_9) + 1) * sizeof(char));
274              strcpy(opt[10], RRD_PARAM_PREFIX_9);
275              opt[11]= (char *) malloc((strlen(RRD_PARAM_PREFIX_10) + 1) * sizeof(char));
276              strcpy(opt[11], RRD_PARAM_PREFIX_10);
277              opt[12]= (char *) malloc((strlen(RRD_PARAM_PREFIX_11) + 1) * sizeof(char));
278              strcpy(opt[12], RRD_PARAM_PREFIX_11);
279              opt[13]= (char *) malloc((strlen(RRD_PARAM_PREFIX_12) + 1) * sizeof(char));
280              strcpy(opt[13], RRD_PARAM_PREFIX_12);
281              opt[14]= (char *) malloc((strlen(RRD_PARAM_PREFIX_13) + 1) * sizeof(char));
282              strcpy(opt[14], RRD_PARAM_PREFIX_13);
283              opt[15]= (char *) malloc((strlen(RRD_PARAM_PREFIX_14) + 1) * sizeof(char));
284              strcpy(opt[15], RRD_PARAM_PREFIX_14);
285              opt[16]= (char *) malloc((strlen(RRD_PARAM_PREFIX_15) + 1) * sizeof(char));
286              strcpy(opt[16], RRD_PARAM_PREFIX_15);
287              opt[17]= (char *) malloc((strlen(RRD_PARAM_PREFIX_16) + 1) * sizeof(char));
288              strcpy(opt[17], RRD_PARAM_PREFIX_16);
289              opt[18]= (char *) malloc((strlen(RRD_PARAM_PREFIX_17) + 1) * sizeof(char));
290              strcpy(opt[18], RRD_PARAM_PREFIX_17);
291              opt[19]= (char *) malloc((strlen(RRD_PARAM_PREFIX_18) + 1) * sizeof(char));
292              strcpy(opt[19], RRD_PARAM_PREFIX_18);
293              opt[20]= (char *) malloc((strlen(RRD_PARAM_PREFIX_19) + 1) * sizeof(char));
294              strcpy(opt[20], RRD_PARAM_PREFIX_19);
295              opt[21]= (char *) malloc((strlen(RRD_PARAM_PREFIX_20) + 1) * sizeof(char));
296              strcpy(opt[21], RRD_PARAM_PREFIX_20);
297              opt[22]= (char *) malloc((strlen(RRD_PARAM_PREFIX_21) + 1) * sizeof(char));
298              strcpy(opt[22], RRD_PARAM_PREFIX_21);
299              opt[23]= (char *) malloc((strlen(RRD_PARAM_PREFIX_22) + 1) * sizeof(char));
300              strcpy(opt[23], RRD_PARAM_PREFIX_22);
301              opt[24]= (char *) malloc((strlen(RRD_PARAM_PREFIX_23) + 1) * sizeof(char));
302              strcpy(opt[24], RRD_PARAM_PREFIX_23);
303              opt[25]= (char *) malloc((strlen(RRD_PARAM_PREFIX_24) + 1) * sizeof(char));
304              strcpy(opt[25], RRD_PARAM_PREFIX_24);
305              opt[26]= (char *) malloc((strlen(RRD_PARAM_PREFIX_25) + 1) * sizeof(char));
306              strcpy(opt[26], RRD_PARAM_PREFIX_25);
307              optind = opterr = 0;
308              rrd_clear_error();
309              if ( rrd_create(27,opt) < 0) {
310                syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
311                rrd_create_er_ct++;
312              } else {
313                rrd_create_ok_ct++;
314              }
315              for ( j=0; j<27; j++) {
316                free(opt[j]);
317                opt[j] = NULL;
318              }
319            }else{
320              fclose(fp);
321              rrd_already_created_ct++;
322              snprintf(buf,
323                       2048,
324                       "%lu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu"
325                       now-300,  /* or ctime(&now) with %s */
326                       shmForAgg->prefixV4SubnetTab[index][i].flowNbIN,  /* 1 */
327                       shmForAgg->prefixV4SubnetTab[index][i].flowNbOUT,
328                       shmForAgg->prefixV4SubnetTab[index][i].bytesNbIN,
329                       shmForAgg->prefixV4SubnetTab[index][i].bytesNbOUT,
330                       shmForAgg->prefixV4SubnetTab[index][i].pktsNbIN,  /* 5 */
331                       shmForAgg->prefixV4SubnetTab[index][i].pktsNbOUT,
332                       shmForAgg->prefixV4SubnetTab[index][i].firstCoSIN,
333                       shmForAgg->prefixV4SubnetTab[index][i].firstCoSOUT,
334                       shmForAgg->prefixV4SubnetTab[index][i].secondCoSIN,
335                       shmForAgg->prefixV4SubnetTab[index][i].secondCoSOUT,
336                       shmForAgg->prefixV4SubnetTab[index][i].thirdCoSIN,
337                       shmForAgg->prefixV4SubnetTab[index][i].thirdCoSOUT,
338                       shmForAgg->prefixV4SubnetTab[index][i].fourthCoSIN,
339                       shmForAgg->prefixV4SubnetTab[index][i].fourthCoSOUT
340                       );
341              optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
342              strcpy(optUpdate[0], RRD_UPDATE_0);
343              optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
344              strcpy(optUpdate[1], name);
345              optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
346              strcpy(optUpdate[2], buf);
347              optind = opterr = 0;
348              rrd_clear_error();
349              if ( rrd_update(3, optUpdate) < 0 ) {
350                syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
351                rrd_update_er_ct++;
352              } else {
353                rrd_update_ok_ct++;
354              }
355              for ( j=0; j<3; j++) {
356                free(optUpdate[j]);
357                optUpdate[j] = NULL;
358              }
359            }
360            /* Reinit the shared tables */
361            shmForAgg->prefixV4SubnetTab[index][i].flowNbIN = 0;
362            shmForAgg->prefixV4SubnetTab[index][i].bytesNbIN = 0;
363            shmForAgg->prefixV4SubnetTab[index][i].pktsNbIN = 0;
364            shmForAgg->prefixV4SubnetTab[index][i].flowNbOUT = 0;
365            shmForAgg->prefixV4SubnetTab[index][i].bytesNbOUT = 0;
366            shmForAgg->prefixV4SubnetTab[index][i].pktsNbOUT = 0;
367            shmForAgg->prefixV4SubnetTab[index][i].firstCoSIN = 0;
368            shmForAgg->prefixV4SubnetTab[index][i].secondCoSIN = 0;
369            shmForAgg->prefixV4SubnetTab[index][i].thirdCoSIN = 0;
370            shmForAgg->prefixV4SubnetTab[index][i].fourthCoSIN = 0;
371            shmForAgg->prefixV4SubnetTab[index][i].firstCoSOUT = 0;
372            shmForAgg->prefixV4SubnetTab[index][i].secondCoSOUT = 0;
373            shmForAgg->prefixV4SubnetTab[index][i].thirdCoSOUT = 0;
374            shmForAgg->prefixV4SubnetTab[index][i].fourthCoSOUT = 0;
375          }
376        }
377#if defined(MATRIX) && defined(IPV4AGGIDR)
378        /* MATRIX INTER POP ACCOUNTING */
379        /* rrd */
380        for (i=0; i<ROUTER_INDEX_MAX; i++){
381          for (j=0; j<ROUTER_INDEX_MAX; j++){
382            /* HERE create or update RRD FILE */
383            for (k=0; k<strlen(name); k++) {name[k] = '\0';}
384            for (k=0; k<strlen(createstr); k++) {createstr[k] = '\0';}
385            for (k=0; k<strlen(indexStr); k++) {indexStr[k] = '\0';}
386            for (k=0; k<strlen(buf); k++) {buf[k] = '\0';}
387            strcat(name, MATRIX_RRD_LOCATION);
388            sprintf(indexStr, "%d_%d",
389                    i,
390                    j
391                    );
392            strcat(name, indexStr);
393            strcat(name, RRD_EXTENSION);
394            if ( (fp=fopen(name,"r")) == NULL ){
395              opt[0]= (char *) malloc((strlen(RRD_PARAM_MATRIX_0) + 1) * sizeof(char));
396              strcpy(opt[0], RRD_PARAM_MATRIX_0);
397              opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
398              strcpy(opt[1], name);
399              opt[2]= (char *) malloc((strlen(RRD_PARAM_MATRIX_1) + 1) * sizeof(char));
400              strcpy(opt[2], RRD_PARAM_MATRIX_1);
401              opt[3]= (char *) malloc((strlen(RRD_PARAM_MATRIX_2) + 1) * sizeof(char));
402              strcpy(opt[3], RRD_PARAM_MATRIX_2);
403              opt[4]= (char *) malloc((strlen(RRD_PARAM_MATRIX_3) + 1) * sizeof(char));
404              strcpy(opt[4], RRD_PARAM_MATRIX_3);
405              opt[5]= (char *) malloc((strlen(RRD_PARAM_MATRIX_4) + 1) * sizeof(char));
406              strcpy(opt[5], RRD_PARAM_MATRIX_4);
407              opt[6]= (char *) malloc((strlen(RRD_PARAM_MATRIX_5) + 1) * sizeof(char));
408              strcpy(opt[6], RRD_PARAM_MATRIX_5);
409              opt[7]= (char *) malloc((strlen(RRD_PARAM_MATRIX_6) + 1) * sizeof(char));
410              strcpy(opt[7], RRD_PARAM_MATRIX_6);
411              opt[8]= (char *) malloc((strlen(RRD_PARAM_MATRIX_7) + 1) * sizeof(char));
412              strcpy(opt[8], RRD_PARAM_MATRIX_7);
413              opt[9]= (char *) malloc((strlen(RRD_PARAM_MATRIX_8) + 1) * sizeof(char));
414              strcpy(opt[9], RRD_PARAM_MATRIX_8);
415              opt[10]= (char *) malloc((strlen(RRD_PARAM_MATRIX_9) + 1) * sizeof(char));
416              strcpy(opt[10], RRD_PARAM_MATRIX_9);
417              opt[11]= (char *) malloc((strlen(RRD_PARAM_MATRIX_10) + 1) * sizeof(char));
418              strcpy(opt[11], RRD_PARAM_MATRIX_10);
419              opt[12]= (char *) malloc((strlen(RRD_PARAM_MATRIX_11) + 1) * sizeof(char));
420              strcpy(opt[12], RRD_PARAM_MATRIX_11);
421              opt[13]= (char *) malloc((strlen(RRD_PARAM_MATRIX_12) + 1) * sizeof(char));
422              strcpy(opt[13], RRD_PARAM_MATRIX_12);
423              opt[14]= (char *) malloc((strlen(RRD_PARAM_MATRIX_13) + 1) * sizeof(char));
424              strcpy(opt[14], RRD_PARAM_MATRIX_13);
425              opt[15]= (char *) malloc((strlen(RRD_PARAM_MATRIX_14) + 1) * sizeof(char));
426              strcpy(opt[15], RRD_PARAM_MATRIX_14);
427              optind = opterr = 0;
428              rrd_clear_error();
429              if ( rrd_create(16,opt) < 0) {
430                syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
431                rrd_create_er_ct++;
432              } else {
433                rrd_create_ok_ct++;
434              }
435              for ( k=0; k<16; k++) {
436                free(opt[k]);
437                opt[k] = NULL;
438              }
439            }else{
440              fclose(fp);
441              rrd_already_created_ct++;
442              snprintf(buf,
443                       2048,
444                       "%lu:%llu:%llu:%llu"
445                       now-300,  /* or ctime(&now) with %s */
446                       shmForAgg->matrixPOP[index][i][j].flowNb,
447                       shmForAgg->matrixPOP[index][i][j].bytesNb,
448                       shmForAgg->matrixPOP[index][i][j].pktsNb
449                       );
450              optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
451              strcpy(optUpdate[0], RRD_UPDATE_0);
452              optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
453              strcpy(optUpdate[1], name);
454              optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
455              strcpy(optUpdate[2], buf);
456              optind = opterr = 0;
457              rrd_clear_error();
458              if ( rrd_update(3, optUpdate) < 0 ) {
459                syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
460                rrd_update_er_ct++;
461              } else {
462                rrd_update_ok_ct++;
463              }
464              for ( k=0; k<3; k++) {
465                free(optUpdate[k]);
466                optUpdate[k] = NULL;
467              }
468            }
469
470          }
471        }
472        /* reinit */
473        for (i=0; i<ROUTER_INDEX_MAX; i++){
474          for (j=0; j<ROUTER_INDEX_MAX; j++) {
475            shmForAgg->matrixPOP[index][i][j].bytesNb = 0;
476            shmForAgg->matrixPOP[index][i][j].pktsNb = 0;
477            shmForAgg->matrixPOP[index][i][j].flowNb = 0;
478          }
479        }
480        /* END MATRIX INTER POP ACC */
481#endif /* MATRIX */
482        now2 = time((time_t *)NULL);
483#ifdef DEBUGAGG
484        fprintf(stderr, "IPv4 subnet managment (rrd update) : \n %d sec, %d files updated, %d errors, total %d\n %d rrd file already created, %d errors in creation, %d new rrd file creations",
485                (int)now2-now,
486                rrd_update_ok_ct,
487                rrd_update_er_ct,
488                rrd_update_ok_ct+rrd_update_er_ct,
489                rrd_already_created_ct,
490                rrd_create_er_ct,
491                rrd_create_ok_ct);
492#endif
493        rrd_update_ok_ct = 0;
494        rrd_update_er_ct = 0;
495        rrd_already_created_ct = 0;
496        rrd_create_er_ct = 0;
497        rrd_create_ok_ct = 0;
498        rrd_clear_error();
499      } else {
500        isFirstUpdate = 0;
501        /* Reinit the shared table */
502        for (i=0; i<shmForAgg->v4PrefixNb; i++){
503          shmForAgg->prefixV4Tab[index][i].flowNbIN = 0;
504          shmForAgg->prefixV4Tab[index][i].bytesNbIN = 0;
505          shmForAgg->prefixV4Tab[index][i].pktsNbIN = 0;
506          shmForAgg->prefixV4Tab[index][i].flowNbOUT = 0;
507          shmForAgg->prefixV4Tab[index][i].bytesNbOUT = 0;
508          shmForAgg->prefixV4Tab[index][i].pktsNbOUT = 0;
509          shmForAgg->prefixV4Tab[index][i].firstCoSIN = 0;
510          shmForAgg->prefixV4Tab[index][i].secondCoSIN = 0;
511          shmForAgg->prefixV4Tab[index][i].thirdCoSIN = 0;
512          shmForAgg->prefixV4Tab[index][i].fourthCoSIN = 0;
513          shmForAgg->prefixV4Tab[index][i].firstCoSOUT = 0;
514          shmForAgg->prefixV4Tab[index][i].secondCoSOUT = 0;
515          shmForAgg->prefixV4Tab[index][i].thirdCoSOUT = 0;
516          shmForAgg->prefixV4Tab[index][i].fourthCoSOUT = 0;
517
518          shmForAgg->prefixV4SubnetTab[index][i].flowNbIN = 0;
519          shmForAgg->prefixV4SubnetTab[index][i].bytesNbIN = 0;
520          shmForAgg->prefixV4SubnetTab[index][i].pktsNbIN = 0;
521          shmForAgg->prefixV4SubnetTab[index][i].flowNbOUT = 0;
522          shmForAgg->prefixV4SubnetTab[index][i].bytesNbOUT = 0;
523          shmForAgg->prefixV4SubnetTab[index][i].pktsNbOUT = 0;
524          shmForAgg->prefixV4SubnetTab[index][i].firstCoSIN = 0;
525          shmForAgg->prefixV4SubnetTab[index][i].secondCoSIN = 0;
526          shmForAgg->prefixV4SubnetTab[index][i].thirdCoSIN = 0;
527          shmForAgg->prefixV4SubnetTab[index][i].fourthCoSIN = 0;
528          shmForAgg->prefixV4SubnetTab[index][i].firstCoSOUT = 0;
529          shmForAgg->prefixV4SubnetTab[index][i].secondCoSOUT = 0;
530          shmForAgg->prefixV4SubnetTab[index][i].thirdCoSOUT = 0;
531          shmForAgg->prefixV4SubnetTab[index][i].fourthCoSOUT = 0;
532        }
533        /* reinit */
534#ifdef MATRIX
535        for (i=0; i<ROUTER_INDEX_MAX; i++){
536          for (j=0; j<ROUTER_INDEX_MAX; j++) {
537            shmForAgg->matrixPOP[index][i][j].bytesNb = 0;
538            shmForAgg->matrixPOP[index][i][j].pktsNb = 0;
539            shmForAgg->matrixPOP[index][i][j].flowNb = 0;
540          }
541        }
542#endif
543      } /* end is first update */ 
544    } /* end read ok */
545    sleep(10);
546  } while (1);
547  exit(0);
548}
Note: See TracBrowser for help on using the browser.