root/trunk/src/renetcolAgg.c

Revision 157, 66.1 KB (checked in by andreu, 11 years ago)

renetcol mib update

  • Property svn:eol-style set to native
Line 
1/*
2 * File: renetcolAgg.c
3 *
4 * Authors: ANDREU Francois-Xavier
5 *
6 * Copyright (C) 2007-2011 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 * end of creation. So the maximum number of files which can be opened for one
32 * process in the same time is reached and 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#ifdef IPV6LINKAGG
56    static char ipv6LinkStr[40]; /* 10_0_0_1_20_136 <router address> + <sampling> + <SNMP index> */
57#endif
58#if defined(MATRIX) && defined(IPV4AGGIDR)
59    static char indexStr[24];
60#endif
61    static char asStr[7];
62    static char createstr[2048];
63    char *opt[27];
64    char *optUpdate[3];
65    FILE *fp;
66    static time_t now, now2;
67
68    key = 8765;
69    if ((shmid = shmget(key, SHMSIZE, 0666)) < 0)
70    {
71        perror("shmget");
72        exit(1);
73    }
74    if ((shmForAgg = (struct SHMForAgg *)shmat(shmid, (void *)0, 0)) == (void *) -1)
75    {
76        perror("shmat");
77        exit(1);
78    }
79
80    fprintf(stderr, "renetcolAgg: I become a deamon, next messages via syslogd. Bye.\n");
81    if (fork () != 0)
82        exit (0);
83    if (setsid() == -1)
84    {
85        exit(4);
86    }
87
88    do
89    {
90        if (shmForAgg->readed == 1)
91        {
92            shmForAgg->readed = 0;
93            if (shmForAgg->currentTable == 0)
94            {
95                index = 1;
96            }
97            else
98            {
99                index = 0;
100            }
101            now = time((time_t *)NULL);
102            if (isFirstUpdate == 0)
103            {
104                /* RRD ACCOUNTING for prefixV4Tab */
105                for (i=0; i<shmForAgg->v4PrefixNb; i++)
106                {
107                    /* HERE create or update RRD FILE */
108                    if (shmForAgg->prefixV4Tab[index][i].sampling != 0)
109                    {
110                        for (j=0; j<strlen(name); j++)
111                        {
112                            name[j] = '\0';
113                        }
114                        for (j=0; j<strlen(createstr); j++)
115                        {
116                            createstr[j] = '\0';
117                        }
118                        for (j=0; j<strlen(ipStr); j++)
119                        {
120                            ipStr[j] = '\0';
121                        }
122                        for (j=0; j<strlen(buf); j++)
123                        {
124                            buf[j] = '\0';
125                        }
126                        strcat(name, PREFIX_RRD_LOCATION);
127                        sprintf(ipStr, "%lu_%lu_%lu_%lu_%hu_%lu",
128                                shmForAgg->prefixV4Tab[index][i].beginning>>24,
129                                shmForAgg->prefixV4Tab[index][i].beginning<<8>>24,
130                                shmForAgg->prefixV4Tab[index][i].beginning<<16>>24,
131                                shmForAgg->prefixV4Tab[index][i].beginning<<24>>24,
132                                shmForAgg->prefixV4Tab[index][i].mask,
133                                shmForAgg->prefixV4Tab[index][i].sampling
134                               );
135                        strcat(name, ipStr);
136                        strcat(name, RRD_EXTENSION);
137                        if ( (fp=fopen(name,"r")) == NULL )
138                        {
139                            opt[0]= (char *) malloc((strlen(RRD_PARAM_PREFIX_0) + 1) * sizeof(char));
140                            strcpy(opt[0], RRD_PARAM_PREFIX_0);
141                            opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
142                            strcpy(opt[1], name);
143                            opt[2]= (char *) malloc((strlen(RRD_PARAM_PREFIX_1) + 1) * sizeof(char));
144                            strcpy(opt[2], RRD_PARAM_PREFIX_1);
145                            opt[3]= (char *) malloc((strlen(RRD_PARAM_PREFIX_2) + 1) * sizeof(char));
146                            strcpy(opt[3], RRD_PARAM_PREFIX_2);
147                            opt[4]= (char *) malloc((strlen(RRD_PARAM_PREFIX_3) + 1) * sizeof(char));
148                            strcpy(opt[4], RRD_PARAM_PREFIX_3);
149                            opt[5]= (char *) malloc((strlen(RRD_PARAM_PREFIX_4) + 1) * sizeof(char));
150                            strcpy(opt[5], RRD_PARAM_PREFIX_4);
151                            opt[6]= (char *) malloc((strlen(RRD_PARAM_PREFIX_5) + 1) * sizeof(char));
152                            strcpy(opt[6], RRD_PARAM_PREFIX_5);
153                            opt[7]= (char *) malloc((strlen(RRD_PARAM_PREFIX_6) + 1) * sizeof(char));
154                            strcpy(opt[7], RRD_PARAM_PREFIX_6);
155                            opt[8]= (char *) malloc((strlen(RRD_PARAM_PREFIX_7) + 1) * sizeof(char));
156                            strcpy(opt[8], RRD_PARAM_PREFIX_7);
157                            opt[9]= (char *) malloc((strlen(RRD_PARAM_PREFIX_8) + 1) * sizeof(char));
158                            strcpy(opt[9], RRD_PARAM_PREFIX_8);
159                            opt[10]= (char *) malloc((strlen(RRD_PARAM_PREFIX_9) + 1) * sizeof(char));
160                            strcpy(opt[10], RRD_PARAM_PREFIX_9);
161                            opt[11]= (char *) malloc((strlen(RRD_PARAM_PREFIX_10) + 1) * sizeof(char));
162                            strcpy(opt[11], RRD_PARAM_PREFIX_10);
163                            opt[12]= (char *) malloc((strlen(RRD_PARAM_PREFIX_11) + 1) * sizeof(char));
164                            strcpy(opt[12], RRD_PARAM_PREFIX_11);
165                            opt[13]= (char *) malloc((strlen(RRD_PARAM_PREFIX_12) + 1) * sizeof(char));
166                            strcpy(opt[13], RRD_PARAM_PREFIX_12);
167                            opt[14]= (char *) malloc((strlen(RRD_PARAM_PREFIX_13) + 1) * sizeof(char));
168                            strcpy(opt[14], RRD_PARAM_PREFIX_13);
169                            opt[15]= (char *) malloc((strlen(RRD_PARAM_PREFIX_14) + 1) * sizeof(char));
170                            strcpy(opt[15], RRD_PARAM_PREFIX_14);
171                            opt[16]= (char *) malloc((strlen(RRD_PARAM_PREFIX_15) + 1) * sizeof(char));
172                            strcpy(opt[16], RRD_PARAM_PREFIX_15);
173                            opt[17]= (char *) malloc((strlen(RRD_PARAM_PREFIX_16) + 1) * sizeof(char));
174                            strcpy(opt[17], RRD_PARAM_PREFIX_16);
175                            opt[18]= (char *) malloc((strlen(RRD_PARAM_PREFIX_17) + 1) * sizeof(char));
176                            strcpy(opt[18], RRD_PARAM_PREFIX_17);
177                            opt[19]= (char *) malloc((strlen(RRD_PARAM_PREFIX_18) + 1) * sizeof(char));
178                            strcpy(opt[19], RRD_PARAM_PREFIX_18);
179                            opt[20]= (char *) malloc((strlen(RRD_PARAM_PREFIX_19) + 1) * sizeof(char));
180                            strcpy(opt[20], RRD_PARAM_PREFIX_19);
181                            opt[21]= (char *) malloc((strlen(RRD_PARAM_PREFIX_20) + 1) * sizeof(char));
182                            strcpy(opt[21], RRD_PARAM_PREFIX_20);
183                            opt[22]= (char *) malloc((strlen(RRD_PARAM_PREFIX_21) + 1) * sizeof(char));
184                            strcpy(opt[22], RRD_PARAM_PREFIX_21);
185                            opt[23]= (char *) malloc((strlen(RRD_PARAM_PREFIX_22) + 1) * sizeof(char));
186                            strcpy(opt[23], RRD_PARAM_PREFIX_22);
187                            opt[24]= (char *) malloc((strlen(RRD_PARAM_PREFIX_23) + 1) * sizeof(char));
188                            strcpy(opt[24], RRD_PARAM_PREFIX_23);
189                            opt[25]= (char *) malloc((strlen(RRD_PARAM_PREFIX_24) + 1) * sizeof(char));
190                            strcpy(opt[25], RRD_PARAM_PREFIX_24);
191                            opt[26]= (char *) malloc((strlen(RRD_PARAM_PREFIX_25) + 1) * sizeof(char));
192                            strcpy(opt[26], RRD_PARAM_PREFIX_25);
193                            optind = opterr = 0;
194                            rrd_clear_error();
195                            if ( rrd_create(27,opt) < 0)
196                            {
197                                syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
198                                rrd_create_er_ct++;
199                            }
200                            else
201                            {
202                                rrd_create_ok_ct++;
203                            }
204                            for ( j=0; j<27; j++)
205                            {
206                                free(opt[j]);
207                                opt[j] = NULL;
208                            }
209                        }
210                        else
211                        {
212                            fclose(fp);
213                            rrd_already_created_ct++;
214                            snprintf(buf,
215                                     2048,
216                                     "%lu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
217                                     now-300,  /* or ctime(&now) with %s */
218                                     shmForAgg->prefixV4Tab[index][i].flowNbIN,  /* 1 */
219                                     shmForAgg->prefixV4Tab[index][i].flowNbOUT,
220                                     shmForAgg->prefixV4Tab[index][i].bytesNbIN,
221                                     shmForAgg->prefixV4Tab[index][i].bytesNbOUT,
222                                     shmForAgg->prefixV4Tab[index][i].pktsNbIN,  /* 5 */
223                                     shmForAgg->prefixV4Tab[index][i].pktsNbOUT,
224                                     shmForAgg->prefixV4Tab[index][i].firstCoSIN,
225                                     shmForAgg->prefixV4Tab[index][i].firstCoSOUT,
226                                     shmForAgg->prefixV4Tab[index][i].secondCoSIN,
227                                     shmForAgg->prefixV4Tab[index][i].secondCoSOUT,
228                                     shmForAgg->prefixV4Tab[index][i].thirdCoSIN,
229                                     shmForAgg->prefixV4Tab[index][i].thirdCoSOUT,
230                                     shmForAgg->prefixV4Tab[index][i].fourthCoSIN,
231                                     shmForAgg->prefixV4Tab[index][i].fourthCoSOUT
232                                    );
233                            optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
234                            strcpy(optUpdate[0], RRD_UPDATE_0);
235                            optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
236                            strcpy(optUpdate[1], name);
237                            optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
238                            strcpy(optUpdate[2], buf);
239                            optind = opterr = 0;
240                            rrd_clear_error();
241                            if ( rrd_update(3, optUpdate) < 0 )
242                            {
243                                syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
244                                rrd_update_er_ct++;
245                            }
246                            else
247                            {
248                                rrd_update_ok_ct++;
249                            }
250                            for ( j=0; j<3; j++)
251                            {
252                                free(optUpdate[j]);
253                                optUpdate[j] = NULL;
254                            }
255                        }
256                        /* Reinit the shared tables */
257                        shmForAgg->prefixV4Tab[index][i].flowNbIN = 0;
258                        shmForAgg->prefixV4Tab[index][i].bytesNbIN = 0;
259                        shmForAgg->prefixV4Tab[index][i].pktsNbIN = 0;
260                        shmForAgg->prefixV4Tab[index][i].flowNbOUT = 0;
261                        shmForAgg->prefixV4Tab[index][i].bytesNbOUT = 0;
262                        shmForAgg->prefixV4Tab[index][i].pktsNbOUT = 0;
263                        shmForAgg->prefixV4Tab[index][i].firstCoSIN = 0;
264                        shmForAgg->prefixV4Tab[index][i].secondCoSIN = 0;
265                        shmForAgg->prefixV4Tab[index][i].thirdCoSIN = 0;
266                        shmForAgg->prefixV4Tab[index][i].fourthCoSIN = 0;
267                        shmForAgg->prefixV4Tab[index][i].firstCoSOUT = 0;
268                        shmForAgg->prefixV4Tab[index][i].secondCoSOUT = 0;
269                        shmForAgg->prefixV4Tab[index][i].thirdCoSOUT = 0;
270                        shmForAgg->prefixV4Tab[index][i].fourthCoSOUT = 0;
271                    }
272                }
273                /* ACCOUNTING for prefixV4SubnetTab */
274                for (i=0; i<shmForAgg->v4SubnetNb; i++)
275                {
276                    /* HERE create or update RRD FILE */
277                    if (shmForAgg->prefixV4SubnetTab[index][i].sampling != 0)
278                    {
279                        for (j=0; j<strlen(name); j++)
280                        {
281                            name[j] = '\0';
282                        }
283                        for (j=0; j<strlen(createstr); j++)
284                        {
285                            createstr[j] = '\0';
286                        }
287                        for (j=0; j<strlen(ipStr); j++)
288                        {
289                            ipStr[j] = '\0';
290                        }
291                        for (j=0; j<strlen(buf); j++)
292                        {
293                            buf[j] = '\0';
294                        }
295                        strcat(name, PREFIX_RRD_LOCATION);
296                        sprintf(ipStr, "%lu_%lu_%lu_%lu_%hu_%lu",
297                                shmForAgg->prefixV4SubnetTab[index][i].beginning>>24,
298                                shmForAgg->prefixV4SubnetTab[index][i].beginning<<8>>24,
299                                shmForAgg->prefixV4SubnetTab[index][i].beginning<<16>>24,
300                                shmForAgg->prefixV4SubnetTab[index][i].beginning<<24>>24,
301                                shmForAgg->prefixV4SubnetTab[index][i].mask,
302                                shmForAgg->prefixV4SubnetTab[index][i].sampling
303                               );
304                        strcat(name, ipStr);
305                        strcat(name, RRD_EXTENSION);
306                        if ( (fp=fopen(name,"r")) == NULL )
307                        {
308                            opt[0]= (char *) malloc((strlen(RRD_PARAM_PREFIX_0) + 1) * sizeof(char));
309                            strcpy(opt[0], RRD_PARAM_PREFIX_0);
310                            opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
311                            strcpy(opt[1], name);
312                            opt[2]= (char *) malloc((strlen(RRD_PARAM_PREFIX_1) + 1) * sizeof(char));
313                            strcpy(opt[2], RRD_PARAM_PREFIX_1);
314                            opt[3]= (char *) malloc((strlen(RRD_PARAM_PREFIX_2) + 1) * sizeof(char));
315                            strcpy(opt[3], RRD_PARAM_PREFIX_2);
316                            opt[4]= (char *) malloc((strlen(RRD_PARAM_PREFIX_3) + 1) * sizeof(char));
317                            strcpy(opt[4], RRD_PARAM_PREFIX_3);
318                            opt[5]= (char *) malloc((strlen(RRD_PARAM_PREFIX_4) + 1) * sizeof(char));
319                            strcpy(opt[5], RRD_PARAM_PREFIX_4);
320                            opt[6]= (char *) malloc((strlen(RRD_PARAM_PREFIX_5) + 1) * sizeof(char));
321                            strcpy(opt[6], RRD_PARAM_PREFIX_5);
322                            opt[7]= (char *) malloc((strlen(RRD_PARAM_PREFIX_6) + 1) * sizeof(char));
323                            strcpy(opt[7], RRD_PARAM_PREFIX_6);
324                            opt[8]= (char *) malloc((strlen(RRD_PARAM_PREFIX_7) + 1) * sizeof(char));
325                            strcpy(opt[8], RRD_PARAM_PREFIX_7);
326                            opt[9]= (char *) malloc((strlen(RRD_PARAM_PREFIX_8) + 1) * sizeof(char));
327                            strcpy(opt[9], RRD_PARAM_PREFIX_8);
328                            opt[10]= (char *) malloc((strlen(RRD_PARAM_PREFIX_9) + 1) * sizeof(char));
329                            strcpy(opt[10], RRD_PARAM_PREFIX_9);
330                            opt[11]= (char *) malloc((strlen(RRD_PARAM_PREFIX_10) + 1) * sizeof(char));
331                            strcpy(opt[11], RRD_PARAM_PREFIX_10);
332                            opt[12]= (char *) malloc((strlen(RRD_PARAM_PREFIX_11) + 1) * sizeof(char));
333                            strcpy(opt[12], RRD_PARAM_PREFIX_11);
334                            opt[13]= (char *) malloc((strlen(RRD_PARAM_PREFIX_12) + 1) * sizeof(char));
335                            strcpy(opt[13], RRD_PARAM_PREFIX_12);
336                            opt[14]= (char *) malloc((strlen(RRD_PARAM_PREFIX_13) + 1) * sizeof(char));
337                            strcpy(opt[14], RRD_PARAM_PREFIX_13);
338                            opt[15]= (char *) malloc((strlen(RRD_PARAM_PREFIX_14) + 1) * sizeof(char));
339                            strcpy(opt[15], RRD_PARAM_PREFIX_14);
340                            opt[16]= (char *) malloc((strlen(RRD_PARAM_PREFIX_15) + 1) * sizeof(char));
341                            strcpy(opt[16], RRD_PARAM_PREFIX_15);
342                            opt[17]= (char *) malloc((strlen(RRD_PARAM_PREFIX_16) + 1) * sizeof(char));
343                            strcpy(opt[17], RRD_PARAM_PREFIX_16);
344                            opt[18]= (char *) malloc((strlen(RRD_PARAM_PREFIX_17) + 1) * sizeof(char));
345                            strcpy(opt[18], RRD_PARAM_PREFIX_17);
346                            opt[19]= (char *) malloc((strlen(RRD_PARAM_PREFIX_18) + 1) * sizeof(char));
347                            strcpy(opt[19], RRD_PARAM_PREFIX_18);
348                            opt[20]= (char *) malloc((strlen(RRD_PARAM_PREFIX_19) + 1) * sizeof(char));
349                            strcpy(opt[20], RRD_PARAM_PREFIX_19);
350                            opt[21]= (char *) malloc((strlen(RRD_PARAM_PREFIX_20) + 1) * sizeof(char));
351                            strcpy(opt[21], RRD_PARAM_PREFIX_20);
352                            opt[22]= (char *) malloc((strlen(RRD_PARAM_PREFIX_21) + 1) * sizeof(char));
353                            strcpy(opt[22], RRD_PARAM_PREFIX_21);
354                            opt[23]= (char *) malloc((strlen(RRD_PARAM_PREFIX_22) + 1) * sizeof(char));
355                            strcpy(opt[23], RRD_PARAM_PREFIX_22);
356                            opt[24]= (char *) malloc((strlen(RRD_PARAM_PREFIX_23) + 1) * sizeof(char));
357                            strcpy(opt[24], RRD_PARAM_PREFIX_23);
358                            opt[25]= (char *) malloc((strlen(RRD_PARAM_PREFIX_24) + 1) * sizeof(char));
359                            strcpy(opt[25], RRD_PARAM_PREFIX_24);
360                            opt[26]= (char *) malloc((strlen(RRD_PARAM_PREFIX_25) + 1) * sizeof(char));
361                            strcpy(opt[26], RRD_PARAM_PREFIX_25);
362                            optind = opterr = 0;
363                            rrd_clear_error();
364                            if ( rrd_create(27,opt) < 0)
365                            {
366                                syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
367                                rrd_create_er_ct++;
368                            }
369                            else
370                            {
371                                rrd_create_ok_ct++;
372                            }
373                            for ( j=0; j<27; j++)
374                            {
375                                free(opt[j]);
376                                opt[j] = NULL;
377                            }
378                        }
379                        else
380                        {
381                            fclose(fp);
382                            rrd_already_created_ct++;
383                            snprintf(buf,
384                                     2048,
385                                     "%lu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
386                                     now-300,  /* or ctime(&now) with %s */
387                                     shmForAgg->prefixV4SubnetTab[index][i].flowNbIN,  /* 1 */
388                                     shmForAgg->prefixV4SubnetTab[index][i].flowNbOUT,
389                                     shmForAgg->prefixV4SubnetTab[index][i].bytesNbIN,
390                                     shmForAgg->prefixV4SubnetTab[index][i].bytesNbOUT,
391                                     shmForAgg->prefixV4SubnetTab[index][i].pktsNbIN,  /* 5 */
392                                     shmForAgg->prefixV4SubnetTab[index][i].pktsNbOUT,
393                                     shmForAgg->prefixV4SubnetTab[index][i].firstCoSIN,
394                                     shmForAgg->prefixV4SubnetTab[index][i].firstCoSOUT,
395                                     shmForAgg->prefixV4SubnetTab[index][i].secondCoSIN,
396                                     shmForAgg->prefixV4SubnetTab[index][i].secondCoSOUT,
397                                     shmForAgg->prefixV4SubnetTab[index][i].thirdCoSIN,
398                                     shmForAgg->prefixV4SubnetTab[index][i].thirdCoSOUT,
399                                     shmForAgg->prefixV4SubnetTab[index][i].fourthCoSIN,
400                                     shmForAgg->prefixV4SubnetTab[index][i].fourthCoSOUT
401                                    );
402                            optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
403                            strcpy(optUpdate[0], RRD_UPDATE_0);
404                            optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
405                            strcpy(optUpdate[1], name);
406                            optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
407                            strcpy(optUpdate[2], buf);
408                            optind = opterr = 0;
409                            rrd_clear_error();
410                            if ( rrd_update(3, optUpdate) < 0 )
411                            {
412                                syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
413                                rrd_update_er_ct++;
414                            }
415                            else
416                            {
417                                rrd_update_ok_ct++;
418                            }
419                            for ( j=0; j<3; j++)
420                            {
421                                free(optUpdate[j]);
422                                optUpdate[j] = NULL;
423                            }
424                        }
425                        /* Reinit the shared tables */
426                        shmForAgg->prefixV4SubnetTab[index][i].flowNbIN = 0;
427                        shmForAgg->prefixV4SubnetTab[index][i].bytesNbIN = 0;
428                        shmForAgg->prefixV4SubnetTab[index][i].pktsNbIN = 0;
429                        shmForAgg->prefixV4SubnetTab[index][i].flowNbOUT = 0;
430                        shmForAgg->prefixV4SubnetTab[index][i].bytesNbOUT = 0;
431                        shmForAgg->prefixV4SubnetTab[index][i].pktsNbOUT = 0;
432                        shmForAgg->prefixV4SubnetTab[index][i].firstCoSIN = 0;
433                        shmForAgg->prefixV4SubnetTab[index][i].secondCoSIN = 0;
434                        shmForAgg->prefixV4SubnetTab[index][i].thirdCoSIN = 0;
435                        shmForAgg->prefixV4SubnetTab[index][i].fourthCoSIN = 0;
436                        shmForAgg->prefixV4SubnetTab[index][i].firstCoSOUT = 0;
437                        shmForAgg->prefixV4SubnetTab[index][i].secondCoSOUT = 0;
438                        shmForAgg->prefixV4SubnetTab[index][i].thirdCoSOUT = 0;
439                        shmForAgg->prefixV4SubnetTab[index][i].fourthCoSOUT = 0;
440                    }
441                }
442#if defined(MATRIX) && defined(IPV4AGGIDR)
443                /* MATRIX INTER POP ACCOUNTING */
444                /* rrd */
445                for (i=0; i<ROUTER_INDEX_MAX; i++)
446                {
447                    for (j=0; j<ROUTER_INDEX_MAX; j++)
448                    {
449                        /* HERE create or update RRD FILE */
450                        for (k=0; k<strlen(name); k++)
451                        {
452                            name[k] = '\0';
453                        }
454                        for (k=0; k<strlen(createstr); k++)
455                        {
456                            createstr[k] = '\0';
457                        }
458                        for (k=0; k<strlen(indexStr); k++)
459                        {
460                            indexStr[k] = '\0';
461                        }
462                        for (k=0; k<strlen(buf); k++)
463                        {
464                            buf[k] = '\0';
465                        }
466                        strcat(name, MATRIX_RRD_LOCATION);
467                        sprintf(indexStr, "%d_%d",
468                                i,
469                                j
470                               );
471                        strcat(name, indexStr);
472                        strcat(name, RRD_EXTENSION);
473                        if ( (fp=fopen(name,"r")) == NULL )
474                        {
475                            opt[0]= (char *) malloc((strlen(RRD_PARAM_MATRIX_0) + 1) * sizeof(char));
476                            strcpy(opt[0], RRD_PARAM_MATRIX_0);
477                            opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
478                            strcpy(opt[1], name);
479                            opt[2]= (char *) malloc((strlen(RRD_PARAM_MATRIX_1) + 1) * sizeof(char));
480                            strcpy(opt[2], RRD_PARAM_MATRIX_1);
481                            opt[3]= (char *) malloc((strlen(RRD_PARAM_MATRIX_2) + 1) * sizeof(char));
482                            strcpy(opt[3], RRD_PARAM_MATRIX_2);
483                            opt[4]= (char *) malloc((strlen(RRD_PARAM_MATRIX_3) + 1) * sizeof(char));
484                            strcpy(opt[4], RRD_PARAM_MATRIX_3);
485                            opt[5]= (char *) malloc((strlen(RRD_PARAM_MATRIX_4) + 1) * sizeof(char));
486                            strcpy(opt[5], RRD_PARAM_MATRIX_4);
487                            opt[6]= (char *) malloc((strlen(RRD_PARAM_MATRIX_5) + 1) * sizeof(char));
488                            strcpy(opt[6], RRD_PARAM_MATRIX_5);
489                            opt[7]= (char *) malloc((strlen(RRD_PARAM_MATRIX_6) + 1) * sizeof(char));
490                            strcpy(opt[7], RRD_PARAM_MATRIX_6);
491                            opt[8]= (char *) malloc((strlen(RRD_PARAM_MATRIX_7) + 1) * sizeof(char));
492                            strcpy(opt[8], RRD_PARAM_MATRIX_7);
493                            opt[9]= (char *) malloc((strlen(RRD_PARAM_MATRIX_8) + 1) * sizeof(char));
494                            strcpy(opt[9], RRD_PARAM_MATRIX_8);
495                            opt[10]= (char *) malloc((strlen(RRD_PARAM_MATRIX_9) + 1) * sizeof(char));
496                            strcpy(opt[10], RRD_PARAM_MATRIX_9);
497                            opt[11]= (char *) malloc((strlen(RRD_PARAM_MATRIX_10) + 1) * sizeof(char));
498                            strcpy(opt[11], RRD_PARAM_MATRIX_10);
499                            opt[12]= (char *) malloc((strlen(RRD_PARAM_MATRIX_11) + 1) * sizeof(char));
500                            strcpy(opt[12], RRD_PARAM_MATRIX_11);
501                            opt[13]= (char *) malloc((strlen(RRD_PARAM_MATRIX_12) + 1) * sizeof(char));
502                            strcpy(opt[13], RRD_PARAM_MATRIX_12);
503                            opt[14]= (char *) malloc((strlen(RRD_PARAM_MATRIX_13) + 1) * sizeof(char));
504                            strcpy(opt[14], RRD_PARAM_MATRIX_13);
505                            opt[15]= (char *) malloc((strlen(RRD_PARAM_MATRIX_14) + 1) * sizeof(char));
506                            strcpy(opt[15], RRD_PARAM_MATRIX_14);
507                            optind = opterr = 0;
508                            rrd_clear_error();
509                            if ( rrd_create(16,opt) < 0)
510                            {
511                                syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
512                                rrd_create_er_ct++;
513                            }
514                            else
515                            {
516                                rrd_create_ok_ct++;
517                            }
518                            for ( k=0; k<16; k++)
519                            {
520                                free(opt[k]);
521                                opt[k] = NULL;
522                            }
523                        }
524                        else
525                        {
526                            fclose(fp);
527                            rrd_already_created_ct++;
528                            snprintf(buf,
529                                     2048,
530                                     "%lu:%llu:%llu:%llu",
531                                     now-300,  /* or ctime(&now) with %s */
532                                     shmForAgg->matrixPOP[index][i][j].flowNb,
533                                     shmForAgg->matrixPOP[index][i][j].bytesNb,
534                                     shmForAgg->matrixPOP[index][i][j].pktsNb
535                                    );
536                            optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
537                            strcpy(optUpdate[0], RRD_UPDATE_0);
538                            optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
539                            strcpy(optUpdate[1], name);
540                            optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
541                            strcpy(optUpdate[2], buf);
542                            optind = opterr = 0;
543                            rrd_clear_error();
544                            if ( rrd_update(3, optUpdate) < 0 )
545                            {
546                                syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
547                                rrd_update_er_ct++;
548                            }
549                            else
550                            {
551                                rrd_update_ok_ct++;
552                            }
553                            for ( k=0; k<3; k++)
554                            {
555                                free(optUpdate[k]);
556                                optUpdate[k] = NULL;
557                            }
558                        }
559
560                    }
561                }
562                /* reinit */
563                for (i=0; i<ROUTER_INDEX_MAX; i++)
564                {
565                    for (j=0; j<ROUTER_INDEX_MAX; j++)
566                    {
567                        shmForAgg->matrixPOP[index][i][j].bytesNb = 0;
568                        shmForAgg->matrixPOP[index][i][j].pktsNb = 0;
569                        shmForAgg->matrixPOP[index][i][j].flowNb = 0;
570                    }
571                }
572                /* END MATRIX INTER POP ACC */
573#endif /* MATRIX */
574#ifdef ASACC
575                /*
576                 *
577                 * AS ACCOUNTING
578                 *
579                 */
580                for (i=0; i<shmForAgg->ASNb; i++)
581                {
582                    /* HERE create or update RRD FILE */
583                    if (shmForAgg->ASTab[index][i].sampling != 0)
584                    {
585                        for (j=0; j<strlen(name); j++)
586                        {
587                            name[j] = '\0';
588                        }
589                        for (j=0; j<strlen(createstr); j++)
590                        {
591                            createstr[j] = '\0';
592                        }
593                        for (j=0; j<strlen(asStr); j++)
594                        {
595                            asStr[j] = '\0';
596                        }
597                        for (j=0; j<strlen(buf); j++)
598                        {
599                            buf[j] = '\0';
600                        }
601                        strcat(name, AS_RRD_LOCATION);
602                        sprintf(asStr, "%hu_%lu",
603                                shmForAgg->ASTab[index][i].as,
604                                shmForAgg->ASTab[index][i].sampling
605                               );
606                        strcat(name, asStr);
607                        strcat(name, RRD_EXTENSION);
608                        if ( (fp=fopen(name,"r")) == NULL )
609                        {
610                            opt[0]= (char *) malloc((strlen(RRD_PARAM_AS_0) + 1) * sizeof(char));
611                            strcpy(opt[0], RRD_PARAM_AS_0);
612                            opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
613                            strcpy(opt[1], name);
614                            opt[2]= (char *) malloc((strlen(RRD_PARAM_AS_1) + 1) * sizeof(char));
615                            strcpy(opt[2], RRD_PARAM_AS_1);
616                            opt[3]= (char *) malloc((strlen(RRD_PARAM_AS_2) + 1) * sizeof(char));
617                            strcpy(opt[3], RRD_PARAM_AS_2);
618                            opt[4]= (char *) malloc((strlen(RRD_PARAM_AS_3) + 1) * sizeof(char));
619                            strcpy(opt[4], RRD_PARAM_AS_3);
620                            opt[5]= (char *) malloc((strlen(RRD_PARAM_AS_4) + 1) * sizeof(char));
621                            strcpy(opt[5], RRD_PARAM_AS_4);
622                            opt[6]= (char *) malloc((strlen(RRD_PARAM_AS_5) + 1) * sizeof(char));
623                            strcpy(opt[6], RRD_PARAM_AS_5);
624                            opt[7]= (char *) malloc((strlen(RRD_PARAM_AS_6) + 1) * sizeof(char));
625                            strcpy(opt[7], RRD_PARAM_AS_6);
626                            opt[8]= (char *) malloc((strlen(RRD_PARAM_AS_7) + 1) * sizeof(char));
627                            strcpy(opt[8], RRD_PARAM_AS_7);
628                            opt[9]= (char *) malloc((strlen(RRD_PARAM_AS_8) + 1) * sizeof(char));
629                            strcpy(opt[9], RRD_PARAM_AS_8);
630                            opt[10]= (char *) malloc((strlen(RRD_PARAM_AS_9) + 1) * sizeof(char));
631                            strcpy(opt[10], RRD_PARAM_AS_9);
632                            opt[11]= (char *) malloc((strlen(RRD_PARAM_AS_10) + 1) * sizeof(char));
633                            strcpy(opt[11], RRD_PARAM_AS_10);
634                            opt[12]= (char *) malloc((strlen(RRD_PARAM_AS_11) + 1) * sizeof(char));
635                            strcpy(opt[12], RRD_PARAM_AS_11);
636                            opt[13]= (char *) malloc((strlen(RRD_PARAM_AS_12) + 1) * sizeof(char));
637                            strcpy(opt[13], RRD_PARAM_AS_12);
638                            opt[14]= (char *) malloc((strlen(RRD_PARAM_AS_13) + 1) * sizeof(char));
639                            strcpy(opt[14], RRD_PARAM_AS_13);
640                            opt[15]= (char *) malloc((strlen(RRD_PARAM_AS_14) + 1) * sizeof(char));
641                            strcpy(opt[15], RRD_PARAM_AS_14);
642                            opt[16]= (char *) malloc((strlen(RRD_PARAM_AS_15) + 1) * sizeof(char));
643                            strcpy(opt[16], RRD_PARAM_AS_15);
644                            opt[17]= (char *) malloc((strlen(RRD_PARAM_AS_16) + 1) * sizeof(char));
645                            strcpy(opt[17], RRD_PARAM_AS_16);
646                            opt[18]= (char *) malloc((strlen(RRD_PARAM_AS_17) + 1) * sizeof(char));
647                            strcpy(opt[18], RRD_PARAM_AS_17);
648                            optind = opterr = 0;
649                            rrd_clear_error();
650                            if ( rrd_create(19,opt) < 0)
651                            {
652                                syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
653                                rrd_create_er_ct++;
654                            }
655                            else
656                            {
657                                rrd_create_ok_ct++;
658                            }
659                            for ( j=0; j<19; j++)
660                            {
661                                free(opt[j]);
662                                opt[j] = NULL;
663                            }
664                        }
665                        else
666                        {
667                            fclose(fp);
668                            rrd_already_created_ct++;
669                            snprintf(buf,
670                                     2048,
671                                     "%lu:%llu:%llu:%llu:%llu:%llu:%llu",
672                                     now-300,  /* or ctime(&now) with %s */
673                                     shmForAgg->ASTab[index][i].flowNbIN,  /* 1 */
674                                     shmForAgg->ASTab[index][i].flowNbOUT,
675                                     shmForAgg->ASTab[index][i].bytesNbIN,
676                                     shmForAgg->ASTab[index][i].bytesNbOUT,
677                                     shmForAgg->ASTab[index][i].pktsNbIN,  /* 5 */
678                                     shmForAgg->ASTab[index][i].pktsNbOUT
679                                    );
680                            optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
681                            strcpy(optUpdate[0], RRD_UPDATE_0);
682                            optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
683                            strcpy(optUpdate[1], name);
684                            optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
685                            strcpy(optUpdate[2], buf);
686                            optind = opterr = 0;
687                            rrd_clear_error();
688                            if ( rrd_update(3, optUpdate) < 0 )
689                            {
690                                syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
691                                rrd_update_er_ct++;
692                            }
693                            else
694                            {
695                                rrd_update_ok_ct++;
696                            }
697                            for ( j=0; j<3; j++)
698                            {
699                                free(optUpdate[j]);
700                                optUpdate[j] = NULL;
701                            }
702                        }
703                        /* Reinit the shared tables */
704                        shmForAgg->ASTab[index][i].flowNbIN = 0;
705                        shmForAgg->ASTab[index][i].bytesNbIN = 0;
706                        shmForAgg->ASTab[index][i].pktsNbIN = 0;
707                        shmForAgg->ASTab[index][i].flowNbOUT = 0;
708                        shmForAgg->ASTab[index][i].bytesNbOUT = 0;
709                        shmForAgg->ASTab[index][i].pktsNbOUT = 0;
710                    }
711                }
712#ifdef IPV6LINKAGG
713                /*
714                 *
715                 * IPv6 Links ACCOUNTING
716                 *
717                 */
718                for (i=0; i<ROUTER_INDEX_MAX; i++)
719                {
720                    for (j=0; j<MAX_INDEX_BY_ROUTER; j++)
721                    {
722                        /* HERE create or update RRD FILE for IPV6 LINKs AGGregation */
723                        if (shmForAgg->indexV6Tab[index][i][j].hasStats == 1)
724                        {
725                            for (k=0; k<strlen(name); k++)
726                            {
727                                name[k] = '\0';
728                            }
729                            for (k=0; k<strlen(createstr); k++)
730                            {
731                                createstr[k] = '\0';
732                            }
733                            for (k=0; k<strlen(ipv6LinkStr); k++)
734                            {
735                                ipv6LinkStr[k] = '\0';
736                            }
737                            for (k=0; k<strlen(buf); k++)
738                            {
739                                buf[k] = '\0';
740                            }
741                            strcat(name, SNMP_INDEX_RRD_LOCATION);
742                            sprintf(ipv6LinkStr, "%lu_%lu_%lu_%lu_%hu_%hu",
743                                    shmForAgg->indexV6Tab[index][i][j].routerIPAddress>>24,
744                                    shmForAgg->indexV6Tab[index][i][j].routerIPAddress<<8>>24,
745                                    shmForAgg->indexV6Tab[index][i][j].routerIPAddress<<16>>24,
746                                    shmForAgg->indexV6Tab[index][i][j].routerIPAddress<<24>>24,
747                                    (unsigned short) 1,
748                                    shmForAgg->indexV6Tab[index][i][j].indexSNMP
749                                   );
750                            strcat(name, ipv6LinkStr);
751                            strcat(name, RRD_EXTENSION);
752                            if ( (fp=fopen(name,"r")) == NULL )
753                            {
754                                opt[0]= (char *) malloc((strlen(RRD_PARAM_INDEX_0) + 1) * sizeof(char));
755                                strcpy(opt[0], RRD_PARAM_INDEX_0);
756                                opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
757                                strcpy(opt[1], name);
758                                opt[2]= (char *) malloc((strlen(RRD_PARAM_INDEX_1) + 1) * sizeof(char));
759                                strcpy(opt[2], RRD_PARAM_INDEX_1);
760                                opt[3]= (char *) malloc((strlen(RRD_PARAM_INDEX_2) + 1) * sizeof(char));
761                                strcpy(opt[3], RRD_PARAM_INDEX_2);
762                                opt[4]= (char *) malloc((strlen(RRD_PARAM_INDEX_3) + 1) * sizeof(char));
763                                strcpy(opt[4], RRD_PARAM_INDEX_3);
764                                opt[5]= (char *) malloc((strlen(RRD_PARAM_INDEX_4) + 1) * sizeof(char));
765                                strcpy(opt[5], RRD_PARAM_INDEX_4);
766                                opt[6]= (char *) malloc((strlen(RRD_PARAM_INDEX_5) + 1) * sizeof(char));
767                                strcpy(opt[6], RRD_PARAM_INDEX_5);
768                                opt[7]= (char *) malloc((strlen(RRD_PARAM_INDEX_6) + 1) * sizeof(char));
769                                strcpy(opt[7], RRD_PARAM_INDEX_6);
770                                opt[8]= (char *) malloc((strlen(RRD_PARAM_INDEX_7) + 1) * sizeof(char));
771                                strcpy(opt[8], RRD_PARAM_INDEX_7);
772                                opt[9]= (char *) malloc((strlen(RRD_PARAM_INDEX_8) + 1) * sizeof(char));
773                                strcpy(opt[9], RRD_PARAM_INDEX_8);
774                                opt[10]= (char *) malloc((strlen(RRD_PARAM_INDEX_9) + 1) * sizeof(char));
775                                strcpy(opt[10], RRD_PARAM_INDEX_9);
776                                opt[11]= (char *) malloc((strlen(RRD_PARAM_INDEX_10) + 1) * sizeof(char));
777                                strcpy(opt[11], RRD_PARAM_INDEX_10);
778                                opt[12]= (char *) malloc((strlen(RRD_PARAM_INDEX_11) + 1) * sizeof(char));
779                                strcpy(opt[12], RRD_PARAM_INDEX_11);
780                                opt[13]= (char *) malloc((strlen(RRD_PARAM_INDEX_12) + 1) * sizeof(char));
781                                strcpy(opt[13], RRD_PARAM_INDEX_12);
782                                opt[14]= (char *) malloc((strlen(RRD_PARAM_INDEX_13) + 1) * sizeof(char));
783                                strcpy(opt[14], RRD_PARAM_INDEX_13);
784                                opt[15]= (char *) malloc((strlen(RRD_PARAM_INDEX_14) + 1) * sizeof(char));
785                                strcpy(opt[15], RRD_PARAM_INDEX_14);
786                                opt[16]= (char *) malloc((strlen(RRD_PARAM_INDEX_15) + 1) * sizeof(char));
787                                strcpy(opt[16], RRD_PARAM_INDEX_15);
788                                opt[17]= (char *) malloc((strlen(RRD_PARAM_INDEX_16) + 1) * sizeof(char));
789                                strcpy(opt[17], RRD_PARAM_INDEX_16);
790                                opt[18]= (char *) malloc((strlen(RRD_PARAM_INDEX_17) + 1) * sizeof(char));
791                                strcpy(opt[18], RRD_PARAM_INDEX_17);
792                                optind = opterr = 0;
793                                rrd_clear_error();
794                                if ( rrd_create(19,opt) < 0)
795                                {
796                                    syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
797                                    rrd_create_er_ct++;
798                                }
799                                else
800                                {
801                                    rrd_create_ok_ct++;
802                                }
803                                for ( k=0; k<19; k++)
804                                {
805                                    free(opt[k]);
806                                    opt[k] = NULL;
807                                }
808                            }
809                            else
810                            {
811                                fclose(fp);
812                                rrd_already_created_ct++;
813                                snprintf(buf,
814                                         2048,
815                                         "%lu:%llu:%llu:%llu:%llu:%llu:%llu",
816                                         now-300,  /* or ctime(&now) with %s */
817                                         shmForAgg->indexV6Tab[index][i][j].flowNbIN,  /* 1 */
818                                         shmForAgg->indexV6Tab[index][i][j].flowNbOUT,
819                                         shmForAgg->indexV6Tab[index][i][j].bytesNbIN,
820                                         shmForAgg->indexV6Tab[index][i][j].bytesNbOUT,
821                                         shmForAgg->indexV6Tab[index][i][j].pktsNbIN,  /* 5 */
822                                         shmForAgg->indexV6Tab[index][i][j].pktsNbOUT
823                                        );
824                                optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
825                                strcpy(optUpdate[0], RRD_UPDATE_0);
826                                optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
827                                strcpy(optUpdate[1], name);
828                                optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
829                                strcpy(optUpdate[2], buf);
830                                optind = opterr = 0;
831                                rrd_clear_error();
832                                if ( rrd_update(3, optUpdate) < 0 )
833                                {
834                                    syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
835                                    rrd_update_er_ct++;
836                                }
837                                else
838                                {
839                                    rrd_update_ok_ct++;
840                                }
841                                for ( k=0; k<3; k++)
842                                {
843                                    free(optUpdate[k]);
844                                    optUpdate[k] = NULL;
845                                }
846                            }
847                            shmForAgg->indexV6Tab[index][i][j].bytesNbIN = 0;
848                            shmForAgg->indexV6Tab[index][i][j].pktsNbIN = 0;
849                            shmForAgg->indexV6Tab[index][i][j].flowNbIN = 0;
850                            shmForAgg->indexV6Tab[index][i][j].bytesNbOUT = 0;
851                            shmForAgg->indexV6Tab[index][i][j].pktsNbOUT = 0;
852                            shmForAgg->indexV6Tab[index][i][j].flowNbOUT = 0;
853                        }
854                    }
855                }
856#endif
857#endif /* AS */
858                if (rrdMibsUpdate(index) != 1)
859                {
860                    syslog(LOG_ERR, "ERROR in RRD MIBs update file\n");
861                }
862                if (rrdL7IPv4MibsUpdate(index) != 1)
863                {
864                    syslog(LOG_ERR, "ERROR in RRD MIBs update file\n");
865                }
866                now2 = time((time_t *)NULL);
867#ifdef DEBUGAGG
868                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",
869                        (int)now2-now,
870                        rrd_update_ok_ct,
871                        rrd_update_er_ct,
872                        rrd_update_ok_ct+rrd_update_er_ct,
873                        rrd_already_created_ct,
874                        rrd_create_er_ct,
875                        rrd_create_ok_ct);
876#endif
877                rrd_update_ok_ct = 0;
878                rrd_update_er_ct = 0;
879                rrd_already_created_ct = 0;
880                rrd_create_er_ct = 0;
881                rrd_create_ok_ct = 0;
882                rrd_clear_error();
883            }
884            else
885            {
886                isFirstUpdate = 0;
887                /* RRD Update only for the MIB counters, else we clear all counters in shared memory (after) */
888                if (rrdMibsUpdate(index) != 1)
889                {
890                    syslog(LOG_ERR, "ERROR in RRD MIBs update file\n");
891                }
892                if (rrdL7IPv4MibsUpdate(index) != 1)
893                {
894                    syslog(LOG_ERR, "ERROR in RRD MIBs update file\n");
895                }
896                /* CLEAR */
897                for (i=0; i<shmForAgg->v4PrefixNb; i++)
898                {
899                    shmForAgg->prefixV4Tab[index][i].flowNbIN = 0;
900                    shmForAgg->prefixV4Tab[index][i].bytesNbIN = 0;
901                    shmForAgg->prefixV4Tab[index][i].pktsNbIN = 0;
902                    shmForAgg->prefixV4Tab[index][i].flowNbOUT = 0;
903                    shmForAgg->prefixV4Tab[index][i].bytesNbOUT = 0;
904                    shmForAgg->prefixV4Tab[index][i].pktsNbOUT = 0;
905                    shmForAgg->prefixV4Tab[index][i].firstCoSIN = 0;
906                    shmForAgg->prefixV4Tab[index][i].secondCoSIN = 0;
907                    shmForAgg->prefixV4Tab[index][i].thirdCoSIN = 0;
908                    shmForAgg->prefixV4Tab[index][i].fourthCoSIN = 0;
909                    shmForAgg->prefixV4Tab[index][i].firstCoSOUT = 0;
910                    shmForAgg->prefixV4Tab[index][i].secondCoSOUT = 0;
911                    shmForAgg->prefixV4Tab[index][i].thirdCoSOUT = 0;
912                    shmForAgg->prefixV4Tab[index][i].fourthCoSOUT = 0;
913                }
914                for (i=0; i<shmForAgg->v4SubnetNb; i++)
915                {
916                    shmForAgg->prefixV4SubnetTab[index][i].flowNbIN = 0;
917                    shmForAgg->prefixV4SubnetTab[index][i].bytesNbIN = 0;
918                    shmForAgg->prefixV4SubnetTab[index][i].pktsNbIN = 0;
919                    shmForAgg->prefixV4SubnetTab[index][i].flowNbOUT = 0;
920                    shmForAgg->prefixV4SubnetTab[index][i].bytesNbOUT = 0;
921                    shmForAgg->prefixV4SubnetTab[index][i].pktsNbOUT = 0;
922                    shmForAgg->prefixV4SubnetTab[index][i].firstCoSIN = 0;
923                    shmForAgg->prefixV4SubnetTab[index][i].secondCoSIN = 0;
924                    shmForAgg->prefixV4SubnetTab[index][i].thirdCoSIN = 0;
925                    shmForAgg->prefixV4SubnetTab[index][i].fourthCoSIN = 0;
926                    shmForAgg->prefixV4SubnetTab[index][i].firstCoSOUT = 0;
927                    shmForAgg->prefixV4SubnetTab[index][i].secondCoSOUT = 0;
928                    shmForAgg->prefixV4SubnetTab[index][i].thirdCoSOUT = 0;
929                    shmForAgg->prefixV4SubnetTab[index][i].fourthCoSOUT = 0;
930                }
931#ifdef MATRIX
932                for (i=0; i<ROUTER_INDEX_MAX; i++)
933                {
934                    for (j=0; j<ROUTER_INDEX_MAX; j++)
935                    {
936                        shmForAgg->matrixPOP[index][i][j].bytesNb = 0;
937                        shmForAgg->matrixPOP[index][i][j].pktsNb = 0;
938                        shmForAgg->matrixPOP[index][i][j].flowNb = 0;
939                    }
940                }
941#endif
942#ifdef ASACC
943                for (i=0; i<shmForAgg->ASNb; i++)
944                {
945                    shmForAgg->ASTab[index][i].flowNbIN = 0;
946                    shmForAgg->ASTab[index][i].bytesNbIN = 0;
947                    shmForAgg->ASTab[index][i].pktsNbIN = 0;
948                    shmForAgg->ASTab[index][i].flowNbOUT = 0;
949                    shmForAgg->ASTab[index][i].bytesNbOUT = 0;
950                    shmForAgg->ASTab[index][i].pktsNbOUT = 0;
951                }
952#endif
953#ifdef IPV6LINKAGG
954                for (i=0; i<ROUTER_INDEX_MAX; i++)
955                {
956                    for (j=0; j<MAX_INDEX_BY_ROUTER; j++)
957                    {
958                        shmForAgg->indexV6Tab[index][i][j].bytesNbIN = 0;
959                        shmForAgg->indexV6Tab[index][i][j].pktsNbIN = 0;
960                        shmForAgg->indexV6Tab[index][i][j].flowNbIN = 0;
961                        shmForAgg->indexV6Tab[index][i][j].bytesNbOUT = 0;
962                        shmForAgg->indexV6Tab[index][i][j].pktsNbOUT = 0;
963                        shmForAgg->indexV6Tab[index][i][j].flowNbOUT = 0;
964                    }
965                }
966#endif
967                /* reinit */
968            } /* end is first update */
969        } /* end read ok */
970        sleep(10);
971    }
972    while (1);
973    exit(0);
974}
975
976
977/*
978 * rrdMibsUpdate
979 */
980short rrdMibsUpdate(int index)
981{
982    int i, j, k = 0;
983    int rrd_update_er_ct = 0;
984    int rrd_update_ok_ct = 0;
985    int rrd_already_created_ct = 0;
986    int rrd_create_er_ct = 0;
987    int rrd_create_ok_ct = 0;
988    static char buf[2048];
989    static char name[2048];
990    static char createstr[2048];
991    char *opt[27];
992    char *optUpdate[3];
993    FILE *fp;
994    static time_t now, now2;
995
996    now = time((time_t *)NULL);
997    for (k=0; k<strlen(name); k++)
998    {
999        name[k] = '\0';
1000    }
1001    for (k=0; k<strlen(buf); k++)
1002    {
1003        buf[k] = '\0';
1004    }
1005    strcat(name, MIB_RRD_LOCATION);
1006    strcat(name, GLOBAL_MIB_RRD_FILE);
1007    if ( (fp=fopen(name,"r")) == NULL )
1008    {
1009        opt[0]= (char *) malloc((strlen(RRD_PARAM_MIB_0) + 1) * sizeof(char));
1010        strcpy(opt[0], RRD_PARAM_MIB_0);
1011        opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
1012        strcpy(opt[1], name);
1013        opt[2]= (char *) malloc((strlen(RRD_PARAM_MIB_1) + 1) * sizeof(char));
1014        strcpy(opt[2], RRD_PARAM_MIB_1);
1015        opt[3]= (char *) malloc((strlen(RRD_PARAM_MIB_2) + 1) * sizeof(char));
1016        strcpy(opt[3], RRD_PARAM_MIB_2);
1017        opt[4]= (char *) malloc((strlen(RRD_PARAM_MIB_3) + 1) * sizeof(char));
1018        strcpy(opt[4], RRD_PARAM_MIB_3);
1019        opt[5]= (char *) malloc((strlen(RRD_PARAM_MIB_4) + 1) * sizeof(char));
1020        strcpy(opt[5], RRD_PARAM_MIB_4);
1021        opt[6]= (char *) malloc((strlen(RRD_PARAM_MIB_5) + 1) * sizeof(char));
1022        strcpy(opt[6], RRD_PARAM_MIB_5);
1023        opt[7]= (char *) malloc((strlen(RRD_PARAM_MIB_6) + 1) * sizeof(char));
1024        strcpy(opt[7], RRD_PARAM_MIB_6);
1025        opt[8]= (char *) malloc((strlen(RRD_PARAM_MIB_7) + 1) * sizeof(char));
1026        strcpy(opt[8], RRD_PARAM_MIB_7);
1027        opt[9]= (char *) malloc((strlen(RRD_PARAM_MIB_8) + 1) * sizeof(char));
1028        strcpy(opt[9], RRD_PARAM_MIB_8);
1029        opt[10]= (char *) malloc((strlen(RRD_PARAM_MIB_9) + 1) * sizeof(char));
1030        strcpy(opt[10], RRD_PARAM_MIB_9);
1031        opt[11]= (char *) malloc((strlen(RRD_PARAM_MIB_10) + 1) * sizeof(char));
1032        strcpy(opt[11], RRD_PARAM_MIB_10);
1033        opt[12]= (char *) malloc((strlen(RRD_PARAM_MIB_11) + 1) * sizeof(char));
1034        strcpy(opt[12], RRD_PARAM_MIB_11);
1035        opt[13]= (char *) malloc((strlen(RRD_PARAM_MIB_12) + 1) * sizeof(char));
1036        strcpy(opt[13], RRD_PARAM_MIB_12);
1037        opt[14]= (char *) malloc((strlen(RRD_PARAM_MIB_13) + 1) * sizeof(char));
1038        strcpy(opt[14], RRD_PARAM_MIB_13);
1039        opt[15]= (char *) malloc((strlen(RRD_PARAM_MIB_14) + 1) * sizeof(char));
1040        strcpy(opt[15], RRD_PARAM_MIB_14);
1041        opt[16]= (char *) malloc((strlen(RRD_PARAM_MIB_15) + 1) * sizeof(char));
1042        strcpy(opt[16], RRD_PARAM_MIB_15);
1043        opt[17]= (char *) malloc((strlen(RRD_PARAM_MIB_16) + 1) * sizeof(char));
1044        strcpy(opt[17], RRD_PARAM_MIB_16);
1045        opt[18]= (char *) malloc((strlen(RRD_PARAM_MIB_17) + 1) * sizeof(char));
1046        strcpy(opt[18], RRD_PARAM_MIB_17);
1047        opt[19]= (char *) malloc((strlen(RRD_PARAM_MIB_18) + 1) * sizeof(char));
1048        strcpy(opt[19], RRD_PARAM_MIB_18);
1049        opt[20]= (char *) malloc((strlen(RRD_PARAM_MIB_19) + 1) * sizeof(char));
1050        strcpy(opt[20], RRD_PARAM_MIB_19);
1051        opt[21]= (char *) malloc((strlen(RRD_PARAM_MIB_20) + 1) * sizeof(char));
1052        strcpy(opt[21], RRD_PARAM_MIB_20);
1053        opt[22]= (char *) malloc((strlen(RRD_PARAM_MIB_21) + 1) * sizeof(char));
1054        strcpy(opt[22], RRD_PARAM_MIB_21);
1055        opt[23]= (char *) malloc((strlen(RRD_PARAM_MIB_22) + 1) * sizeof(char));
1056        strcpy(opt[23], RRD_PARAM_MIB_22);
1057        optind = opterr = 0;
1058        rrd_clear_error();
1059        if ( rrd_create(24,opt) < 0)
1060        {
1061            syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
1062            rrd_create_er_ct++;
1063        }
1064        else
1065        {
1066            rrd_create_ok_ct++;
1067        }
1068        for ( k=0; k<24; k++)
1069        {
1070            free(opt[k]);
1071            opt[k] = NULL;
1072        }
1073    }
1074    else
1075    {
1076        fclose(fp);
1077        rrd_already_created_ct++;
1078        snprintf(buf,
1079                 2048,
1080                 "%lu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
1081                 now-300,  /* or ctime(&now) with %s */
1082                 shmForAgg->myMIB[index].ipPacketNb,
1083                 shmForAgg->myMIB[index].udpPacketNb,
1084                 shmForAgg->myMIB[index].v9PacketNb,
1085                 shmForAgg->myMIB[index].v9UnSeqNb,
1086                 shmForAgg->myMIB[index].flowSetNb,
1087                 shmForAgg->myMIB[index].dataFlowSetNb,
1088                 shmForAgg->myMIB[index].defFlowSetNb,
1089                 shmForAgg->myMIB[index].optDataFlowSetNb,
1090                 shmForAgg->myMIB[index].optDefFlowSetNb,
1091                 shmForAgg->myMIB[index].flowNb,
1092                 shmForAgg->myMIB[index].realFlowNb
1093                );
1094        fprintf(stderr,"\n%s\n",buf);
1095        optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
1096        strcpy(optUpdate[0], RRD_UPDATE_0);
1097        optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
1098        strcpy(optUpdate[1], name);
1099        optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
1100        strcpy(optUpdate[2], buf);
1101        optind = opterr = 0;
1102        rrd_clear_error();
1103        if ( rrd_update(3, optUpdate) < 0 )
1104        {
1105            syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
1106            rrd_update_er_ct++;
1107        }
1108        else
1109        {
1110            rrd_update_ok_ct++;
1111        }
1112        for ( k=0; k<3; k++)
1113        {
1114            free(optUpdate[k]);
1115            optUpdate[k] = NULL;
1116        }
1117    }
1118    shmForAgg->myMIB[index].ipPacketNb = 0;
1119    shmForAgg->myMIB[index].udpPacketNb = 0;
1120    shmForAgg->myMIB[index].v9PacketNb = 0;
1121    shmForAgg->myMIB[index].v9UnSeqNb = 0;
1122    shmForAgg->myMIB[index].flowSetNb = 0;
1123    shmForAgg->myMIB[index].dataFlowSetNb = 0;
1124    shmForAgg->myMIB[index].defFlowSetNb = 0;
1125    shmForAgg->myMIB[index].optDataFlowSetNb = 0;
1126    shmForAgg->myMIB[index].optDefFlowSetNb = 0;
1127    shmForAgg->myMIB[index].flowNb = 0;
1128    shmForAgg->myMIB[index].realFlowNb = 0;
1129    return 1;
1130}
1131
1132/*
1133 * rrdL7IPv4MibsUpdate
1134 */
1135short rrdL7IPv4MibsUpdate(int index)
1136{
1137    int i, j, k = 0;
1138    int rrd_update_er_ct = 0;
1139    int rrd_update_ok_ct = 0;
1140    int rrd_already_created_ct = 0;
1141    int rrd_create_er_ct = 0;
1142    int rrd_create_ok_ct = 0;
1143    static char buf[2048];
1144    static char name[2048];
1145    static char createstr[2048];
1146    char *opt[27];
1147    char *optUpdate[3];
1148    FILE *fp;
1149    static time_t now, now2;
1150
1151    now = time((time_t *)NULL);
1152    for (k=0; k<strlen(name); k++)
1153    {
1154        name[k] = '\0';
1155    }
1156    for (k=0; k<strlen(buf); k++)
1157    {
1158        buf[k] = '\0';
1159    }
1160    strcat(name, MIB_RRD_LOCATION);
1161    strcat(name, L7_IPV4_MIB_RRD_FILE);
1162    if ( (fp=fopen(name,"r")) == NULL )
1163    {
1164        opt[0]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_0) + 1) * sizeof(char));
1165        strcpy(opt[0], RRD_PARAM_MIB_IPV4_AP_0);
1166        opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
1167        strcpy(opt[1], name);
1168        opt[2]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_1) + 1) * sizeof(char));
1169        strcpy(opt[2], RRD_PARAM_MIB_IPV4_AP_1);
1170        opt[3]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_2) + 1) * sizeof(char));
1171        strcpy(opt[3], RRD_PARAM_MIB_IPV4_AP_2);
1172        opt[4]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_3) + 1) * sizeof(char));
1173        strcpy(opt[4], RRD_PARAM_MIB_IPV4_AP_3);
1174        opt[5]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_4) + 1) * sizeof(char));
1175        strcpy(opt[5], RRD_PARAM_MIB_IPV4_AP_4);
1176        opt[6]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_5) + 1) * sizeof(char));
1177        strcpy(opt[6], RRD_PARAM_MIB_IPV4_AP_5);
1178        opt[7]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_6) + 1) * sizeof(char));
1179        strcpy(opt[7], RRD_PARAM_MIB_IPV4_AP_6);
1180        opt[8]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_7) + 1) * sizeof(char));
1181        strcpy(opt[8], RRD_PARAM_MIB_IPV4_AP_7);
1182        opt[9]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_8) + 1) * sizeof(char));
1183        strcpy(opt[9], RRD_PARAM_MIB_IPV4_AP_8);
1184        opt[10]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_9) + 1) * sizeof(char));
1185        strcpy(opt[10], RRD_PARAM_MIB_IPV4_AP_9);
1186        opt[11]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_10) + 1) * sizeof(char));
1187        strcpy(opt[11], RRD_PARAM_MIB_IPV4_AP_10);
1188        opt[12]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_11) + 1) * sizeof(char));
1189        strcpy(opt[12], RRD_PARAM_MIB_IPV4_AP_11);
1190        opt[13]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_12) + 1) * sizeof(char));
1191        strcpy(opt[13], RRD_PARAM_MIB_IPV4_AP_12);
1192        opt[14]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_13) + 1) * sizeof(char));
1193        strcpy(opt[14], RRD_PARAM_MIB_IPV4_AP_13);
1194        opt[15]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_14) + 1) * sizeof(char));
1195        strcpy(opt[15], RRD_PARAM_MIB_IPV4_AP_14);
1196        opt[16]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_15) + 1) * sizeof(char));
1197        strcpy(opt[16], RRD_PARAM_MIB_IPV4_AP_15);
1198        opt[17]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_16) + 1) * sizeof(char));
1199        strcpy(opt[17], RRD_PARAM_MIB_IPV4_AP_16);
1200        opt[18]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_17) + 1) * sizeof(char));
1201        strcpy(opt[18], RRD_PARAM_MIB_IPV4_AP_17);
1202        opt[19]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_18) + 1) * sizeof(char));
1203        strcpy(opt[19], RRD_PARAM_MIB_IPV4_AP_18);
1204        opt[20]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_19) + 1) * sizeof(char));
1205        strcpy(opt[20], RRD_PARAM_MIB_IPV4_AP_19);
1206        opt[21]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_20) + 1) * sizeof(char));
1207        strcpy(opt[21], RRD_PARAM_MIB_IPV4_AP_20);
1208        opt[22]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_21) + 1) * sizeof(char));
1209        strcpy(opt[22], RRD_PARAM_MIB_IPV4_AP_21);
1210        opt[23]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_22) + 1) * sizeof(char));
1211        strcpy(opt[23], RRD_PARAM_MIB_IPV4_AP_22);
1212        opt[24]= (char *) malloc((strlen(RRD_PARAM_MIB_IPV4_AP_23) + 1) * sizeof(char));
1213        strcpy(opt[24], RRD_PARAM_MIB_IPV4_AP_23);
1214        optind = opterr = 0;
1215        rrd_clear_error();
1216        if ( rrd_create(25,opt) < 0)
1217        {
1218            syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
1219            rrd_create_er_ct++;
1220        }
1221        else
1222        {
1223            rrd_create_ok_ct++;
1224        }
1225        for ( k=0; k<25; k++)
1226        {
1227            free(opt[k]);
1228            opt[k] = NULL;
1229        }
1230    }
1231    else
1232    {
1233        fclose(fp);
1234        rrd_already_created_ct++;
1235        snprintf(buf,
1236                 2048,
1237                 "%lu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
1238                 now-300,  /* or ctime(&now) with %s */
1239                 shmForAgg->myMIB[index].ipv4WebBytesNb,
1240                 shmForAgg->myMIB[index].ipv4WebPcktsNb,
1241                 shmForAgg->myMIB[index].ipv4WebFlowNb,
1242                 shmForAgg->myMIB[index].ipv4MailBytesNb,
1243                 shmForAgg->myMIB[index].ipv4MailPcktsNb,
1244                 shmForAgg->myMIB[index].ipv4MailFlowNb,
1245                 shmForAgg->myMIB[index].ipv4DNSBytesNb,
1246                 shmForAgg->myMIB[index].ipv4DNSPcktsNb,
1247                 shmForAgg->myMIB[index].ipv4DNSFlowNb,
1248                 shmForAgg->myMIB[index].ipv4OthersApBytesNb,
1249                 shmForAgg->myMIB[index].ipv4OthersApPcktsNb,
1250                 shmForAgg->myMIB[index].ipv4OthersApFlowNb
1251                );
1252        fprintf(stderr,"\n%s\n",buf);
1253        optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
1254        strcpy(optUpdate[0], RRD_UPDATE_0);
1255        optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
1256        strcpy(optUpdate[1], name);
1257        optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
1258        strcpy(optUpdate[2], buf);
1259        optind = opterr = 0;
1260        rrd_clear_error();
1261        if ( rrd_update(3, optUpdate) < 0 )
1262        {
1263            syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
1264            rrd_update_er_ct++;
1265        }
1266        else
1267        {
1268            rrd_update_ok_ct++;
1269        }
1270        for ( k=0; k<3; k++)
1271        {
1272            free(optUpdate[k]);
1273            optUpdate[k] = NULL;
1274        }
1275    }
1276    shmForAgg->myMIB[index].ipv4WebBytesNb = 0;
1277    shmForAgg->myMIB[index].ipv4WebPcktsNb = 0;
1278    shmForAgg->myMIB[index].ipv4WebFlowNb = 0;
1279    shmForAgg->myMIB[index].ipv4MailBytesNb = 0;
1280    shmForAgg->myMIB[index].ipv4MailPcktsNb = 0;
1281    shmForAgg->myMIB[index].ipv4MailFlowNb = 0;
1282    shmForAgg->myMIB[index].ipv4DNSBytesNb = 0;
1283    shmForAgg->myMIB[index].ipv4DNSPcktsNb = 0;
1284    shmForAgg->myMIB[index].ipv4DNSFlowNb = 0;
1285    shmForAgg->myMIB[index].ipv4OthersApBytesNb = 0;
1286    shmForAgg->myMIB[index].ipv4OthersApPcktsNb = 0;
1287    shmForAgg->myMIB[index].ipv4OthersApFlowNb = 0;
1288    return 1;
1289}
Note: See TracBrowser for help on using the browser.