Update License text
[vnfsdk/compliance.git] / veslibrary / ves_clibrary / VESreporting_vFW / vpp_measurement_reporter.c
1
2 /*************************************************************************//**
3  *
4  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  *
6  * Unless otherwise specified, all software contained herein is
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  *
18  ****************************************************************************/
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/time.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 #ifdef DOCKER
43 int measure_traffic() 
44 {
45
46   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
47   FILE *fp;
48   int status;
49   char count[10];
50   time_t rawtime;
51   struct tm * timeinfo;
52   char period [21];
53   char cmd [100];
54   int concurrent_sessions = 0;
55   int configured_entities = 0;
56   double mean_request_latency = 0;
57   double measurement_interval = 1;
58   double memory_configured = 0;
59   double memory_used = 0;
60   int request_rate=0;
61   char secs [3];
62   int sec;
63   double loadavg;
64
65   printf("Checking app traffic\n");
66   time (&rawtime);
67   timeinfo = localtime (&rawtime);
68   strftime(period,21,"%d/%b/%Y:%H:%M:",timeinfo);
69   strftime(secs,3,"%S",timeinfo);
70   sec = atoi(secs);
71   if (sec == 0) sec = 59;
72   sprintf(secs, "%02d", sec);
73   strncat(period, secs, 21);
74   // ....x....1....x....2.
75   // 15/Oct/2016:17:51:19
76   strcpy(cmd, "sudo docker logs vHello | grep -c ");
77   strncat(cmd, period, 100);
78
79   fp = popen(cmd, "r");
80   if (fp == NULL) {
81     EVEL_ERROR("popen failed to execute command");
82   }
83
84   if (fgets(count, 10, fp) != NULL) {
85     request_rate = atoi(count);
86     printf("Reporting request rate for second: %s as %d\n", period, request_rate);
87
88     }
89     else {
90       EVEL_ERROR("New Measurement failed");
91     }
92     printf("Processed measurement\n");
93   
94   status = pclose(fp);
95   if (status == -1) {
96     EVEL_ERROR("pclose returned an error");
97   }
98   return request_rate;
99 }
100
101 #endif
102
103
104
105 /**************************************************************************//**
106  * tap live cpu stats
107  *****************************************************************************/
108 void evel_get_cpu_stats(EVENT_MEASUREMENT * measurement)
109 {
110   FILE *fp;
111   char path[1024];
112   double usage=0.0;
113   double idle;
114   double intrpt;
115   double nice;
116   double softirq;
117   double steal;
118   double sys;
119   double user;
120   double wait;
121   MEASUREMENT_CPU_USE *cpu_use = NULL;
122
123   /* Open the command for reading. */
124   //fp = popen("/bin/ls /etc/", "r");
125   fp = popen("/usr/bin/top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 ", "r");
126   if (fp == NULL) {
127     printf("Failed to run command\n" );
128     exit(1);
129   }
130
131   /* Read the output a line at a time - output it. */
132   while (fgets(path, sizeof(path)-1, fp) != NULL) {
133     printf("%s", path+10);
134     sscanf(path+10," %lf us, %lf sy,  %lf ni,  %lf id,  %lf wa,  %lf hi,  %lf si,  %lf st",
135     &user,&sys,&nice,&idle,&wait,&intrpt,&softirq,&steal);
136   }
137
138   /* close */
139   pclose(fp);
140
141   cpu_use = evel_measurement_new_cpu_use_add(measurement, "cpu1", usage);
142   if( cpu_use != NULL ){
143   evel_measurement_cpu_use_idle_set(cpu_use,idle);
144   //evel_measurement_cpu_use_interrupt_set(cpu_use,intrpt);
145   //evel_measurement_cpu_use_nice_set(cpu_use,nice);
146   //evel_measurement_cpu_use_softirq_set(cpu_use,softirq);
147   //evel_measurement_cpu_use_steal_set(cpu_use,steal);
148   evel_measurement_cpu_use_system_set(cpu_use,sys);
149   evel_measurement_cpu_use_usageuser_set(cpu_use,user);
150   //evel_measurement_cpu_use_wait_set(cpu_use,wait);
151   //evel_measurement_cpu_use_add(measurement, "cpu2", usage,idle,intrpt,nice,softirq,steal,sys,user,wait);
152   }
153 }
154
155
156
157 int main(int argc, char** argv)
158 {
159   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
160   EVENT_MEASUREMENT* vpp_m = NULL;
161   EVENT_HEADER* vpp_m_header = NULL;
162   int bytes_in_this_round;
163   int bytes_out_this_round;
164   int packets_in_this_round;
165   int packets_out_this_round;
166   vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
167   vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
168   struct timeval time_val;
169   //time_t start_epoch;
170   //time_t last_epoch;
171   char hostname[BUFSIZE];
172   char* fqdn = argv[1];
173   int port = atoi(argv[2]);
174   char* vnic = argv[3];
175   MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
176   //struct timeval tv_start;
177
178   printf("\nVector Packet Processing (VPP) measurement collection\n");
179   fflush(stdout);
180
181   if (argc != 4)
182   {
183     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
184     exit(-1);
185   }
186   srand(time(NULL));
187
188   /**************************************************************************/
189   /* Initialize                                                             */
190   /**************************************************************************/
191   if(evel_initialize(fqdn,                         /* FQDN                  */
192                      port,                         /* Port                  */
193                      NULL,                         /* optional path         */
194                      NULL,                         /* optional topic        */
195                      0,                            /* HTTPS?                */
196                      "",                           /* Username              */
197                      "",                           /* Password              */
198                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
199                      "vFirewall",                  /* Role                  */
200                      1))                           /* Verbosity             */
201   {
202     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
203     exit(-1);
204   }
205   else
206   {
207     printf("\nInitialization completed\n");
208   }
209
210   gethostname(hostname, BUFSIZE);
211   memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
212   read_vpp_metrics(last_vpp_metrics, vnic);
213   gettimeofday(&time_val, NULL);
214   epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
215   sleep(READ_INTERVAL);
216
217   /***************************************************************************/
218   /* Collect metrics from the VNIC                                           */
219   /***************************************************************************/
220   while(1) {
221     memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
222     read_vpp_metrics(curr_vpp_metrics, vnic);
223
224     if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
225       bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
226     }
227     else {
228       bytes_in_this_round = 0;
229     }
230     if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
231       bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
232     }
233     else {
234       bytes_out_this_round = 0;
235     }
236     if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
237       packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
238     }
239     else {
240       packets_in_this_round = 0;
241     }
242     if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
243       packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
244     }
245     else {
246       packets_out_this_round = 0;
247     }
248
249     vpp_m = evel_new_measurement(READ_INTERVAL,"Measurement_vVNF","TrafficStats_1.2.3.4");
250     vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
251     evel_meas_vnic_performance_add(vpp_m, vnic_performance);
252
253     if(vpp_m != NULL) {
254       printf("New measurement report created...\n");
255
256       evel_measurement_type_set(vpp_m, "HTTP request rate");
257       evel_measurement_request_rate_set(vpp_m, rand()%10000);
258
259       evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
260       evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
261
262       evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
263       evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
264       evel_get_cpu_stats(vpp_m);
265
266       /***************************************************************************/
267       /* Set parameters in the MEASUREMENT header packet                         */
268       /***************************************************************************/
269       struct timeval tv_now;
270       gettimeofday(&tv_now, NULL);
271       unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
272
273       //last_epoch = start_epoch + READ_INTERVAL * 1000000;
274       vpp_m_header = (EVENT_HEADER *)vpp_m;
275       //vpp_m_header->start_epoch_microsec = start_epoch;
276       //vpp_m_header->last_epoch_microsec = last_epoch;
277       evel_start_epoch_set(&vpp_m->header, epoch_start);
278       evel_last_epoch_set(&vpp_m->header, epoch_now);
279       epoch_start = epoch_now;
280
281       evel_nfcnamingcode_set(&vpp_m->header, "vVNF");
282       evel_nfnamingcode_set(&vpp_m->header, "vVNF");
283       //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
284       //strcpy(vpp_m_header->reporting_entity_name, hostname);
285       evel_reporting_entity_name_set(&vpp_m->header, "fwll");
286       evel_reporting_entity_id_set(&vpp_m->header, "No UUID available");
287       evel_rc = evel_post_event(vpp_m_header);
288
289       if(evel_rc == EVEL_SUCCESS) {
290         printf("Measurement report correctly sent to the collector!\n");
291       }
292       else {
293         printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
294       }
295     }
296     else {
297       printf("New measurement report failed (%s)\n", evel_error_string());
298     }
299
300     last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
301     last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
302     last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
303     last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
304     //gettimeofday(&time_val, NULL);
305     //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
306
307     sleep(READ_INTERVAL);
308   }
309
310   /***************************************************************************/
311   /* Terminate                                                               */
312   /***************************************************************************/
313   sleep(1);
314   free(last_vpp_metrics);
315   free(curr_vpp_metrics);
316   evel_terminate();
317   printf("Terminated\n");
318
319   return 0;
320 }
321
322 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
323   // Define an array of char that contains the parameters of the unix 'cut' command
324   char* params[] = {"-f3", "-f11", "-f4", "-f12"};
325   // Define the unix command to execute in order to read metrics from the vNIC
326   char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
327   char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
328   char cmd[BUFSIZE];
329   // Define other variables
330   char buf[BUFSIZE];            /* buffer used to store VPP metrics     */
331   int temp[] = {0, 0, 0, 0};    /* temp array that contains VPP values  */
332   FILE *fp;                     /* file descriptor to pipe cmd to shell */
333   int i;
334
335   for(i = 0; i < 4; i++) {
336     // Clear buffers
337     memset(buf, 0, BUFSIZE);
338     memset(cmd, 0, BUFSIZE);
339     // Build shell command to read metrics from the vNIC
340     strcat(cmd, cmd_prefix);
341     strcat(cmd, vnic);
342     strcat(cmd, cmd_mid);
343     strcat(cmd, params[i]);
344     
345     // Open a pipe and read VPP values
346     if ((fp = popen(cmd, "r")) == NULL) {
347         printf("Error opening pipe!\n");
348         return;
349     }
350
351     while (fgets(buf, BUFSIZE, fp) != NULL);
352     temp[i] = atoi(buf);
353
354     if(pclose(fp))  {
355         printf("Command not found or exited with error status\n");
356         return;
357     }
358   }
359
360   // Store metrics read from the vNIC in the struct passed from the main function
361   vpp_metrics->bytes_in = temp[0];
362   vpp_metrics->bytes_out = temp[1];
363   vpp_metrics->packets_in = temp[2];
364   vpp_metrics->packets_out = temp[3];
365 }