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 *HeartbeatThread(void *threadarg);
35 unsigned long long epoch_start = 0;
37 int main(int argc, char** argv)
40 int port = atoi(argv[2]);
51 port2 = atoi(argv[4]);
54 if (!((argc == 3) || (argc == 5)))
56 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <FQDN>|<IP address> <port> \n", argv[0]);
57 fprintf(stderr, "OR\n");
58 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> \n", argv[0]);
62 /**************************************************************************/
64 /**************************************************************************/
65 if(evel_initialize(fqdn, /* FQDN */
67 fqdn2, /* Backup FQDN */
68 port2, /* Backup port */
69 NULL, /* optional path */
70 NULL, /* optional topic */
71 100, /* Ring Buffer size */
79 "sample1", /* Username */
80 "sample1", /* Password */
81 "sample1", /* Username2 */
82 "sample1", /* Password2 */
84 NULL, /* Source ip2 */
85 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
86 "vHeartbeat", /* Role */
89 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
94 printf("\nInitialization completed\n");
97 /* Initialize and set thread detached attribute */
98 pthread_attr_init(&attr);
99 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
101 printf("Main:Creating thread \n");
102 rc = pthread_create(&hb_thread, NULL, HeartbeatThread, &i);
105 printf("ERROR; return code from pthread_create() is %d\n", rc);
108 printf("Main:Created HB thread \n");
110 pthread_join(hb_thread, NULL);
113 printf("Terminated\n");
117 void *HeartbeatThread(void *threadarg)
120 EVENT_HEARTBEAT_FIELD * event = NULL;
121 EVENT_HEADER* hb_header = NULL;
122 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
125 jsmntok_t tokens[MAX_TOKENS];
126 char js[MAX_BUFFER_SIZE];
131 char stringVal[BUFSIZE];
135 char event_id1[10] = "heartbeat";
136 char event_id2[15] = {0};
138 char event_id[BUFSIZE] = {0};
141 printf("Running HB thread \n");
147 FILE * file = fopen("hb_config.json", "r");
149 memset(js, 0, MAX_BUFFER_SIZE);
150 memset(ch, 0, BUFSIZE);
152 while((fgets(ch, (BUFSIZE-1), file)) !=NULL)
155 memset(ch, 0, BUFSIZE);
157 // printf("the file content is \n %s \n", js);
160 numToken = jsmn_parse(&p, js, strlen(js), tokens, MAX_TOKENS);
161 printf("Token count-%d\n", numToken);
163 // printToken(js,tokens, numToken);
165 ret = getStringToken(js, tokens, numToken, "eventName", "tmp_directParameters", eName, BUFSIZE);
168 printf("Missing mandatory parameters - eventName is not there in tmp_directParameters. Exiting...\n");
172 ret = getIntToken(js, tokens, numToken, "heartbeatInterval", "tmp_directParameters", &hb_interval);
175 printf("The parameter heartbeatInterval is not defined, defaulted to 60 seconds\n");
179 /***************************************************************************/
181 /***************************************************************************/
182 memset(event_id, 0, BUFSIZE);
183 memset(event_id2, 0, 15);
184 sprintf(event_id2, "%09d", event_id3++);
185 strcat(event_id, event_id1);
186 strcat(event_id, event_id2);
188 event = evel_new_heartbeat_field(hb_interval, eName, event_id);
191 hb_header = (EVENT_HEADER *)event;
193 ret = getStringToken(js, tokens, numToken, "eventType", "tmp_directParameters", stringVal, BUFSIZE);
196 evel_header_type_set(&event->header, stringVal);
199 struct timeval tv_now;
200 gettimeofday(&tv_now, NULL);
201 unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
203 evel_start_epoch_set(&event->header, epoch_start);
204 evel_last_epoch_set(&event->header, epoch_now);
205 epoch_start = epoch_now;
207 ret = getStringToken(js, tokens, numToken, "nfcNamingCode", "tmp_directParameters", stringVal, BUFSIZE);
209 evel_nfcnamingcode_set(&event->header, stringVal);
211 ret = getStringToken(js, tokens, numToken, "nfNamingCode", "tmp_directParameters", stringVal, BUFSIZE);
213 evel_nfnamingcode_set(&event->header, stringVal);
215 ret = getStringToken(js, tokens, numToken, "reportingEntityName", "tmp_directParameters", stringVal, BUFSIZE);
217 evel_reporting_entity_name_set(&event->header, stringVal);
220 printf("Missing mandatory parameters - reportingEntityName is not there in tmp_directParameters\n");
221 printf("Defaulting reportingEntityName to hostname\n");
224 ret = getStringToken(js, tokens, numToken, "reportingEntityId", "tmp_directParameters", stringVal, BUFSIZE);
226 evel_reporting_entity_id_set(&event->header, stringVal);
228 ret = getStringToken(js, tokens, numToken, "sourceId", "tmp_directParameters", stringVal, BUFSIZE);
230 evel_source_id_set(&event->header, stringVal);
232 ret = getStringToken(js, tokens, numToken, "sourceName", "tmp_directParameters", stringVal, BUFSIZE);
234 evel_source_name_set(&event->header, stringVal);
237 printf("Missing mandatory parameters - sourceName is not there in tmp_directParameters\n");
238 printf("Defaulting sourceName to hostname\n");
241 evel_rc = evel_post_event(hb_header);
242 if (evel_rc != EVEL_SUCCESS)
244 EVEL_ERROR("Post failed %d (%s)", evel_rc, evel_error_string());
249 EVEL_ERROR("New Heartbeat failed");
251 printf(" Processed Heartbeat\n");