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];
174 MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
175 //struct timeval tv_start;
177 printf("\nVector Packet Processing (VPP) measurement collection\n");
182 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
187 /**************************************************************************/
189 /**************************************************************************/
190 if(evel_initialize(fqdn, /* FQDN */
192 NULL, /* optional path */
193 NULL, /* optional topic */
194 100, /* Ring Buffer size */
196 NULL, /* cert file */
204 NULL, /* Source ip */
205 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
206 "vFirewall", /* Role */
209 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
214 printf("\nInitialization completed\n");
217 gethostname(hostname, BUFSIZE);
218 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
219 read_vpp_metrics(last_vpp_metrics, vnic);
220 gettimeofday(&time_val, NULL);
221 epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
222 sleep(READ_INTERVAL);
224 /***************************************************************************/
225 /* Collect metrics from the VNIC */
226 /***************************************************************************/
228 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
229 read_vpp_metrics(curr_vpp_metrics, vnic);
231 if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
232 bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
235 bytes_in_this_round = 0;
237 if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
238 bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
241 bytes_out_this_round = 0;
243 if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
244 packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
247 packets_in_this_round = 0;
249 if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
250 packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
253 packets_out_this_round = 0;
256 vpp_m = evel_new_measurement(READ_INTERVAL,"vFirewallBroadcastPackets","TrafficStats_1.2.3.4");
257 vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
258 evel_meas_vnic_performance_add(vpp_m, vnic_performance);
261 printf("New measurement report created...\n");
263 evel_measurement_type_set(vpp_m, "HTTP request rate");
264 evel_measurement_request_rate_set(vpp_m, rand()%10000);
266 evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
267 evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
269 evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
270 evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
271 evel_get_cpu_stats(vpp_m);
273 /***************************************************************************/
274 /* Set parameters in the MEASUREMENT header packet */
275 /***************************************************************************/
276 struct timeval tv_now;
277 gettimeofday(&tv_now, NULL);
278 unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
280 //last_epoch = start_epoch + READ_INTERVAL * 1000000;
281 vpp_m_header = (EVENT_HEADER *)vpp_m;
282 //vpp_m_header->start_epoch_microsec = start_epoch;
283 //vpp_m_header->last_epoch_microsec = last_epoch;
284 evel_start_epoch_set(&vpp_m->header, epoch_start);
285 evel_last_epoch_set(&vpp_m->header, epoch_now);
286 epoch_start = epoch_now;
288 evel_nfcnamingcode_set(&vpp_m->header, "vVNF");
289 evel_nfnamingcode_set(&vpp_m->header, "vVNF");
290 //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
291 //strcpy(vpp_m_header->reporting_entity_name, hostname);
292 evel_reporting_entity_name_set(&vpp_m->header, "fwll");
293 evel_reporting_entity_id_set(&vpp_m->header, "No UUID available");
294 evel_rc = evel_post_event(vpp_m_header);
296 if(evel_rc == EVEL_SUCCESS) {
297 printf("Measurement report correctly sent to the collector!\n");
300 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
304 printf("New measurement report failed (%s)\n", evel_error_string());
307 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
308 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
309 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
310 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
311 //gettimeofday(&time_val, NULL);
312 //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
314 sleep(READ_INTERVAL);
317 /***************************************************************************/
319 /***************************************************************************/
321 free(last_vpp_metrics);
322 free(curr_vpp_metrics);
324 printf("Terminated\n");
329 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
330 // Define an array of char that contains the parameters of the unix 'cut' command
331 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
332 // Define the unix command to execute in order to read metrics from the vNIC
333 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
334 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
336 // Define other variables
337 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
338 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
339 FILE *fp; /* file descriptor to pipe cmd to shell */
342 for(i = 0; i < 4; i++) {
344 memset(buf, 0, BUFSIZE);
345 memset(cmd, 0, BUFSIZE);
346 // Build shell command to read metrics from the vNIC
347 strcat(cmd, cmd_prefix);
349 strcat(cmd, cmd_mid);
350 strcat(cmd, params[i]);
352 // Open a pipe and read VPP values
353 if ((fp = popen(cmd, "r")) == NULL) {
354 printf("Error opening pipe!\n");
358 while (fgets(buf, BUFSIZE, fp) != NULL);
362 printf("Command not found or exited with error status\n");
367 // Store metrics read from the vNIC in the struct passed from the main function
368 vpp_metrics->bytes_in = temp[0];
369 vpp_metrics->bytes_out = temp[1];
370 vpp_metrics->packets_in = temp[2];
371 vpp_metrics->packets_out = temp[3];