2 /*************************************************************************//**
4 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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.
17 ****************************************************************************/
28 #define READ_INTERVAL 10
30 typedef struct dummy_vpp_metrics_struct {
37 void read_vpp_metrics(vpp_metrics_struct *, char *);
39 unsigned long long epoch_start = 0;
42 int main(int argc, char** argv)
44 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
45 EVENT_MEASUREMENT* vpp_m = NULL;
46 EVENT_HEADER* vpp_m_header = NULL;
47 int bytes_in_this_round;
48 int bytes_out_this_round;
49 int packets_in_this_round;
50 int packets_out_this_round;
51 vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
52 vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
53 struct timeval time_val;
56 char hostname[BUFSIZE];
58 int port = atoi(argv[2]);
60 MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
61 //struct timeval tv_start;
63 printf("\nVector Packet Processing (VPP) measurement collection\n");
68 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
72 /**************************************************************************/
74 /**************************************************************************/
75 if(evel_initialize(fqdn, /* FQDN */
77 NULL, /* optional path */
78 NULL, /* optional topic */
82 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
83 "vFirewall", /* Role */
86 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
91 printf("\nInitialization completed\n");
94 gethostname(hostname, BUFSIZE);
95 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
96 read_vpp_metrics(last_vpp_metrics, vnic);
97 gettimeofday(&time_val, NULL);
98 epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
101 /***************************************************************************/
102 /* Collect metrics from the VNIC */
103 /***************************************************************************/
105 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
106 read_vpp_metrics(curr_vpp_metrics, vnic);
108 if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
109 bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
112 bytes_in_this_round = 0;
114 if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
115 bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
118 bytes_out_this_round = 0;
120 if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
121 packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
124 packets_in_this_round = 0;
126 if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
127 packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
130 packets_out_this_round = 0;
133 vpp_m = evel_new_measurement(READ_INTERVAL);
134 vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
135 evel_meas_vnic_performance_add(vpp_m, vnic_performance);
138 printf("New measurement report created...\n");
141 evel_vnic_performance_rx_total_pkt_acc_set(vnic_performance, packets_in_this_round);
142 evel_vnic_performance_tx_total_pkt_acc_set(vnic_performance, packets_out_this_round);
144 evel_vnic_performance_rx_octets_acc_set(vnic_performance, bytes_in_this_round);
145 evel_vnic_performance_tx_octets_acc_set(vnic_performance, bytes_out_this_round);
147 /***************************************************************************/
148 /* Set parameters in the MEASUREMENT header packet */
149 /***************************************************************************/
150 struct timeval tv_now;
151 gettimeofday(&tv_now, NULL);
152 unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
154 //last_epoch = start_epoch + READ_INTERVAL * 1000000;
155 vpp_m_header = (EVENT_HEADER *)vpp_m;
156 //vpp_m_header->start_epoch_microsec = start_epoch;
157 //vpp_m_header->last_epoch_microsec = last_epoch;
158 evel_start_epoch_set(&vpp_m->header, epoch_start);
159 evel_last_epoch_set(&vpp_m->header, epoch_now);
160 epoch_start = epoch_now;
162 strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
163 strcpy(vpp_m_header->reporting_entity_name, hostname);
164 evel_rc = evel_post_event(vpp_m_header);
166 if(evel_rc == EVEL_SUCCESS) {
167 printf("Measurement report correctly sent to the collector!\n");
170 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
174 printf("New measurement report failed (%s)\n", evel_error_string());
177 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
178 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
179 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
180 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
181 //gettimeofday(&time_val, NULL);
182 //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
184 sleep(READ_INTERVAL);
187 /***************************************************************************/
189 /***************************************************************************/
191 free(last_vpp_metrics);
192 free(curr_vpp_metrics);
194 printf("Terminated\n");
199 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
200 // Define an array of char that contains the parameters of the unix 'cut' command
201 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
202 // Define the unix command to execute in order to read metrics from the vNIC
203 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
204 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
206 // Define other variables
207 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
208 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
209 FILE *fp; /* file descriptor to pipe cmd to shell */
212 for(i = 0; i < 4; i++) {
214 memset(buf, 0, BUFSIZE);
215 memset(cmd, 0, BUFSIZE);
216 // Build shell command to read metrics from the vNIC
217 strcat(cmd, cmd_prefix);
219 strcat(cmd, cmd_mid);
220 strcat(cmd, params[i]);
222 // Open a pipe and read VPP values
223 if ((fp = popen(cmd, "r")) == NULL) {
224 printf("Error opening pipe!\n");
228 while (fgets(buf, BUFSIZE, fp) != NULL);
232 printf("Command not found or exited with error status\n");
237 // Store metrics read from the vNIC in the struct passed from the main function
238 vpp_metrics->bytes_in = temp[0];
239 vpp_metrics->bytes_out = temp[1];
240 vpp_metrics->packets_in = temp[2];
241 vpp_metrics->packets_out = temp[3];