aboutsummaryrefslogtreecommitdiffstats
path: root/vnfs/VES5.0/evel/evel-library/code/VESreporting_vAFX/afx_ves_reporter.c
blob: 45c842dcb3c5e4a6d65dfa50ad1254dbd750e6b5 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*************************************************************************//**
 *
 * Main Agent which spins up monitoring threads
 *
 *   Version 1.0:  Gokul Singaraju   gs244f   Tech Mahindra Inc.
 *
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include "evel.h"
#include "afx_ves_reporter.h"

int (*afxFunctions[NUM_THREADS]) (void *threadarg);

char *messages[NUM_THREADS];
char hostname[BUFSIZE];
char oam_intfaddr[BUFSIZE];
struct thread_data thread_data_array[NUM_THREADS];


void *HeartbeatAfxThread(void *threadarg)
{
   int taskid, sum;
   char *hello_msg;
   struct thread_data *my_data;
   char hrtbtevid[256];

   sleep(1);
   my_data = (struct thread_data *) threadarg;
   taskid = my_data->thread_id;
   sum = my_data->sum;
   hello_msg = my_data->message;
   printf("Thread %d: %s  Sum=%d\n", taskid, hello_msg, sum);

while(1)
{
  EVENT_HEADER * heartbeat = NULL;
  EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;

  /***************************************************************************/
  /* Heartbeat                                                               */
  /***************************************************************************/
  sprintf(hrtbtevid,"Heartbeat_vAfx_%s",oam_intfaddr);
  heartbeat = evel_new_heartbeat_nameid("Heartbeat_vAfx",hrtbtevid);
  //heartbeat = evel_new_heartbeat();
  if (heartbeat != NULL)
  {
    evel_header_type_set(heartbeat, "applicationVnf");
    evel_nfcnamingcode_set(heartbeat, "AFX");
    evel_nfnamingcode_set(heartbeat, "AFX");
    evel_rc = evel_post_event(heartbeat);
    if (evel_rc != EVEL_SUCCESS)
    {
      EVEL_ERROR("Post failed %d (%s)", evel_rc, evel_error_string());
    }
  }
  else
  {
    EVEL_ERROR("New Heartbeat failed");
  }
  printf("   Processed Heartbeat\n");
  sleep(15);
}

   pthread_exit(NULL);
}

int checklist(char *modname, char **list, int numt)
{
 int i;
 for(i=0;i<numt;i++)
 {
   if( !strcasecmp(list[i],modname) )
	return 1;
 }
 return 0;
}


int start_threads(void)
{

pthread_t threads[NUM_THREADS];
int *taskids[NUM_THREADS];
int rc, t, sum;
char *modlist[NUM_THREADS];
int modcounter = 0;
char line[128];
char *pos;
FILE *file;

pthread_attr_t attr;

sum=0;
messages[0] = "Heartbeat started!";
messages[1] = "Link monitoring started";
messages[2] = "AFX Measurement started!";
messages[3] = "Service Monitoring started";
messages[4] = "BGP Monitoring started";

    file = fopen(AFX_MODULES_FILE, "r"); /* should check the result */

    if( file != NULL ){
      while ( file != NULL && fgets(line, sizeof(line), file)) {
        /* note that fgets don't strip the terminating \n, checking its
           presence would allow to handle lines longer that sizeof(line) */
        //printf("%s", line);
        remove_spaces(line);
        if ((pos=strchr(line, '\n')) != NULL)
           *pos = '\0';
        if( modcounter >= NUM_THREADS )
        {
	   EVEL_ERROR("AFX modules file %s has more modules than allowed\n",AFX_MODULES_FILE);
	   exit(1);
	}
        modlist[modcounter] = strdup(line);
        modcounter++;
     }
     fclose(file);
   }


for(t=0;t<NUM_THREADS;t++) {
  if( file == NULL || (t == 0 && checklist("HeartBeat",modlist, modcounter)) ||
      (t == 1 && checklist("LinkMonitor",modlist, modcounter)) ||
      (t == 2 && checklist("ScalingMeasurements",modlist, modcounter)) ||
      (t == 3 && checklist("ServiceMonitor",modlist, modcounter)) ||
      (t == 4 && checklist("SyslogBgp",modlist, modcounter))
     ) 
  {
  sum = sum + t;
  thread_data_array[t].thread_id = t;
  thread_data_array[t].sum = sum;
  thread_data_array[t].message = messages[t];

  /* Initialize and set thread detached attribute */
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

  printf("Creating thread %d\n", t);
  rc = pthread_create(&threads[t], NULL, afxFunctions[t], (void *) 
       &thread_data_array[t]);
  if (rc) {
    printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
    }
  }
  else threads[t] = NULL;
}

  //pthread_exit(NULL);
  for(t=0;t<NUM_THREADS;t++) {
     if( threads[t] != NULL )
       pthread_join(threads[t],NULL);
  }
  return sum;
}

