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 ****************************************************************************/
29 #define READ_INTERVAL 10
31 typedef struct dummy_vpp_metrics_struct {
38 void read_vpp_metrics(vpp_metrics_struct *, char *);
40 int main(int argc, char** argv)
42 EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
43 EVENT_MEASUREMENT* vpp_m = NULL;
44 EVENT_HEADER* vpp_m_header = NULL;
45 int bytes_in_this_round;
46 int bytes_out_this_round;
47 int packets_in_this_round;
48 int packets_out_this_round;
49 vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
50 vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
51 struct timeval time_val;
54 char hostname[BUFSIZE];
56 int port = atoi(argv[2]);
59 printf("\nVector Packet Processing (VPP) measurement collection\n");
64 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
68 /**************************************************************************/
70 /**************************************************************************/
71 if(evel_initialize(fqdn, /* FQDN */
73 NULL, /* optional path */
74 NULL, /* optional topic */
78 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
79 "vLoadBalancer", /* Role */
82 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
87 printf("\nInitialization completed\n");
90 gethostname(hostname, BUFSIZE);
91 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
92 read_vpp_metrics(last_vpp_metrics, vnic);
93 gettimeofday(&time_val, NULL);
94 start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
97 /***************************************************************************/
98 /* Collect metrics from the VNIC */
99 /***************************************************************************/
101 // Read the number of vDNSs currently active
103 FILE* fd = fopen("active_dns.txt", "r");
105 if(fscanf(fd, "%d", &active_dns) != 1) /* Avoid infinite loops if the file doesn't contain exactly one number */
110 printf("Error when closing file\n");
114 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
115 read_vpp_metrics(curr_vpp_metrics, vnic);
117 if(active_dns > 0 && (curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0)) {
118 bytes_in_this_round = (int) round((curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in) / active_dns);
121 bytes_in_this_round = 0;
123 if(active_dns > 0 && (curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0)) {
124 bytes_out_this_round = (int) round((curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out) / active_dns);
127 bytes_out_this_round = 0;
129 if(active_dns > 0 && (curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0)) {
130 packets_in_this_round = (int) round((curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in) / active_dns);
133 packets_in_this_round = 0;
135 if(active_dns > 0 && (curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0)) {
136 packets_out_this_round = (int) round((curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out) / active_dns);
139 packets_out_this_round = 0;
142 vpp_m = evel_new_measurement(READ_INTERVAL);
145 printf("New measurement report created...\n");
146 evel_measurement_vnic_use_add(vpp_m, /* Pointer to the measurement */
147 vnic, /* ASCII string with the vNIC's ID */
148 packets_in_this_round, /* Packets received */
149 packets_out_this_round, /* Packets transmitted */
150 0, /* Broadcast packets received */
151 0, /* Broadcast packets transmitted */
152 bytes_in_this_round, /* Total bytes received */
153 bytes_out_this_round, /* Total bytes transmitted */
154 0, /* Multicast packets received */
155 0, /* Multicast packets transmitted */
156 0, /* Unicast packets received */
157 0); /* Unicast packets transmitted */
159 /***************************************************************************/
160 /* Set parameters in the MEASUREMENT header packet */
161 /***************************************************************************/
162 last_epoch = start_epoch + READ_INTERVAL * 1000000;
163 vpp_m_header = (EVENT_HEADER *)vpp_m;
164 vpp_m_header->start_epoch_microsec = start_epoch;
165 vpp_m_header->last_epoch_microsec = last_epoch;
166 strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
167 strcpy(vpp_m_header->reporting_entity_name, hostname);
168 evel_rc = evel_post_event(vpp_m_header);
170 if(evel_rc == EVEL_SUCCESS) {
171 printf("Measurement report correctly sent to the collector!\n");
174 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
178 printf("New measurement report failed (%s)\n", evel_error_string());
181 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
182 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
183 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
184 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
185 gettimeofday(&time_val, NULL);
186 start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
188 sleep(READ_INTERVAL);
191 /***************************************************************************/
193 /***************************************************************************/
195 free(last_vpp_metrics);
196 free(curr_vpp_metrics);
198 printf("Terminated\n");
203 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
204 // Define an array of char that contains the parameters of the unix 'cut' command
205 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
206 // Define the unix command to execute in order to read metrics from the vNIC
207 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
208 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
210 // Define other variables
211 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
212 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
213 FILE *fp; /* file descriptor to pipe cmd to shell */
216 for(i = 0; i < 4; i++) {
218 memset(buf, 0, BUFSIZE);
219 memset(cmd, 0, BUFSIZE);
220 // Build shell command to read metrics from the vNIC
221 strcat(cmd, cmd_prefix);
223 strcat(cmd, cmd_mid);
224 strcat(cmd, params[i]);
226 // Open a pipe and read VPP values
227 if ((fp = popen(cmd, "r")) == NULL) {
228 printf("Error opening pipe!\n");
232 while (fgets(buf, BUFSIZE, fp) != NULL);
236 printf("Command not found or exited with error status\n");
241 // Store metrics read from the vNIC in the struct passed from the main function
242 vpp_metrics->bytes_in = temp[0];
243 vpp_metrics->bytes_out = temp[1];
244 vpp_metrics->packets_in = temp[2];
245 vpp_metrics->packets_out = temp[3];