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 */
197 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
198 "vFirewall", /* Role */
201 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
206 printf("\nInitialization completed\n");
209 gethostname(hostname, BUFSIZE);
210 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
211 read_vpp_metrics(last_vpp_metrics, vnic);
212 gettimeofday(&time_val, NULL);
213 epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
214 sleep(READ_INTERVAL);
216 /***************************************************************************/
217 /* Collect metrics from the VNIC */
218 /***************************************************************************/
220 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
221 read_vpp_metrics(curr_vpp_metrics, vnic);
223 if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
224 bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
227 bytes_in_this_round = 0;
229 if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
230 bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
233 bytes_out_this_round = 0;
235 if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
236 packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
239 packets_in_this_round = 0;
241 if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
242 packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
245 packets_out_this_round = 0;
248 vpp_m = evel_new_measurement(READ_INTERVAL,"vFirewallBroadcastPackets","TrafficStats_1.2.3.4");
249 vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
250 evel_meas_vnic_performance_add(vpp_m, vnic_performance);
253 printf("New measurement report created...\n");
255 evel_measurement_type_set(vpp_m, "HTTP request rate");
256 evel_measurement_request_rate_set(vpp_m, rand()%10000);
258 evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
259 evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
261 evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
262 evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
263 evel_get_cpu_stats(vpp_m);
265 /***************************************************************************/
266 /* Set parameters in the MEASUREMENT header packet */
267 /***************************************************************************/
268 struct timeval tv_now;
269 gettimeofday(&tv_now, NULL);
270 unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
272 //last_epoch = start_epoch + READ_INTERVAL * 1000000;
273 vpp_m_header = (EVENT_HEADER *)vpp_m;
274 //vpp_m_header->start_epoch_microsec = start_epoch;
275 //vpp_m_header->last_epoch_microsec = last_epoch;
276 evel_start_epoch_set(&vpp_m->header, epoch_start);
277 evel_last_epoch_set(&vpp_m->header, epoch_now);
278 epoch_start = epoch_now;
280 evel_nfcnamingcode_set(&vpp_m->header, "vVNF");
281 evel_nfnamingcode_set(&vpp_m->header, "vVNF");
282 //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
283 //strcpy(vpp_m_header->reporting_entity_name, hostname);
284 evel_reporting_entity_name_set(&vpp_m->header, "fwll");
285 evel_reporting_entity_id_set(&vpp_m->header, "No UUID available");
286 evel_rc = evel_post_event(vpp_m_header);
288 if(evel_rc == EVEL_SUCCESS) {
289 printf("Measurement report correctly sent to the collector!\n");
292 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
296 printf("New measurement report failed (%s)\n", evel_error_string());
299 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
300 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
301 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
302 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
303 //gettimeofday(&time_val, NULL);
304 //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
306 sleep(READ_INTERVAL);
309 /***************************************************************************/
311 /***************************************************************************/
313 free(last_vpp_metrics);
314 free(curr_vpp_metrics);
316 printf("Terminated\n");
321 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
322 // Define an array of char that contains the parameters of the unix 'cut' command
323 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
324 // Define the unix command to execute in order to read metrics from the vNIC
325 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
326 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
328 // Define other variables
329 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
330 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
331 FILE *fp; /* file descriptor to pipe cmd to shell */
334 for(i = 0; i < 4; i++) {
336 memset(buf, 0, BUFSIZE);
337 memset(cmd, 0, BUFSIZE);
338 // Build shell command to read metrics from the vNIC
339 strcat(cmd, cmd_prefix);
341 strcat(cmd, cmd_mid);
342 strcat(cmd, params[i]);
344 // Open a pipe and read VPP values
345 if ((fp = popen(cmd, "r")) == NULL) {
346 printf("Error opening pipe!\n");
350 while (fgets(buf, BUFSIZE, fp) != NULL);
354 printf("Command not found or exited with error status\n");
359 // Store metrics read from the vNIC in the struct passed from the main function
360 vpp_metrics->bytes_in = temp[0];
361 vpp_metrics->bytes_out = temp[1];
362 vpp_metrics->packets_in = temp[2];
363 vpp_metrics->packets_out = temp[3];