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;
45 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
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;
64 printf("Checking app traffic\n");
66 timeinfo = localtime (&rawtime);
67 strftime(period,21,"%d/%b/%Y:%H:%M:",timeinfo);
68 strftime(secs,3,"%S",timeinfo);
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);
80 EVEL_ERROR("popen failed to execute command");
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);
89 EVEL_ERROR("New Measurement failed");
91 printf("Processed measurement\n");
95 EVEL_ERROR("pclose returned an error");
104 /**************************************************************************//**
106 *****************************************************************************/
107 void evel_get_cpu_stats(EVENT_MEASUREMENT * measurement)
120 MEASUREMENT_CPU_USE *cpu_use = NULL;
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");
126 printf("Failed to run command\n" );
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);
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);
156 int main(int argc, char** argv)
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;
170 char hostname[BUFSIZE];
171 char* fqdn = argv[1];
172 int port = atoi(argv[2]);
173 char* vnic = argv[3];
177 MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
178 //struct timeval tv_start;
183 port2 = atoi(argv[4]);
187 printf("\nVector Packet Processing (VPP) measurement collection\n");
190 if (!((argc == 6) || (argc == 4)))
192 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <FQDN>|<IP address> <port> <interface> \n", argv[0]);
193 fprintf(stderr, "OR\n");
194 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface> \n", argv[0]);
200 /**************************************************************************/
202 /**************************************************************************/
203 if(evel_initialize(fqdn, /* FQDN */
205 fqdn2, /* Backup FQDN */
206 port2, /* Backup port */
207 NULL, /* optional path */
208 NULL, /* optional topic */
209 100, /* Ring Buffer size */
211 NULL, /* cert file */
217 "sample1", /* Username */
218 "sample1", /* Password */
219 "sample1", /* Username2 */
220 "sample1", /* Password2 */
221 NULL, /* Source ip */
222 NULL, /* Backup Source IP */
223 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
224 "vFirewall", /* Role */
227 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
232 printf("\nInitialization completed\n");
235 gethostname(hostname, BUFSIZE);
236 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
237 read_vpp_metrics(last_vpp_metrics, vnic);
238 gettimeofday(&time_val, NULL);
239 epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
240 sleep(READ_INTERVAL);
242 /***************************************************************************/
243 /* Collect metrics from the VNIC */
244 /***************************************************************************/
246 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
247 read_vpp_metrics(curr_vpp_metrics, vnic);
249 if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
250 bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
253 bytes_in_this_round = 0;
255 if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
256 bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
259 bytes_out_this_round = 0;
261 if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
262 packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
265 packets_in_this_round = 0;
267 if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
268 packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
271 packets_out_this_round = 0;
274 vpp_m = evel_new_measurement(READ_INTERVAL,"vFirewallBroadcastPackets","TrafficStats_1.2.3.4");
275 vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
276 evel_meas_vnic_performance_add(vpp_m, vnic_performance);
279 printf("New measurement report created...\n");
281 evel_measurement_type_set(vpp_m, "HTTP request rate");
282 evel_measurement_request_rate_set(vpp_m, rand()%10000);
284 evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
285 evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
287 evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
288 evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
289 evel_get_cpu_stats(vpp_m);
291 /***************************************************************************/
292 /* Set parameters in the MEASUREMENT header packet */
293 /***************************************************************************/
294 struct timeval tv_now;
295 gettimeofday(&tv_now, NULL);
296 unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
298 //last_epoch = start_epoch + READ_INTERVAL * 1000000;
299 vpp_m_header = (EVENT_HEADER *)vpp_m;
300 //vpp_m_header->start_epoch_microsec = start_epoch;
301 //vpp_m_header->last_epoch_microsec = last_epoch;
302 evel_start_epoch_set(&vpp_m->header, epoch_start);
303 evel_last_epoch_set(&vpp_m->header, epoch_now);
304 epoch_start = epoch_now;
306 evel_nfcnamingcode_set(&vpp_m->header, "vVNF");
307 evel_nfnamingcode_set(&vpp_m->header, "vVNF");
308 //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
309 //strcpy(vpp_m_header->reporting_entity_name, hostname);
310 evel_reporting_entity_name_set(&vpp_m->header, "fwll");
311 evel_reporting_entity_id_set(&vpp_m->header, "No UUID available");
312 evel_rc = evel_post_event(vpp_m_header);
314 if(evel_rc == EVEL_SUCCESS) {
315 printf("Measurement report correctly sent to the collector!\n");
318 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
322 printf("New measurement report failed (%s)\n", evel_error_string());
325 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
326 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
327 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
328 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
329 //gettimeofday(&time_val, NULL);
330 //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
332 sleep(READ_INTERVAL);
335 /***************************************************************************/
337 /***************************************************************************/
339 free(last_vpp_metrics);
340 free(curr_vpp_metrics);
342 printf("Terminated\n");
347 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
348 // Define an array of char that contains the parameters of the unix 'cut' command
349 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
350 // Define the unix command to execute in order to read metrics from the vNIC
351 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
352 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
354 // Define other variables
355 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
356 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
357 FILE *fp; /* file descriptor to pipe cmd to shell */
360 for(i = 0; i < 4; i++) {
362 memset(buf, 0, BUFSIZE);
363 memset(cmd, 0, BUFSIZE);
364 // Build shell command to read metrics from the vNIC
365 strcat(cmd, cmd_prefix);
367 strcat(cmd, cmd_mid);
368 strcat(cmd, params[i]);
370 // Open a pipe and read VPP values
371 if ((fp = popen(cmd, "r")) == NULL) {
372 printf("Error opening pipe!\n");
376 while (fgets(buf, BUFSIZE, fp) != NULL);
380 printf("Command not found or exited with error status\n");
385 // Store metrics read from the vNIC in the struct passed from the main function
386 vpp_metrics->bytes_in = temp[0];
387 vpp_metrics->bytes_out = temp[1];
388 vpp_metrics->packets_in = temp[2];
389 vpp_metrics->packets_out = temp[3];