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.bgpmanager.oam;
11 import javax.management.Attribute;
12 import javax.management.JMException;
13 import javax.management.MBeanServer;
14 import javax.management.ObjectName;
16 import java.lang.management.ManagementFactory;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 //import org.opendaylight.bgpmanager.globals.BgpConstants;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
27 * Created by ECHIAPT on 8/4/2015.
29 public class BgpCounters extends TimerTask {
31 private static final Logger LOGGER = LoggerFactory.getLogger(BgpCounters.class);
32 public static BgpCountersBroadcaster bgpStatsBroadcaster = null;
33 public MBeanServer bgpStatsServer = null;
34 public Map <String, String> countersMap = new HashMap<String, String>();
39 if (LOGGER.isDebugEnabled()) {
40 LOGGER.debug("Fetching counters from BGP at " + new Date());
43 fetchCmdOutputs("cmd_ip_bgp_summary.txt","show ip bgp summary");
44 fetchCmdOutputs("cmd_bgp_ipv4_unicast_statistics.txt", "show bgp ipv4 unicast statistics");
45 fetchCmdOutputs("cmd_ip_bgp_vpnv4_all.txt", "show ip bgp vpnv4 all");
46 parse_ip_bgp_summary();
47 parse_bgp_ipv4_unicast_statistics();
48 parse_ip_bgp_vpnv4_all();
49 if (LOGGER.isDebugEnabled()) {
52 if (bgpStatsBroadcaster == null) {
53 //First time execution
55 bgpStatsBroadcaster = new BgpCountersBroadcaster();
56 bgpStatsServer = ManagementFactory.getPlatformMBeanServer();
57 ObjectName bgpStatsObj = new ObjectName("SDNC.PM:type=BgpCountersBroadcaster");
58 bgpStatsServer.registerMBean(bgpStatsBroadcaster, bgpStatsObj);
59 LOGGER.info("BGP Counters MBean Registered :::");
60 } catch (JMException e) {
61 LOGGER.error("Adding a NotificationBroadcaster failed." , e);
65 bgpStatsBroadcaster.setBgpCountersMap(countersMap);
66 if (LOGGER.isDebugEnabled()) {
67 LOGGER.debug("Finished updating the counters from BGP at " + new Date());
69 } catch (Exception e) {
70 LOGGER.error("Failed to publish bgp counters ", e);
74 public void dumpCounters () {
75 Iterator<Map.Entry<String, String>> entries = countersMap.entrySet().iterator();
76 while (entries.hasNext()) {
77 Map.Entry<String, String> entry = entries.next();
78 LOGGER.debug(entry.getKey() + ", Value = " + entry.getValue());
82 public static void fetchCmdOutputs (String filename, String cmdName) throws IOException {
84 int serverPort = 2605;
85 String serverName = BgpConstants.DEFAULT_BGP_HOST_NAME;
87 PrintWriter out_to_socket;
88 BufferedReader in_from_socket;
89 char cbuf[] = new char[10];
91 StringBuilder sb = new StringBuilder();
94 char ch, gt = '>', hash = '#';
95 String vtyPassword = BgpConstants.QBGP_VTY_PASSWORD;
96 String passwordCheckStr = "Password:";
97 String enableString = "en";
98 String prompt, replacedStr;
102 socket = new Socket(serverName, serverPort);
105 catch (UnknownHostException ioe) {
106 LOGGER.error("No host exists: " + ioe.getMessage());
109 catch (IOException ioe) {
110 LOGGER.error("I/O error occured " + ioe.getMessage());
114 socket.setSoTimeout(sockTimeout*1000);
115 out_to_socket = new PrintWriter(socket.getOutputStream(), true);
116 in_from_socket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
118 } catch (IOException ioe) {
119 LOGGER.error("IOException thrown.");
125 ret = in_from_socket.read(cbuf);
127 catch (SocketTimeoutException ste) {
128 LOGGER.error("Read from Socket timed Out while asking for password.");
132 catch (IOException ioe) {
133 LOGGER.error("Caught IOException");
137 if ((ret == (int) gt) || (ret == (int)hash)) {
139 } else if (ret == -1) {
140 LOGGER.error("Connection closed by BGPd.");
145 if (sb.toString().contains(passwordCheckStr)) {
152 out_to_socket.println(vtyPassword);
156 ip = in_from_socket.read();
158 catch (SocketTimeoutException ste) {
159 LOGGER.error(sb.toString());
160 LOGGER.error("Read from Socket timed Out while verifying the password.");
166 } else if (ip == -1) {
167 LOGGER.error(sb.toString());
168 LOGGER.error("Connection closed by BGPd.");
178 prompt = sb.toString();
179 prompt = prompt.trim();
181 out_to_socket.println(enableString);
185 ip = in_from_socket.read();
187 catch (SocketTimeoutException ste) {
188 LOGGER.error(sb.toString());
189 LOGGER.error("Read from Socket timed Out while keying the en keyword.");
193 if (ip == (int)hash) {
195 } else if (ip == -1) {
196 LOGGER.error(sb.toString());
197 LOGGER.error("Connection closed by BGPd.");
207 temp = new StringBuilder();
209 FileWriter fileWritter;
210 BufferedWriter bufferWritter;
213 file = new File(filename);
214 if (!file.exists()) {
215 file.createNewFile();
217 fileWritter = new FileWriter(file.getName(), true);
218 bufferWritter = new BufferedWriter(fileWritter);
219 } catch (IOException e) {
222 out_to_socket.println(cmdName);
226 op_buf = new char[100];
227 ret = in_from_socket.read(op_buf);
229 } catch (SocketTimeoutException ste) {
231 } catch (SocketException soc) {
233 } catch (IOException ioe) {
234 ioe.printStackTrace();
243 String outputStr = temp.toString();
244 StringBuffer output = new StringBuffer();
246 outputStr.replaceAll("^\\s+|\\s+$", "");
247 output.append(outputStr);
248 if (output.toString().trim().contains(prompt)) {
249 int index = output.toString().lastIndexOf(prompt);
250 String newString = output.toString().substring(0, index);
252 output.append(newString);
255 bufferWritter.write(output.toString().trim());
257 } catch (IOException e) {
262 bufferWritter.close();
266 } catch (IOException e) {
272 public boolean validate(final String ip){
273 if (ip == null || ip.equals("")) {
276 final String PATTERN =
277 "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
278 Pattern pattern = Pattern.compile(PATTERN);
279 Matcher matcher = pattern.matcher(ip);
280 return matcher.matches();
285 * The below function parses the output of "show ip bgp summary" saved in a file.
286 * Below is the snippet for the same :-
288 BGP router identifier 10.183.254.53, local AS number 101
290 Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
291 10.183.254.76 4 100 3 4 0 0 0 00:01:27 0
293 Total number of neighbors 1
297 public void parse_ip_bgp_summary() {
298 File file = new File("cmd_ip_bgp_summary.txt");
301 List<String> inputStrs = new ArrayList<String>();
304 boolean startEntries = false;
309 scanner = new Scanner(file);
310 } catch (IOException e) {
311 LOGGER.error("Could not process the file " + file.getAbsolutePath());
314 while (scanner.hasNextLine()) {
316 lineFromFile = scanner.nextLine();
317 inputStrs.add(lineFromFile);
320 StringBuilder NbrInfoKey = new StringBuilder();
322 while (i < inputStrs.size()) {
323 str = inputStrs.get(i);
324 if (str.contains("State/PfxRcd")) {
326 } else if (startEntries == true) {
327 result = str.split("\\s+");
329 StrIP = result[0].trim();
330 if (!validate(StrIP)) {
337 NbrInfoKey.setLength(0);
338 NbrInfoKey.append(BgpConstants.BGP_COUNTER_NBR_PKTS_RX).append(":").
339 append("BGP_Nbr_IP_").append(StrIP).append("_AS_").append(as).append("_PktsReceived");
340 countersMap.put(NbrInfoKey.toString(), rx);
343 NbrInfoKey.setLength(0);
344 NbrInfoKey.append(BgpConstants.BGP_COUNTER_NBR_PKTS_TX).append(":").
345 append("BGP_Nbr_IP_").append(StrIP).append("_AS_").append(as).append("_PktsSent");
346 countersMap.put(NbrInfoKey.toString(), tx);
347 } catch (Exception e) {
355 * The below function parses the output of "show bgp ipv4 unicast statistics" saved in a file.
356 * Below is the sample output for the same :-
358 BGP IPv4 Unicast RIB statistics
365 public void parse_bgp_ipv4_unicast_statistics() {
366 File file = new File("cmd_bgp_ipv4_unicast_statistics.txt");
369 StringBuilder key = new StringBuilder();
371 List<String> inputStrs = new ArrayList<String>();
373 scanner = new Scanner(file);
374 } catch (IOException e) {
375 System.err.println("Could not process the file " + file.getAbsolutePath());
378 while (scanner.hasNextLine()) {
380 lineFromFile = scanner.nextLine();
381 inputStrs.add(lineFromFile);
386 while (i < inputStrs.size()) {
387 instr = inputStrs.get(i);
388 if (instr.contains("Total Prefixes")) {
389 String[] result = instr.split(":");
391 totPfx = result[1].trim();
392 } catch (Exception e) {
400 key.append(BgpConstants.BGP_COUNTER_TOTAL_PFX).append(":").
401 append("Bgp_Total_Prefixes");
402 countersMap.put(key.toString(), totPfx);
406 * The below function parses the output of "show ip bgp vpnv4 all" saved in a file.
407 * Below is the sample output for the same :-
408 * show ip bgp vpnv4 all
410 BGP table version is 0, local router ID is 10.183.181.21
412 Route Distinguisher: 100:1
413 *>i15.15.15.15/32 10.183.181.25 0 100 0 ?
414 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
415 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
416 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
417 Route Distinguisher: 100:2
418 *>i16.16.16.16/32 10.183.181.25 0 100 0 ?
419 *>i18.18.18.18/32 10.183.181.25 0 100 0 ?
420 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
423 public void parse_ip_bgp_vpnv4_all() {
424 File file = new File("cmd_ip_bgp_vpnv4_all.txt");
427 List<String> inputStrs = new ArrayList<String>();
430 scanner = new Scanner(file);
431 } catch (IOException e) {
432 System.err.println("Could not process the file " + file.getAbsolutePath());
435 while (scanner.hasNextLine()) {
436 lineFromFile = scanner.nextLine();
437 inputStrs.add(lineFromFile);
441 while (i < inputStrs.size()) {
442 instr = inputStrs.get(i);
443 if (instr.contains("Route Distinguisher")) {
444 String[] result = instr.split(":");
445 rd = result[1].trim() + "_" + result[2].trim();
446 i = processRouteCount(rd, i + 1, inputStrs);
454 public int processRouteCount(String rd, int startIndex, List<String> inputStrs) {
455 int num = startIndex, route_count = 0;
457 StringBuilder key = new StringBuilder();
458 str = inputStrs.get(num);
460 while (str != null && !str.trim().equals("") &&
461 num <inputStrs.size()) {
462 if (str.contains("Route Distinguisher")) {
464 key.append(BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT).append(":").
465 append("BGP_RD_").append(rd).append("_route_count");
466 countersMap.put(key.toString(), Integer.toString(route_count));
471 if (num == inputStrs.size()) {
474 str = inputStrs.get(num);
476 if (route_count == 0) {
477 // Erroneous condition, should never happen.
478 // Implies that the file contains marker for RD without routes.
479 // will form an infinite loop if not broken
480 // by sending a big number back.
484 key.append(BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT).append(":").
485 append("BGP_RD_").append(rd).append("_route_count");
486 countersMap.put(key.toString(), Integer.toString(route_count));
490 public void resetCounters() {
492 resetFile("cmd_ip_bgp_summary.txt");
493 resetFile("cmd_bgp_ipv4_unicast_statistics.txt");
494 resetFile("cmd_ip_bgp_vpnv4_all.txt");
497 public void resetFile(String fileName) {
498 File fileHndl = (new File(fileName));
503 success = fileHndl.delete();
506 writer = new PrintWriter(fileHndl);
509 } catch (Exception e) {