void read_lpfile(char *fname, char **usn, char **pwd)
{
    FILE *fp;
    int i=0;   // count how many lines are in the file
    char line[256];
    char *pos;

    fp=fopen(fname, "r");
    while (fgets(line, sizeof(line), fp)) {
      //printf("%s", line);
      if ((pos=strchr(line, '\n')) != NULL)
           *pos = '\0';
      i++;
      if( i == 1 && strlen(line) < 64 ) *usn = strdup(line);
      if( i == 2 && strlen(line) < 64 ) *pwd = strdup(line);
    }
    fclose(fp);
}


int main(int argc, char** argv)
{
  char* fqdn = argv[1];
  int port = atoi(argv[2]);
  char *fqdn2 = NULL;
  int port2;
  char* lpfile = argv[3];
  char* lpfile2 = NULL;
  //char* usname = argv[3];
  //char* passwd = argv[4];
  char* usname = NULL;
  char* passwd = NULL;
  char* usname2 = NULL;
  char* passwd2 = NULL;
  int secty = atoi(argv[4]);
  int dbglvl = atoi(argv[5]);
  //int hrtbtintval = atoi(argv[6]);
  int rc = EVEL_SUCCESS;
  char oam_intf[64];
  char const* const fileName = "afxintf.conf";
  char line[128];
  FILE *file=NULL;
  char *pos;
  struct stat sb;


  printf("\nvAFX VES Processing (VPP) measurement collection\n");
  fflush(stdout);

if( argc == 9 )
{
  fqdn2 = argv[6];
  port2 = atoi(argv[7]);
  lpfile2 = argv[8];
}


  if ( !(argc == 6 || argc == 9 ) || port < 0 || dbglvl < 0 )
  {
    fprintf(stderr, "Usage: %s <DCAE FQDN>|<IP address> <port> <credential file> <debug level> \n", argv[0]);
    fprintf(stderr, "Or: %s <DCAE FQDN>|<IP address> <port> <credential file> <debug level> <DCAE FQDN2>|<IP address2> <port2> <credential file2> \n", argv[0]);
    exit(-1);
  }

  if( stat(lpfile,&sb)<0  )
  {
    fprintf(stderr, "Error: Invalid Login password file %s \n", lpfile);
    EVEL_ERROR("Error: Invalid Login password file %s \n", lpfile);
    exit(-1);
  }
  read_lpfile(lpfile,&usname,&passwd);
  //fprintf(stderr, "Login:%s:\n", usname);
  //fprintf(stderr, "Password:%s:\n", passwd);
  if ( usname == NULL || passwd == NULL || strlen(usname) < 5 || strlen(passwd) < 5 )
  {
    fprintf(stderr, "Error: Invalid credentials in file %s \n", lpfile);
    EVEL_ERROR("Error: Invalid credentials in file %s \n", lpfile);
    exit(-1);
  }

if( argc == 9 )
{
  if( stat(lpfile2,&sb)<0  )
  {
    fprintf(stderr, "Error: Invalid Redundant collector Login password file %s \n", lpfile2);
    EVEL_ERROR("Error: Invalid Login password file %s \n", lpfile2);
    exit(-1);
  }
  read_lpfile(lpfile2,&usname2,&passwd2);
  if ( usname2 == NULL || passwd2 == NULL || strlen(usname2) < 5 || strlen(passwd2) < 5 )
  {
    fprintf(stderr, "Error: Invalid credentials in file %s \n", lpfile2);
    EVEL_ERROR("Error: Invalid credentials in file %s \n", lpfile2);
    exit(-1);
  }
  //fprintf(stderr, "Login:%s:\n", usname2);
  //fprintf(stderr, "Password:%s:\n", passwd2);
}

  srand(time(NULL));
  gethostname(hostname, BUFSIZE);

  strcpy(oam_intf,OAM_INTERFACE);
  sprintf(oam_intfaddr,"%s",get_oam_intfaddr(oam_intf));

  /**************************************************************************/
  /* Initialize                                                             */
  /**************************************************************************/
  do {

if( argc == 6 )
{
  rc = evel_initialize(fqdn,                       /* FQDN                  */
                     port,                         /* Port                  */
                     NULL,                         /* backup fqdn         */
                     0,                            /* backup port         */
                     NULL,                         /* optional path         */
                     NULL,                         /* optional topic        */
                     1000,                         /* Ring buf size         */
                     secty,                            /* HTTPS?                */
                     NULL, /*"/home/gs244f/sslcerts/testclient.crt",*/
                     NULL, /*"/home/gs244f/sslcerts/testclient.key",*/
                     NULL, /*"/etc/pki/ca-trust/source/ca-bundle.legacy.crt",*/
                     NULL, /*"/home/gs244f/sslcerts/www.testsite.com.crt",*/
                     0, 0,
                     usname,                      /* Username              */
                     passwd,                      /* Password              */
                     NULL,                        /* Username2             */
                     NULL,                        /* Password2              */
                     NULL,                        /* source ip             */
                     NULL,                        /* backup ip              */
                     EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
                     "vAFX",                    /* Role                  */
                     dbglvl);                /* Verbosity             */
   if(rc != EVEL_SUCCESS){
    fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
    exit(-1);
   }
   else
   {
    printf("\nInitialization completed\n");
   }
} else {

  rc = evel_initialize(fqdn,          /* FQDN           */
                     port,            /* Port           */
                     fqdn2,           /* backup fqdn    */
                     port2,           /* backup port    */
                     NULL,            /* optional path  */
                     NULL,            /* optional topic */
                     1000,            /* RingB size     */
                     secty,           /* HTTPS? */
                     NULL, /*"/home/gs244f/sslcerts/testclient.crt",*/
                     NULL, /*"/home/gs244f/sslcerts/testclient.key",*/
                     NULL, /*"/etc/pki/ca-trust/source/ca-bundle.legacy.crt",*/
                     NULL, /*"/home/gs244f/sslcerts/www.testsite.com.crt",*/
                     0, 0,
                     usname,          /* Username              */
                     passwd,          /* Password              */
                     usname2,         /* Username2             */
                     passwd2,         /* Password2             */
                     NULL,            /* source ip             */
                     NULL,            /* backup ip             */
                     EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type   */
                     "vAFX",          /* Role                  */
                     dbglvl);         /* Verbosity             */
   if(rc != EVEL_SUCCESS){
    fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
    evel_terminate();
    exit(-1);
   }
   else
   {
    printf("\nInitialization completed\n");
   }
}

  } while( rc != EVEL_SUCCESS);

  afxFunctions[0] = HeartbeatAfxThread;
  afxFunctions[1] = LinkMonitorAfxThread;
  afxFunctions[2] = MeasureAfxThread;
  afxFunctions[3] = ServiceMonitorAfxThread;
  afxFunctions[4] = BgpLoggingAfxThread;

  start_threads();

  evel_terminate();
  printf("Terminated\n");
}