aboutsummaryrefslogtreecommitdiffstats
path: root/vnfs/VES5.0/evel/evel-library/code/VESreporting_vAFX/afx_bgpsyslog.c
blob: 9cbb63c5dccb71e082b30be965d577fbb1cb73cd (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
 /*****************************************************************************//***
 * Copyright(c) <2017>, AT&T Intellectual Property.  All other rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:  This product includes
 *    software developed by the AT&T.
 * 4. Neither the name of AT&T nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/

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

#include "evel.h"
#include "afx_ves_reporter.h"

/* tracks syslog file */
static unsigned long long prevpos=0;
/* memory for word parsing */
static char ** bgp_res  = NULL;
/* Search list of words */
char* searchlist[MAX_SYSLOG_WORDS];
int numWords=0;

/**************************************************************************//**
 * Sends syslog message with parsed syslog info
 *
 * @param info      Pointer to the line
 * @param tag       Matched TAG field
 * @param sevrty    Severity of parsed line Emergency etc.
 *
 * @retvalue  int   
 *****************************************************************************/
void report_syslog( char *info, char *tag, char *sevrty )
{
  EVENT_SYSLOG * psyslog = NULL;
  EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
  char vesevid[128];

  sprintf(vesevid,"Syslog_vAfx_%s",oam_intfaddr);
  psyslog = evel_new_syslog("Syslog_vAfx", vesevid,
                            EVEL_SOURCE_VIRTUAL_MACHINE,
                           escape_json(info),
                           tag);
  if (psyslog != NULL)
  {
    evel_header_type_set((EVENT_HEADER *)psyslog, "applicationVnf");
    evel_nfnamingcode_set((EVENT_HEADER *)psyslog, "AFX");
    evel_nfcnamingcode_set((EVENT_HEADER *)psyslog, "AFX");
    evel_syslog_event_source_host_set(psyslog, "Virtual host");
    evel_syslog_facility_set(psyslog, EVEL_SYSLOG_FACILITY_LOCAL0);
    evel_syslog_proc_set(psyslog, tag);
    //evel_syslog_proc_id_set(psyslog, 1423);
    evel_syslog_version_set(psyslog, 1);
    //evel_syslog_addl_filter_set(psyslog, "Name1=Value1|Name2=Value2|Name3=Value3");
    evel_syslog_severity_set(psyslog, sevrty);
    //evel_syslog_sdid_set(psyslog, "ourSDID@32473");
    //evel_syslog_s_data_set(psyslog,info);
    evel_rc = evel_post_event((EVENT_HEADER *)psyslog);
    if (evel_rc != EVEL_SUCCESS)
    {
      EVEL_ERROR(" AFX: Post failed %d (%s)", evel_rc, evel_error_string());
    }
  }
  else
  {
    EVEL_ERROR("New Syslog failed");
  }
  EVEL_DEBUG("AFX: Processed full Syslog\n");

}

/**************************************************************************//**
 * Parses Syslog line for exabgp messages
 * Raises BGP Fault event for peer connection setup and teardown
 * Sends Syslog events for matching lines
 *
 * @param line      Pointer to line
 *
 * @retvalue  int   Returns if matching line
 *****************************************************************************/
int processLine(char *line)
{
    int n_spaces = 0, i;
    char *pos;
    char *p;
    int srchcondn = 0;
    int peer_conn = 0;
    int peer_reset = 0;
    char vesevid[128];
    char sevrty[128];
    char *line_bk = NULL;
    int status = 1; /** VFA Status is ACTIVE **/

   if ((pos=strchr(line, '\n')) != NULL)
           *pos = '\0';
   line_bk = strdup(line);

   if( !strstr(line,"EVEL") && !strstr(line,"commonEventHeader")  && strstr(line,"exabgp") )
   {
	if( strstr(line,"Connected to peer neighbor") ) peer_conn = 1;
	else if( strstr(line,"peer reset") ) peer_reset = 1;

       if( peer_conn || peer_reset )
       {
	n_spaces = 0;
	p    = strtok (line, " ");
	/* split string and append tokens to 'res' */

	while (p) {
	  bgp_res = realloc (bgp_res, sizeof (char*) * ++n_spaces);

	  if (bgp_res == NULL){
	    EVEL_DEBUG("Memory allocation failed");
	    exit (-1); /* memory allocation failed */
	  }

	  bgp_res[n_spaces-1] = p;

	  p = strtok (NULL, " ");
	}

	/* realloc one extra element for the last NULL */
	bgp_res = realloc (bgp_res, sizeof (char*) * (n_spaces+1));
	bgp_res[n_spaces] = 0;

	  if( n_spaces > 0 && (peer_conn || peer_reset) )
	  {
	    EVEL_DEBUG ("Exabgp %d %d %d\n", peer_conn, peer_reset, n_spaces);
	   for (i = 0; i < (n_spaces+1); ++i)
	   {
	    if( bgp_res[i] != NULL )EVEL_DEBUG ("res[%d] = %s\n", i, bgp_res[i]);
	   }
	   if( peer_conn == 1 && n_spaces >= 21 )
	    //if( !strcmp(bgp_res[21],"(out)") )
	   {
	     char buf[256];
	     EVEL_DEBUG("****************sending clear fault\n");
	     sprintf(vesevid,"Fault_vAfx_bgp_nbr_%s",oam_intfaddr);
	     sprintf(buf," %s %s %s %s %s %s %s %s %s %s %s ", bgp_res[10], bgp_res[11], bgp_res[12], bgp_res[13], bgp_res[14],bgp_res[15], bgp_res[16], bgp_res[17], bgp_res[18], bgp_res[19],  bgp_res[20]);
	     report_fault("Fault_vAfx_bgp_nbr", vesevid, EVEL_SEVERITY_NORMAL, "routing", "lo:0", "Bgp neighbor up alarm","bgp session to remote router is up",bgp_res[16],bgp_res[18],bgp_res[10], status);
	   }
	   if( peer_reset == 1 && n_spaces >= 21 )
	    //if( !strcmp(bgp_res[21],"Shutdown]") )
	   { char buf[256];
	     EVEL_DEBUG("****************sending fault\n");
	     sprintf(vesevid,"Fault_vAfx_bgp_nbr_%s",oam_intfaddr);
	     sprintf(buf," %s %s %s %s %s %s %s ", bgp_res[15], bgp_res[16], bgp_res[17], bgp_res[18], bgp_res[19],bgp_res[20], bgp_res[21]);
	       report_fault("Fault_vAfx_bgp_nbr", vesevid, EVEL_SEVERITY_MAJOR,"routing","lo:0","Bgp neighbor down alarm","bgp session to remote router is down",bgp_res[9],bgp_res[4],bgp_res[7], status);
	   }
	  }
	}

      }// end exabgp


   if( !strstr(line_bk,"EVEL") && !strstr(line,"commonEventHeader")  && numWords > 0 ){
     for (i=0;i<numWords;i++)
     {
        if( strstr(line_bk,searchlist[i]) != NULL &&
                    strstr(line_bk,"commonEventHeader") == NULL )
        {
          srchcondn = 1;
          pos = searchlist[i];
          break;
        }
        printf("srch:%s:%d:%d\n",searchlist[i],srchcondn,numWords);
     }

    if( srchcondn )
    {
     strcpy(sevrty,"Informational");
     if( strcasestr(line,"Emergency") != NULL ) strcpy(sevrty,"Emergency");
     else if( strcasestr(line_bk,"Alert") != NULL ) strcpy(sevrty,"Alert");
     else if( strcasestr(line_bk,"Critical") != NULL ) strcpy(sevrty,"Critical");
     else if( strcasestr(line_bk,"Error") != NULL ) strcpy(sevrty,"Error");
     else if( strcasestr(line_bk,"Warn") != NULL ) strcpy(sevrty,"Warning");
     else if( strcasestr(line_bk,"Notice") != NULL ) strcpy(sevrty,"Notice");
     else if( strcasestr(line_bk,"Debug") != NULL ) strcpy(sevrty,"Debug");
     report_syslog(line_bk,pos,sevrty);
    }
   }

  if(!srchcondn) 
    free(line_bk);
  return srchcondn;

}


/**************************************************************************//**
 * function to read last n lines from the file
 * at any point without reading the entire file
 *****************************************************************************/
void bgp_tail(FILE* in, int n)
{
    int count = 0;  // To count '\n' characters

    // unsigned long long pos (stores upto 2^64 – 1
    // chars) assuming that long long int takes 8
    // bytes
    unsigned long long pos;
    char str[2*BGPBUFSIZE] = {0};

    // Go to End of file
    if (fseek(in, 0, SEEK_END))
        perror("fseek() failed");
    else
    {
        // pos will contain no. of chars in
        // input file.
        pos = ftell(in);

        // search for '\n' characters
        while (pos>prevpos)
        {
            // Move 'pos' away from end of file.
            if (!fseek(in, --pos, SEEK_SET))
            {
                if (fgetc(in) == '\n')

                    // stop reading when n newlines
                    // is found
                    if (count++ == n)
                        break;
            }
            else
                perror("fseek() failed");
        }
        //printf("pos %d prevpos %d\n",pos,prevpos);

        // print last n lines
        prevpos = pos;
        //printf("Printing %d lines %d -\n", n,pos);
        while (fgets(str, sizeof(str), in))
        {
            //printf("%s\n", str);
            processLine(strdup(str));
            prevpos += strlen(str);
        }
    }
    //printf("\n\n");
}


/**************************************************************************//**
 * Thread function to Monitor Syslog for BGP connections 
 * Reads the filter words from AFX Filter input file
 * Starts tailing syslog for BGP messages and filter words
 *
 * @param threadarg  Thread arguments for startup message
 *
 *****************************************************************************/
void *BgpLoggingAfxThread(void *threadarg)
{
    FILE* fp;
    int i=0,j;
    char line[64];
    char *pos;
    FILE *file;
    int taskid, sum;
    char *hello_msg;
    struct thread_data *my_data;

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

   sleep(1);

   /* Opens filter file and reads word list */
   file = fopen(AFX_SYSLOG_FILE, "r");

    while(fgets(line, sizeof line, file)!=NULL) {
        //check to be sure reading correctly
        //printf("%s", line);
        if ((pos=strchr(line, '\n')) != NULL)
         *pos = '\0';
        remove_spaces(line);
        //add each filename into array of programs
        if( strlen(line) > 0 )
        {
           searchlist[numWords]=strdup(line);
           //count number of programs in file
           numWords++;
        }
    }
    fclose(file);

    //check to be sure going into array correctly
    for (int j=0 ; j<numWords; j++) {
        EVEL_DEBUG("Search %s\n", searchlist[j]);
    }


    // Open file in read mode
    fp = fopen("/var/log/syslog", "r");
    if (fp == NULL)
    {
        printf("Error while opening file");
        exit(EXIT_FAILURE);
    }

    // call tail() each time
    while(1)
    {
        // read last index lines from the file
        bgp_tail(fp, 8);

        // sleep for 3 seconds
        // note difference in timestamps in logs
        sleep(1);
    }

    /* close the file before ending program */
    fclose(fp);

    if( bgp_res != NULL ) free(bgp_res);

    return 0;
}