VES EVEL Library VES 5.4.1 enhancements
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / VESreporting_fault / ves_fault_reporter.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and 
14  * limitations under the License.
15  *
16  ****************************************************************************/
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <pthread.h>
22 #include <string.h>
23 #include <netdb.h>
24 #include <sys/time.h>
25 #include <sys/stat.h>
26 #include "jsmn.h"
27 #include "evel.h"
28
29 #define BUFSIZE 128
30 #define MAX_BUFFER_SIZE 4096
31 #define MAX_TOKENS 1000
32 #define MAX_INTERFACES 40
33
34 void *FaultThread(void *threadarg);
35 void *FaultThread01(void *threadarg);
36 void *FaultThread02(void *threadarg);
37 void *FaultThread03(void *threadarg);
38
39 typedef struct dummy_vpp_metrics_struct {
40   int curr_bytes_in;
41   int curr_bytes_out;
42   int curr_packets_in;
43   int curr_packets_out;
44   int last_bytes_in;
45   int last_bytes_out;
46   int last_packets_in;
47   int last_packets_out;
48 } vpp_metrics_struct;
49
50 typedef struct linkstat {
51
52   char linkname[32];
53   char linkdescr[64];
54   char linkmode[64];
55   int  speedmbps;
56   int  fault_raised;
57   unsigned long long last_epoch;
58
59 }LINKSTAT;
60
61 //vpp_metrics_struct meas_intfstat[MAX_INTERFACES];
62 //LINKSTAT meas_linkstat[MAX_INTERFACES];
63 //int request_rate=0;
64 vpp_metrics_struct fault_intfstat[MAX_INTERFACES];
65 LINKSTAT fault_linkstat[MAX_INTERFACES];
66
67 unsigned long long epoch_start = 0;
68
69 int format_val_params(KEYVALRESULT * keyValArray, int numElements, const char *replace, const char *search)
70 {
71      char *sp;
72      int i =0;
73      int search_len;
74      int replace_len;
75      int tail_len;
76
77      for (i=0; i<numElements; i++)
78      {
79         if ((sp = strstr(keyValArray[i].valStr, search)) == NULL) {
80            printf("\n String not found\n");
81            return 1; //Error search string not found
82         }
83         
84         search_len = strlen(search);
85         replace_len = strlen(replace);
86         tail_len = strlen(sp+search_len);
87         memmove(sp+replace_len,sp+search_len,tail_len+1);
88         memcpy(sp, replace, replace_len);
89 //       printf("\n Changed value for i=%d is %s", i, keyValArray[i].valStr);
90      }
91      return 0; //search and replace is successful
92 }
93
94 void runCommands(KEYVALRESULT * commandArray, int numCommands)
95 {
96
97   char buf[BUFSIZE];            /* buffer used to store VPP metrics     */
98   FILE *fp;                     /* file descriptor to pipe cmd to shell */
99   int i;
100
101   for(i = 0; i < numCommands; i++)
102   {
103       memset(buf, 0, BUFSIZE);
104
105       // Open a pipe and read VPP values
106       if ((fp = popen(commandArray[i].valStr, "r")) == NULL) {
107           printf("Error opening pipe!\n");
108           return;
109       }
110
111       while (fgets(buf, BUFSIZE, fp) != NULL);
112       // printf("\n the vpp value is %d\n", atoi(buf));
113       strcpy(commandArray[i].resultStr, buf);
114
115       if(pclose(fp))  {
116           printf("Command not found or exited with error status\n");
117           return;
118       }
119    }
120    //for(i = 0; i < numCommands; i++)
121    //   printf("\n commandArray[%d].resultStr is- %s\n", i, commandArray[i].resultStr);
122 }
123
124
125 void copy_vpp_metic_data(vpp_metrics_struct *intfstats, KEYVALRESULT * cmdArray, int numCmds, int linkNum)
126 {
127     int i;
128
129     // Store the current metric in the last metric
130     intfstats[linkNum].last_bytes_in = intfstats[linkNum].curr_bytes_in;
131     intfstats[linkNum].last_bytes_out = intfstats[linkNum].curr_bytes_out;
132     intfstats[linkNum].last_packets_in = intfstats[linkNum].curr_packets_in;
133     intfstats[linkNum].last_packets_out = intfstats[linkNum].curr_packets_out;
134
135     // Store metrics read from the vNIC in the current
136     for(i=0; i<numCmds; i++)
137     {
138        if((strcmp(cmdArray[i].keyStr, "tmp_t0BytesIn") == 0) ||
139           (strcmp(cmdArray[i].keyStr, "tmp_t1BytesIn") == 0))
140           intfstats[linkNum].curr_bytes_in = atoi(cmdArray[i].resultStr);
141
142        if((strcmp(cmdArray[i].keyStr, "tmp_t0BytesOut") == 0) ||
143           (strcmp(cmdArray[i].keyStr, "tmp_t1BytesOut") == 0))
144           intfstats[linkNum].curr_bytes_out = atoi(cmdArray[i].resultStr);
145
146        if((strcmp(cmdArray[i].keyStr, "tmp_t0PacketsIn") == 0) ||
147           (strcmp(cmdArray[i].keyStr, "tmp_t1PacketsIn") == 0))
148           intfstats[linkNum].curr_packets_in = atoi(cmdArray[i].resultStr);
149
150        if((strcmp(cmdArray[i].keyStr, "tmp_t0PacketsOut") == 0) ||
151           (strcmp(cmdArray[i].keyStr, "tmp_t1PacketsOut") == 0))
152           intfstats[linkNum].curr_packets_out = atoi(cmdArray[i].resultStr);
153     }
154     // printf("intfstats[%d].curr_bytes_in = %d\n", linkNum, intfstats[linkNum].curr_bytes_in);
155     // printf("intfstats[%d].curr_bytes_out = %d\n", linkNum, intfstats[linkNum].curr_bytes_out);
156     // printf("intfstats[%d].curr_packets_in = %d\n", linkNum, intfstats[linkNum].curr_packets_in);
157     // printf("intfstats[%d].curr_packets_out = %d\n", linkNum, intfstats[linkNum].curr_packets_out);
158 }
159
160 int get_severity(char * inStr)
161 {
162    int result = -1;
163
164    if(strcmp(inStr, "CRITICAL") == 0)
165      result = EVEL_SEVERITY_CRITICAL;
166    else if(strcmp(inStr, "MAJOR") == 0)
167      result = EVEL_SEVERITY_MAJOR;
168    else if(strcmp(inStr, "MINOR") == 0)
169      result = EVEL_SEVERITY_MINOR;
170    else if(strcmp(inStr, "WARNING") == 0)
171      result = EVEL_SEVERITY_WARNING;
172    else if(strcmp(inStr, "NORMAL") == 0)
173      result = EVEL_SEVERITY_NORMAL;
174
175   return result;
176 }
177
178 int get_priority(char * inStr)
179 {
180    int result = -1;
181
182    if(strcmp(inStr, "High") == 0)
183      result = EVEL_PRIORITY_HIGH;
184    else if(strcmp(inStr, "Medium") == 0)
185      result = EVEL_PRIORITY_MEDIUM;
186    else if(strcmp(inStr, "Normal") == 0)
187      result = EVEL_PRIORITY_NORMAL;
188    else if(strcmp(inStr, "Low") == 0)
189      result = EVEL_PRIORITY_LOW;
190
191   return result;
192 }
193
194 int get_source(char * inStr)
195 {
196    int result = -1;
197
198    if(strcmp(inStr, "other") == 0)
199      result = EVEL_SOURCE_OTHER;
200    else if(strcmp(inStr, "router") == 0)
201      result = EVEL_SOURCE_ROUTER;
202    else if(strcmp(inStr, "switch") == 0)
203      result = EVEL_SOURCE_SWITCH;
204    else if(strcmp(inStr, "host") == 0)
205      result = EVEL_SOURCE_HOST;
206    else if(strcmp(inStr, "card") == 0)
207      result = EVEL_SOURCE_CARD;
208    else if(strcmp(inStr, "port") == 0)
209      result = EVEL_SOURCE_PORT;
210    else if(strcmp(inStr, "slotThreshold") == 0)
211      result = EVEL_SOURCE_SLOT_THRESHOLD;
212    else if(strcmp(inStr, "portThreshold") == 0)
213      result = EVEL_SOURCE_PORT_THRESHOLD;
214    else if(strcmp(inStr, "virtualMachine") == 0)
215      result = EVEL_SOURCE_VIRTUAL_MACHINE;
216    else if(strcmp(inStr, "virtualNetworkFunction") == 0)
217      result = EVEL_SOURCE_VIRTUAL_NETWORK_FUNCTION;
218
219   return result;
220 }
221
222 int get_vf_status(char * inStr)
223 {
224    int result = -1;
225
226    if(strcmp(inStr, "Active") == 0)
227      result = EVEL_VF_STATUS_ACTIVE;
228    else if(strcmp(inStr, "Idle") == 0)
229      result = EVEL_VF_STATUS_IDLE;
230    else if(strcmp(inStr, "Preparing to terminate") == 0)
231      result = EVEL_VF_STATUS_PREP_TERMINATE;
232    else if(strcmp(inStr, "Ready to terminate") == 0)
233      result = EVEL_VF_STATUS_READY_TERMINATE;
234    else if(strcmp(inStr, "Requesting termination") == 0)
235      result = EVEL_VF_STATUS_REQ_TERMINATE;
236
237   return result;
238 }
239
240 int main(int argc, char** argv)
241 {
242   char* fqdn = argv[1];
243   int port = atoi(argv[2]);
244   int i=0;
245   int rc;
246   pthread_attr_t attr;
247   pthread_t flt_thread;
248   char* fqdn2 = NULL;
249   int port2 = 0;
250
251   if(argc == 5)
252   {
253      fqdn2 = argv[3];
254      port2 = atoi(argv[4]);
255   }
256
257   if (!((argc == 3) || (argc == 5)))
258   {
259     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <FQDN>|<IP address> <port>  \n", argv[0]);
260     fprintf(stderr, "OR\n");
261     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> \n", argv[0]);
262     exit(-1);
263   }
264
265   /**************************************************************************/
266   /* Initialize                                                             */
267   /**************************************************************************/
268   if(evel_initialize(fqdn,                         /* FQDN                  */
269                      port,                         /* Port                  */
270                      fqdn2,                        /* Backup FQDN           */
271                      port2,                        /* Backup port           */
272                      NULL,                         /* optional path         */
273                      NULL,                         /* optional topic        */
274                      100,                          /* Ring Buffer size      */
275                      0,                            /* HTTPS?                */
276                      NULL,                         /* cert file             */
277                      NULL,                         /* key  file             */
278                      NULL,                         /* ca   info             */
279                      NULL,                         /* ca   file             */
280                      0,                            /* verify peer           */
281                      0,                            /* verify host           */
282                      "sample1",                    /* Username              */
283                      "sample1",                    /* Password              */
284                      "sample1",                    /* Username2             */
285                      "sample1",                    /* Password2             */
286                      NULL,                         /* Source ip             */
287                      NULL,                         /* Source ip2            */
288                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
289                      "vFault",                     /* Role                  */
290                      1))                           /* Verbosity             */
291   {
292     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
293     exit(-1);
294   }
295   else
296   {
297     printf("\nInitialization completed\n");
298   }
299
300   /* Initialize and set thread detached attribute */
301   pthread_attr_init(&attr);
302   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
303
304   printf("Main:Creating thread \n");
305   rc = pthread_create(&flt_thread, NULL, FaultThread, &i);
306   if (rc)
307   {
308     printf("ERROR; return code from pthread_create() is %d\n", rc);
309     exit(-1);
310   }
311   printf("Main:Created Fault thread \n");
312
313   pthread_join(flt_thread, NULL);
314
315   evel_terminate();
316   printf("Terminated\n");
317   return 0;
318 }
319
320 void *FaultThread(void *mainFault)
321 {
322
323    pthread_attr_t attr;
324    pthread_t flt_thread01;
325    pthread_t flt_thread02;
326    pthread_t flt_thread03;
327    int faultInstance01 = 0;
328    int faultInstance02 = 0;
329    int faultInstance03 = 0;
330    int rc;
331
332    int numToken;
333    jsmntok_t tokens[MAX_TOKENS];
334    char js[MAX_BUFFER_SIZE]; 
335
336    jsmn_parser p;
337    char ch[BUFSIZE];
338    int ret = 0;
339
340    memset(js, 0, MAX_BUFFER_SIZE);
341    memset(ch, 0, BUFSIZE);
342
343    sleep(1);
344    printf("Running Main Fault thread \n");
345    fflush(stdout);
346
347    /* Initialize and set thread detached attribute */
348    pthread_attr_init(&attr);
349    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
350
351    FILE * file = fopen("flt_config.json", "r");
352
353    while((fgets(ch, (BUFSIZE-1), file)) !=NULL)
354    {
355       strcat(js, ch);
356       memset(ch, 0, BUFSIZE);
357    }
358    //printf("MainFaulThread::the file content is \n %s \n", js);
359    //printf("\n MainFaulThread::MMMMMMMMMMMMMMM\n");
360
361    jsmn_init(&p);
362    numToken = jsmn_parse(&p, js, strlen(js), tokens, MAX_TOKENS);
363    //printf("count-%d\n", numToken);
364
365    //printToken(js,tokens, numToken);
366
367    printf("Main Fault Thread: Creating other fault threads\n");
368
369    ret = isTokenPresent(js, tokens, numToken, "tmp_faultInstance01", "tmp_indirectParameters");
370    if (ret == 0)
371    {
372 //      rc = pthread_create(&flt_thread01, NULL, FaultThread01, "tmp_faultInstance01");
373       rc = pthread_create(&flt_thread01, NULL, FaultThread01, &ret);
374       if (rc)
375       {
376         printf("Main Fault Thread::ERROR; return code from pthread_create() is %d\n", rc);
377         exit(-1);
378       }
379       faultInstance01 = 1;
380    }
381
382    ret = isTokenPresent(js, tokens, numToken, "tmp_faultInstance02", "tmp_indirectParameters");
383    if (ret == 0)
384    {
385 //      rc = pthread_create(&flt_thread02, NULL, FaultThread02, "tmp_faultInstance02");
386       rc = pthread_create(&flt_thread02, NULL, FaultThread02, &ret);
387       if (rc)
388       {
389         printf("Main Fault Thread::ERROR; return code from pthread_create() is %d\n", rc);
390         exit(-1);
391       }
392       faultInstance02 = 1;
393    }
394
395    ret = isTokenPresent(js, tokens, numToken, "tmp_faultInstance03", "tmp_indirectParameters");
396    if (ret == 0)
397    {
398       rc = pthread_create(&flt_thread03, NULL, FaultThread03, &ret);
399       if (rc)
400       {
401         printf("Main Fault Thread::ERROR; return code from pthread_create() is %d\n", rc);
402         exit(-1);
403       }
404       faultInstance03 = 1;
405    }
406
407    if (faultInstance01 == 1)
408    {
409        pthread_join(flt_thread01, NULL);
410    }
411
412    if (faultInstance02 == 1)
413    {
414        pthread_join(flt_thread02, NULL);
415    }
416
417    if (faultInstance03 == 1)
418    {
419        pthread_join(flt_thread03, NULL);
420    }
421
422    while(1)
423    {
424        sleep(100);
425    }
426 }
427
428 void *FaultThread01(void *faultInstanceTag)
429 {
430   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
431   EVENT_FAULT * fault = NULL;
432   EVENT_HEADER* fault_header = NULL;
433   int bytes_in;
434   int bytes_out;
435   int packets_in;
436   int packets_out;
437   unsigned long long epoch_now;
438   int lowWaterMark;
439
440   struct timeval time_val;
441   char event_id1[10] = "fault";
442   char event_id2[15] = {0};
443   char event_id[BUFSIZE] = {0};
444   int fault_event_id = 0;
445
446    int numToken;
447    jsmntok_t tokens[MAX_TOKENS];
448    char js[MAX_BUFFER_SIZE]; 
449
450    jsmn_parser p;
451    char ch[BUFSIZE];
452    int ret = 0;
453    char eName[BUFSIZE];
454    char eType[BUFSIZE];
455    char nfcCode[BUFSIZE];
456    char nfCode[BUFSIZE];
457    char prio[BUFSIZE];
458    char reportEId[BUFSIZE];
459    char reportEName[BUFSIZE];
460    char srcId[BUFSIZE];
461    char srcName[BUFSIZE];
462    char eCategory[BUFSIZE];
463    char eventSrcTyp[BUFSIZE];
464    char specProb[BUFSIZE];
465    char alarmCondn[BUFSIZE];
466    char eventSev[BUFSIZE];
467    char vfStatus[BUFSIZE];
468
469    int priority;
470    int srcTyp;
471    int vfStat;
472    int eSev;
473
474    char hostname[BUFSIZE];
475
476    int flt_interval;
477    ARRAYVAL intfArray[MAX_INTERFACES];
478    KEYVALRESULT keyValResultArray[32];
479    KEYVALRESULT commandArray[32];
480    int numInitCommands = 0;
481    int numCommands = 0;
482    int linkCount = 0;
483    int i = 0;
484
485    memset(hostname, 0, BUFSIZE);
486    gethostname(hostname, BUFSIZE);
487    printf("FAULT01::The hostname is %s\n", hostname);
488
489    sleep(1);
490    printf("FAULT01::Running Fault thread \n");
491    fflush(stdout);
492
493    memset(&intfArray[0],0,(sizeof(ARRAYVAL) * MAX_INTERFACES));
494    memset(&keyValResultArray[0],0,(sizeof(KEYVALRESULT) * 32));
495
496    memset(js, 0, MAX_BUFFER_SIZE);
497    memset(ch, 0, BUFSIZE);
498    memset(&fault_intfstat[0],0,(sizeof(vpp_metrics_struct)* MAX_INTERFACES));
499    memset(&fault_linkstat[0],0,(sizeof(LINKSTAT) * MAX_INTERFACES));
500
501    FILE * file = fopen("flt_config.json", "r");
502
503    while((fgets(ch, (BUFSIZE-1), file)) !=NULL)
504    {
505       strcat(js, ch);
506       memset(ch, 0, BUFSIZE);
507    }
508 //   printf("FAULT01::the file content is \n %s \n", js);
509
510    jsmn_init(&p);
511    numToken = jsmn_parse(&p, js, strlen(js), tokens, MAX_TOKENS);
512    printf("FAULT01::count-%d\n", numToken);
513
514 //   printToken(js,tokens, numToken);
515
516
517    ret = getIntToken(js, tokens, numToken, "tmp_faultCheckInterval", "tmp_faultInstance01", &flt_interval);
518    if (ret != 0)
519    {
520       printf("FAULT01::The parameter tmp_faultCheckInterval is not defined, defaulted to 60 seconds\n");
521       flt_interval = 60;
522    }
523
524    ret = getIntToken(js, tokens, numToken, "tmp_lowWaterMark", "tmp_faultInstance01", &lowWaterMark);
525    if (ret != 0)
526    {
527       printf("FAULT01::The parameter tmp_lowWaterMark is not defined, defaulted to 100\n");
528       lowWaterMark = 100;
529    }
530
531   ret = getArrayTokens(js, tokens, numToken, "tmp_device", "tmp_directParameters", intfArray, &linkCount);
532
533   printf("FAULT01::Array link count is %d\n", linkCount); 
534
535   /* Copy the link information */
536   for(i=0;i<linkCount;i++)
537   {
538      strcpy(fault_linkstat[i].linkname, &intfArray[i]);
539 //     printf("FAULT01::Link name %s\n", fault_linkstat[i].linkname);
540   }
541   
542   //read_fault_config_file();
543
544   read_keyVal_params(js, tokens, numToken, "tmp_init", "tmp_faultInstance01", keyValResultArray, &numInitCommands);
545
546   for(i=0;i<linkCount;i++)
547   {
548      memset(&commandArray[0],0,(sizeof(KEYVALRESULT) * 32));
549      memcpy(commandArray, keyValResultArray, (sizeof(KEYVALRESULT) * 32)); 
550      format_val_params(commandArray, numInitCommands, fault_linkstat[i].linkname, "$tmp_device");
551      runCommands(commandArray, numInitCommands);
552      copy_vpp_metic_data(fault_intfstat, commandArray, numInitCommands, i); 
553   }
554
555   gettimeofday(&time_val, NULL);
556
557   sleep(flt_interval);
558
559   /***************************************************************************/
560   /* Collect metrics from the VNIC                                           */
561   /***************************************************************************/
562   while(1) 
563   {
564
565     ret = getIntToken(js, tokens, numToken, "tmp_faultCheckInterval", "tmp_faultInstance01", &flt_interval);
566    if (ret != 0)
567    {
568       flt_interval = 60;
569    }
570
571     ret = getIntToken(js, tokens, numToken, "tmp_lowWaterMark", "tmp_faultInstance01", &lowWaterMark);
572    if (ret != 0)
573    {
574       lowWaterMark = 100;
575    }
576
577    ret = getStringToken(js, tokens, numToken, "eventName", "tmp_faultInstance01", eName, BUFSIZE);
578    if (ret != 0)
579    {
580       printf("FAULT01::Missing mandatory parameters - eventName is not there in tmp_faultInstance01. Exiting...\n");
581       exit(1);
582    }
583
584    ret = getStringToken(js, tokens, numToken, "eventType", "tmp_directParameters", eType, BUFSIZE);
585    ret = getStringToken(js, tokens, numToken, "nfcNamingCode", "tmp_directParameters", nfcCode, BUFSIZE);
586    ret = getStringToken(js, tokens, numToken, "nfNamingCode", "tmp_directParameters", nfCode, BUFSIZE);
587    ret = getStringToken(js, tokens, numToken, "reportingEntityId", "tmp_directParameters", reportEId, BUFSIZE);
588    ret = getStringToken(js, tokens, numToken, "sourceId", "tmp_directParameters", srcId, BUFSIZE);
589    ret = getStringToken(js, tokens, numToken, "eventCategory", "tmp_faultInstance01", eCategory, BUFSIZE);  
590
591    ret = getStringToken(js, tokens, numToken, "eventSourceType", "tmp_faultInstance01", eventSrcTyp, BUFSIZE);
592    if (ret != 0)
593    {
594       printf("FAULT01::Missing mandatory parameters - eventSourceType is not there in tmp_directParameters, exiting..\n");
595       exit(1); 
596    }
597    srcTyp = get_source(eventSrcTyp);
598    if(srcTyp == -1)
599    {
600       printf("FAULT01::Fault eventSourceType value is not matching, eventSourceType-%s \n", eventSrcTyp);
601       exit(1);
602    }
603
604    ret = getStringToken(js, tokens, numToken, "vfStatus", "tmp_directParameters", vfStatus, BUFSIZE);
605    if (ret != 0)
606    {
607       printf("FAULT01::Missing mandatory parameters - vfStatus is not there in tmp_directParameters, exiting..\n");
608       exit(1); 
609    }
610    vfStat = get_vf_status(vfStatus);
611    if(vfStat == -1)
612    {
613       printf("FAULT01::Fault vfStatus value is not matching, vfStatus-%s \n", vfStatus);
614       exit(1);
615    }
616
617    read_keyVal_params(js, tokens, numToken, "tmp_command", "tmp_faultInstance01", keyValResultArray, &numCommands);
618
619    for(i=0;i<linkCount;i++)
620    {
621        memset(&commandArray[0],0,(sizeof(KEYVALRESULT) * 32));
622        memcpy(commandArray, keyValResultArray, (sizeof(KEYVALRESULT) * 32)); 
623        format_val_params(commandArray, numCommands, fault_linkstat[i].linkname, "$tmp_device");
624        runCommands(commandArray, numCommands);
625        copy_vpp_metic_data(fault_intfstat, commandArray, numInitCommands, i); 
626    }
627
628    for (int i = 0; i < linkCount; i++)
629    {
630       if(fault_intfstat[i].curr_bytes_in - fault_intfstat[i].last_bytes_in > 0) {
631         bytes_in = fault_intfstat[i].curr_bytes_in - fault_intfstat[i].last_bytes_in;
632       }
633       else {
634         bytes_in = 0;
635       }
636       if(fault_intfstat[i].curr_bytes_out - fault_intfstat[i].last_bytes_out > 0) {
637         bytes_out = fault_intfstat[i].curr_bytes_out - fault_intfstat[i].last_bytes_out;
638       }
639       else {
640         bytes_out = 0;
641       }
642       if(fault_intfstat[i].curr_packets_in - fault_intfstat[i].last_packets_in > 0) {
643         packets_in = fault_intfstat[i].curr_packets_in - fault_intfstat[i].last_packets_in;
644       }
645       else {
646         packets_in = 0;
647       }
648       if(fault_intfstat[i].curr_packets_out - fault_intfstat[i].last_packets_out > 0) {
649         packets_out = fault_intfstat[i].curr_packets_out - fault_intfstat[i].last_packets_out;
650       }
651       else {
652         packets_out = 0;
653       }
654       if (((bytes_in < lowWaterMark) || (bytes_out < lowWaterMark) || 
655           (packets_in < lowWaterMark) || (packets_out < lowWaterMark)) &&
656           (fault_linkstat[i].fault_raised == 0)) 
657       {
658         printf("\n%d - bytes in %d, ouot %d, packets in %d, out %d", i, bytes_in, bytes_out, packets_in, packets_out);
659         printf("\nFAULT01::Raising fault\n");
660         memset(event_id, 0, BUFSIZE);
661         fault_event_id = fault_event_id+1;
662         sprintf(event_id2, "%09d", fault_event_id);
663         strcat(event_id, event_id1);
664         strcat(event_id, event_id2);
665
666         ret = getIntToken(js, tokens, numToken, "tmp_faultCheckInterval", "tmp_faultInstance01", &flt_interval);
667         if (ret != 0)
668         {
669            printf("FAULT01::The parameter tmp_faultCheckInterval is not defined, defaulted to 60 seconds\n");
670            flt_interval = 60;
671         }
672
673         ret = getIntToken(js, tokens, numToken, "tmp_lowWaterMark", "tmp_faultInstance01", &lowWaterMark);
674         if (ret != 0)
675         {
676            printf("FAULT01::The parameter tmp_lowWaterMark is not defined, defaulted to 100\n");
677            lowWaterMark = 100;
678         }
679         ret = getStringToken(js, tokens, numToken, "reportingEntityName", "tmp_directParameters", reportEName, BUFSIZE);
680         if (ret != 0)
681         {
682            printf("FAULT01::Missing mandatory parameters - reportingEntityName is not there in tmp_directParameters\n");
683            printf("FAULT01::Defaulting reportingEntityName to hostname\n");
684            strcpy(reportEName, hostname);
685         }
686         ret = getStringToken(js, tokens, numToken, "sourceName", "tmp_directParameters", srcName, BUFSIZE);
687         if (ret != 0)
688         {
689            printf("FAULT01::Missing mandatory parameters - sourceName is not there in tmp_directParameters\n");
690            printf("FAULT01::Defaulting sourceName to hostname\n");
691            strcpy(srcName, hostname);
692         }
693
694         ret = getStringToken(js, tokens, numToken, "priority", "tmp_directParameters", prio, BUFSIZE);
695         if (ret != 0)
696         {
697            printf("FAULT01::Missing mandatory parameters - priority is not there in tmp_directParameters\nDefaulting priority to Low\n");
698            strcpy(prio, "Medium");
699         }
700         priority = get_priority(prio);
701         if(priority == -1)
702         {
703            printf("FAULT01::Fault priority value is not matching, prioirty-%s \n", prio);
704            exit(1);
705         }
706
707         ret = getStringTokenV2(js, tokens, numToken, "specificProblem", "tmp_alarmSetParameters", "tmp_faultInstance01", specProb, BUFSIZE);
708         if (ret != 0)
709         {
710            printf("FAULT01::Missing mandatory parameters - specificProblem is not there in tmp_alarmSetParameters, exiting ...\n");
711            exit(1);
712         }
713         ret = getStringTokenV2(js, tokens, numToken, "alarmCondition", "tmp_alarmSetParameters", "tmp_faultInstance01", alarmCondn, BUFSIZE);
714         if (ret != 0)
715         {
716            printf("FAULT01::Missing mandatory parameters - alarmCondition is not there in tmp_alarmSetParameters, exiting ...\n");
717            exit(1);
718         }
719         ret = getStringTokenV2(js, tokens, numToken, "eventSeverity", "tmp_alarmSetParameters", "tmp_faultInstance01", eventSev, BUFSIZE);
720         if (ret != 0)
721         {
722            printf("FAULT01::Missing mandatory parameters - eventSeverity is not there in tmp_alarmSetParameters\n");
723            printf("FAULT01::Defaulting eventSeverity to MAJOR\n");
724            strcpy(eventSev, "MAJOR");
725         }
726         eSev = get_severity(eventSev);
727         if(eSev == -1)
728         {
729            printf("FAULT01::Fault eventSeverity value is not matching, eventSeverity-%s \n", eventSev);
730            exit(1);
731         }
732
733         fault = evel_new_fault(eName, event_id, alarmCondn, 
734                                specProb, priority, eSev, srcTyp,vfStat);
735         if (fault != NULL)
736         {
737             fault_linkstat[i].fault_raised = 1;
738
739             struct timeval tv_now;
740             gettimeofday(&tv_now, NULL);
741             epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
742             fault_linkstat[i].last_epoch = epoch_now;
743   
744             fault_header = (EVENT_HEADER *)fault;
745   
746             evel_fault_category_set(fault, eCategory);
747             evel_fault_interface_set(fault, fault_linkstat[i].linkname);
748     
749             if (eType != NULL)
750                 evel_fault_type_set(fault, eType); 
751       
752             evel_start_epoch_set(&fault->header, epoch_now);
753             evel_last_epoch_set(&fault->header, epoch_now);
754             if(nfcCode != NULL)
755                 evel_nfcnamingcode_set(&fault->header, nfcCode);
756             if(nfCode != NULL)
757                 evel_nfnamingcode_set(&fault->header, nfCode);
758             evel_reporting_entity_name_set(&fault->header, reportEName);
759             if(reportEId != NULL)
760                 evel_reporting_entity_id_set(&fault->header, reportEId);
761             if(srcId != NULL )
762                 evel_source_id_set(&fault->header, srcId);
763             if(srcName!= NULL )
764                 evel_source_name_set(&fault->header, srcName);
765     
766             evel_rc = evel_post_event(fault_header);
767
768             if(evel_rc == EVEL_SUCCESS)
769                 printf("FAULT01::Fault event is correctly sent to the collector!\n");
770             else
771                 printf("FAULT01::Post failed %d (%s)\n", evel_rc, evel_error_string());
772         }
773         else
774         {
775            printf("FAULT01::New new fault failed (%s)\n", evel_error_string());
776         }
777       }
778       else if (((bytes_in > lowWaterMark) && (bytes_out > lowWaterMark) && 
779           (packets_in > lowWaterMark) && (packets_out > lowWaterMark)) &&
780           (fault_linkstat[i].fault_raised == 1)) 
781       {
782          printf("\nFAULT01:: Clearing fault\n");
783          memset(event_id, 0, BUFSIZE);
784          sprintf(event_id2, "%09d", (i+1));
785          strcat(event_id, event_id1);
786          strcat(event_id, event_id2);
787  
788          ret = getIntToken(js, tokens, numToken, "tmp_faultCheckInterval", "tmp_faultInstance01", &flt_interval);
789          if (ret != 0)
790          {
791             printf("FAULT01::The parameter tmp_faultCheckInterval is not defined, defaulted to 60 seconds\n");
792             flt_interval = 60;
793          }
794
795          ret = getIntToken(js, tokens, numToken, "tmp_lowWaterMark", "tmp_faultInstance01", &lowWaterMark);
796          if (ret != 0)
797          {
798            printf("FAULT01::The parameter tmp_lowWaterMark is not defined, defaulted to 100\n");
799            lowWaterMark = 100;
800          }
801          ret = getStringToken(js, tokens, numToken, "reportingEntityName", "tmp_directParameters", reportEName, BUFSIZE);
802          if (ret != 0)
803          {
804             printf("FAULT01::Missing mandatory parameters - reportingEntityName is not there in tmp_directParameters\n");
805             printf("FAULT01::Defaulting reportingEntityName to hostname\n");
806             strcpy(reportEName, hostname);
807          }
808          ret = getStringToken(js, tokens, numToken, "sourceName", "tmp_directParameters", srcName, BUFSIZE);
809          if (ret != 0)
810          {
811             printf("FAULT01::Missing mandatory parameters - sourceName is not there in tmp_directParameters\n");
812             printf("FAULT01::Defaulting sourceName to hostname\n");
813             strcpy(srcName, hostname);
814          }
815  
816          ret = getStringToken(js, tokens, numToken, "priority", "tmp_directParameters", prio, BUFSIZE);
817          if (ret != 0)
818          {
819             printf("FAULT01::Missing mandatory parameters - priority is not there in tmp_directParameters\nDefaulting priority to Low\n");
820             strcpy(prio, "Medium");
821          }
822          priority = get_priority(prio);
823          if(priority == -1)
824          {
825             printf("FAULT01::Fault priority value is not matching, prioirty-%s \n", prio);
826             exit(1);
827          }
828  
829          ret = getStringTokenV2(js, tokens, numToken, "specificProblem", "tmp_alarmClearParameters", "tmp_faultInstance01", specProb, BUFSIZE);
830          if (ret != 0)
831          {
832             printf("FAULT01::Missing mandatory parameters - specificProblem is not there in tmp_alarmClearParameters, exiting ...\n");
833             exit(1);
834          }
835  
836          ret = getStringTokenV2(js, tokens, numToken, "alarmCondition", "tmp_alarmClearParameters", "tmp_faultInstance01", alarmCondn, BUFSIZE);
837          if (ret != 0)
838          {
839             printf("FAULT01::Missing mandatory parameters - alarmCondition is not there in tmp_alarmClearParameters, exiting ...\n");
840             exit(1);
841          }
842  
843          ret = getStringTokenV2(js, tokens, numToken, "eventSeverity", "tmp_alarmClearParameters", "tmp_faultInstance01", eventSev, BUFSIZE);
844          if (ret != 0)
845          {
846             printf("FAULT01::Missing mandatory parameters - eventSeverity is not there in tmp_alarmClearParameters\n");
847             printf("FAULT01::Defaulting eventSeverity to MAJOR\n");
848             strcpy(eventSev, "NORMAL");
849          }
850          eSev = get_severity(eventSev);
851          if(eSev == -1)
852          {
853             printf("FAULT01::Fault eventSeverity value is not matching, eventSeverity-%s \n", eventSev);
854             exit(1);
855          }
856  
857          fault = evel_new_fault(eName, event_id, alarmCondn, 
858                                specProb, priority, eSev, srcTyp,vfStat);
859          if (fault != NULL)
860          {
861             fault_linkstat[i].fault_raised = 0;
862
863             struct timeval tv_now;
864             gettimeofday(&tv_now, NULL);
865             epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
866   
867             fault_header = (EVENT_HEADER *)fault;
868             evel_fault_category_set(fault, eCategory);
869             evel_fault_interface_set(fault, fault_linkstat[i].linkname);
870     
871             if (eType != NULL)
872               evel_fault_type_set(fault, eType); 
873       
874             evel_start_epoch_set(&fault->header, fault_linkstat[i].last_epoch);
875             evel_last_epoch_set(&fault->header, epoch_now);
876             fault_linkstat[i].last_epoch = 0;
877
878             if(nfcCode != NULL)
879               evel_nfcnamingcode_set(&fault->header, nfcCode);
880             if(nfCode != NULL)
881               evel_nfnamingcode_set(&fault->header, nfCode);
882             evel_reporting_entity_name_set(&fault->header, reportEName);
883             if(reportEId != NULL)
884               evel_reporting_entity_id_set(&fault->header, reportEId);
885             if(srcId != NULL )
886             evel_source_id_set(&fault->header, srcId);
887             if(srcName!= NULL )
888               evel_source_name_set(&fault->header, srcName);
889       
890             evel_rc = evel_post_event(fault_header);
891   
892             if(evel_rc == EVEL_SUCCESS)
893               printf("FAULT01::Fault event is correctly sent to the collector!\n");
894             else
895               printf("FAULT01::Post failed %d (%s)\n", evel_rc, evel_error_string());
896          }
897          else 
898            printf("FAULT01::New fault failed (%s)\n", evel_error_string());
899       }
900    }
901
902    sleep(flt_interval);
903   }
904
905   /***************************************************************************/
906   /* Terminate                                                               */
907   /***************************************************************************/
908   sleep(1);
909 }
910
911 void *FaultThread02(void *faultInstanceTag)
912 {
913   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
914   EVENT_FAULT * fault = NULL;
915   EVENT_HEADER* fault_header = NULL;
916   unsigned long long epoch_now;
917   unsigned long long last_epoch;
918
919   struct timeval time_val;
920   char event_id1[10] = "fault";
921   char event_id2[15] = {0};
922   char event_id[BUFSIZE] = {0};
923   int fault_event_id = 0;
924   int i=0;
925
926    int numToken;
927    jsmntok_t tokens[MAX_TOKENS];
928    char js[MAX_BUFFER_SIZE]; 
929
930    jsmn_parser p;
931    char ch[BUFSIZE];
932    int ret = 0;
933    char eName[BUFSIZE];
934    char eType[BUFSIZE];
935    char aInterface[BUFSIZE];
936    char nfcCode[BUFSIZE];
937    char nfCode[BUFSIZE];
938    char prio[BUFSIZE];
939    char reportEId[BUFSIZE];
940    char reportEName[BUFSIZE];
941    char srcId[BUFSIZE];
942    char srcName[BUFSIZE];
943    char eCategory[BUFSIZE];
944    char eventSrcTyp[BUFSIZE];
945    char specProb[BUFSIZE];
946    char alarmCondn[BUFSIZE];
947    char eventSev[BUFSIZE];
948    char vfStatus[BUFSIZE];
949
950    int priority;
951    int srcTyp;
952    int vfStat;
953    int eSev;
954
955    char hostname[BUFSIZE];
956
957    int flt_interval;
958    KEYVALRESULT keyValResultArray[32];
959    KEYVALRESULT commandArray[32];
960    int numInitCommands = 0;
961    int numCommands = 0;
962    int fault_raised = 0;
963
964    memset(hostname, 0, BUFSIZE);
965    gethostname(hostname, BUFSIZE);
966    printf("FAULT02::The hostname is %s\n", hostname);
967
968    sleep(1);
969    printf("FAULT02::Running Fault thread \n");
970    fflush(stdout);
971
972    memset(&keyValResultArray[0],0,(sizeof(KEYVALRESULT) * 32));
973
974    memset(js, 0, MAX_BUFFER_SIZE);
975    memset(ch, 0, BUFSIZE);
976
977    FILE * file = fopen("flt_config.json", "r");
978
979    while((fgets(ch, (BUFSIZE-1), file)) !=NULL)
980    {
981       strcat(js, ch);
982       memset(ch, 0, BUFSIZE);
983    }
984 //   printf("FAULT02::the file content is \n %s \n", js);
985
986    jsmn_init(&p);
987    numToken = jsmn_parse(&p, js, strlen(js), tokens, MAX_TOKENS);
988    printf("FAULT02::count-%d\n", numToken);
989
990 //   printToken(js,tokens, numToken);
991
992    ret = getIntToken(js, tokens, numToken, "tmp_faultCheckInterval", "tmp_faultInstance02", &flt_interval);
993    if (ret != 0)
994    {
995       printf("FAULT02::The parameter tmp_faultCheckInterval is not defined, defaulted to 60 seconds\n");
996       flt_interval = 60;
997    }
998   
999
1000   read_keyVal_params(js, tokens, numToken, "tmp_init", "tmp_faultInstance02", keyValResultArray, &numInitCommands);
1001
1002   memset(&commandArray[0],0,(sizeof(KEYVALRESULT) * 32));
1003   memcpy(commandArray, keyValResultArray, (sizeof(KEYVALRESULT) * 32)); 
1004   runCommands(commandArray, numInitCommands);
1005
1006   gettimeofday(&time_val, NULL);
1007
1008   sleep(flt_interval);
1009
1010   /***************************************************************************/
1011   /* Collect metrics from the VNIC                                           */
1012   /***************************************************************************/
1013   while(1) {
1014
1015    ret = getIntToken(js, tokens, numToken, "tmp_faultCheckInterval", "tmp_faultInstance02", &flt_interval);
1016    if (ret != 0)
1017    {
1018       flt_interval = 60;
1019    }
1020
1021    ret = getStringToken(js, tokens, numToken, "eventName", "tmp_faultInstance02", eName, BUFSIZE);
1022    if (ret != 0)
1023    {
1024       printf("FAULT02::Missing mandatory parameters - eventName is not there in tmp_faultInstance02. Exiting...\n");
1025       exit(1);
1026    }
1027
1028    ret = getStringToken(js, tokens, numToken, "eventType", "tmp_directParameters", eType, BUFSIZE);
1029    ret = getStringToken(js, tokens, numToken, "nfcNamingCode", "tmp_directParameters", nfcCode, BUFSIZE);
1030    ret = getStringToken(js, tokens, numToken, "nfNamingCode", "tmp_directParameters", nfCode, BUFSIZE);
1031    ret = getStringToken(js, tokens, numToken, "reportingEntityId", "tmp_directParameters", reportEId, BUFSIZE);
1032    ret = getStringToken(js, tokens, numToken, "sourceId", "tmp_directParameters", srcId, BUFSIZE);
1033    ret = getStringToken(js, tokens, numToken, "eventCategory", "tmp_faultInstance02", eCategory, BUFSIZE);  
1034    ret = getStringToken(js, tokens, numToken, "alarmInterfaceA", "tmp_faultInstance02", aInterface, BUFSIZE);  
1035
1036    ret = getStringToken(js, tokens, numToken, "eventSourceType", "tmp_faultInstance02", eventSrcTyp, BUFSIZE);
1037    if (ret != 0)
1038    {
1039       printf("FAULT02::Missing mandatory parameters - eventSourceType is not there in tmp_directParameters, exiting..\n");
1040       exit(1); 
1041    }
1042    srcTyp = get_source(eventSrcTyp);
1043    if(srcTyp == -1)
1044    {
1045       printf("FAULT02::Fault eventSourceType value is not matching, eventSourceType-%s \n", eventSrcTyp);
1046       exit(1);
1047    }
1048
1049    ret = getStringToken(js, tokens, numToken, "vfStatus", "tmp_directParameters", vfStatus, BUFSIZE);
1050    if (ret != 0)
1051    {
1052       printf("FAULT02::Missing mandatory parameters - vfStatus is not there in tmp_directParameters, exiting..\n");
1053       exit(1); 
1054    }
1055    vfStat = get_vf_status(vfStatus);
1056    if(vfStat == -1)
1057    {
1058       printf("FAULT02::Fault vfStatus value is not matching, vfStatus-%s \n", vfStatus);
1059       exit(1);
1060    }
1061
1062    read_keyVal_params(js, tokens, numToken, "tmp_command", "tmp_faultInstance02", keyValResultArray, &numCommands);
1063
1064    memset(&commandArray[0],0,(sizeof(KEYVALRESULT) * 32));
1065    memcpy(commandArray, keyValResultArray, (sizeof(KEYVALRESULT) * 32)); 
1066    runCommands(commandArray, numCommands);
1067
1068    /********************************************************************************
1069     * Put the condition to set the fault here
1070     *******************************************************************************/
1071    if ((atoi(commandArray[0].resultStr) == 1) && (fault_raised == 0)) 
1072    {
1073         printf("\nFAULT02::Raising fault\n");
1074         memset(event_id, 0, BUFSIZE);
1075         fault_event_id = fault_event_id+1;
1076         sprintf(event_id2, "%09d", fault_event_id);
1077         strcat(event_id, event_id1);
1078         strcat(event_id, event_id2);
1079
1080         ret = getIntToken(js, tokens, numToken, "tmp_faultCheckInterval", "tmp_faultInstance02", &flt_interval);
1081         if (ret != 0)
1082         {
1083            printf("FAULT02::The parameter tmp_faultCheckInterval is not defined, defaulted to 60 seconds\n");
1084            flt_interval = 60;
1085         }
1086
1087         ret = getStringToken(js, tokens, numToken, "reportingEntityName", "tmp_directParameters", reportEName, BUFSIZE);
1088         if (ret != 0)
1089         {
1090            printf("FAULT02::Missing mandatory parameters - reportingEntityName is not there in tmp_directParameters\n");
1091            printf("FAULT02::Defaulting reportingEntityName to hostname\n");
1092            strcpy(reportEName, hostname);
1093         }
1094         ret = getStringToken(js, tokens, numToken, "sourceName", "tmp_directParameters", srcName, BUFSIZE);
1095         if (ret != 0)
1096         {
1097            printf("FAULT02::Missing mandatory parameters - sourceName is not there in tmp_directParameters\n");
1098            printf("FAULT02::Defaulting sourceName to hostname\n");
1099            strcpy(srcName, hostname);
1100         }
1101
1102         ret = getStringToken(js, tokens, numToken, "priority", "tmp_directParameters", prio, BUFSIZE);
1103         if (ret != 0)
1104         {
1105            printf("FAULT02::Missing mandatory parameters - priority is not there in tmp_directParameters\nDefaulting priority to Low\n");
1106            strcpy(prio, "Medium");
1107         }
1108         priority = get_priority(prio);
1109         if(priority == -1)
1110         {
1111            printf("FAULT02::Fault priority value is not matching, prioirty-%s \n", prio);
1112            exit(1);
1113         }
1114
1115         ret = getStringTokenV2(js, tokens, numToken, "specificProblem", "tmp_alarmSetParameters", "tmp_faultInstance02", specProb, BUFSIZE);
1116         if (ret != 0)
1117         {
1118            printf("FAULT02::Missing mandatory parameters - specificProblem is not there in tmp_alarmSetParameters, exiting ...\n");
1119            exit(1);
1120         }
1121         ret = getStringTokenV2(js, tokens, numToken, "alarmCondition", "tmp_alarmSetParameters", "tmp_faultInstance02", alarmCondn, BUFSIZE);
1122         if (ret != 0)
1123         {
1124            printf("FAULT02::Missing mandatory parameters - alarmCondition is not there in tmp_alarmSetParameters, exiting ...\n");
1125            exit(1);
1126         }
1127         ret = getStringTokenV2(js, tokens, numToken, "eventSeverity", "tmp_alarmSetParameters", "tmp_faultInstance02", eventSev, BUFSIZE);
1128         if (ret != 0)
1129         {
1130            printf("FAULT02::Missing mandatory parameters - eventSeverity is not there in tmp_alarmSetParameters\n");
1131            printf("FAULT02::Defaulting eventSeverity to MAJOR\n");
1132            strcpy(eventSev, "MAJOR");
1133         }
1134         eSev = get_severity(eventSev);
1135         if(eSev == -1)
1136         {
1137            printf("FAULT02::Fault eventSeverity value is not matching, eventSeverity-%s \n", eventSev);
1138            exit(1);
1139         }
1140
1141         fault = evel_new_fault(eName, event_id, alarmCondn, 
1142                                specProb, priority, eSev, srcTyp,vfStat);
1143         if (fault != NULL)
1144         {
1145           fault_raised = 1;
1146
1147           struct timeval tv_now;
1148           gettimeofday(&tv_now, NULL);
1149           epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
1150           last_epoch = epoch_now;
1151
1152           fault_header = (EVENT_HEADER *)fault;
1153
1154           evel_fault_category_set(fault, eCategory);
1155           if (aInterface != NULL)
1156             evel_fault_interface_set(fault, aInterface);
1157   
1158           if (eType != NULL)
1159             evel_fault_type_set(fault, eType); 
1160     
1161           evel_start_epoch_set(&fault->header, epoch_now);
1162           evel_last_epoch_set(&fault->header, epoch_now);
1163           if(nfcCode != NULL)
1164             evel_nfcnamingcode_set(&fault->header, nfcCode);
1165           if(nfCode != NULL)
1166             evel_nfnamingcode_set(&fault->header, nfCode);
1167           evel_reporting_entity_name_set(&fault->header, reportEName);
1168           if(reportEId != NULL)
1169             evel_reporting_entity_id_set(&fault->header, reportEId);
1170           if(srcId != NULL )
1171           evel_source_id_set(&fault->header, srcId);
1172           if(srcName!= NULL )
1173             evel_source_name_set(&fault->header, srcName);
1174     
1175           evel_rc = evel_post_event(fault_header);
1176
1177           if(evel_rc == EVEL_SUCCESS) {
1178             printf("FAULT02::Fault event is correctly sent to the collector!\n");
1179           }
1180           else {
1181             printf("FAULT02::Post failed %d (%s)\n", evel_rc, evel_error_string());
1182         }
1183       }
1184       else {
1185         printf("FAULT02::New new fault failed (%s)\n", evel_error_string());
1186       }
1187     }
1188     /********************************************************************************
1189      * Put the condition to clear the fault here
1190      *******************************************************************************/
1191     else if ((atoi(commandArray[0].resultStr) == 0) && (fault_raised == 1)) 
1192     {
1193         printf("\nFAULT02:: Clearing fault\n");
1194         memset(event_id, 0, BUFSIZE);
1195         sprintf(event_id2, "%09d", (i+1));
1196         strcat(event_id, event_id1);
1197         strcat(event_id, event_id2);
1198
1199         ret = getIntToken(js, tokens, numToken, "tmp_faultCheckInterval", "tmp_faultInstance02", &flt_interval);
1200         if (ret != 0)
1201         {
1202            printf("FAULT02::The parameter tmp_faultCheckInterval is not defined, defaulted to 60 seconds\n");
1203            flt_interval = 60;
1204         }
1205
1206         ret = getStringToken(js, tokens, numToken, "reportingEntityName", "tmp_directParameters", reportEName, BUFSIZE);
1207         if (ret != 0)
1208         {
1209            printf("FAULT02::Missing mandatory parameters - reportingEntityName is not there in tmp_directParameters\n");
1210            printf("FAULT02::Defaulting reportingEntityName to hostname\n");
1211            strcpy(reportEName, hostname);
1212         }
1213         ret = getStringToken(js, tokens, numToken, "sourceName", "tmp_directParameters", srcName, BUFSIZE);
1214         if (ret != 0)
1215         {
1216            printf("FAULT02::Missing mandatory parameters - sourceName is not there in tmp_directParameters\n");
1217            printf("FAULT02::Defaulting sourceName to hostname\n");
1218            strcpy(srcName, hostname);
1219         }
1220
1221         ret = getStringToken(js, tokens, numToken, "priority", "tmp_directParameters", prio, BUFSIZE);
1222         if (ret != 0)
1223         {
1224            printf("FAULT02::Missing mandatory parameters - priority is not there in tmp_directParameters\nDefaulting priority to Low\n");
1225            strcpy(prio, "Medium");
1226         }
1227         priority = get_priority(prio);
1228         if(priority == -1)
1229         {
1230            printf("FAULT02::Fault priority value is not matching, prioirty-%s \n", prio);
1231            exit(1);
1232         }
1233
1234         ret = getStringTokenV2(js, tokens, numToken, "specificProblem", "tmp_alarmClearParameters", "tmp_faultInstance02", specProb, BUFSIZE);
1235         if (ret != 0)
1236         {
1237            printf("FAULT02::Missing mandatory parameters - specificProblem is not there in tmp_alarmClearParameters, exiting ...\n");
1238            exit(1);
1239         }
1240
1241         ret = getStringTokenV2(js, tokens, numToken, "alarmCondition", "tmp_alarmClearParameters", "tmp_faultInstance02", alarmCondn, BUFSIZE);
1242         if (ret != 0)
1243         {
1244            printf("FAULT02::Missing mandatory parameters - alarmCondition is not there in tmp_alarmClearParameters, exiting ...\n");
1245            exit(1);
1246         }
1247
1248         ret = getStringTokenV2(js, tokens, numToken, "eventSeverity", "tmp_alarmClearParameters", "tmp_faultInstance02", eventSev, BUFSIZE);
1249         if (ret != 0)
1250         {
1251            printf("FAULT02::Missing mandatory parameters - eventSeverity is not there in tmp_alarmClearParameters\n");
1252            printf("FAULT02::Defaulting eventSeverity to MAJOR\n");
1253            strcpy(eventSev, "NORMAL");
1254         }
1255         eSev = get_severity(eventSev);
1256         if(eSev == -1)
1257         {
1258            printf("FAULT02::Fault eventSeverity value is not matching, eventSeverity-%s \n", eventSev);
1259            exit(1);
1260         }
1261
1262         fault = evel_new_fault(eName, event_id, alarmCondn, 
1263                                specProb, priority, eSev, srcTyp,vfStat);
1264         if (fault != NULL)
1265         {
1266           fault_raised = 0;
1267
1268           struct timeval tv_now;
1269           gettimeofday(&tv_now, NULL);
1270           epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
1271
1272           fault_header = (EVENT_HEADER *)fault;
1273           evel_fault_category_set(fault, eCategory);
1274           if (aInterface != NULL)
1275             evel_fault_interface_set(fault, aInterface);
1276   
1277           if (eType != NULL)
1278             evel_fault_type_set(fault, eType); 
1279     
1280           evel_start_epoch_set(&fault->header, last_epoch);
1281           evel_last_epoch_set(&fault->header, epoch_now);
1282           last_epoch = 0;
1283
1284           if(nfcCode != NULL)
1285             evel_nfcnamingcode_set(&fault->header, nfcCode);
1286           if(nfCode != NULL)
1287             evel_nfnamingcode_set(&fault->header, nfCode);
1288           evel_reporting_entity_name_set(&fault->header, reportEName);
1289           if(reportEId != NULL)
1290             evel_reporting_entity_id_set(&fault->header, reportEId);
1291           if(srcId != NULL )
1292           evel_source_id_set(&fault->header, srcId);
1293           if(srcName!= NULL )
1294             evel_source_name_set(&fault->header, srcName);
1295     
1296           evel_rc = evel_post_event(fault_header);
1297
1298           if(evel_rc == EVEL_SUCCESS) {
1299             printf("FAULT02::Fault event is correctly sent to the collector!\n");
1300           }
1301           else {
1302             printf("FAULT02::Post failed %d (%s)\n", evel_rc, evel_error_string());
1303         }
1304       }
1305       else {
1306         printf("FAULT02::New fault failed (%s)\n", evel_error_string());
1307       }
1308
1309     }
1310
1311     sleep(flt_interval);
1312   }
1313
1314   /***************************************************************************/
1315   /* Terminate                                                               */
1316   /***************************************************************************/
1317   sleep(1);
1318 }
1319
1320 void *FaultThread03(void *faultInstanceTag)
1321 {
1322   sleep(4);
1323   printf("FAULT03::thread created\n");
1324   fflush(stdout);
1325   while (1)
1326   { sleep (100); }
1327 }
1328