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