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;
47 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
55 int concurrent_sessions = 0;
56 int configured_entities = 0;
57 double mean_request_latency = 0;
58 double measurement_interval = 1;
59 double memory_configured = 0;
60 double memory_used = 0;
66 printf("Checking app traffic\n");
68 timeinfo = localtime (&rawtime);
69 strftime(period,21,"%d/%b/%Y:%H:%M:",timeinfo);
70 strftime(secs,3,"%S",timeinfo);
72 if (sec == 0) sec = 59;
73 sprintf(secs, "%02d", sec);
74 strncat(period, secs, 21);
75 // ....x....1....x....2.
76 // 15/Oct/2016:17:51:19
77 strcpy(cmd, "sudo docker logs vHello | grep -c ");
78 strncat(cmd, period, 100);
82 EVEL_ERROR("popen failed to execute command");
85 if (fgets(count, 10, fp) != NULL) {
86 request_rate = atoi(count);
87 printf("Reporting request rate for second: %s as %d\n", period, request_rate);
91 EVEL_ERROR("New Measurement failed");
93 printf("Processed measurement\n");
97 EVEL_ERROR("pclose returned an error");
105 int main(int argc, char** argv)
107 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
108 EVENT_MEASUREMENT* vpp_m = NULL;
109 EVENT_HEADER* vpp_m_header = NULL;
110 int bytes_in_this_round;
111 int bytes_out_this_round;
112 int packets_in_this_round;
113 int packets_out_this_round;
114 vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
115 vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
116 struct timeval time_val;
117 //time_t start_epoch;
119 char hostname[BUFSIZE];
120 char* fqdn = argv[1];
121 int port = atoi(argv[2]);
122 char* vnic = argv[3];
129 port2 = atoi(argv[4]);
135 MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
137 printf("\nVector Packet Processing (VPP) measurement collection\n");
140 if (!((argc == 6) || (argc == 4)))
142 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <FQDN>|<IP address> <port> <interface> \n", argv[0]);
143 fprintf(stderr, "OR\n");
144 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface> \n", argv[0]);
150 /**************************************************************************/
152 /**************************************************************************/
153 if(evel_initialize(fqdn, /* FQDN */
155 fqdn2, /* Backup FQDN */
156 port2, /* Backup port */
157 NULL, /* optional path */
158 NULL, /* optional topic */
159 100, /* Ring Buffer size */
161 NULL, /* cert file */
167 "sample1", /* Username */
168 "sample1", /* Password */
169 "sample1", /* Username2 */
170 "sample1", /* Password2 */
171 NULL, /* Source ip */
172 NULL, /* Backup Source IP */
173 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
174 "vLoadBalancer", /* Role */
177 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
182 printf("\nInitialization completed\n");
185 gethostname(hostname, BUFSIZE);
186 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
187 read_vpp_metrics(last_vpp_metrics, vnic);
188 gettimeofday(&time_val, NULL);
189 epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
190 sleep(READ_INTERVAL);
192 /***************************************************************************/
193 /* Collect metrics from the VNIC */
194 /***************************************************************************/
196 // Read the number of vDNSs currently active
198 FILE* fd = fopen("active_dns.txt", "r");
200 if(fscanf(fd, "%d", &active_dns) != 1) /* Avoid infinite loops if the file doesn't contain exactly one number */
205 printf("Error when closing file\n");
209 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
210 read_vpp_metrics(curr_vpp_metrics, vnic);
212 if(active_dns > 0 && (curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0)) {
213 bytes_in_this_round = (int) round((curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in) / active_dns);
216 bytes_in_this_round = 0;
218 if(active_dns > 0 && (curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0)) {
219 bytes_out_this_round = (int) round((curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out) / active_dns);
222 bytes_out_this_round = 0;
224 if(active_dns > 0 && (curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0)) {
225 packets_in_this_round = (int) round((curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in) / active_dns);
228 packets_in_this_round = 0;
230 if(active_dns > 0 && (curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0)) {
231 packets_out_this_round = (int) round((curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out) / active_dns);
234 packets_out_this_round = 0;
237 vpp_m = evel_new_measurement(READ_INTERVAL,"vLoadBalancer","TrafficStats_1.2.3.4");
238 vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance(vnic, "true");
239 evel_meas_vnic_performance_add(vpp_m, vnic_performance);
242 printf("New measurement report created...\n");
244 evel_measurement_type_set(vpp_m, "HTTP request rate");
245 evel_measurement_request_rate_set(vpp_m, rand()%10000);
247 evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
248 evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
250 evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
251 evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
253 /***************************************************************************/
254 /* Set parameters in the MEASUREMENT header packet */
255 /***************************************************************************/
256 struct timeval tv_now;
257 gettimeofday(&tv_now, NULL);
258 unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
260 //last_epoch = start_epoch + READ_INTERVAL * 1000000;
261 vpp_m_header = (EVENT_HEADER *)vpp_m;
262 //vpp_m_header->start_epoch_microsec = start_epoch;
263 //vpp_m_header->last_epoch_microsec = last_epoch;
264 evel_start_epoch_set(&vpp_m->header, epoch_start);
265 evel_last_epoch_set(&vpp_m->header, epoch_now);
266 epoch_start = epoch_now;
268 evel_nfcnamingcode_set(&vpp_m->header, "vVNF");
269 evel_nfnamingcode_set(&vpp_m->header, "vVNF");
270 //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
271 //strcpy(vpp_m_header->reporting_entity_name, hostname);
272 evel_reporting_entity_name_set(&vpp_m->header, "lbll");
273 evel_reporting_entity_id_set(&vpp_m->header, "No UUID available");
274 evel_rc = evel_post_event(vpp_m_header);
276 if(evel_rc == EVEL_SUCCESS) {
277 printf("Measurement report correctly sent to the collector!\n");
280 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
284 printf("New measurement report failed (%s)\n", evel_error_string());
287 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
288 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
289 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
290 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
291 //gettimeofday(&time_val, NULL);
292 //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
294 sleep(READ_INTERVAL);
297 /***************************************************************************/
299 /***************************************************************************/
301 evel_free_measurement(vpp_m);
302 free(last_vpp_metrics);
303 free(curr_vpp_metrics);
305 printf("Terminated\n");
310 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
311 // Define an array of char that contains the parameters of the unix 'cut' command
312 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
313 // Define the unix command to execute in order to read metrics from the vNIC
314 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
315 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
317 // Define other variables
318 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
319 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
320 FILE *fp; /* file descriptor to pipe cmd to shell */
323 for(i = 0; i < 4; i++) {
325 memset(buf, 0, BUFSIZE);
326 memset(cmd, 0, BUFSIZE);
327 // Build shell command to read metrics from the vNIC
328 strcat(cmd, cmd_prefix);
330 strcat(cmd, cmd_mid);
331 strcat(cmd, params[i]);
333 // Open a pipe and read VPP values
334 if ((fp = popen(cmd, "r")) == NULL) {
335 printf("Error opening pipe!\n");
339 while (fgets(buf, BUFSIZE, fp) != NULL);
343 printf("Command not found or exited with error status\n");
348 // Store metrics read from the vNIC in the struct passed from the main function
349 vpp_metrics->bytes_in = temp[0];
350 vpp_metrics->bytes_out = temp[1];
351 vpp_metrics->packets_in = temp[2];
352 vpp_metrics->packets_out = temp[3];