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