Update INFO.yaml with new PTL
[demo.git] / vnfs / VESreporting_vFW5.0 / vpp_measurement_reporter.c
1
2 /*************************************************************************//**
3  *
4  * Copyright © 2019 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 int main(int argc, char** argv)
40 {
41   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
42   EVENT_MEASUREMENT* vpp_m = NULL;
43   EVENT_HEADER* vpp_m_header = NULL;
44   EVENT_HEADER* batch_header = NULL;
45   MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
46   int bytes_in_this_round;
47   int bytes_out_this_round;
48   int packets_in_this_round;
49   int packets_out_this_round;
50   vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
51   vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
52   struct timeval time_val;
53   time_t start_epoch;
54   time_t last_epoch;
55   char hostname[BUFSIZE];
56   char eventName[BUFSIZE];
57   char eventId[BUFSIZE];
58   char* fqdn2 = NULL;
59   int port2 = 0;
60   char * vnic = NULL;
61   memset(eventName, 0, BUFSIZE);
62   memset(eventId, 0, BUFSIZE);
63   memset(hostname, 0, BUFSIZE);
64
65   strcpy(eventName, "vFirewallBroadcastPackets");
66   strcpy(eventId, "mvfs00000001");
67
68   char* fqdn = argv[1];
69   int port = atoi(argv[2]);
70   char* caFile = "/opt/config/onap-ca.crt";
71   char* userName = "sample1";
72   char* passWord = "sample1";
73
74   if(argc == 6)
75   {
76      fqdn2 = argv[3];
77      port2 = atoi(argv[4]);
78      vnic = argv[5];
79   }
80   else
81      vnic = argv[3];
82
83   printf("\nVector Packet Processing (VPP) measurement collection\n");
84   fflush(stdout);
85
86   if (!((argc == 4) || (argc == 6)))
87   {
88     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <FQDN>|<IP address> <port> <interface> \n", argv[0]);
89     fprintf(stderr, "OR\n");
90     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
91     exit(-1);
92   }
93
94   /**************************************************************************/
95   /* Initialize                                                             */
96   /**************************************************************************/
97   if(evel_initialize(fqdn,                         /* FQDN                  */
98                      port,                         /* Port                  */
99                      fqdn2,                        /* Backup FQDN           */
100                      port2,                        /* Backup port           */
101                      NULL,                         /* optional path         */
102                      NULL,                         /* optional topic        */
103                      100,                          /* Ring Buffer size      */
104                      1,                            /* HTTPS?                */
105                      NULL,                         /* cert file             */
106                      NULL,                         /* key  file             */
107                      caFile,                       /* ca   file             */
108                      NULL,                         /* ca   directory        */
109                      0,                            /* verify peer           */
110                      0,                            /* verify host           */
111                      userName,                     /* Username              */
112                      passWord,                     /* Password              */
113                      "sample1",                    /* Username2             */
114                      "sample1",                    /* Password2             */
115                      NULL,                         /* Source ip             */
116                      NULL,                         /* Source ip2            */
117                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
118                      "vFirewall",                  /* Role                  */
119                      1))                           /* Verbosity             */
120
121   {
122     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
123     exit(-1);
124   }
125   else
126   {
127     printf("\nInitialization completed\n");
128   }
129
130   gethostname(hostname, BUFSIZE);
131   memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
132   read_vpp_metrics(last_vpp_metrics, vnic);
133   gettimeofday(&time_val, NULL);
134   start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
135   sleep(READ_INTERVAL);
136
137   /***************************************************************************/
138   /* Collect metrics from the VNIC                                           */
139   /***************************************************************************/
140   while(1) {
141     memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
142     read_vpp_metrics(curr_vpp_metrics, vnic);
143
144     if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
145       bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
146     }
147     else {
148       bytes_in_this_round = 0;
149     }
150     if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
151       bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
152     }
153     else {
154       bytes_out_this_round = 0;
155     }
156     if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
157       packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
158     }
159     else {
160       packets_in_this_round = 0;
161     }
162     if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
163       packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
164     }
165     else {
166       packets_out_this_round = 0;
167     }
168
169     vpp_m = evel_new_measurement(READ_INTERVAL, eventName, eventId);
170
171     if(vpp_m != NULL) {
172       printf("New measurement report created...\n");
173       vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance(vnic, "true");
174       evel_meas_vnic_performance_add(vpp_m, vnic_performance);
175       evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
176       evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
177
178       evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
179       evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
180
181       /***************************************************************************/
182       /* Set parameters in the MEASUREMENT header packet                         */
183       /***************************************************************************/
184       last_epoch = start_epoch + READ_INTERVAL * 1000000;
185       vpp_m_header = (EVENT_HEADER *)vpp_m;
186       vpp_m_header->start_epoch_microsec = start_epoch;
187       vpp_m_header->last_epoch_microsec = last_epoch;
188       evel_reporting_entity_id_set(vpp_m_header, "No UUID available");
189       printf("1111\n");
190       evel_reporting_entity_name_set(vpp_m_header, hostname);
191       printf("1111\n");
192       // evel_rc = evel_post_event(vpp_m_header);
193       batch_header = evel_new_batch("batch_event_name", "bevent_id");
194       evel_batch_add_event(batch_header, vpp_m_header);
195       evel_rc = evel_post_event(batch_header);
196
197       if(evel_rc == EVEL_SUCCESS) {
198         printf("Measurement report correctly sent to the collector!\n");
199       }
200       else {
201         printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
202       }
203     }
204     else {
205       printf("New measurement report failed (%s)\n", evel_error_string());
206     }
207
208     last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
209     last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
210     last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
211     last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
212     gettimeofday(&time_val, NULL);
213     start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
214
215     sleep(READ_INTERVAL);
216   }
217
218   /***************************************************************************/
219   /* Terminate                                                               */
220   /***************************************************************************/
221   sleep(1);
222   free(last_vpp_metrics);
223   free(curr_vpp_metrics);
224   evel_terminate();
225   printf("Terminated\n");
226
227   return 0;
228 }
229
230 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
231   // Define an array of char that contains the parameters of the unix 'cut' command
232   char* params[] = {"-f3", "-f11", "-f4", "-f12"};
233   // Define the unix command to execute in order to read metrics from the vNIC
234   char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
235   char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
236   char cmd[BUFSIZE];
237   // Define other variables
238   char buf[BUFSIZE];            /* buffer used to store VPP metrics     */
239   int temp[] = {0, 0, 0, 0};    /* temp array that contains VPP values  */
240   FILE *fp;                     /* file descriptor to pipe cmd to shell */
241   int i;
242
243   for(i = 0; i < 4; i++) {
244     // Clear buffers
245     memset(buf, 0, BUFSIZE);
246     memset(cmd, 0, BUFSIZE);
247     // Build shell command to read metrics from the vNIC
248     strcat(cmd, cmd_prefix);
249     strcat(cmd, vnic);
250     strcat(cmd, cmd_mid);
251     strcat(cmd, params[i]);
252     
253     // Open a pipe and read VPP values
254     if ((fp = popen(cmd, "r")) == NULL) {
255         printf("Error opening pipe!\n");
256         return;
257     }
258
259     while (fgets(buf, BUFSIZE, fp) != NULL);
260     temp[i] = atoi(buf);
261
262     if(pclose(fp))  {
263         printf("Command not found or exited with error status\n");
264         return;
265     }
266   }
267
268   // Store metrics read from the vNIC in the struct passed from the main function
269   vpp_metrics->bytes_in = temp[0];
270   vpp_metrics->bytes_out = temp[1];
271   vpp_metrics->packets_in = temp[2];
272   vpp_metrics->packets_out = temp[3];
273 }