2 /*************************************************************************//**
4 * Copyright © 2019 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 EVENT_HEADER* batch_header = NULL;
45 MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
46 int bytes_in_this_round;
47 int bytes_out_this_round;
48 int packets_in_this_round;
49 int packets_out_this_round;
50 vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
51 vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
52 struct timeval time_val;
55 char hostname[BUFSIZE];
56 char eventName[BUFSIZE];
57 char eventId[BUFSIZE];
61 memset(eventName, 0, BUFSIZE);
62 memset(eventId, 0, BUFSIZE);
63 memset(hostname, 0, BUFSIZE);
65 strcpy(eventName, "vFirewallBroadcastPackets");
66 strcpy(eventId, "mvfs00000001");
69 int port = atoi(argv[2]);
70 char* caFile = "/opt/config/onap-ca.crt";
71 char* userName = "sample1";
72 char* passWord = "sample1";
77 port2 = atoi(argv[4]);
83 printf("\nVector Packet Processing (VPP) measurement collection\n");
86 if (!((argc == 4) || (argc == 6)))
88 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <FQDN>|<IP address> <port> <interface> \n", argv[0]);
89 fprintf(stderr, "OR\n");
90 fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
94 /**************************************************************************/
96 /**************************************************************************/
97 if(evel_initialize(fqdn, /* FQDN */
99 fqdn2, /* Backup FQDN */
100 port2, /* Backup port */
101 NULL, /* optional path */
102 NULL, /* optional topic */
103 100, /* Ring Buffer size */
105 NULL, /* cert file */
107 caFile, /* ca file */
108 NULL, /* ca directory */
111 userName, /* Username */
112 passWord, /* Password */
113 "sample1", /* Username2 */
114 "sample1", /* Password2 */
115 NULL, /* Source ip */
116 NULL, /* Source ip2 */
117 EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */
118 "vFirewall", /* Role */
122 fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
127 printf("\nInitialization completed\n");
130 gethostname(hostname, BUFSIZE);
131 memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
132 read_vpp_metrics(last_vpp_metrics, vnic);
133 gettimeofday(&time_val, NULL);
134 start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
135 sleep(READ_INTERVAL);
137 /***************************************************************************/
138 /* Collect metrics from the VNIC */
139 /***************************************************************************/
141 memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
142 read_vpp_metrics(curr_vpp_metrics, vnic);
144 if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) {
145 bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in;
148 bytes_in_this_round = 0;
150 if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) {
151 bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out;
154 bytes_out_this_round = 0;
156 if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) {
157 packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in;
160 packets_in_this_round = 0;
162 if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) {
163 packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out;
166 packets_out_this_round = 0;
169 vpp_m = evel_new_measurement(READ_INTERVAL, eventName, eventId);
172 printf("New measurement report created...\n");
173 vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance(vnic, "true");
174 evel_meas_vnic_performance_add(vpp_m, vnic_performance);
175 evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
176 evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
178 evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
179 evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
181 /***************************************************************************/
182 /* Set parameters in the MEASUREMENT header packet */
183 /***************************************************************************/
184 last_epoch = start_epoch + READ_INTERVAL * 1000000;
185 vpp_m_header = (EVENT_HEADER *)vpp_m;
186 vpp_m_header->start_epoch_microsec = start_epoch;
187 vpp_m_header->last_epoch_microsec = last_epoch;
188 evel_reporting_entity_id_set(vpp_m_header, "No UUID available");
190 evel_reporting_entity_name_set(vpp_m_header, hostname);
192 // evel_rc = evel_post_event(vpp_m_header);
193 batch_header = evel_new_batch("batch_event_name", "bevent_id");
194 evel_batch_add_event(batch_header, vpp_m_header);
195 evel_rc = evel_post_event(batch_header);
197 if(evel_rc == EVEL_SUCCESS) {
198 printf("Measurement report correctly sent to the collector!\n");
201 printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
205 printf("New measurement report failed (%s)\n", evel_error_string());
208 last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
209 last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
210 last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
211 last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
212 gettimeofday(&time_val, NULL);
213 start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
215 sleep(READ_INTERVAL);
218 /***************************************************************************/
220 /***************************************************************************/
222 free(last_vpp_metrics);
223 free(curr_vpp_metrics);
225 printf("Terminated\n");
230 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
231 // Define an array of char that contains the parameters of the unix 'cut' command
232 char* params[] = {"-f3", "-f11", "-f4", "-f12"};
233 // Define the unix command to execute in order to read metrics from the vNIC
234 char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
235 char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
237 // Define other variables
238 char buf[BUFSIZE]; /* buffer used to store VPP metrics */
239 int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */
240 FILE *fp; /* file descriptor to pipe cmd to shell */
243 for(i = 0; i < 4; i++) {
245 memset(buf, 0, BUFSIZE);
246 memset(cmd, 0, BUFSIZE);
247 // Build shell command to read metrics from the vNIC
248 strcat(cmd, cmd_prefix);
250 strcat(cmd, cmd_mid);
251 strcat(cmd, params[i]);
253 // Open a pipe and read VPP values
254 if ((fp = popen(cmd, "r")) == NULL) {
255 printf("Error opening pipe!\n");
259 while (fgets(buf, BUFSIZE, fp) != NULL);
263 printf("Command not found or exited with error status\n");
268 // Store metrics read from the vNIC in the struct passed from the main function
269 vpp_metrics->bytes_in = temp[0];
270 vpp_metrics->bytes_out = temp[1];
271 vpp_metrics->packets_in = temp[2];
272 vpp_metrics->packets_out = temp[3];