Update INFO.yaml with new PTL
[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
43 #ifdef DOCKER
44 int measure_traffic() 
45 {
46
47   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
48   FILE *fp;
49   int status;
50   char count[10];
51   time_t rawtime;
52   struct tm * timeinfo;
53   char period [21];
54   char cmd [100];
55   int concurrent_sessions = 0;
56   int configured_entities = 0;
57   double mean_request_latency = 0;
58   double measurement_interval = 1;
59   double memory_configured = 0;
60   double memory_used = 0;
61   int request_rate=0;
62   char secs [3];
63   int sec;
64   double loadavg;
65
66   printf("Checking app traffic\n");
67   time (&rawtime);
68   timeinfo = localtime (&rawtime);
69   strftime(period,21,"%d/%b/%Y:%H:%M:",timeinfo);
70   strftime(secs,3,"%S",timeinfo);
71   sec = atoi(secs);
72   if (sec == 0) sec = 59;
73   sprintf(secs, "%02d", sec);
74   strncat(period, secs, 21);
75   // ....x....1....x....2.
76   // 15/Oct/2016:17:51:19
77   strcpy(cmd, "sudo docker logs vHello | grep -c ");
78   strncat(cmd, period, 100);
79
80   fp = popen(cmd, "r");
81   if (fp == NULL) {
82     EVEL_ERROR("popen failed to execute command");
83   }
84
85   if (fgets(count, 10, fp) != NULL) {
86     request_rate = atoi(count);
87     printf("Reporting request rate for second: %s as %d\n", period, request_rate);
88
89     }
90     else {
91       EVEL_ERROR("New Measurement failed");
92     }
93     printf("Processed measurement\n");
94   
95   status = pclose(fp);
96   if (status == -1) {
97     EVEL_ERROR("pclose returned an error");
98   }
99   return request_rate;
100 }
101
102 #endif
103
104
105 int main(int argc, char** argv)
106 {
107   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
108   EVENT_MEASUREMENT* vpp_m = NULL;
109   EVENT_HEADER* vpp_m_header = NULL;
110   int bytes_in_this_round;
111   int bytes_out_this_round;
112   int packets_in_this_round;
113   int packets_out_this_round;
114   vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
115   vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
116   struct timeval time_val;
117   //time_t start_epoch;
118   //time_t last_epoch;
119   char hostname[BUFSIZE];
120   char* fqdn = argv[1];
121   int port = atoi(argv[2]);
122   char* vnic = argv[3];
123   char* fqdn2 = NULL;
124   int port2 = 0;
125   char* caFile = "/opt/config/onap-ca.crt";
126   char* userName = "sample1";
127   char* passWord = "sample1";
128
129
130   if(argc == 6)
131   {
132      fqdn2 = argv[3];
133      port2 = atoi(argv[4]);
134      vnic = argv[5];
135   }
136   else
137      vnic = argv[3];
138
139   MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
140
141   printf("\nVector Packet Processing (VPP) measurement collection\n");
142   fflush(stdout);
143
144   if (!((argc == 6) || (argc == 4)))
145   {
146     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <FQDN>|<IP address> <port> <interface> \n", argv[0]);
147     fprintf(stderr, "OR\n");
148     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface> \n", argv[0]);
149     exit(-1);
150   }
151
152   srand(time(NULL));
153
154   /**************************************************************************/
155   /* Initialize                                                             */
156   /**************************************************************************/
157   if(evel_initialize(fqdn,                         /* FQDN                  */
158                        port,                         /* Port                  */
159                        fqdn2,                        /* Backup FQDN           */
160                        port2,                        /* Backup port           */
161                        NULL,                         /* optional path         */
162                        NULL,                         /* optional topic        */
163                        100,                          /* Ring Buffer size      */
164                        1,                            /* HTTPS?                */
165                        NULL,                         /* cert file             */
166                        NULL,                         /* key  file             */
167                        caFile,                         /* ca   info             */
168                        NULL,                         /* ca   file             */
169                        0,                            /* verify peer           */
170                        0,                            /* verify host           */
171                        userName,                    /* Username              */
172                        passWord,                    /* Password              */
173                        "sample1",                    /* Username2             */
174                        "sample1",                    /* Password2             */
175                        NULL,                         /* Source ip             */
176                        NULL,                         /* Backup Source IP      */
177                        EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
178                        "vLoadBalancer",              /* Role                  */
179                        1))                           /* Verbosity             */
180   {
181     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
182     exit(-1);
183   }
184   else
185   {
186     printf("\nInitialization completed\n");
187   }
188
189   gethostname(hostname, BUFSIZE);
190   memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
191   read_vpp_metrics(last_vpp_metrics, vnic);
192   gettimeofday(&time_val, NULL);
193   epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
194   sleep(READ_INTERVAL);
195
196   /***************************************************************************/
197   /* Collect metrics from the VNIC                                           */
198   /***************************************************************************/
199   while(1) {
200     // Read the number of vDNSs currently active
201     int active_dns = 0;
202     FILE* fd = fopen("active_dns.txt", "r");
203     while(!feof(fd)) {
204       if(fscanf(fd, "%d", &active_dns) != 1) /* Avoid infinite loops if the file doesn't contain exactly one number */
205         break;
206     }
207
208     if(fclose(fd))  {
209       printf("Error when closing file\n");
210       return 1;
211     }
212
213     memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
214     read_vpp_metrics(curr_vpp_metrics, vnic);
215
216     if(active_dns > 0 && (curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0)) {
217       bytes_in_this_round = (int) round((curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in) / active_dns);
218     }
219     else {
220       bytes_in_this_round = 0;
221     }
222     if(active_dns > 0 && (curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0)) {
223       bytes_out_this_round = (int) round((curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out) / active_dns);
224     }
225     else {
226       bytes_out_this_round = 0;
227     }
228     if(active_dns > 0 && (curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0)) {
229       packets_in_this_round = (int) round((curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in) / active_dns);
230     }
231     else {
232       packets_in_this_round = 0;
233     }
234     if(active_dns > 0 && (curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0)) {
235       packets_out_this_round = (int) round((curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out) / active_dns);
236     }
237     else {
238       packets_out_this_round = 0;
239     }
240
241     vpp_m = evel_new_measurement(READ_INTERVAL,"vLoadBalancer","TrafficStats_1.2.3.4");
242     vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance(vnic, "true");
243     evel_meas_vnic_performance_add(vpp_m, vnic_performance);
244
245     if(vpp_m != NULL) {
246       printf("New measurement report created...\n");
247
248       evel_measurement_type_set(vpp_m, "HTTP request rate");
249       evel_measurement_request_rate_set(vpp_m, rand()%10000);
250
251       evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
252       evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
253
254       evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
255       evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
256
257       /***************************************************************************/
258       /* Set parameters in the MEASUREMENT header packet                         */
259       /***************************************************************************/
260       struct timeval tv_now;
261       gettimeofday(&tv_now, NULL);
262       unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
263
264       //last_epoch = start_epoch + READ_INTERVAL * 1000000;
265       vpp_m_header = (EVENT_HEADER *)vpp_m;
266       //vpp_m_header->start_epoch_microsec = start_epoch;
267       //vpp_m_header->last_epoch_microsec = last_epoch;
268       evel_start_epoch_set(&vpp_m->header, epoch_start);
269       evel_last_epoch_set(&vpp_m->header, epoch_now);
270       epoch_start = epoch_now;
271
272       evel_nfcnamingcode_set(&vpp_m->header, "vVNF");
273       evel_nfnamingcode_set(&vpp_m->header, "vVNF");
274       //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
275       //strcpy(vpp_m_header->reporting_entity_name, hostname);
276       evel_reporting_entity_name_set(&vpp_m->header, "lbll");
277       evel_reporting_entity_id_set(&vpp_m->header, "No UUID available");
278       evel_rc = evel_post_event(vpp_m_header);
279
280       if(evel_rc == EVEL_SUCCESS) {
281         printf("Measurement report correctly sent to the collector!\n");
282       }
283       else {
284         printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
285       }
286     }
287     else {
288       printf("New measurement report failed (%s)\n", evel_error_string());
289     }
290
291     last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
292     last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
293     last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
294     last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
295     //gettimeofday(&time_val, NULL);
296     //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
297
298     sleep(READ_INTERVAL);
299   }
300
301   /***************************************************************************/
302   /* Terminate                                                               */
303   /***************************************************************************/
304   sleep(1);
305   evel_free_measurement(vpp_m);
306   free(last_vpp_metrics);
307   free(curr_vpp_metrics);
308   evel_terminate();
309   printf("Terminated\n");
310
311   return 0;
312 }
313
314 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
315   // Define an array of char that contains the parameters of the unix 'cut' command
316   char* params[] = {"-f3", "-f11", "-f4", "-f12"};
317   // Define the unix command to execute in order to read metrics from the vNIC
318   char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
319   char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
320   char cmd[BUFSIZE];
321   // Define other variables
322   char buf[BUFSIZE];            /* buffer used to store VPP metrics     */
323   int temp[] = {0, 0, 0, 0};    /* temp array that contains VPP values  */
324   FILE *fp;                     /* file descriptor to pipe cmd to shell */
325   int i;
326
327   for(i = 0; i < 4; i++) {
328     // Clear buffers
329     memset(buf, 0, BUFSIZE);
330     memset(cmd, 0, BUFSIZE);
331     // Build shell command to read metrics from the vNIC
332     strcat(cmd, cmd_prefix);
333     strcat(cmd, vnic);
334     strcat(cmd, cmd_mid);
335     strcat(cmd, params[i]);
336     
337     // Open a pipe and read VPP values
338     if ((fp = popen(cmd, "r")) == NULL) {
339       printf("Error opening pipe!\n");
340       return;
341     }
342
343     while (fgets(buf, BUFSIZE, fp) != NULL);
344     temp[i] = atoi(buf);
345
346     if(pclose(fp))  {
347       printf("Command not found or exited with error status\n");
348       return;
349     }
350   }
351
352   // Store metrics read from the vNIC in the struct passed from the main function
353   vpp_metrics->bytes_in = temp[0];
354   vpp_metrics->bytes_out = temp[1];
355   vpp_metrics->packets_in = temp[2];
356   vpp_metrics->packets_out = temp[3];
357 }