2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netvirt.bgpmanager.oam;
11 import javax.management.JMException;
12 import javax.management.MBeanServer;
13 import javax.management.ObjectName;
14 import java.io.BufferedReader;
15 import java.io.BufferedWriter;
17 import java.io.FileWriter;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.io.PrintWriter;
21 import java.lang.management.ManagementFactory;
22 import java.net.Socket;
23 import java.net.SocketException;
24 import java.net.SocketTimeoutException;
25 import java.net.UnknownHostException;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.Scanner;
33 import java.util.TimerTask;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 import org.opendaylight.netvirt.bgpmanager.BgpManager;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
42 * Created by ECHIAPT on 8/4/2015.
44 public class BgpCounters extends TimerTask {
46 private static final Logger LOGGER = LoggerFactory.getLogger(BgpCounters.class);
47 public static BgpCountersBroadcaster bgpStatsBroadcaster = null;
48 public MBeanServer bgpStatsServer = null;
49 public Map <String, String> countersMap = new HashMap<String, String>();
50 private String bgpSdncMip = "127.0.0.1";
52 public BgpCounters(String mipAddress) {
53 bgpSdncMip = mipAddress;
59 LOGGER.debug("Fetching counters from BGP");
61 fetchCmdOutputs("cmd_ip_bgp_summary.txt","show ip bgp summary");
62 fetchCmdOutputs("cmd_bgp_ipv4_unicast_statistics.txt", "show bgp ipv4 unicast statistics");
63 fetchCmdOutputs("cmd_ip_bgp_vpnv4_all.txt", "show ip bgp vpnv4 all");
64 parse_ip_bgp_summary();
65 parse_bgp_ipv4_unicast_statistics();
66 parse_ip_bgp_vpnv4_all();
67 if (LOGGER.isDebugEnabled()) {
70 if (bgpStatsBroadcaster == null) {
71 //First time execution
73 bgpStatsBroadcaster = new BgpCountersBroadcaster();
74 bgpStatsServer = ManagementFactory.getPlatformMBeanServer();
75 ObjectName bgpStatsObj = new ObjectName("SDNC.PM:type=BgpCountersBroadcaster");
76 bgpStatsServer.registerMBean(bgpStatsBroadcaster, bgpStatsObj);
77 LOGGER.info("BGP Counters MBean Registered :::");
78 } catch (JMException e) {
79 LOGGER.error("Adding a NotificationBroadcaster failed." , e);
83 bgpStatsBroadcaster.setBgpCountersMap(countersMap);
84 LOGGER.debug("Finished updating the counters from BGP");
85 } catch (Exception e) {
86 LOGGER.error("Failed to publish bgp counters ", e);
90 public void dumpCounters () {
91 for (Map.Entry<String, String> entry : countersMap.entrySet()) {
92 LOGGER.debug("{}, Value = {}", entry.getKey(), entry.getValue());
96 public void fetchCmdOutputs (String filename, String cmdName) throws IOException {
98 int serverPort = 2605;
99 String serverName = bgpSdncMip;
101 PrintWriter out_to_socket;
102 BufferedReader in_from_socket;
103 char cbuf[] = new char[10];
105 StringBuilder sb = new StringBuilder();
108 char ch, gt = '>', hash = '#';
109 String vtyPassword = BgpConstants.QBGP_VTY_PASSWORD;
110 String passwordCheckStr = "Password:";
111 String enableString = "en";
112 String prompt, replacedStr;
116 socket = new Socket(serverName, serverPort);
119 catch (UnknownHostException ioe) {
120 LOGGER.error("No host exists: " + ioe.getMessage());
123 catch (IOException ioe) {
124 LOGGER.error("I/O error occured " + ioe.getMessage());
128 socket.setSoTimeout(sockTimeout*1000);
129 out_to_socket = new PrintWriter(socket.getOutputStream(), true);
130 in_from_socket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
132 } catch (IOException ioe) {
133 LOGGER.error("IOException thrown.");
139 ret = in_from_socket.read(cbuf);
141 catch (SocketTimeoutException ste) {
142 LOGGER.error("Read from Socket timed Out while asking for password.");
146 catch (IOException ioe) {
147 LOGGER.error("Caught IOException");
152 LOGGER.error("Connection closed by BGPd.");
157 if (sb.toString().contains(passwordCheckStr)) {
164 out_to_socket.println(vtyPassword);
168 ip = in_from_socket.read();
170 catch (SocketTimeoutException ste) {
171 LOGGER.error(sb.toString());
172 LOGGER.error("Read from Socket timed Out while verifying the password.");
176 if (ip == (int)gt || ip == (int)hash) {
178 } else if (ip == -1) {
179 LOGGER.error(sb.toString());
180 LOGGER.error("Connection closed by BGPd.");
190 prompt = sb.toString();
191 prompt = prompt.trim();
193 out_to_socket.println(enableString);
197 ip = in_from_socket.read();
199 catch (SocketTimeoutException ste) {
200 LOGGER.error(sb.toString());
201 LOGGER.error("Read from Socket timed Out while keying the en keyword.");
205 if (ip == (int)hash) {
207 } else if (ip == -1) {
208 LOGGER.error(sb.toString());
209 LOGGER.error("Connection closed by BGPd.");
219 temp = new StringBuilder();
221 FileWriter fileWritter;
222 BufferedWriter bufferWritter;
225 file = new File(filename);
226 if (!file.exists()) {
227 file.createNewFile();
229 fileWritter = new FileWriter(file.getName(), true);
230 bufferWritter = new BufferedWriter(fileWritter);
231 } catch (IOException e) {
234 out_to_socket.println(cmdName);
238 op_buf = new char[100];
239 ret = in_from_socket.read(op_buf);
241 } catch (SocketTimeoutException ste) {
243 } catch (SocketException soc) {
245 } catch (IOException ioe) {
246 ioe.printStackTrace();
255 String outputStr = temp.toString();
256 StringBuffer output = new StringBuffer();
258 outputStr.replaceAll("^\\s+|\\s+$", "");
259 output.append(outputStr);
260 if (output.toString().trim().contains(prompt)) {
261 int index = output.toString().lastIndexOf(prompt);
262 String newString = output.toString().substring(0, index);
264 output.append(newString);
267 bufferWritter.write(output.toString().trim());
269 } catch (IOException e) {
274 bufferWritter.close();
278 } catch (IOException e) {
284 public static boolean validate(final String ip){
285 if (ip == null || ip.equals("")) {
288 final String PATTERN =
289 "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
290 Pattern pattern = Pattern.compile(PATTERN);
291 Matcher matcher = pattern.matcher(ip);
292 return matcher.matches();
297 * The below function parses the output of "show ip bgp summary" saved in a file.
298 * Below is the snippet for the same :-
300 BGP router identifier 10.183.254.53, local AS number 101
302 Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
303 10.183.254.76 4 100 3 4 0 0 0 00:01:27 0
305 Total number of neighbors 1
309 public void parse_ip_bgp_summary() {
310 File file = new File("cmd_ip_bgp_summary.txt");
313 List<String> inputStrs = new ArrayList<String>();
316 boolean startEntries = false;
321 scanner = new Scanner(file);
322 } catch (IOException e) {
323 LOGGER.error("Could not process the file " + file.getAbsolutePath());
326 while (scanner.hasNextLine()) {
328 lineFromFile = scanner.nextLine();
329 inputStrs.add(lineFromFile);
332 StringBuilder NbrInfoKey = new StringBuilder();
334 while (i < inputStrs.size()) {
335 str = inputStrs.get(i);
336 if (str.contains("State/PfxRcd")) {
338 } else if (startEntries == true) {
339 result = str.split("\\s+");
341 StrIP = result[0].trim();
342 if (!validate(StrIP)) {
349 NbrInfoKey.setLength(0);
350 NbrInfoKey.append(BgpConstants.BGP_COUNTER_NBR_PKTS_RX).append(":").
351 append("BGP_Nbr_IP_").append(StrIP).append("_AS_").append(as).append("_PktsReceived");
352 countersMap.put(NbrInfoKey.toString(), rx);
355 NbrInfoKey.setLength(0);
356 NbrInfoKey.append(BgpConstants.BGP_COUNTER_NBR_PKTS_TX).append(":").
357 append("BGP_Nbr_IP_").append(StrIP).append("_AS_").append(as).append("_PktsSent");
358 countersMap.put(NbrInfoKey.toString(), tx);
359 } catch (Exception e) {
367 * The below function parses the output of "show bgp ipv4 unicast statistics" saved in a file.
368 * Below is the sample output for the same :-
370 BGP IPv4 Unicast RIB statistics
377 public void parse_bgp_ipv4_unicast_statistics() {
378 File file = new File("cmd_bgp_ipv4_unicast_statistics.txt");
381 StringBuilder key = new StringBuilder();
383 List<String> inputStrs = new ArrayList<String>();
385 scanner = new Scanner(file);
386 } catch (IOException e) {
387 System.err.println("Could not process the file " + file.getAbsolutePath());
390 while (scanner.hasNextLine()) {
392 lineFromFile = scanner.nextLine();
393 inputStrs.add(lineFromFile);
398 while (i < inputStrs.size()) {
399 instr = inputStrs.get(i);
400 if (instr.contains("Total Prefixes")) {
401 String[] result = instr.split(":");
403 totPfx = result[1].trim();
404 } catch (Exception e) {
412 key.append(BgpConstants.BGP_COUNTER_TOTAL_PFX).append(":").
413 append("Bgp_Total_Prefixes");
414 countersMap.put(key.toString(), totPfx);
418 * The below function parses the output of "show ip bgp vpnv4 all" saved in a file.
419 * Below is the sample output for the same :-
420 * show ip bgp vpnv4 all
422 BGP table version is 0, local router ID is 10.183.181.21
424 Route Distinguisher: 100:1
425 *>i15.15.15.15/32 10.183.181.25 0 100 0 ?
426 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
427 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
428 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
429 Route Distinguisher: 100:2
430 *>i16.16.16.16/32 10.183.181.25 0 100 0 ?
431 *>i18.18.18.18/32 10.183.181.25 0 100 0 ?
432 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
435 public void parse_ip_bgp_vpnv4_all() {
436 File file = new File("cmd_ip_bgp_vpnv4_all.txt");
439 List<String> inputStrs = new ArrayList<String>();
442 scanner = new Scanner(file);
443 } catch (IOException e) {
444 System.err.println("Could not process the file " + file.getAbsolutePath());
447 while (scanner.hasNextLine()) {
448 lineFromFile = scanner.nextLine();
449 inputStrs.add(lineFromFile);
453 while (i < inputStrs.size()) {
454 instr = inputStrs.get(i);
455 if (instr.contains("Route Distinguisher")) {
456 String[] result = instr.split(":");
457 rd = result[1].trim() + "_" + result[2].trim();
458 i = processRouteCount(rd, i + 1, inputStrs);
466 public int processRouteCount(String rd, int startIndex, List<String> inputStrs) {
467 int num = startIndex, route_count = 0;
469 StringBuilder key = new StringBuilder();
470 str = inputStrs.get(num);
472 while (str != null && !str.trim().equals("") &&
473 num <inputStrs.size()) {
474 if (str.contains("Route Distinguisher")) {
476 key.append(BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT).append(":").
477 append("BGP_RD_").append(rd).append("_route_count");
478 countersMap.put(key.toString(), Integer.toString(route_count));
483 if (num == inputStrs.size()) {
486 str = inputStrs.get(num);
488 if (route_count == 0) {
489 // Erroneous condition, should never happen.
490 // Implies that the file contains marker for RD without routes.
491 // will form an infinite loop if not broken
492 // by sending a big number back.
496 key.append(BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT).append(":").
497 append("BGP_RD_").append(rd).append("_route_count");
498 countersMap.put(key.toString(), Integer.toString(route_count));
502 public void resetCounters() {
504 resetFile("cmd_ip_bgp_summary.txt");
505 resetFile("cmd_bgp_ipv4_unicast_statistics.txt");
506 resetFile("cmd_ip_bgp_vpnv4_all.txt");
509 public static void resetFile(String fileName) {
510 File fileHndl = (new File(fileName));
514 success = fileHndl.delete();
517 writer = new PrintWriter(fileHndl);
520 } catch (Exception e) {
527 public static Map<String, String> parse_ip_bgp_vpnv4_all_summary(Map<String, String> countMap) {
528 File file = new File("cmd_ip_bgp_vpnv4_all_summary.txt");
531 List<String> inputStrs = new ArrayList<String>();
533 boolean startEntries = false;
538 scanner = new Scanner(file);
539 } catch (IOException e) {
540 LOGGER.trace("Could not process the file " + file.getAbsolutePath());
543 while (scanner.hasNextLine()) {
545 lineFromFile = scanner.nextLine();
546 inputStrs.add(lineFromFile);
549 while (i < inputStrs.size()) {
550 str = inputStrs.get(i);
551 LOGGER.trace("str is:: {}", str);
552 if (str.contains("State/PfxRcd")) {
554 } else if (startEntries == true) {
555 result = str.split("\\s+");
557 StrIP = result[0].trim();
558 LOGGER.trace("strIP " + StrIP);
560 if (!validate(StrIP)) {
563 String state_pfxRcvd = result[9];
564 countMap.put(StrIP, state_pfxRcvd);
565 } catch (Exception e) {
566 LOGGER.trace("Exception {} caught while processing ip bgp vpnv4 all summary command output, e");