Merge "Implement sequencing for faults only"
[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                      100,                          /* Ring Buffer size      */
195                      0,                            /* HTTPS?                */
196                      NULL,                         /* cert file             */
197                      NULL,                         /* key  file             */
198                      NULL,                         /* ca   info             */
199                      NULL,                         /* ca   file             */
200                      0,                            /* verify peer           */
201                      0,                            /* verify host           */
202                      "",                           /* Username              */
203                      "",                           /* Password              */
204                      NULL,                         /* Source ip             */
205                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
206                      "vFirewall",                  /* Role                  */
207                      1))                           /* Verbosity             */
208   {
209     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
210     exit(-1);
211   }
212   else
213   {
214     printf("\nInitialization completed\n");
215   }
216
217   gethostname(hostname, BUFSIZE);
218   memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
219   read_vpp_metrics(last_vpp_metrics, vnic);
220   gettimeofday(&time_val, NULL);
221   epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
222   sleep(READ_INTERVAL);
223
224   /***************************************************************************/
225   /* Collect metrics from the VNIC                                           */
226   /***************************************************************************/
227   while(1) {
228     memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
229     read_vpp_metrics(curr_vpp_metrics, vnic);
230
231     if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
232       bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
233     }
234     else {
235       bytes_in_this_round = 0;
236     }
237     if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
238       bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
239     }
240     else {
241       bytes_out_this_round = 0;
242     }
243     if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
244       packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
245     }
246     else {
247       packets_in_this_round = 0;
248     }
249     if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
250       packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
251     }
252     else {
253       packets_out_this_round = 0;
254     }
255
256     vpp_m = evel_new_measurement(READ_INTERVAL,"vFirewallBroadcastPackets","TrafficStats_1.2.3.4");
257     vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
258     evel_meas_vnic_performance_add(vpp_m, vnic_performance);
259
260     if(vpp_m != NULL) {
261       printf("New measurement report created...\n");
262
263       evel_measurement_type_set(vpp_m, "HTTP request rate");
264       evel_measurement_request_rate_set(vpp_m, rand()%10000);
265
266       evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
267       evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
268
269       evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
270       evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
271       evel_get_cpu_stats(vpp_m);
272
273       /***************************************************************************/
274       /* Set parameters in the MEASUREMENT header packet                         */
275       /***************************************************************************/
276       struct timeval tv_now;
277       gettimeofday(&tv_now, NULL);
278       unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
279
280       //last_epoch = start_epoch + READ_INTERVAL * 1000000;
281       vpp_m_header = (EVENT_HEADER *)vpp_m;
282       //vpp_m_header->start_epoch_microsec = start_epoch;
283       //vpp_m_header->last_epoch_microsec = last_epoch;
284       evel_start_epoch_set(&vpp_m->header, epoch_start);
285       evel_last_epoch_set(&vpp_m->header, epoch_now);
286       epoch_start = epoch_now;
287
288       evel_nfcnamingcode_set(&vpp_m->header, "vVNF");
289       evel_nfnamingcode_set(&vpp_m->header, "vVNF");
290       //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
291       //strcpy(vpp_m_header->reporting_entity_name, hostname);
292       evel_reporting_entity_name_set(&vpp_m->header, "fwll");
293       evel_reporting_entity_id_set(&vpp_m->header, "No UUID available");
294       evel_rc = evel_post_event(vpp_m_header);
295
296       if(evel_rc == EVEL_SUCCESS) {
297         printf("Measurement report correctly sent to the collector!\n");
298       }
299       else {
300         printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
301       }
302     }
303     else {
304       printf("New measurement report failed (%s)\n", evel_error_string());
305     }
306
307     last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
308     last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
309     last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
310     last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
311     //gettimeofday(&time_val, NULL);
312     //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
313
314     sleep(READ_INTERVAL);
315   }
316
317   /***************************************************************************/
318   /* Terminate                                                               */
319   /***************************************************************************/
320   sleep(1);
321   free(last_vpp_metrics);
322   free(curr_vpp_metrics);
323   evel_terminate();
324   printf("Terminated\n");
325
326   return 0;
327 }
328
329 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
330   // Define an array of char that contains the parameters of the unix 'cut' command
331   char* params[] = {"-f3", "-f11", "-f4", "-f12"};
332   // Define the unix command to execute in order to read metrics from the vNIC
333   char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
334   char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
335   char cmd[BUFSIZE];
336   // Define other variables
337   char buf[BUFSIZE];            /* buffer used to store VPP metrics     */
338   int temp[] = {0, 0, 0, 0};    /* temp array that contains VPP values  */
339   FILE *fp;                     /* file descriptor to pipe cmd to shell */
340   int i;
341
342   for(i = 0; i < 4; i++) {
343     // Clear buffers
344     memset(buf, 0, BUFSIZE);
345     memset(cmd, 0, BUFSIZE);
346     // Build shell command to read metrics from the vNIC
347     strcat(cmd, cmd_prefix);
348     strcat(cmd, vnic);
349     strcat(cmd, cmd_mid);
350     strcat(cmd, params[i]);
351     
352     // Open a pipe and read VPP values
353     if ((fp = popen(cmd, "r")) == NULL) {
354         printf("Error opening pipe!\n");
355         return;
356     }
357
358     while (fgets(buf, BUFSIZE, fp) != NULL);
359     temp[i] = atoi(buf);
360
361     if(pclose(fp))  {
362         printf("Command not found or exited with error status\n");
363         return;
364     }
365   }
366
367   // Store metrics read from the vNIC in the struct passed from the main function
368   vpp_metrics->bytes_in = temp[0];
369   vpp_metrics->bytes_out = temp[1];
370   vpp_metrics->packets_in = temp[2];
371   vpp_metrics->packets_out = temp[3];
372 }