2 /*************************************************************************//**
4 * Main Agent which spins up monitoring threads
6 * Version 1.0: Gokul Singaraju gs244f Tech Mahindra Inc.
8 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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.
21 ****************************************************************************/
32 #include <sys/types.h>
34 #include "afx_ves_reporter.h"
36 int (*afxFunctions[NUM_THREADS]) (void *threadarg);
38 char *messages[NUM_THREADS];
39 char hostname[BUFSIZE];
40 char oam_intfaddr[BUFSIZE];
41 struct thread_data thread_data_array[NUM_THREADS];
44 void *HeartbeatAfxThread(void *threadarg)
48 struct thread_data *my_data;
52 my_data = (struct thread_data *) threadarg;
53 taskid = my_data->thread_id;
55 hello_msg = my_data->message;
56 printf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sum);
60 EVENT_HEADER * heartbeat = NULL;
61 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
63 /***************************************************************************/
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)
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)
77 EVEL_ERROR("Post failed %d (%s)", evel_rc, evel_error_string());
82 EVEL_ERROR("New Heartbeat failed");
84 printf(" Processed Heartbeat\n");
91 int checklist(char *modname, char **list, int numt)
96 if( !strcasecmp(list[i],modname) )
103 int start_threads(void)
106 pthread_t threads[NUM_THREADS];
107 int *taskids[NUM_THREADS];
109 char *modlist[NUM_THREADS];
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";
124 file = fopen(AFX_MODULES_FILE, "r"); /* should check the result */
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);
132 if ((pos=strchr(line, '\n')) != NULL)
134 if( modcounter >= NUM_THREADS )
136 EVEL_ERROR("AFX modules file %s has more modules than allowed\n",AFX_MODULES_FILE);
139 modlist[modcounter] = strdup(line);
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))
155 thread_data_array[t].thread_id = t;
156 thread_data_array[t].sum = sum;
157 thread_data_array[t].message = messages[t];
159 /* Initialize and set thread detached attribute */
160 pthread_attr_init(&attr);
161 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
163 printf("Creating thread %d\n", t);
164 rc = pthread_create(&threads[t], NULL, afxFunctions[t], (void *)
165 &thread_data_array[t]);
167 printf("ERROR; return code from pthread_create() is %d\n", rc);
171 else threads[t] = NULL;
174 //pthread_exit(NULL);
175 for(t=0;t<NUM_THREADS;t++) {
176 if( threads[t] != NULL )
177 pthread_join(threads[t],NULL);
182 void read_lpfile(char *fname, char **usn, char **pwd)
185 int i=0; // count how many lines are in the file
189 fp=fopen(fname, "r");
190 while (fgets(line, sizeof(line), fp)) {
191 //printf("%s", line);
192 if ((pos=strchr(line, '\n')) != NULL)
195 if( i == 1 && strlen(line) < 64 ) *usn = strdup(line);
196 if( i == 2 && strlen(line) < 64 ) *pwd = strdup(line);
202 int main(int argc, char** argv)
204 char* fqdn = argv[1];
205 int port = atoi(argv[2]);
208 char* lpfile = argv[3];
209 char* lpfile2 = NULL;
210 //char* usname = argv[3];
211 //char* passwd = argv[4];
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;
221 char const* const fileName = "afxintf.conf";
228 printf("\nvAFX VES Processing (VPP) measurement collection\n");
234 port2 = atoi(argv[7]);
239 if ( !(argc == 6 || argc == 9 ) || port < 0 || dbglvl < 0 )
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]);
246 if( stat(lpfile,&sb)<0 )
248 fprintf(stderr, "Error: Invalid Login password file %s \n", lpfile);
249 EVEL_ERROR("Error: Invalid Login password file %s \n", lpfile);
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 )
257 fprintf(stderr, "Error: Invalid credentials in file %s \n", lpfile);
258 EVEL_ERROR("Error: Invalid credentials in file %s \n", lpfile);
264 if( stat(lpfile2,&sb)<0 )
266 fprintf(stderr, "Error: Invalid Redundant collector Login password file %s \n", lpfile2);
267 EVEL_ERROR("Error: Invalid Login password file %s \n", lpfile2);
270 read_lpfile(lpfile2,&usname2,&passwd2);
271 if ( usname2 == NULL || passwd2 == NULL || strlen(usname2) < 5 || strlen(passwd2) < 5 )
273 fprintf(stderr, "Error: Invalid credentials in file %s \n", lpfile2);
274 EVEL_ERROR("Error: Invalid credentials in file %s \n", lpfile2);
277 //fprintf(stderr, "Login:%s:\n", usname2);
278 //fprintf(stderr, "Password:%s:\n", passwd2);
282 gethostname(hostname, BUFSIZE);
284 strcpy(oam_intf,OAM_INTERFACE);
285 sprintf(oam_intfaddr,"%s",get_oam_intfaddr(oam_intf));
287 /**************************************************************************/
289 /**************************************************************************/
294 rc = evel_initialize(fqdn, /* FQDN */
296 NULL, /* backup fqdn */
298 NULL, /* optional path */
299 NULL, /* optional topic */
300 1000, /* Ring buf size */
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",*/
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 */
315 dbglvl); /* Verbosity */
316 if(rc != EVEL_SUCCESS){
317 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
322 printf("\nInitialization completed\n");
326 rc = evel_initialize(fqdn, /* FQDN */
328 fqdn2, /* backup fqdn */
329 port2, /* backup port */
330 NULL, /* optional path */
331 NULL, /* optional topic */
332 1000, /* RingB size */
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",*/
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 */
347 dbglvl); /* Verbosity */
348 if(rc != EVEL_SUCCESS){
349 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
355 printf("\nInitialization completed\n");
359 } while( rc != EVEL_SUCCESS);
361 afxFunctions[0] = HeartbeatAfxThread;
362 afxFunctions[1] = LinkMonitorAfxThread;
363 afxFunctions[2] = MeasureAfxThread;
364 afxFunctions[3] = ServiceMonitorAfxThread;
365 afxFunctions[4] = BgpLoggingAfxThread;
370 printf("Terminated\n");