1 /*************************************************************************//**
3 * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
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
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.
16 ****************************************************************************/
30 #define MAX_BUFFER_SIZE 4096
31 #define MAX_TOKENS 1000
33 void *SyslogThread(void *threadarg);
35 unsigned long long epoch_start = 0;
37 void report_syslog(char * js, jsmntok_t * tokens, int numToken, char * syslog_tag, char * eName, int srcTyp, char * syslog_msg)
39 EVENT_HEARTBEAT_FIELD * event = NULL;
40 EVENT_HEADER* syslog_header = NULL;
41 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
42 char stringVal[BUFSIZE];
44 char event_id1[10] = "syslog";
45 char event_id2[15] = {0};
47 char event_id[BUFSIZE] = {0};
50 /***************************************************************************/
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);
60 event = evel_new_syslog(eName, event_id, srcTyp, syslog_msg, syslog_tag);
64 syslog_header = (EVENT_HEADER *)event;
66 ret = getStringToken(js, tokens, numToken, "syslogProc", "tmp_directParameters", stringVal, BUFSIZE);
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);
72 evel_header_type_set(&event->header, stringVal);
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;
78 evel_start_epoch_set(&event->header, epoch_start);
79 evel_last_epoch_set(&event->header, epoch_now);
80 epoch_start = epoch_now;
82 ret = getStringToken(js, tokens, numToken, "nfcNamingCode", "tmp_directParameters", stringVal, BUFSIZE);
84 evel_nfcnamingcode_set(&event->header, stringVal);
86 ret = getStringToken(js, tokens, numToken, "nfNamingCode", "tmp_directParameters", stringVal, BUFSIZE);
88 evel_nfnamingcode_set(&event->header, stringVal);
90 ret = getStringToken(js, tokens, numToken, "reportingEntityName", "tmp_directParameters", stringVal, BUFSIZE);
92 evel_reporting_entity_name_set(&event->header, stringVal);
95 printf("Missing mandatory parameters - reportingEntityName is not there in tmp_directParameters\n");
96 printf("Defaulting reportingEntityName to hostname\n");
99 ret = getStringToken(js, tokens, numToken, "reportingEntityId", "tmp_directParameters", stringVal, BUFSIZE);
101 evel_reporting_entity_id_set(&event->header, stringVal);
103 ret = getStringToken(js, tokens, numToken, "sourceId", "tmp_directParameters", stringVal, BUFSIZE);
105 evel_source_id_set(&event->header, stringVal);
107 ret = getStringToken(js, tokens, numToken, "sourceName", "tmp_directParameters", stringVal, BUFSIZE);
109 evel_source_name_set(&event->header, stringVal);
112 printf("Missing mandatory parameters - sourceName is not there in tmp_directParameters\n");
113 printf("Defaulting sourceName to hostname\n");
116 evel_rc = evel_post_event(syslog_header);
117 if (evel_rc != EVEL_SUCCESS)
119 EVEL_ERROR("Post failed %d (%s)", evel_rc, evel_error_string());
124 EVEL_ERROR("New Syslog failed");
126 printf(" Processed Syslog\n");
129 int get_source(char * inStr)
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;
158 int main(int argc, char** argv)
160 char* fqdn = argv[1];
161 int port = atoi(argv[2]);
165 pthread_t syslog_thread;
172 port2 = atoi(argv[4]);
175 if (!((argc == 3) || (argc == 5)))
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]);
183 /**************************************************************************/
185 /**************************************************************************/
186 if(evel_initialize(fqdn, /* FQDN */
188 fqdn2, /* Backup FQDN */
189 port2, /* Backup port */
190 NULL, /* optional path */
191 NULL, /* optional topic */
192 100, /* Ring Buffer size */
194 NULL, /* cert file */
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 */
210 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
215 printf("\nInitialization completed\n");
218 /* Initialize and set thread detached attribute */
219 pthread_attr_init(&attr);
220 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
222 printf("Main:Creating thread \n");
223 rc = pthread_create(&syslog_thread, NULL, SyslogThread, &i);
226 printf("ERROR; return code from pthread_create() is %d\n", rc);
229 printf("Main:Created Syslog thread \n");
231 pthread_join(syslog_thread, NULL);
234 printf("Terminated\n");
238 void *SyslogThread(void *threadarg)
242 jsmntok_t tokens[MAX_TOKENS];
243 char js[MAX_BUFFER_SIZE];
249 char eventSrcTyp[BUFSIZE];
250 char syslog_tag[BUFSIZE];
251 char syslog_msg[8*BUFSIZE];
253 char syslog_file[BUFSIZE];
256 unsigned long long pos;
257 unsigned long long prevpos = 0;
260 printf("Running Syslog thread \n");
266 FILE * file = fopen("syslog_config.json", "r");
268 memset(js, 0, MAX_BUFFER_SIZE);
269 memset(ch, 0, BUFSIZE);
271 while((fgets(ch, (BUFSIZE-1), file)) !=NULL)
274 memset(ch, 0, BUFSIZE);
276 // printf("the file content is \n %s \n", js);
279 numToken = jsmn_parse(&p, js, strlen(js), tokens, MAX_TOKENS);
280 // printf("Token count-%d\n", numToken);
282 // printToken(js,tokens, numToken);
284 ret = getStringToken(js, tokens, numToken, "tmp_syslogFile", "tmp_indirectParameters", syslog_file, BUFSIZE);
287 printf("Missing mandatory parameters - tmp_syslogFile is not there in tmp_indirectParameters. Exiting...\n");
291 ret = getStringToken(js, tokens, numToken, "syslogTag", "tmp_directParameters", syslog_tag, BUFSIZE);
294 printf("Missing mandatory parameters - syslogTag is not there in tmp_directParameters, exiting..\n");
298 ret = getStringToken(js, tokens, numToken, "eventName", "tmp_directParameters", eName, BUFSIZE);
301 printf("Missing mandatory parameters - eventName is not there in tmp_directParameters. Exiting...\n");
305 ret = getStringToken(js, tokens, numToken, "eventSourceType", "tmp_directParameters", eventSrcTyp, BUFSIZE);
308 printf("Missing mandatory parameters - eventSourceType is not there in tmp_directParameters, exiting..\n");
311 srcTyp = get_source(eventSrcTyp);
314 printf("Fault eventSourceType value is not matching, eventSourceType-%s \n", eventSrcTyp);
318 // Open file in read mode
319 fp = fopen(syslog_file, "r");
322 printf("Error while opening file");
326 memset(str, 0, 8*BUFSIZE);
327 if (fseek(fp, 0, SEEK_END))
328 perror("fseek() failed");
331 // pos will contain no. of chars in input file.
335 // search for '\n' characters
338 // Move 'pos' away from end of file.
339 if (!fseek(fp, --pos, SEEK_SET))
341 if (fgetc(fp) == '\n')
343 // stop reading when n newlines is found
348 perror("fseek() failed");
350 //printf("pos %d prevpos %d\n",pos,prevpos);
352 // print last n lines
355 while (fgets(str, sizeof(str), fp))
358 // printf("str is - %s\n", str);
359 if ((position=strchr(str, '\n')) != NULL)
361 if(strstr(str, syslog_tag) && !strstr(str,"EVEL") && !strstr(str,"commonEventHeader") && !strstr(str,"syslogMsg") && !strstr(str,"syslogTag"))
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);
368 prevpos += strlen(str);
369 //printf("new prevpos is %d, size of str is %d\n", prevpos, strlen(str));