VES5.4.1 EVEL Library enhancements
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / VESreporting_vAFX / afx_ves_reporter.c
1
2 /*************************************************************************//**
3  *
4  * Main Agent which spins up monitoring threads
5  *
6  *   Version 1.0:  Gokul Singaraju   gs244f   Tech Mahindra Inc.
7  *
8  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *        http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and 
19  * limitations under the License.
20  *
21  ****************************************************************************/
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <unistd.h>
29 #include <pthread.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include "evel.h"
34 #include "afx_ves_reporter.h"
35
36 int (*afxFunctions[NUM_THREADS]) (void *threadarg);
37
38 char *messages[NUM_THREADS];
39 char hostname[BUFSIZE];
40 char oam_intfaddr[BUFSIZE];
41 struct thread_data thread_data_array[NUM_THREADS];
42
43
44 void *HeartbeatAfxThread(void *threadarg)
45 {
46    int taskid, sum;
47    char *hello_msg;
48    struct thread_data *my_data;
49    char hrtbtevid[256];
50
51    sleep(1);
52    my_data = (struct thread_data *) threadarg;
53    taskid = my_data->thread_id;
54    sum = my_data->sum;
55    hello_msg = my_data->message;
56    printf("Thread %d: %s  Sum=%d\n", taskid, hello_msg, sum);
57
58 while(1)
59 {
60   EVENT_HEADER * heartbeat = NULL;
61   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
62
63   /***************************************************************************/
64   /* Heartbeat                                                               */
65   /***************************************************************************/
66   sprintf(hrtbtevid,"Heartbeat_vAfx_%s",oam_intfaddr);
67   heartbeat = evel_new_heartbeat_nameid("Heartbeat_vAfx",hrtbtevid);
68   //heartbeat = evel_new_heartbeat();
69   if (heartbeat != NULL)
70   {
71     evel_header_type_set(heartbeat, "applicationVnf");
72     evel_nfcnamingcode_set(heartbeat, "AFX");
73     evel_nfnamingcode_set(heartbeat, "AFX");
74     evel_rc = evel_post_event(heartbeat);
75     if (evel_rc != EVEL_SUCCESS)
76     {
77       EVEL_ERROR("Post failed %d (%s)", evel_rc, evel_error_string());
78     }
79   }
80   else
81   {
82     EVEL_ERROR("New Heartbeat failed");
83   }
84   printf("   Processed Heartbeat\n");
85   sleep(15);
86 }
87
88    pthread_exit(NULL);
89 }
90
91 int checklist(char *modname, char **list, int numt)
92 {
93  int i;
94  for(i=0;i<numt;i++)
95  {
96    if( !strcasecmp(list[i],modname) )
97         return 1;
98  }
99  return 0;
100 }
101
102
103 int start_threads(void)
104 {
105
106 pthread_t threads[NUM_THREADS];
107 int *taskids[NUM_THREADS];
108 int rc, t, sum;
109 char *modlist[NUM_THREADS];
110 int modcounter = 0;
111 char line[128];
112 char *pos;
113 FILE *file;
114
115 pthread_attr_t attr;
116
117 sum=0;
118 messages[0] = "Heartbeat started!";
119 messages[1] = "Link monitoring started";
120 messages[2] = "AFX Measurement started!";
121 messages[3] = "Service Monitoring started";
122 messages[4] = "BGP Monitoring started";
123
124     file = fopen(AFX_MODULES_FILE, "r"); /* should check the result */
125
126     if( file != NULL ){
127       while ( file != NULL && fgets(line, sizeof(line), file)) {
128         /* note that fgets don't strip the terminating \n, checking its
129            presence would allow to handle lines longer that sizeof(line) */
130         //printf("%s", line);
131         remove_spaces(line);
132         if ((pos=strchr(line, '\n')) != NULL)
133            *pos = '\0';
134         if( modcounter >= NUM_THREADS )
135         {
136            EVEL_ERROR("AFX modules file %s has more modules than allowed\n",AFX_MODULES_FILE);
137            exit(1);
138         }
139         modlist[modcounter] = strdup(line);
140         modcounter++;
141      }
142      fclose(file);
143    }
144
145
146 for(t=0;t<NUM_THREADS;t++) {
147   if( file == NULL || (t == 0 && checklist("HeartBeat",modlist, modcounter)) ||
148       (t == 1 && checklist("LinkMonitor",modlist, modcounter)) ||
149       (t == 2 && checklist("ScalingMeasurements",modlist, modcounter)) ||
150       (t == 3 && checklist("ServiceMonitor",modlist, modcounter)) ||
151       (t == 4 && checklist("SyslogBgp",modlist, modcounter))
152      ) 
153   {
154   sum = sum + t;
155   thread_data_array[t].thread_id = t;
156   thread_data_array[t].sum = sum;
157   thread_data_array[t].message = messages[t];
158
159   /* Initialize and set thread detached attribute */
160   pthread_attr_init(&attr);
161   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
162
163   printf("Creating thread %d\n", t);
164   rc = pthread_create(&threads[t], NULL, afxFunctions[t], (void *) 
165        &thread_data_array[t]);
166   if (rc) {
167     printf("ERROR; return code from pthread_create() is %d\n", rc);
168     exit(-1);
169     }
170   }
171   else threads[t] = NULL;
172 }
173
174   //pthread_exit(NULL);
175   for(t=0;t<NUM_THREADS;t++) {
176      if( threads[t] != NULL )
177        pthread_join(threads[t],NULL);
178   }
179   return sum;
180 }
181
182 void read_lpfile(char *fname, char **usn, char **pwd)
183 {
184     FILE *fp;
185     int i=0;   // count how many lines are in the file
186     char line[256];
187     char *pos;
188
189     fp=fopen(fname, "r");
190     while (fgets(line, sizeof(line), fp)) {
191       //printf("%s", line);
192       if ((pos=strchr(line, '\n')) != NULL)
193            *pos = '\0';
194       i++;
195       if( i == 1 && strlen(line) < 64 ) *usn = strdup(line);
196       if( i == 2 && strlen(line) < 64 ) *pwd = strdup(line);
197     }
198     fclose(fp);
199 }
200
201
202 int main(int argc, char** argv)
203 {
204   char* fqdn = argv[1];
205   int port = atoi(argv[2]);
206   char *fqdn2 = NULL;
207   int port2;
208   char* lpfile = argv[3];
209   char* lpfile2 = NULL;
210   //char* usname = argv[3];
211   //char* passwd = argv[4];
212   char* usname = NULL;
213   char* passwd = NULL;
214   char* usname2 = NULL;
215   char* passwd2 = NULL;
216   int secty = atoi(argv[4]);
217   int dbglvl = atoi(argv[5]);
218   //int hrtbtintval = atoi(argv[6]);
219   int rc = EVEL_SUCCESS;
220   char oam_intf[64];
221   char const* const fileName = "afxintf.conf";
222   char line[128];
223   FILE *file=NULL;
224   char *pos;
225   struct stat sb;
226
227
228   printf("\nvAFX VES Processing (VPP) measurement collection\n");
229   fflush(stdout);
230
231 if( argc == 9 )
232 {
233   fqdn2 = argv[6];
234   port2 = atoi(argv[7]);
235   lpfile2 = argv[8];
236 }
237
238
239   if ( !(argc == 6 || argc == 9 ) || port < 0 || dbglvl < 0 )
240   {
241     fprintf(stderr, "Usage: %s <DCAE FQDN>|<IP address> <port> <credential file> <debug level> \n", argv[0]);
242     fprintf(stderr, "Or: %s <DCAE FQDN>|<IP address> <port> <credential file> <debug level> <DCAE FQDN2>|<IP address2> <port2> <credential file2> \n", argv[0]);
243     exit(-1);
244   }
245
246   if( stat(lpfile,&sb)<0  )
247   {
248     fprintf(stderr, "Error: Invalid Login password file %s \n", lpfile);
249     EVEL_ERROR("Error: Invalid Login password file %s \n", lpfile);
250     exit(-1);
251   }
252   read_lpfile(lpfile,&usname,&passwd);
253   //fprintf(stderr, "Login:%s:\n", usname);
254   //fprintf(stderr, "Password:%s:\n", passwd);
255   if ( usname == NULL || passwd == NULL || strlen(usname) < 5 || strlen(passwd) < 5 )
256   {
257     fprintf(stderr, "Error: Invalid credentials in file %s \n", lpfile);
258     EVEL_ERROR("Error: Invalid credentials in file %s \n", lpfile);
259     exit(-1);
260   }
261
262 if( argc == 9 )
263 {
264   if( stat(lpfile2,&sb)<0  )
265   {
266     fprintf(stderr, "Error: Invalid Redundant collector Login password file %s \n", lpfile2);
267     EVEL_ERROR("Error: Invalid Login password file %s \n", lpfile2);
268     exit(-1);
269   }
270   read_lpfile(lpfile2,&usname2,&passwd2);
271   if ( usname2 == NULL || passwd2 == NULL || strlen(usname2) < 5 || strlen(passwd2) < 5 )
272   {
273     fprintf(stderr, "Error: Invalid credentials in file %s \n", lpfile2);
274     EVEL_ERROR("Error: Invalid credentials in file %s \n", lpfile2);
275     exit(-1);
276   }
277   //fprintf(stderr, "Login:%s:\n", usname2);
278   //fprintf(stderr, "Password:%s:\n", passwd2);
279 }
280
281   srand(time(NULL));
282   gethostname(hostname, BUFSIZE);
283
284   strcpy(oam_intf,OAM_INTERFACE);
285   sprintf(oam_intfaddr,"%s",get_oam_intfaddr(oam_intf));
286
287   /**************************************************************************/
288   /* Initialize                                                             */
289   /**************************************************************************/
290   do {
291
292 if( argc == 6 )
293 {
294   rc = evel_initialize(fqdn,                       /* FQDN                  */
295                      port,                         /* Port                  */
296                      NULL,                         /* backup fqdn         */
297                      0,                            /* backup port         */
298                      NULL,                         /* optional path         */
299                      NULL,                         /* optional topic        */
300                      1000,                         /* Ring buf size         */
301                      secty,                            /* HTTPS?                */
302                      NULL, /*"/home/gs244f/sslcerts/testclient.crt",*/
303                      NULL, /*"/home/gs244f/sslcerts/testclient.key",*/
304                      NULL, /*"/etc/pki/ca-trust/source/ca-bundle.legacy.crt",*/
305                      NULL, /*"/home/gs244f/sslcerts/www.testsite.com.crt",*/
306                      0, 0,
307                      usname,                      /* Username              */
308                      passwd,                      /* Password              */
309                      NULL,                        /* Username2             */
310                      NULL,                        /* Password2              */
311                      NULL,                        /* source ip             */
312                      NULL,                        /* backup ip              */
313                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
314                      "vAFX",                    /* Role                  */
315                      dbglvl);                /* Verbosity             */
316    if(rc != EVEL_SUCCESS){
317     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
318     exit(-1);
319    }
320    else
321    {
322     printf("\nInitialization completed\n");
323    }
324 } else {
325
326   rc = evel_initialize(fqdn,          /* FQDN           */
327                      port,            /* Port           */
328                      fqdn2,           /* backup fqdn    */
329                      port2,           /* backup port    */
330                      NULL,            /* optional path  */
331                      NULL,            /* optional topic */
332                      1000,            /* RingB size     */
333                      secty,           /* HTTPS? */
334                      NULL, /*"/home/gs244f/sslcerts/testclient.crt",*/
335                      NULL, /*"/home/gs244f/sslcerts/testclient.key",*/
336                      NULL, /*"/etc/pki/ca-trust/source/ca-bundle.legacy.crt",*/
337                      NULL, /*"/home/gs244f/sslcerts/www.testsite.com.crt",*/
338                      0, 0,
339                      usname,          /* Username              */
340                      passwd,          /* Password              */
341                      usname2,         /* Username2             */
342                      passwd2,         /* Password2             */
343                      NULL,            /* source ip             */
344                      NULL,            /* backup ip             */
345                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type   */
346                      "vAFX",          /* Role                  */
347                      dbglvl);         /* Verbosity             */
348    if(rc != EVEL_SUCCESS){
349     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
350     evel_terminate();
351     exit(-1);
352    }
353    else
354    {
355     printf("\nInitialization completed\n");
356    }
357 }
358
359   } while( rc != EVEL_SUCCESS);
360
361   afxFunctions[0] = HeartbeatAfxThread;
362   afxFunctions[1] = LinkMonitorAfxThread;
363   afxFunctions[2] = MeasureAfxThread;
364   afxFunctions[3] = ServiceMonitorAfxThread;
365   afxFunctions[4] = BgpLoggingAfxThread;
366
367   start_threads();
368
369   evel_terminate();
370   printf("Terminated\n");
371 }
372