VES5.4.1 EVEL Library enhancements
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / VESreporting_vAFX / afx_util.c
1 /*************************************************************************//**
2  *
3  * Main Agent which spins up monitoring threads
4  *
5  *   Version 1.0:  Gokul Singaraju   gs244f   Tech Mahindra Inc.
6  *
7  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  ****************************************************************************/
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h> /* for strncpy */
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <netinet/in.h>
29 #include <net/if.h>
30 #include <arpa/inet.h>
31
32 #include "evel.h"
33 #include "afx_ves_reporter.h"
34
35
36 /**************************************************************************//**
37  * Utility function to Create and send fault events.
38  *****************************************************************************/
39 void report_fault( char* evname, char *evid, EVEL_SEVERITIES sevty, char *categ, char *intf, char *trapname, char *descr, char *rem_router, char *routername, char *router_ip, int status )
40 {
41   EVENT_FAULT * fault = NULL;
42   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
43
44   /***************************************************************************/
45   /* Fault                                                                   */
46   /***************************************************************************/
47   if (status == 1)  /** ACTIVE **/
48   {
49     fault = evel_new_fault(evname,
50                          evid,
51                          trapname,
52                          descr,
53                          EVEL_PRIORITY_HIGH,
54                          sevty,
55                          EVEL_SOURCE_ROUTER,
56                          EVEL_VF_STATUS_ACTIVE);
57   }
58   else
59   {
60     fault = evel_new_fault(evname,
61                          evid,
62                          trapname,
63                          descr,
64                          EVEL_PRIORITY_HIGH,
65                          sevty,
66                          EVEL_SOURCE_ROUTER,
67                          EVEL_VF_STATUS_IDLE);
68   }
69
70   if (fault != NULL)
71   {
72     evel_header_type_set((EVENT_HEADER *)fault, "applicationVnf");
73     evel_nfcnamingcode_set((EVENT_HEADER *)fault, "AFX");
74     evel_nfnamingcode_set((EVENT_HEADER *)fault, "AFX");
75
76     evel_fault_category_set(fault, categ);
77     if( intf != NULL)
78       evel_fault_interface_set(fault, intf );
79
80     if( strstr(evname,"Fault_vAfx_Link") != NULL || strstr(evname,"Fault_vAfx_bgp") != NULL)
81     {
82       evel_fault_addl_info_add(fault, "remote_router", rem_router);
83       evel_fault_addl_info_add(fault, "router_name", routername);
84     }
85
86     if( strstr(evname,"Fault_vAfx_bgp") != NULL )
87       evel_fault_addl_info_add(fault, "router_ip", router_ip);
88
89     evel_rc = evel_post_event((EVENT_HEADER *)fault);
90     if (evel_rc != EVEL_SUCCESS)
91     {
92       EVEL_ERROR("Post failed %d (%s)", evel_rc, evel_error_string());
93     }
94   }
95   else
96   {
97     EVEL_ERROR("New Fault failed");
98   }
99   printf(" Generated Fault event\n");
100 }
101
102
103 int file_is_modified(const char *path, time_t *oldMTime) {
104     struct stat file_stat;
105     int err = stat(path, &file_stat);
106     if (err != 0) {
107         perror(" [file_is_modified] stat");
108         exit(1);
109     }
110     if( file_stat.st_mtime > *oldMTime)
111     {
112       *oldMTime = file_stat.st_mtime;
113       return 1;
114     }
115     else
116       return 0;
117 }
118
119 void remove_spaces(char* source)
120 {
121   char* i = source;
122   char* j = source;
123   while(*j != 0)
124   {
125     *i = *j++;
126     if(*i != ' ')
127       i++;
128   }
129   *i = 0;
130 }
131
132
133 char *get_oam_intfaddr()
134 {
135  int fd;
136  int ret;
137  struct ifreq ifr;
138  char oamaddr[64];
139
140  fd = socket(AF_INET, SOCK_DGRAM, 0);
141
142  /* I want to get an IPv4 IP address */
143  ifr.ifr_addr.sa_family = AF_INET;
144
145  /* I want IP address attached to "eth0" */
146  strncpy(ifr.ifr_name, OAM_INTERFACE, IFNAMSIZ-1);
147
148  ret = ioctl(fd, SIOCGIFADDR, &ifr);
149
150  close(fd);
151
152  if( ret == 0 )
153  {
154    /* display result */
155    sprintf(oamaddr,"%s", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
156    //printf("Loopback address %s\n", oamaddr);
157  }
158  else
159    sprintf(oamaddr,"127.0.0.1");
160
161  return strdup(oamaddr);
162 }
163
164
165 char *escape_json(char *in) {
166     char out[2048];
167     char *c;
168     char *o;
169     o = &out[0];
170     for (c = in; *c != '\0'; c++) {
171         switch (*c) {
172         case '"': strcat(o,"\\\"");o+=2; break;
173         case '\\': strcat(o, "\\\\");o+=2; break;
174         case '\b': strcat(o,  "\\b");o+=2; break;
175         case '\f': strcat(o, "\\f");o+=2; break;
176         case '\n': strcat(o, "\\n");o+=2; break;
177         case '\r': strcat(o, "\\r");o+=2; break;
178         case '\t': strcat(o, "\\t");o+=2; break;
179         default:
180             if ('\x00' <= *c && *c <= '\x1f') {
181                 char tmp[8];
182                 strcat(o, "\\u");
183                 sprintf(tmp,"%04X",(int)*c);
184                 strcat(o,tmp); o+=6;
185             } else {
186                 *o = *c; o++;
187             }
188         }
189     }
190     *o = '\0';
191     //free(in);
192     return strdup(out);
193 }
194