VES EVEL Library VES 5.4.1 enhancements
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / VESreporting_syslog / ves_syslog_reporter.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2018 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
33 void *SyslogThread(void *threadarg);
34
35 unsigned long long epoch_start = 0;
36
37 void report_syslog(char * js, jsmntok_t * tokens, int numToken, char * syslog_tag, char * eName, int srcTyp, char * syslog_msg)
38 {
39   EVENT_HEARTBEAT_FIELD * event = NULL;
40   EVENT_HEADER* syslog_header = NULL;
41   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
42   char stringVal[BUFSIZE];
43
44   char event_id1[10] = "syslog";
45   char event_id2[15] = {0};
46   int event_id3 = 0;
47   char event_id[BUFSIZE] = {0};
48
49   int ret = 0;
50   /***************************************************************************/
51   /* Syslog                                                               */
52   /***************************************************************************/
53   memset(event_id, 0, BUFSIZE);
54   memset(event_id2, 0, 15);
55   sprintf(event_id2, "%09d", event_id3++);
56   strcat(event_id, event_id1);
57   strcat(event_id, event_id2);
58
59      
60   event = evel_new_syslog(eName, event_id, srcTyp, syslog_msg, syslog_tag);
61   
62   if (event != NULL)
63   {
64        syslog_header = (EVENT_HEADER *)event;
65
66        ret = getStringToken(js, tokens, numToken, "syslogProc", "tmp_directParameters", stringVal, BUFSIZE);
67        if (ret == 0)
68           evel_syslog_proc_set(event, stringVal);
69        evel_syslog_facility_set(event, EVEL_SYSLOG_FACILITY_LOCAL0);
70        ret = getStringToken(js, tokens, numToken, "eventType", "tmp_directParameters", stringVal, BUFSIZE);
71        if (ret == 0)
72          evel_header_type_set(&event->header, stringVal);
73
74        struct timeval tv_now;
75        gettimeofday(&tv_now, NULL);
76        unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
77
78        evel_start_epoch_set(&event->header, epoch_start);
79        evel_last_epoch_set(&event->header, epoch_now);
80        epoch_start = epoch_now;
81
82        ret = getStringToken(js, tokens, numToken, "nfcNamingCode", "tmp_directParameters", stringVal, BUFSIZE);
83        if (ret == 0)
84          evel_nfcnamingcode_set(&event->header, stringVal);
85  
86        ret = getStringToken(js, tokens, numToken, "nfNamingCode", "tmp_directParameters", stringVal, BUFSIZE);
87        if (ret == 0)
88          evel_nfnamingcode_set(&event->header, stringVal);
89
90        ret = getStringToken(js, tokens, numToken, "reportingEntityName", "tmp_directParameters", stringVal, BUFSIZE);
91        if (ret == 0)
92          evel_reporting_entity_name_set(&event->header, stringVal);
93        else 
94        {
95           printf("Missing mandatory parameters - reportingEntityName is not there in tmp_directParameters\n");
96           printf("Defaulting reportingEntityName to hostname\n");
97        }
98
99        ret = getStringToken(js, tokens, numToken, "reportingEntityId", "tmp_directParameters", stringVal, BUFSIZE);
100        if (ret == 0)
101          evel_reporting_entity_id_set(&event->header, stringVal);
102
103        ret = getStringToken(js, tokens, numToken, "sourceId", "tmp_directParameters", stringVal, BUFSIZE);
104        if (ret == 0)
105          evel_source_id_set(&event->header, stringVal);
106
107        ret = getStringToken(js, tokens, numToken, "sourceName", "tmp_directParameters", stringVal, BUFSIZE);
108        if (ret == 0)
109          evel_source_name_set(&event->header, stringVal);
110        else 
111        {
112           printf("Missing mandatory parameters - sourceName is not there in tmp_directParameters\n");
113           printf("Defaulting sourceName to hostname\n");
114        }
115
116        evel_rc = evel_post_event(syslog_header);
117        if (evel_rc != EVEL_SUCCESS)
118        {
119          EVEL_ERROR("Post failed %d (%s)", evel_rc, evel_error_string());
120        }
121     }
122     else
123     {
124       EVEL_ERROR("New Syslog failed");
125     }
126     printf("   Processed Syslog\n");
127 }
128
129 int get_source(char * inStr)
130 {
131    int result = -1;
132
133    if(strcmp(inStr, "other") == 0)
134      result = EVEL_SOURCE_OTHER;
135    else if(strcmp(inStr, "router") == 0)
136      result = EVEL_SOURCE_ROUTER;
137    else if(strcmp(inStr, "switch") == 0)
138      result = EVEL_SOURCE_SWITCH;
139    else if(strcmp(inStr, "host") == 0)
140      result = EVEL_SOURCE_HOST;
141    else if(strcmp(inStr, "card") == 0)
142      result = EVEL_SOURCE_CARD;
143    else if(strcmp(inStr, "port") == 0)
144      result = EVEL_SOURCE_PORT;
145    else if(strcmp(inStr, "slotThreshold") == 0)
146      result = EVEL_SOURCE_SLOT_THRESHOLD;
147    else if(strcmp(inStr, "portThreshold") == 0)
148      result = EVEL_SOURCE_PORT_THRESHOLD;
149    else if(strcmp(inStr, "virtualMachine") == 0)
150      result = EVEL_SOURCE_VIRTUAL_MACHINE;
151    else if(strcmp(inStr, "virtualNetworkFunction") == 0)
152      result = EVEL_SOURCE_VIRTUAL_NETWORK_FUNCTION;
153
154   return result;
155 }
156
157
158 int main(int argc, char** argv)
159 {
160   char* fqdn = argv[1];
161   int port = atoi(argv[2]);
162   int i=0;
163   int rc;
164   pthread_attr_t attr;
165   pthread_t syslog_thread;
166   char* fqdn2 = NULL;
167   int port2 = 0;
168
169   if(argc == 5)
170   {
171      fqdn2 = argv[3];
172      port2 = atoi(argv[4]);
173   }
174
175   if (!((argc == 3) || (argc == 5)))
176   {
177     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <FQDN>|<IP address> <port>  \n", argv[0]);
178     fprintf(stderr, "OR\n");
179     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> \n", argv[0]);
180     exit(-1);
181   }
182
183   /**************************************************************************/
184   /* Initialize                                                             */
185   /**************************************************************************/
186   if(evel_initialize(fqdn,                         /* FQDN                  */
187                      port,                         /* Port                  */
188                      fqdn2,                        /* Backup FQDN           */
189                      port2,                        /* Backup port           */
190                      NULL,                         /* optional path         */
191                      NULL,                         /* optional topic        */
192                      100,                          /* Ring Buffer size      */
193                      0,                            /* HTTPS?                */
194                      NULL,                         /* cert file             */
195                      NULL,                         /* key  file             */
196                      NULL,                         /* ca   info             */
197                      NULL,                         /* ca   file             */
198                      0,                            /* verify peer           */
199                      0,                            /* verify host           */
200                      "sample1",                    /* Username              */
201                      "sample1",                    /* Password              */
202                      "sample1",                    /* Username2             */
203                      "sample1",                    /* Password2             */
204                      NULL,                         /* Source ip             */
205                      NULL,                         /* Source ip2            */
206                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
207                      "vSyslog",                    /* Role                  */
208                      1))                           /* Verbosity             */
209   {
210     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
211     exit(-1);
212   }
213   else
214   {
215     printf("\nInitialization completed\n");
216   }
217
218   /* Initialize and set thread detached attribute */
219   pthread_attr_init(&attr);
220   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
221
222   printf("Main:Creating thread \n");
223   rc = pthread_create(&syslog_thread, NULL, SyslogThread, &i);
224   if (rc)
225   {
226     printf("ERROR; return code from pthread_create() is %d\n", rc);
227     exit(-1);
228   }
229   printf("Main:Created Syslog thread \n");
230
231   pthread_join(syslog_thread, NULL);
232
233   evel_terminate();
234   printf("Terminated\n");
235   return 0;
236 }
237
238 void *SyslogThread(void *threadarg)
239 {
240
241   int numToken;
242   jsmntok_t tokens[MAX_TOKENS];
243   char js[MAX_BUFFER_SIZE]; 
244   FILE * fp;
245   jsmn_parser p;
246   char ch[BUFSIZE];
247   int ret = 0;
248   char eName[BUFSIZE];
249   char eventSrcTyp[BUFSIZE];
250   char syslog_tag[BUFSIZE];
251   char syslog_msg[8*BUFSIZE];
252   char str[8*BUFSIZE];
253   char syslog_file[BUFSIZE];
254   int srcTyp = 0;
255   int count = 0;
256   unsigned long long pos;
257   unsigned long long prevpos = 0;
258
259   sleep(1);
260   printf("Running Syslog thread \n");
261   fflush(stdout);
262
263   while(1)
264   {
265
266      FILE * file = fopen("syslog_config.json", "r");
267
268      memset(js, 0, MAX_BUFFER_SIZE);
269      memset(ch, 0, BUFSIZE);
270
271      while((fgets(ch, (BUFSIZE-1), file)) !=NULL)
272      {
273         strcat(js, ch);
274         memset(ch, 0, BUFSIZE);
275      }
276 //   printf("the file content is \n %s \n", js);
277
278      jsmn_init(&p);
279      numToken = jsmn_parse(&p, js, strlen(js), tokens, MAX_TOKENS);
280 //     printf("Token count-%d\n", numToken);
281
282 //   printToken(js,tokens, numToken);
283
284      ret = getStringToken(js, tokens, numToken, "tmp_syslogFile", "tmp_indirectParameters", syslog_file, BUFSIZE);
285      if (ret != 0)
286      {
287         printf("Missing mandatory parameters - tmp_syslogFile is not there in tmp_indirectParameters. Exiting...\n");
288         exit(1);
289      }
290
291      ret = getStringToken(js, tokens, numToken, "syslogTag", "tmp_directParameters", syslog_tag, BUFSIZE);
292      if (ret != 0)
293      {
294         printf("Missing mandatory parameters - syslogTag is not there in tmp_directParameters, exiting..\n");
295         exit(1);
296      }
297
298      ret = getStringToken(js, tokens, numToken, "eventName", "tmp_directParameters", eName, BUFSIZE);
299      if (ret != 0)
300      {
301         printf("Missing mandatory parameters - eventName is not there in tmp_directParameters. Exiting...\n");
302         exit(1);
303      }
304
305      ret = getStringToken(js, tokens, numToken, "eventSourceType", "tmp_directParameters", eventSrcTyp, BUFSIZE);
306      if (ret != 0)
307      {
308         printf("Missing mandatory parameters - eventSourceType is not there in tmp_directParameters, exiting..\n");
309         exit(1);
310      }
311      srcTyp = get_source(eventSrcTyp);
312      if(srcTyp == -1)
313      {
314         printf("Fault eventSourceType value is not matching, eventSourceType-%s \n", eventSrcTyp);
315         exit(1);
316      }
317
318     // Open file in read mode
319     fp = fopen(syslog_file, "r");
320     if (fp == NULL)
321     {
322         printf("Error while opening file");
323         exit(EXIT_FAILURE);
324     }
325
326      memset(str, 0, 8*BUFSIZE);
327      if (fseek(fp, 0, SEEK_END))
328         perror("fseek() failed");
329      else
330      {
331         // pos will contain no. of chars in input file.
332         int n = 8;
333         pos = ftell(fp);
334
335         // search for '\n' characters
336         while (pos>prevpos)
337         {
338             // Move 'pos' away from end of file.
339             if (!fseek(fp, --pos, SEEK_SET))
340             {
341                 if (fgetc(fp) == '\n')
342
343                     // stop reading when n newlines is found
344                     if (count++ == n)
345                         break;
346             }
347             else
348                 perror("fseek() failed");
349         }
350         //printf("pos %d prevpos %d\n",pos,prevpos);
351
352         // print last n lines
353         prevpos = pos;
354
355         while (fgets(str, sizeof(str), fp))
356         {
357             char * position;
358            // printf("str is - %s\n", str);
359             if ((position=strchr(str, '\n')) != NULL)
360                *position = '\0';
361             if(strstr(str, syslog_tag) && !strstr(str,"EVEL") && !strstr(str,"commonEventHeader") && !strstr(str,"syslogMsg") && !strstr(str,"syslogTag"))
362             {
363                memset(syslog_msg, 0, 8*BUFSIZE);
364                memcpy(syslog_msg, str, 8*BUFSIZE);
365                report_syslog(js, tokens, numToken, syslog_tag, eName, srcTyp, syslog_msg);
366             }
367        
368             prevpos += strlen(str);
369             //printf("new prevpos is %d, size of str is %d\n", prevpos, strlen(str));
370         }
371     }
372     sleep(3);
373   }
374 }
375