[24] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | |
---|
[27] | 26 | |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | |
---|
[24] | 35 | #include "renetcolAgg.h" |
---|
| 36 | |
---|
| 37 | struct SHMForAgg *shmForAgg; |
---|
| 38 | |
---|
| 39 | int |
---|
| 40 | main(int argc, char *argv[]) |
---|
| 41 | { |
---|
| 42 | int shmid; |
---|
[27] | 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; |
---|
[24] | 50 | key_t key; |
---|
| 51 | static char buf[2048]; |
---|
| 52 | static char name[2048]; |
---|
| 53 | static char ipStr[20]; |
---|
| 54 | static char createstr[2048]; |
---|
[27] | 55 | char *opt[27]; |
---|
| 56 | char *optUpdate[3]; |
---|
[24] | 57 | int res_rrd = 0; |
---|
| 58 | FILE *fp; |
---|
[27] | 59 | static time_t now, now2; |
---|
| 60 | |
---|
[24] | 61 | key = 8765; |
---|
| 62 | if ((shmid = shmget(key, SHMSIZE, 0666)) < 0) { |
---|
| 63 | perror("shmget"); |
---|
| 64 | exit(1); |
---|
| 65 | } |
---|
| 66 | if ((shmForAgg = (struct SHMForAgg *)shmat(shmid, (void *)0, 0)) == (void *) -1) { |
---|
| 67 | perror("shmat"); |
---|
| 68 | exit(1); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | fprintf(stderr, "renetcolAgg: I become a deamon, next messages via syslogd. By.\n"); |
---|
| 72 | if (fork () != 0) |
---|
| 73 | exit (0); |
---|
| 74 | if (setsid() == -1){ |
---|
| 75 | exit(4); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | do { |
---|
| 79 | if (shmForAgg->readed == 1) { |
---|
| 80 | shmForAgg->readed = 0; |
---|
| 81 | if (shmForAgg->currentTable == 0) { |
---|
| 82 | index = 1; |
---|
| 83 | }else{ |
---|
| 84 | index = 0; |
---|
| 85 | } |
---|
| 86 | now = time((time_t *)NULL); |
---|
| 87 | for (i=0; i<shmForAgg->v4PrefixNb; i++){ |
---|
| 88 | |
---|
[25] | 89 | name[0] = '\0'; |
---|
[24] | 90 | createstr[0] = '\0'; |
---|
| 91 | ipStr[0] = '\0'; |
---|
| 92 | buf[0] = '\0'; |
---|
[25] | 93 | strcat(name, RRD_LOCATION); |
---|
[27] | 94 | sprintf(ipStr, "%lu_%lu_%lu_%lu_%hu", |
---|
| 95 | shmForAgg->prefixV4Tab[index][i].beginning>>24, |
---|
| 96 | shmForAgg->prefixV4Tab[index][i].beginning<<8>>24, |
---|
| 97 | shmForAgg->prefixV4Tab[index][i].beginning<<16>>24, |
---|
| 98 | shmForAgg->prefixV4Tab[index][i].beginning<<24>>24, |
---|
| 99 | shmForAgg->prefixV4Tab[index][i].mask |
---|
| 100 | ); |
---|
[24] | 101 | strcat(name, ipStr); |
---|
[25] | 102 | |
---|
[24] | 103 | strcat(name, RRD_EXTENSION); |
---|
| 104 | if ( (fp=fopen(name,"r")) == NULL ){ |
---|
[27] | 105 | opt[0]= (char *) malloc((strlen(RRD_PARAM_0) + 1) * sizeof(char)); |
---|
| 106 | strcpy(opt[0], RRD_PARAM_0); |
---|
| 107 | opt[1]= (char *) malloc((strlen(name) + 1) * sizeof(char)); |
---|
| 108 | strcpy(opt[1], name); |
---|
| 109 | opt[2]= (char *) malloc((strlen(RRD_PARAM_1) + 1) * sizeof(char)); |
---|
| 110 | strcpy(opt[2], RRD_PARAM_1); |
---|
| 111 | opt[3]= (char *) malloc((strlen(RRD_PARAM_2) + 1) * sizeof(char)); |
---|
| 112 | strcpy(opt[3], RRD_PARAM_2); |
---|
| 113 | opt[4]= (char *) malloc((strlen(RRD_PARAM_3) + 1) * sizeof(char)); |
---|
| 114 | strcpy(opt[4], RRD_PARAM_3); |
---|
| 115 | opt[5]= (char *) malloc((strlen(RRD_PARAM_4) + 1) * sizeof(char)); |
---|
| 116 | strcpy(opt[5], RRD_PARAM_4); |
---|
| 117 | opt[6]= (char *) malloc((strlen(RRD_PARAM_5) + 1) * sizeof(char)); |
---|
| 118 | strcpy(opt[6], RRD_PARAM_5); |
---|
| 119 | opt[7]= (char *) malloc((strlen(RRD_PARAM_6) + 1) * sizeof(char)); |
---|
| 120 | strcpy(opt[7], RRD_PARAM_6); |
---|
| 121 | opt[8]= (char *) malloc((strlen(RRD_PARAM_7) + 1) * sizeof(char)); |
---|
| 122 | strcpy(opt[8], RRD_PARAM_7); |
---|
| 123 | opt[9]= (char *) malloc((strlen(RRD_PARAM_8) + 1) * sizeof(char)); |
---|
| 124 | strcpy(opt[9], RRD_PARAM_8); |
---|
| 125 | opt[10]= (char *) malloc((strlen(RRD_PARAM_9) + 1) * sizeof(char)); |
---|
| 126 | strcpy(opt[10], RRD_PARAM_9); |
---|
| 127 | opt[12]= (char *) malloc((strlen(RRD_PARAM_10) + 1) * sizeof(char)); |
---|
| 128 | strcpy(opt[11], RRD_PARAM_10); |
---|
| 129 | opt[12]= (char *) malloc((strlen(RRD_PARAM_11) + 1) * sizeof(char)); |
---|
| 130 | strcpy(opt[12], RRD_PARAM_11); |
---|
| 131 | opt[13]= (char *) malloc((strlen(RRD_PARAM_12) + 1) * sizeof(char)); |
---|
| 132 | strcpy(opt[13], RRD_PARAM_12); |
---|
| 133 | opt[14]= (char *) malloc((strlen(RRD_PARAM_13) + 1) * sizeof(char)); |
---|
| 134 | strcpy(opt[14], RRD_PARAM_13); |
---|
| 135 | opt[15]= (char *) malloc((strlen(RRD_PARAM_14) + 1) * sizeof(char)); |
---|
| 136 | strcpy(opt[15], RRD_PARAM_14); |
---|
| 137 | opt[16]= (char *) malloc((strlen(RRD_PARAM_15) + 1) * sizeof(char)); |
---|
| 138 | strcpy(opt[16], RRD_PARAM_15); |
---|
| 139 | opt[17]= (char *) malloc((strlen(RRD_PARAM_16) + 1) * sizeof(char)); |
---|
| 140 | strcpy(opt[17], RRD_PARAM_16); |
---|
| 141 | opt[18]= (char *) malloc((strlen(RRD_PARAM_17) + 1) * sizeof(char)); |
---|
| 142 | strcpy(opt[18], RRD_PARAM_17); |
---|
| 143 | opt[19]= (char *) malloc((strlen(RRD_PARAM_18) + 1) * sizeof(char)); |
---|
| 144 | strcpy(opt[19], RRD_PARAM_18); |
---|
| 145 | opt[20]= (char *) malloc((strlen(RRD_PARAM_19) + 1) * sizeof(char)); |
---|
| 146 | strcpy(opt[20], RRD_PARAM_19); |
---|
| 147 | opt[21]= (char *) malloc((strlen(RRD_PARAM_20) + 1) * sizeof(char)); |
---|
| 148 | strcpy(opt[21], RRD_PARAM_20); |
---|
| 149 | opt[22]= (char *) malloc((strlen(RRD_PARAM_21) + 1) * sizeof(char)); |
---|
| 150 | strcpy(opt[22], RRD_PARAM_21); |
---|
| 151 | opt[23]= (char *) malloc((strlen(RRD_PARAM_22) + 1) * sizeof(char)); |
---|
| 152 | strcpy(opt[23], RRD_PARAM_22); |
---|
| 153 | opt[24]= (char *) malloc((strlen(RRD_PARAM_23) + 1) * sizeof(char)); |
---|
| 154 | strcpy(opt[24], RRD_PARAM_23); |
---|
| 155 | opt[25]= (char *) malloc((strlen(RRD_PARAM_24) + 1) * sizeof(char)); |
---|
| 156 | strcpy(opt[25], RRD_PARAM_24); |
---|
| 157 | opt[26]= (char *) malloc((strlen(RRD_PARAM_25) + 1) * sizeof(char)); |
---|
| 158 | strcpy(opt[26], RRD_PARAM_25); |
---|
| 159 | optind = opterr = 0; |
---|
| 160 | rrd_clear_error(); |
---|
| 161 | if ( rrd_create(27,opt) < 0) { |
---|
| 162 | syslog(LOG_ERR, "RRD create file %s, error: %s\n", name, rrd_get_error()); |
---|
| 163 | rrd_create_er_ct++; |
---|
| 164 | } else { |
---|
| 165 | rrd_create_ok_ct++; |
---|
| 166 | } |
---|
| 167 | for ( j=0; j<27; j++) { |
---|
| 168 | free(opt[j]); |
---|
| 169 | opt[j] = NULL; |
---|
| 170 | } |
---|
| 171 | }else{ |
---|
[24] | 172 | fclose(fp); |
---|
[27] | 173 | rrd_already_created_ct++; |
---|
| 174 | snprintf(buf, |
---|
| 175 | 2048, |
---|
| 176 | "%lu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu", |
---|
| 177 | now-300, |
---|
| 178 | shmForAgg->prefixV4Tab[index][i].flowNbIN, |
---|
| 179 | shmForAgg->prefixV4Tab[index][i].flowNbOUT, |
---|
| 180 | shmForAgg->prefixV4Tab[index][i].bytesNbIN, |
---|
| 181 | shmForAgg->prefixV4Tab[index][i].bytesNbOUT, |
---|
| 182 | shmForAgg->prefixV4Tab[index][i].pktsNbIN, |
---|
| 183 | shmForAgg->prefixV4Tab[index][i].pktsNbOUT, |
---|
| 184 | shmForAgg->prefixV4Tab[index][i].firstCoSIN, |
---|
| 185 | shmForAgg->prefixV4Tab[index][i].firstCoSOUT, |
---|
| 186 | shmForAgg->prefixV4Tab[index][i].secondCoSIN, |
---|
| 187 | shmForAgg->prefixV4Tab[index][i].secondCoSOUT, |
---|
| 188 | shmForAgg->prefixV4Tab[index][i].thirdCoSIN, |
---|
| 189 | shmForAgg->prefixV4Tab[index][i].thirdCoSOUT, |
---|
| 190 | shmForAgg->prefixV4Tab[index][i].fourthCoSIN, |
---|
| 191 | shmForAgg->prefixV4Tab[index][i].fourthCoSOUT |
---|
| 192 | ); |
---|
| 193 | optUpdate[0]= (char *) malloc((strlen(RRD_UPDATE_0) + 1) * sizeof(char)); |
---|
| 194 | strcpy(optUpdate[0], RRD_UPDATE_0); |
---|
| 195 | optUpdate[1]= (char *) malloc((strlen(name) + 1) * sizeof(char)); |
---|
| 196 | strcpy(optUpdate[1], name); |
---|
| 197 | optUpdate[2]= (char *) malloc((strlen(buf) + 1) * sizeof(char)); |
---|
| 198 | strcpy(optUpdate[2], buf); |
---|
| 199 | optind = opterr = 0; |
---|
| 200 | rrd_clear_error(); |
---|
| 201 | if ( rrd_update(3, optUpdate) < 0 ) { |
---|
| 202 | syslog(LOG_ERR, "RRD update file %s, error: %s\n", name, rrd_get_error()); |
---|
| 203 | rrd_update_er_ct++; |
---|
| 204 | } else { |
---|
| 205 | rrd_update_ok_ct++; |
---|
| 206 | } |
---|
| 207 | for ( j=0; j<3; j++) { |
---|
| 208 | free(optUpdate[j]); |
---|
| 209 | optUpdate[j] = NULL; |
---|
| 210 | } |
---|
[24] | 211 | } |
---|
[25] | 212 | |
---|
[24] | 213 | shmForAgg->prefixV4Tab[index][i].flowNbIN = 0; |
---|
| 214 | shmForAgg->prefixV4Tab[index][i].bytesNbIN = 0; |
---|
| 215 | shmForAgg->prefixV4Tab[index][i].pktsNbIN = 0; |
---|
| 216 | shmForAgg->prefixV4Tab[index][i].flowNbOUT = 0; |
---|
| 217 | shmForAgg->prefixV4Tab[index][i].bytesNbOUT = 0; |
---|
| 218 | shmForAgg->prefixV4Tab[index][i].pktsNbOUT = 0; |
---|
[27] | 219 | shmForAgg->prefixV4Tab[index][i].firstCoSIN = 0; |
---|
| 220 | shmForAgg->prefixV4Tab[index][i].secondCoSIN = 0; |
---|
| 221 | shmForAgg->prefixV4Tab[index][i].thirdCoSIN = 0; |
---|
| 222 | shmForAgg->prefixV4Tab[index][i].fourthCoSIN = 0; |
---|
| 223 | shmForAgg->prefixV4Tab[index][i].firstCoSOUT = 0; |
---|
| 224 | shmForAgg->prefixV4Tab[index][i].secondCoSOUT = 0; |
---|
| 225 | shmForAgg->prefixV4Tab[index][i].thirdCoSOUT = 0; |
---|
| 226 | shmForAgg->prefixV4Tab[index][i].fourthCoSOUT = 0; |
---|
[24] | 227 | } |
---|
[27] | 228 | now2 = time((time_t *)NULL); |
---|
| 229 | 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", |
---|
| 230 | now2-now, |
---|
| 231 | rrd_update_ok_ct, |
---|
| 232 | rrd_update_er_ct, |
---|
| 233 | rrd_update_ok_ct+rrd_update_er_ct, |
---|
| 234 | rrd_already_created_ct, |
---|
| 235 | rrd_create_er_ct, |
---|
| 236 | rrd_create_ok_ct); |
---|
| 237 | rrd_update_ok_ct = 0; |
---|
| 238 | rrd_update_er_ct = 0; |
---|
| 239 | rrd_already_created_ct = 0; |
---|
| 240 | rrd_create_er_ct = 0; |
---|
| 241 | rrd_create_ok_ct = 0; |
---|
| 242 | rrd_clear_error(); |
---|
[24] | 243 | } |
---|
| 244 | sleep(5); |
---|
| 245 | } while (1); |
---|
| 246 | |
---|
| 247 | exit(0); |
---|
| 248 | } |
---|