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 int main(int argc, char** argv)
41 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
42 EVENT_MEASUREMENT* vpp_m = NULL;
43 EVENT_HEADER* vpp_m_header = NULL;
44 int bytes_in_this_round;
45 int bytes_out_this_round;
46 int packets_in_this_round;
47 int packets_out_this_round;
48 vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
49 vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
50 struct timeval time_val;
53 char hostname[BUFSIZE];
55 int port = atoi(argv[2]);
58 printf("\nVector Packet Processing (VPP) measurement collection\n");
63 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
67 /**************************************************************************/
69 /**************************************************************************/
70 if(evel_initialize(fqdn, /* FQDN */
72 NULL, /* optional path */
73 NULL, /* optional topic */
77 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
78 "vFirewall", /* Role */
81 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
86 printf("\nInitialization completed\n");
89 gethostname(hostname, BUFSIZE);
90 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
91 read_vpp_metrics(last_vpp_metrics, vnic);
92 gettimeofday(&time_val, NULL);
93 start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
96 /***************************************************************************/
97 /* Collect metrics from the VNIC */
98 /***************************************************************************/
100 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
101 read_vpp_metrics(curr_vpp_metrics, vnic);
103 if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
104 bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
107 bytes_in_this_round = 0;
109 if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
110 bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
113 bytes_out_this_round = 0;
115 if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
116 packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
119 packets_in_this_round = 0;
121 if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
122 packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
125 packets_out_this_round = 0;
128 vpp_m = evel_new_measurement(READ_INTERVAL);
131 printf("New measurement report created...\n");
132 evel_measurement_vnic_use_add(vpp_m, /* Pointer to the measurement */
133 vnic, /* ASCII string with the vNIC's ID */
134 packets_in_this_round, /* Packets received */
135 packets_out_this_round, /* Packets transmitted */
136 0, /* Broadcast packets received */
137 0, /* Broadcast packets transmitted */
138 bytes_in_this_round, /* Total bytes received */
139 bytes_out_this_round, /* Total bytes transmitted */
140 0, /* Multicast packets received */
141 0, /* Multicast packets transmitted */
142 0, /* Unicast packets received */
143 0); /* Unicast packets transmitted */
145 /***************************************************************************/
146 /* Set parameters in the MEASUREMENT header packet */
147 /***************************************************************************/
148 last_epoch = start_epoch + READ_INTERVAL * 1000000;
149 vpp_m_header = (EVENT_HEADER *)vpp_m;
150 vpp_m_header->start_epoch_microsec = start_epoch;
151 vpp_m_header->last_epoch_microsec = last_epoch;
152 strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
153 strcpy(vpp_m_header->reporting_entity_name, hostname);
154 evel_rc = evel_post_event(vpp_m_header);
156 if(evel_rc == EVEL_SUCCESS) {
157 printf("Measurement report correctly sent to the collector!\n");
160 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
164 printf("New measurement report failed (%s)\n", evel_error_string());
167 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
168 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
169 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
170 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
171 gettimeofday(&time_val, NULL);
172 start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
174 sleep(READ_INTERVAL);
177 /***************************************************************************/
179 /***************************************************************************/
181 free(last_vpp_metrics);
182 free(curr_vpp_metrics);
184 printf("Terminated\n");
189 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
190 // Define an array of char that contains the parameters of the unix 'cut' command
191 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
192 // Define the unix command to execute in order to read metrics from the vNIC
193 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
194 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
196 // Define other variables
197 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
198 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
199 FILE *fp; /* file descriptor to pipe cmd to shell */
202 for(i = 0; i < 4; i++) {
204 memset(buf, 0, BUFSIZE);
205 memset(cmd, 0, BUFSIZE);
206 // Build shell command to read metrics from the vNIC
207 strcat(cmd, cmd_prefix);
209 strcat(cmd, cmd_mid);
210 strcat(cmd, params[i]);
212 // Open a pipe and read VPP values
213 if ((fp = popen(cmd, "r")) == NULL) {
214 printf("Error opening pipe!\n");
218 while (fgets(buf, BUFSIZE, fp) != NULL);
222 printf("Command not found or exited with error status\n");
227 // Store metrics read from the vNIC in the struct passed from the main function
228 vpp_metrics->bytes_in = temp[0];
229 vpp_metrics->bytes_out = temp[1];
230 vpp_metrics->packets_in = temp[2];
231 vpp_metrics->packets_out = temp[3];