Update INFO.yaml with new PTL
[demo.git] / vnfs / VESreporting_vFW / vpp_measurement_reporter.c
1
2 /*************************************************************************//**
3  *
4  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and 
15  * limitations under the License.
16  *
17  ****************************************************************************/
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <sys/time.h>
24
25 #include "evel.h"
26
27 #define BUFSIZE 128
28 #define READ_INTERVAL 10
29
30 typedef struct dummy_vpp_metrics_struct {
31   int bytes_in;
32   int bytes_out;
33   int packets_in;
34   int packets_out;
35 } vpp_metrics_struct;
36
37 void read_vpp_metrics(vpp_metrics_struct *, char *);
38
39 int main(int argc, char** argv)
40 {
41   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
42   EVENT_MEASUREMENT* vpp_m = NULL;
43   EVENT_HEADER* vpp_m_header = NULL;
44   int bytes_in_this_round;
45   int bytes_out_this_round;
46   int packets_in_this_round;
47   int packets_out_this_round;
48   vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
49   vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
50   struct timeval time_val;
51   time_t start_epoch;
52   time_t last_epoch;
53   char hostname[BUFSIZE];
54   char* fqdn = argv[1];
55   int port = atoi(argv[2]);
56   char* vnic = argv[3];
57
58   printf("\nVector Packet Processing (VPP) measurement collection\n");
59   fflush(stdout);
60
61   if (argc != 4)
62   {
63     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
64     exit(-1);
65   }
66
67   /**************************************************************************/
68   /* Initialize                                                             */
69   /**************************************************************************/
70   if(evel_initialize(fqdn,                         /* FQDN                  */
71                      port,                         /* Port                  */
72                      NULL,                         /* optional path         */
73                      NULL,                         /* optional topic        */
74                      0,                            /* HTTPS?                */
75                      "",                           /* Username              */
76                      "",                           /* Password              */
77                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
78                      "vFirewall",                  /* Role                  */
79                      1))                           /* Verbosity             */
80   {
81     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
82     exit(-1);
83   }
84   else
85   {
86     printf("\nInitialization completed\n");
87   }
88
89   gethostname(hostname, BUFSIZE);
90   memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
91   read_vpp_metrics(last_vpp_metrics, vnic);
92   gettimeofday(&time_val, NULL);
93   start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
94   sleep(READ_INTERVAL);
95
96   /***************************************************************************/
97   /* Collect metrics from the VNIC                                           */
98   /***************************************************************************/
99   while(1) {
100     memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
101     read_vpp_metrics(curr_vpp_metrics, vnic);
102
103     if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
104       bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
105     }
106     else {
107       bytes_in_this_round = 0;
108     }
109     if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
110       bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
111     }
112     else {
113       bytes_out_this_round = 0;
114     }
115     if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
116       packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
117     }
118     else {
119       packets_in_this_round = 0;
120     }
121     if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
122       packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
123     }
124     else {
125       packets_out_this_round = 0;
126     }
127
128     vpp_m = evel_new_measurement(READ_INTERVAL);
129
130     if(vpp_m != NULL) {
131       printf("New measurement report created...\n");
132       evel_measurement_vnic_use_add(vpp_m,              /* Pointer to the measurement      */ 
133                                      vnic,              /* ASCII string with the vNIC's ID */
134                     packets_in_this_round,              /* Packets received                */
135                    packets_out_this_round,              /* Packets transmitted             */
136                                         0,              /* Broadcast packets received      */
137                                         0,              /* Broadcast packets transmitted   */
138                       bytes_in_this_round,              /* Total bytes received            */
139                      bytes_out_this_round,              /* Total bytes transmitted         */
140                                         0,              /* Multicast packets received      */
141                                         0,              /* Multicast packets transmitted   */
142                                         0,              /* Unicast packets received        */
143                                         0);             /* Unicast packets transmitted     */
144
145       /***************************************************************************/
146       /* Set parameters in the MEASUREMENT header packet                         */
147       /***************************************************************************/
148       last_epoch = start_epoch + READ_INTERVAL * 1000000;
149       vpp_m_header = (EVENT_HEADER *)vpp_m;
150       vpp_m_header->start_epoch_microsec = start_epoch;
151       vpp_m_header->last_epoch_microsec = last_epoch;
152       strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
153       strcpy(vpp_m_header->reporting_entity_name, hostname);
154       evel_rc = evel_post_event(vpp_m_header);
155
156       if(evel_rc == EVEL_SUCCESS) {
157         printf("Measurement report correctly sent to the collector!\n");
158       }
159       else {
160         printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
161       }
162     }
163     else {
164       printf("New measurement report failed (%s)\n", evel_error_string());
165     }
166
167     last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
168     last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
169     last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
170     last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
171     gettimeofday(&time_val, NULL);
172     start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
173
174     sleep(READ_INTERVAL);
175   }
176
177   /***************************************************************************/
178   /* Terminate                                                               */
179   /***************************************************************************/
180   sleep(1);
181   free(last_vpp_metrics);
182   free(curr_vpp_metrics);
183   evel_terminate();
184   printf("Terminated\n");
185
186   return 0;
187 }
188
189 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
190   // Define an array of char that contains the parameters of the unix 'cut' command
191   char* params[] = {"-f3", "-f11", "-f4", "-f12"};
192   // Define the unix command to execute in order to read metrics from the vNIC
193   char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
194   char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
195   char cmd[BUFSIZE];
196   // Define other variables
197   char buf[BUFSIZE];            /* buffer used to store VPP metrics     */
198   int temp[] = {0, 0, 0, 0};    /* temp array that contains VPP values  */
199   FILE *fp;                     /* file descriptor to pipe cmd to shell */
200   int i;
201
202   for(i = 0; i < 4; i++) {
203     // Clear buffers
204     memset(buf, 0, BUFSIZE);
205     memset(cmd, 0, BUFSIZE);
206     // Build shell command to read metrics from the vNIC
207     strcat(cmd, cmd_prefix);
208     strcat(cmd, vnic);
209     strcat(cmd, cmd_mid);
210     strcat(cmd, params[i]);
211     
212     // Open a pipe and read VPP values
213     if ((fp = popen(cmd, "r")) == NULL) {
214         printf("Error opening pipe!\n");
215         return;
216     }
217
218     while (fgets(buf, BUFSIZE, fp) != NULL);
219     temp[i] = atoi(buf);
220
221     if(pclose(fp))  {
222         printf("Command not found or exited with error status\n");
223         return;
224     }
225   }
226
227   // Store metrics read from the vNIC in the struct passed from the main function
228   vpp_metrics->bytes_in = temp[0];
229   vpp_metrics->bytes_out = temp[1];
230   vpp_metrics->packets_in = temp[2];
231   vpp_metrics->packets_out = temp[3];
232 }