root/trunk/src/renetcolAgg.c @ 29

Revision 29, 9.6 KB (checked in by andreu, 15 years ago)

sampling information in rrd filename - optional template first step

  • 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 (CPU+MEM+HD) 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, index;
44  int rrd_update_er_ct = 0;
45  int rrd_update_ok_ct = 0;
46  int rrd_already_created_ct = 0;
47  int rrd_create_er_ct = 0;
48  int rrd_create_ok_ct = 0;
49  int rrd_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 createstr[2048];
56  char *opt[27];
57  char *optUpdate[3];
58  int res_rrd = 0;
59  FILE *fp;
60  static time_t now, now2;
61  struct shmid_ds shminfo;
62 
63  key = 8765;
64  if ((shmid = shmget(key, SHMSIZE, 0666)) < 0) {
65    perror("shmget");
66    exit(1);
67  }
68  if ((shmForAgg = (struct SHMForAgg *)shmat(shmid, (void *)0, 0)) == (void *) -1) {
69    perror("shmat");
70    exit(1);
71  }
72 
73  fprintf(stderr, "renetcolAgg: I become a deamon, next messages via syslogd. By.\n");
74  if (fork () != 0)
75    exit (0);
76  if (setsid() == -1){
77    exit(4);
78  }
79 
80  do {
81    if (shmForAgg->readed == 1) {
82      shmForAgg->readed = 0;
83      if (shmForAgg->currentTable == 0) {
84        index = 1;
85      }else{
86        index = 0;
87      }
88      now = time((time_t *)NULL);
89      if (isFirstUpdate == 0) {
90        for (i=0; i<shmForAgg->v4PrefixNb; i++){
91          /* HERE Put in RRD FILE */
92          for (j=0; j<strlen(name); j++) {name[j] = '\0';}
93          for (j=0; j<strlen(createstr); j++) {createstr[j] = '\0';}
94          for (j=0; j<strlen(ipStr); j++) {ipStr[j] = '\0';}
95          for (j=0; j<strlen(buf); j++) {buf[j] = '\0';}
96          strcat(name, RRD_LOCATION);
97          fprintf(stderr," SPL : %lu\n ",shmForAgg->prefixV4Tab[index][i].sampling);
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#ifdef DEBUGAGG
110            fprintf(stderr, "Creation %s ... ", name);
111#endif
112            opt[0]= (char *) malloc((strlen(RRD_PARAM_0) + 1) * sizeof(char));
113            strcpy(opt[0], RRD_PARAM_0);
114            opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
115            strcpy(opt[1], name);
116            opt[2]= (char *) malloc((strlen(RRD_PARAM_1) + 1) * sizeof(char));
117            strcpy(opt[2], RRD_PARAM_1);
118            opt[3]= (char *) malloc((strlen(RRD_PARAM_2) + 1) * sizeof(char));
119            strcpy(opt[3], RRD_PARAM_2);
120            opt[4]= (char *) malloc((strlen(RRD_PARAM_3) + 1) * sizeof(char));
121            strcpy(opt[4], RRD_PARAM_3);
122            opt[5]= (char *) malloc((strlen(RRD_PARAM_4) + 1) * sizeof(char));
123            strcpy(opt[5], RRD_PARAM_4);
124            opt[6]= (char *) malloc((strlen(RRD_PARAM_5) + 1) * sizeof(char));
125            strcpy(opt[6], RRD_PARAM_5);
126            opt[7]= (char *) malloc((strlen(RRD_PARAM_6) + 1) * sizeof(char));
127            strcpy(opt[7], RRD_PARAM_6);
128            opt[8]= (char *) malloc((strlen(RRD_PARAM_7) + 1) * sizeof(char));
129            strcpy(opt[8], RRD_PARAM_7);
130            opt[9]= (char *) malloc((strlen(RRD_PARAM_8) + 1) * sizeof(char));
131            strcpy(opt[9], RRD_PARAM_8);
132            opt[10]= (char *) malloc((strlen(RRD_PARAM_9) + 1) * sizeof(char));
133            strcpy(opt[10], RRD_PARAM_9);
134            opt[11]= (char *) malloc((strlen(RRD_PARAM_10) + 1) * sizeof(char));
135            strcpy(opt[11], RRD_PARAM_10);
136            opt[12]= (char *) malloc((strlen(RRD_PARAM_11) + 1) * sizeof(char));
137            strcpy(opt[12], RRD_PARAM_11);
138            opt[13]= (char *) malloc((strlen(RRD_PARAM_12) + 1) * sizeof(char));
139            strcpy(opt[13], RRD_PARAM_12);
140            opt[14]= (char *) malloc((strlen(RRD_PARAM_13) + 1) * sizeof(char));
141            strcpy(opt[14], RRD_PARAM_13);
142            opt[15]= (char *) malloc((strlen(RRD_PARAM_14) + 1) * sizeof(char));
143            strcpy(opt[15], RRD_PARAM_14);
144            opt[16]= (char *) malloc((strlen(RRD_PARAM_15) + 1) * sizeof(char));
145            strcpy(opt[16], RRD_PARAM_15);
146            opt[17]= (char *) malloc((strlen(RRD_PARAM_16) + 1) * sizeof(char));
147            strcpy(opt[17], RRD_PARAM_16);
148            opt[18]= (char *) malloc((strlen(RRD_PARAM_17) + 1) * sizeof(char));
149            strcpy(opt[18], RRD_PARAM_17);
150            opt[19]= (char *) malloc((strlen(RRD_PARAM_18) + 1) * sizeof(char));
151            strcpy(opt[19], RRD_PARAM_18);
152            opt[20]= (char *) malloc((strlen(RRD_PARAM_19) + 1) * sizeof(char));
153            strcpy(opt[20], RRD_PARAM_19);
154            opt[21]= (char *) malloc((strlen(RRD_PARAM_20) + 1) * sizeof(char));
155            strcpy(opt[21], RRD_PARAM_20);
156            opt[22]= (char *) malloc((strlen(RRD_PARAM_21) + 1) * sizeof(char));
157            strcpy(opt[22], RRD_PARAM_21);
158            opt[23]= (char *) malloc((strlen(RRD_PARAM_22) + 1) * sizeof(char));
159            strcpy(opt[23], RRD_PARAM_22);
160            opt[24]= (char *) malloc((strlen(RRD_PARAM_23) + 1) * sizeof(char));
161            strcpy(opt[24], RRD_PARAM_23);
162            opt[25]= (char *) malloc((strlen(RRD_PARAM_24) + 1) * sizeof(char));
163            strcpy(opt[25], RRD_PARAM_24);
164            opt[26]= (char *) malloc((strlen(RRD_PARAM_25) + 1) * sizeof(char));
165            strcpy(opt[26], RRD_PARAM_25);
166            optind = opterr = 0;
167            rrd_clear_error();
168            if ( rrd_create(27,opt) < 0) {
169              syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error());
170              rrd_create_er_ct++;
171            } else {
172              rrd_create_ok_ct++;
173            }
174            for ( j=0; j<27; j++) {
175              free(opt[j]);
176              opt[j] = NULL;
177            }
178#ifdef DEBUGAGG
179            fprintf(stderr, "end creation \n", name);
180#endif
181          }else{
182            fclose(fp);
183#ifdef DEBUGAGG
184            fprintf(stderr, "Update %s \n", name);
185#endif
186            rrd_already_created_ct++;
187            snprintf(buf,
188                     2048,
189                     "%lu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu"
190                     now-300,  /* or ctime(&now) with %s */
191                     shmForAgg->prefixV4Tab[index][i].flowNbIN,  /* 1 */
192                     shmForAgg->prefixV4Tab[index][i].flowNbOUT,
193                     shmForAgg->prefixV4Tab[index][i].bytesNbIN,
194                     shmForAgg->prefixV4Tab[index][i].bytesNbOUT,
195                     shmForAgg->prefixV4Tab[index][i].pktsNbIN,  /* 5 */
196                     shmForAgg->prefixV4Tab[index][i].pktsNbOUT,
197                     shmForAgg->prefixV4Tab[index][i].firstCoSIN,
198                     shmForAgg->prefixV4Tab[index][i].firstCoSOUT,
199                     shmForAgg->prefixV4Tab[index][i].secondCoSIN,
200                     shmForAgg->prefixV4Tab[index][i].secondCoSOUT,
201                     shmForAgg->prefixV4Tab[index][i].thirdCoSIN,
202                     shmForAgg->prefixV4Tab[index][i].thirdCoSOUT,
203                     shmForAgg->prefixV4Tab[index][i].fourthCoSIN,
204                     shmForAgg->prefixV4Tab[index][i].fourthCoSOUT
205                     );
206            optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char));
207            strcpy(optUpdate[0], RRD_UPDATE_0);
208            optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char));
209            strcpy(optUpdate[1], name);
210            optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char));
211            strcpy(optUpdate[2], buf);
212            optind = opterr = 0;
213            rrd_clear_error();
214            if ( rrd_update(3, optUpdate) < 0 ) {
215              syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error());
216              rrd_update_er_ct++;
217            } else {
218              rrd_update_ok_ct++;
219            }
220            for ( j=0; j<3; j++) {
221              free(optUpdate[j]);
222              optUpdate[j] = NULL;
223            }
224          }
225          /* Reinit the shared table */
226          shmForAgg->prefixV4Tab[index][i].flowNbIN = 0;
227          shmForAgg->prefixV4Tab[index][i].bytesNbIN = 0;
228          shmForAgg->prefixV4Tab[index][i].pktsNbIN = 0;
229          shmForAgg->prefixV4Tab[index][i].flowNbOUT = 0;
230          shmForAgg->prefixV4Tab[index][i].bytesNbOUT = 0;
231          shmForAgg->prefixV4Tab[index][i].pktsNbOUT = 0;
232          shmForAgg->prefixV4Tab[index][i].firstCoSIN = 0;
233          shmForAgg->prefixV4Tab[index][i].secondCoSIN = 0;
234          shmForAgg->prefixV4Tab[index][i].thirdCoSIN = 0;
235          shmForAgg->prefixV4Tab[index][i].fourthCoSIN = 0;
236          shmForAgg->prefixV4Tab[index][i].firstCoSOUT = 0;
237          shmForAgg->prefixV4Tab[index][i].secondCoSOUT = 0;
238          shmForAgg->prefixV4Tab[index][i].thirdCoSOUT = 0;
239          shmForAgg->prefixV4Tab[index][i].fourthCoSOUT = 0;
240        }     
241        now2 = time((time_t *)NULL);
242/* #ifdef DEBUGAGG */
243        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",
244                now2-now,
245                rrd_update_ok_ct,
246                rrd_update_er_ct,
247                rrd_update_ok_ct+rrd_update_er_ct,
248                rrd_already_created_ct,
249                rrd_create_er_ct,
250                rrd_create_ok_ct);
251/* #endif */
252        rrd_update_ok_ct = 0;
253        rrd_update_er_ct = 0;
254        rrd_already_created_ct = 0;
255        rrd_create_er_ct = 0;
256        rrd_create_ok_ct = 0;
257        rrd_clear_error();
258      } else {
259        isFirstUpdate = 0;
260      } 
261    }
262    sleep(10);
263  } while (1);
264  exit(0);
265}
Note: See TracBrowser for help on using the browser.