2 /*************************************************************************//**
4 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Unless otherwise specified, all software contained herein is
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ****************************************************************************/
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;
46 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
54 int concurrent_sessions = 0;
55 int configured_entities = 0;
56 double mean_request_latency = 0;
57 double measurement_interval = 1;
58 double memory_configured = 0;
59 double memory_used = 0;
65 printf("Checking app traffic\n");
67 timeinfo = localtime (&rawtime);
68 strftime(period,21,"%d/%b/%Y:%H:%M:",timeinfo);
69 strftime(secs,3,"%S",timeinfo);
71 if (sec == 0) sec = 59;
72 sprintf(secs, "%02d", sec);
73 strncat(period, secs, 21);
74 // ....x....1....x....2.
75 // 15/Oct/2016:17:51:19
76 strcpy(cmd, "sudo docker logs vHello | grep -c ");
77 strncat(cmd, period, 100);
81 EVEL_ERROR("popen failed to execute command");
84 if (fgets(count, 10, fp) != NULL) {
85 request_rate = atoi(count);
86 printf("Reporting request rate for second: %s as %d\n", period, request_rate);
90 EVEL_ERROR("New Measurement failed");
92 printf("Processed measurement\n");
96 EVEL_ERROR("pclose returned an error");
105 /**************************************************************************//**
107 *****************************************************************************/
108 void evel_get_cpu_stats(EVENT_MEASUREMENT * measurement)
121 MEASUREMENT_CPU_USE *cpu_use = NULL;
123 /* Open the command for reading. */
124 //fp = popen("/bin/ls /etc/", "r");
125 fp = popen("/usr/bin/top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 ", "r");
127 printf("Failed to run command\n" );
131 /* Read the output a line at a time - output it. */
132 while (fgets(path, sizeof(path)-1, fp) != NULL) {
133 printf("%s", path+10);
134 sscanf(path+10," %lf us, %lf sy, %lf ni, %lf id, %lf wa, %lf hi, %lf si, %lf st",
135 &user,&sys,&nice,&idle,&wait,&intrpt,&softirq,&steal);
141 cpu_use = evel_measurement_new_cpu_use_add(measurement, "cpu1", usage);
142 if( cpu_use != NULL ){
143 evel_measurement_cpu_use_idle_set(cpu_use,idle);
144 //evel_measurement_cpu_use_interrupt_set(cpu_use,intrpt);
145 //evel_measurement_cpu_use_nice_set(cpu_use,nice);
146 //evel_measurement_cpu_use_softirq_set(cpu_use,softirq);
147 //evel_measurement_cpu_use_steal_set(cpu_use,steal);
148 evel_measurement_cpu_use_system_set(cpu_use,sys);
149 evel_measurement_cpu_use_usageuser_set(cpu_use,user);
150 //evel_measurement_cpu_use_wait_set(cpu_use,wait);
151 //evel_measurement_cpu_use_add(measurement, "cpu2", usage,idle,intrpt,nice,softirq,steal,sys,user,wait);
157 int main(int argc, char** argv)
159 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
160 EVENT_MEASUREMENT* vpp_m = NULL;
161 EVENT_HEADER* vpp_m_header = NULL;
162 int bytes_in_this_round;
163 int bytes_out_this_round;
164 int packets_in_this_round;
165 int packets_out_this_round;
166 vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
167 vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
168 struct timeval time_val;
169 //time_t start_epoch;
171 char hostname[BUFSIZE];
172 char* fqdn = argv[1];
173 int port = atoi(argv[2]);
174 char* vnic = argv[3];
175 MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
176 //struct timeval tv_start;
178 printf("\nVector Packet Processing (VPP) measurement collection\n");
183 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
188 /**************************************************************************/
190 /**************************************************************************/
191 if(evel_initialize(fqdn, /* FQDN */
193 NULL, /* optional path */
194 NULL, /* optional topic */
198 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
199 "vFirewall", /* Role */
202 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
207 printf("\nInitialization completed\n");
210 gethostname(hostname, BUFSIZE);
211 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
212 read_vpp_metrics(last_vpp_metrics, vnic);
213 gettimeofday(&time_val, NULL);
214 epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
215 sleep(READ_INTERVAL);
217 /***************************************************************************/
218 /* Collect metrics from the VNIC */
219 /***************************************************************************/
221 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
222 read_vpp_metrics(curr_vpp_metrics, vnic);
224 if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
225 bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
228 bytes_in_this_round = 0;
230 if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
231 bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
234 bytes_out_this_round = 0;
236 if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
237 packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
240 packets_in_this_round = 0;
242 if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
243 packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
246 packets_out_this_round = 0;
249 vpp_m = evel_new_measurement(READ_INTERVAL,"Measurement_vVNF","TrafficStats_1.2.3.4");
250 vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
251 evel_meas_vnic_performance_add(vpp_m, vnic_performance);
254 printf("New measurement report created...\n");
256 evel_measurement_type_set(vpp_m, "HTTP request rate");
257 evel_measurement_request_rate_set(vpp_m, rand()%10000);
259 evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
260 evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
262 evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
263 evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
264 evel_get_cpu_stats(vpp_m);
266 /***************************************************************************/
267 /* Set parameters in the MEASUREMENT header packet */
268 /***************************************************************************/
269 struct timeval tv_now;
270 gettimeofday(&tv_now, NULL);
271 unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
273 //last_epoch = start_epoch + READ_INTERVAL * 1000000;
274 vpp_m_header = (EVENT_HEADER *)vpp_m;
275 //vpp_m_header->start_epoch_microsec = start_epoch;
276 //vpp_m_header->last_epoch_microsec = last_epoch;
277 evel_start_epoch_set(&vpp_m->header, epoch_start);
278 evel_last_epoch_set(&vpp_m->header, epoch_now);
279 epoch_start = epoch_now;
281 evel_nfcnamingcode_set(&vpp_m->header, "vVNF");
282 evel_nfnamingcode_set(&vpp_m->header, "vVNF");
283 //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
284 //strcpy(vpp_m_header->reporting_entity_name, hostname);
285 evel_reporting_entity_name_set(&vpp_m->header, "fwll");
286 evel_reporting_entity_id_set(&vpp_m->header, "No UUID available");
287 evel_rc = evel_post_event(vpp_m_header);
289 if(evel_rc == EVEL_SUCCESS) {
290 printf("Measurement report correctly sent to the collector!\n");
293 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
297 printf("New measurement report failed (%s)\n", evel_error_string());
300 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
301 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
302 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
303 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
304 //gettimeofday(&time_val, NULL);
305 //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
307 sleep(READ_INTERVAL);
310 /***************************************************************************/
312 /***************************************************************************/
314 free(last_vpp_metrics);
315 free(curr_vpp_metrics);
317 printf("Terminated\n");
322 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
323 // Define an array of char that contains the parameters of the unix 'cut' command
324 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
325 // Define the unix command to execute in order to read metrics from the vNIC
326 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
327 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
329 // Define other variables
330 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
331 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
332 FILE *fp; /* file descriptor to pipe cmd to shell */
335 for(i = 0; i < 4; i++) {
337 memset(buf, 0, BUFSIZE);
338 memset(cmd, 0, BUFSIZE);
339 // Build shell command to read metrics from the vNIC
340 strcat(cmd, cmd_prefix);
342 strcat(cmd, cmd_mid);
343 strcat(cmd, params[i]);
345 // Open a pipe and read VPP values
346 if ((fp = popen(cmd, "r")) == NULL) {
347 printf("Error opening pipe!\n");
351 while (fgets(buf, BUFSIZE, fp) != NULL);
355 printf("Command not found or exited with error status\n");
360 // Store metrics read from the vNIC in the struct passed from the main function
361 vpp_metrics->bytes_in = temp[0];
362 vpp_metrics->bytes_out = temp[1];
363 vpp_metrics->packets_in = temp[2];
364 vpp_metrics->packets_out = temp[3];