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 ****************************************************************************/
29 #define READ_INTERVAL 10
31 typedef struct dummy_vpp_metrics_struct {
38 void read_vpp_metrics(vpp_metrics_struct *, char *);
40 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;
62 printf("\nVector Packet Processing (VPP) measurement collection\n");
67 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
71 /**************************************************************************/
73 /**************************************************************************/
74 if(evel_initialize(fqdn, /* FQDN */
76 NULL, /* optional path */
77 NULL, /* optional topic */
81 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
82 "vLoadBalancer", /* Role */
85 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
90 printf("\nInitialization completed\n");
93 gethostname(hostname, BUFSIZE);
94 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
95 read_vpp_metrics(last_vpp_metrics, vnic);
96 gettimeofday(&time_val, NULL);
97 epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
100 /***************************************************************************/
101 /* Collect metrics from the VNIC */
102 /***************************************************************************/
104 // Read the number of vDNSs currently active
106 FILE* fd = fopen("active_dns.txt", "r");
108 if(fscanf(fd, "%d", &active_dns) != 1) /* Avoid infinite loops if the file doesn't contain exactly one number */
113 printf("Error when closing file\n");
117 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
118 read_vpp_metrics(curr_vpp_metrics, vnic);
120 if(active_dns > 0 && (curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0)) {
121 bytes_in_this_round = (int) round((curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in) / active_dns);
124 bytes_in_this_round = 0;
126 if(active_dns > 0 && (curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0)) {
127 bytes_out_this_round = (int) round((curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out) / active_dns);
130 bytes_out_this_round = 0;
132 if(active_dns > 0 && (curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0)) {
133 packets_in_this_round = (int) round((curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in) / active_dns);
136 packets_in_this_round = 0;
138 if(active_dns > 0 && (curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0)) {
139 packets_out_this_round = (int) round((curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out) / active_dns);
142 packets_out_this_round = 0;
145 vpp_m = evel_new_measurement(READ_INTERVAL);
146 vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
147 evel_meas_vnic_performance_add(vpp_m, vnic_performance);
150 printf("New measurement report created...\n");
152 evel_vnic_performance_rx_total_pkt_acc_set(vnic_performance, packets_in_this_round);
153 evel_vnic_performance_tx_total_pkt_acc_set(vnic_performance, packets_out_this_round);
155 evel_vnic_performance_rx_octets_acc_set(vnic_performance, bytes_in_this_round);
156 evel_vnic_performance_tx_octets_acc_set(vnic_performance, bytes_out_this_round);
158 /***************************************************************************/
159 /* Set parameters in the MEASUREMENT header packet */
160 /***************************************************************************/
161 struct timeval tv_now;
162 gettimeofday(&tv_now, NULL);
163 unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
165 //last_epoch = start_epoch + READ_INTERVAL * 1000000;
166 vpp_m_header = (EVENT_HEADER *)vpp_m;
167 //vpp_m_header->start_epoch_microsec = start_epoch;
168 //vpp_m_header->last_epoch_microsec = last_epoch;
169 evel_start_epoch_set(&vpp_m->header, epoch_start);
170 evel_last_epoch_set(&vpp_m->header, epoch_now);
171 epoch_start = epoch_now;
173 strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
174 strcpy(vpp_m_header->reporting_entity_name, hostname);
175 evel_rc = evel_post_event(vpp_m_header);
177 if(evel_rc == EVEL_SUCCESS) {
178 printf("Measurement report correctly sent to the collector!\n");
181 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
185 printf("New measurement report failed (%s)\n", evel_error_string());
188 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
189 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
190 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
191 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
192 //gettimeofday(&time_val, NULL);
193 //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
195 sleep(READ_INTERVAL);
198 /***************************************************************************/
200 /***************************************************************************/
202 evel_free_measurement(vpp_m);
203 free(last_vpp_metrics);
204 free(curr_vpp_metrics);
206 printf("Terminated\n");
211 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
212 // Define an array of char that contains the parameters of the unix 'cut' command
213 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
214 // Define the unix command to execute in order to read metrics from the vNIC
215 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
216 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
218 // Define other variables
219 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
220 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
221 FILE *fp; /* file descriptor to pipe cmd to shell */
224 for(i = 0; i < 4; i++) {
226 memset(buf, 0, BUFSIZE);
227 memset(cmd, 0, BUFSIZE);
228 // Build shell command to read metrics from the vNIC
229 strcat(cmd, cmd_prefix);
231 strcat(cmd, cmd_mid);
232 strcat(cmd, params[i]);
234 // Open a pipe and read VPP values
235 if ((fp = popen(cmd, "r")) == NULL) {
236 printf("Error opening pipe!\n");
240 while (fgets(buf, BUFSIZE, fp) != NULL);
244 printf("Command not found or exited with error status\n");
249 // Store metrics read from the vNIC in the struct passed from the main function
250 vpp_metrics->bytes_in = temp[0];
251 vpp_metrics->bytes_out = temp[1];
252 vpp_metrics->packets_in = temp[2];
253 vpp_metrics->packets_out = temp[3];