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