2e54d744ab52ebd091ab4c41eea065250d0511f3
[demo.git] / vnfs / VESreporting_vFW5.0 / 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 unsigned long long epoch_start = 0;
40
41
42 int main(int argc, char** argv)
43 {
44   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
45   EVENT_MEASUREMENT* vpp_m = NULL;
46   EVENT_HEADER* vpp_m_header = NULL;
47   int bytes_in_this_round;
48   int bytes_out_this_round;
49   int packets_in_this_round;
50   int packets_out_this_round;
51   vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
52   vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
53   struct timeval time_val;
54   //time_t start_epoch;
55   //time_t last_epoch;
56   char hostname[BUFSIZE];
57   char* fqdn = argv[1];
58   int port = atoi(argv[2]);
59   char* vnic = argv[3];
60   MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
61   //struct timeval tv_start;
62
63   printf("\nVector Packet Processing (VPP) measurement collection\n");
64   fflush(stdout);
65
66   if (argc != 4)
67   {
68     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
69     exit(-1);
70   }
71
72   /**************************************************************************/
73   /* Initialize                                                             */
74   /**************************************************************************/
75   if(evel_initialize(fqdn,                         /* FQDN                  */
76                      port,                         /* Port                  */
77                      NULL,                         /* optional path         */
78                      NULL,                         /* optional topic        */
79                      0,                            /* HTTPS?                */
80                      "",                           /* Username              */
81                      "",                           /* Password              */
82                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
83                      "vFirewall",                  /* Role                  */
84                      1))                           /* Verbosity             */
85   {
86     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
87     exit(-1);
88   }
89   else
90   {
91     printf("\nInitialization completed\n");
92   }
93
94   gethostname(hostname, BUFSIZE);
95   memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
96   read_vpp_metrics(last_vpp_metrics, vnic);
97   gettimeofday(&time_val, NULL);
98   epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
99   sleep(READ_INTERVAL);
100
101   /***************************************************************************/
102   /* Collect metrics from the VNIC                                           */
103   /***************************************************************************/
104   while(1) {
105     memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
106     read_vpp_metrics(curr_vpp_metrics, vnic);
107
108     if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
109       bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
110     }
111     else {
112       bytes_in_this_round = 0;
113     }
114     if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
115       bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
116     }
117     else {
118       bytes_out_this_round = 0;
119     }
120     if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
121       packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
122     }
123     else {
124       packets_in_this_round = 0;
125     }
126     if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
127       packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
128     }
129     else {
130       packets_out_this_round = 0;
131     }
132
133     vpp_m = evel_new_measurement(READ_INTERVAL);
134     vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
135     evel_meas_vnic_performance_add(vpp_m, vnic_performance);
136
137     if(vpp_m != NULL) {
138       printf("New measurement report created...\n");
139
140
141       evel_vnic_performance_rx_total_pkt_acc_set(vnic_performance, packets_in_this_round);
142       evel_vnic_performance_tx_total_pkt_acc_set(vnic_performance, packets_out_this_round);
143       
144       evel_vnic_performance_rx_octets_acc_set(vnic_performance, bytes_in_this_round);
145       evel_vnic_performance_tx_octets_acc_set(vnic_performance, bytes_out_this_round);
146
147       /***************************************************************************/
148       /* Set parameters in the MEASUREMENT header packet                         */
149       /***************************************************************************/
150       struct timeval tv_now;
151       gettimeofday(&tv_now, NULL);
152       unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
153
154       //last_epoch = start_epoch + READ_INTERVAL * 1000000;
155       vpp_m_header = (EVENT_HEADER *)vpp_m;
156       //vpp_m_header->start_epoch_microsec = start_epoch;
157       //vpp_m_header->last_epoch_microsec = last_epoch;
158       evel_start_epoch_set(&vpp_m->header, epoch_start);
159       evel_last_epoch_set(&vpp_m->header, epoch_now);
160       epoch_start = epoch_now;
161
162       strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
163       strcpy(vpp_m_header->reporting_entity_name, hostname);
164       evel_rc = evel_post_event(vpp_m_header);
165
166       if(evel_rc == EVEL_SUCCESS) {
167         printf("Measurement report correctly sent to the collector!\n");
168       }
169       else {
170         printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
171       }
172     }
173     else {
174       printf("New measurement report failed (%s)\n", evel_error_string());
175     }
176
177     last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
178     last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
179     last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
180     last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
181     //gettimeofday(&time_val, NULL);
182     //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
183
184     sleep(READ_INTERVAL);
185   }
186
187   /***************************************************************************/
188   /* Terminate                                                               */
189   /***************************************************************************/
190   sleep(1);
191   free(last_vpp_metrics);
192   free(curr_vpp_metrics);
193   evel_terminate();
194   printf("Terminated\n");
195
196   return 0;
197 }
198
199 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
200   // Define an array of char that contains the parameters of the unix 'cut' command
201   char* params[] = {"-f3", "-f11", "-f4", "-f12"};
202   // Define the unix command to execute in order to read metrics from the vNIC
203   char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
204   char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
205   char cmd[BUFSIZE];
206   // Define other variables
207   char buf[BUFSIZE];            /* buffer used to store VPP metrics     */
208   int temp[] = {0, 0, 0, 0};    /* temp array that contains VPP values  */
209   FILE *fp;                     /* file descriptor to pipe cmd to shell */
210   int i;
211
212   for(i = 0; i < 4; i++) {
213     // Clear buffers
214     memset(buf, 0, BUFSIZE);
215     memset(cmd, 0, BUFSIZE);
216     // Build shell command to read metrics from the vNIC
217     strcat(cmd, cmd_prefix);
218     strcat(cmd, vnic);
219     strcat(cmd, cmd_mid);
220     strcat(cmd, params[i]);
221     
222     // Open a pipe and read VPP values
223     if ((fp = popen(cmd, "r")) == NULL) {
224         printf("Error opening pipe!\n");
225         return;
226     }
227
228     while (fgets(buf, BUFSIZE, fp) != NULL);
229     temp[i] = atoi(buf);
230
231     if(pclose(fp))  {
232         printf("Command not found or exited with error status\n");
233         return;
234     }
235   }
236
237   // Store metrics read from the vNIC in the struct passed from the main function
238   vpp_metrics->bytes_in = temp[0];
239   vpp_metrics->bytes_out = temp[1];
240   vpp_metrics->packets_in = temp[2];
241   vpp_metrics->packets_out = temp[3];
242 }