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");
138 LOGGER.error("Connection closed by BGPd.");
143 if (sb.toString().contains(passwordCheckStr)) {
150 out_to_socket.println(vtyPassword);
154 ip = in_from_socket.read();
156 catch (SocketTimeoutException ste) {
157 LOGGER.error(sb.toString());
158 LOGGER.error("Read from Socket timed Out while verifying the password.");
164 } else if (ip == -1) {
165 LOGGER.error(sb.toString());
166 LOGGER.error("Connection closed by BGPd.");
176 prompt = sb.toString();
177 prompt = prompt.trim();
179 out_to_socket.println(enableString);
183 ip = in_from_socket.read();
185 catch (SocketTimeoutException ste) {
186 LOGGER.error(sb.toString());
187 LOGGER.error("Read from Socket timed Out while keying the en keyword.");
191 if (ip == (int)hash) {
193 } else if (ip == -1) {
194 LOGGER.error(sb.toString());
195 LOGGER.error("Connection closed by BGPd.");
205 temp = new StringBuilder();
207 FileWriter fileWritter;
208 BufferedWriter bufferWritter;
211 file = new File(filename);
212 if (!file.exists()) {
213 file.createNewFile();
215 fileWritter = new FileWriter(file.getName(), true);
216 bufferWritter = new BufferedWriter(fileWritter);
217 } catch (IOException e) {
220 out_to_socket.println(cmdName);
224 op_buf = new char[100];
225 ret = in_from_socket.read(op_buf);
227 } catch (SocketTimeoutException ste) {
229 } catch (SocketException soc) {
231 } catch (IOException ioe) {
232 ioe.printStackTrace();
241 String outputStr = temp.toString();
242 StringBuffer output = new StringBuffer();
244 outputStr.replaceAll("^\\s+|\\s+$", "");
245 output.append(outputStr);
246 if (output.toString().trim().contains(prompt)) {
247 int index = output.toString().lastIndexOf(prompt);
248 String newString = output.toString().substring(0, index);
250 output.append(newString);
253 bufferWritter.write(output.toString().trim());
255 } catch (IOException e) {
260 bufferWritter.close();
264 } catch (IOException e) {
270 public boolean validate(final String ip){
271 if (ip == null || ip.equals("")) {
274 final String PATTERN =
275 "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
276 Pattern pattern = Pattern.compile(PATTERN);
277 Matcher matcher = pattern.matcher(ip);
278 return matcher.matches();
283 * The below function parses the output of "show ip bgp summary" saved in a file.
284 * Below is the snippet for the same :-
286 BGP router identifier 10.183.254.53, local AS number 101
288 Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
289 10.183.254.76 4 100 3 4 0 0 0 00:01:27 0
291 Total number of neighbors 1
295 public void parse_ip_bgp_summary() {
296 File file = new File("cmd_ip_bgp_summary.txt");
299 List<String> inputStrs = new ArrayList<String>();
302 boolean startEntries = false;
307 scanner = new Scanner(file);
308 } catch (IOException e) {
309 LOGGER.error("Could not process the file " + file.getAbsolutePath());
312 while (scanner.hasNextLine()) {
314 lineFromFile = scanner.nextLine();
315 inputStrs.add(lineFromFile);
318 StringBuilder NbrInfoKey = new StringBuilder();
320 while (i < inputStrs.size()) {
321 str = inputStrs.get(i);
322 if (str.contains("State/PfxRcd")) {
324 } else if (startEntries == true) {
325 result = str.split("\\s+");
327 StrIP = result[0].trim();
328 if (!validate(StrIP)) {
335 NbrInfoKey.setLength(0);
336 NbrInfoKey.append(BgpConstants.BGP_COUNTER_NBR_PKTS_RX).append(":").
337 append("BGP_Nbr_IP_").append(StrIP).append("_AS_").append(as).append("_PktsReceived");
338 countersMap.put(NbrInfoKey.toString(), rx);
341 NbrInfoKey.setLength(0);
342 NbrInfoKey.append(BgpConstants.BGP_COUNTER_NBR_PKTS_TX).append(":").
343 append("BGP_Nbr_IP_").append(StrIP).append("_AS_").append(as).append("_PktsSent");
344 countersMap.put(NbrInfoKey.toString(), tx);
345 } catch (Exception e) {
353 * The below function parses the output of "show bgp ipv4 unicast statistics" saved in a file.
354 * Below is the sample output for the same :-
356 BGP IPv4 Unicast RIB statistics
363 public void parse_bgp_ipv4_unicast_statistics() {
364 File file = new File("cmd_bgp_ipv4_unicast_statistics.txt");
367 StringBuilder key = new StringBuilder();
369 List<String> inputStrs = new ArrayList<String>();
371 scanner = new Scanner(file);
372 } catch (IOException e) {
373 System.err.println("Could not process the file " + file.getAbsolutePath());
376 while (scanner.hasNextLine()) {
378 lineFromFile = scanner.nextLine();
379 inputStrs.add(lineFromFile);
384 while (i < inputStrs.size()) {
385 instr = inputStrs.get(i);
386 if (instr.contains("Total Prefixes")) {
387 String[] result = instr.split(":");
389 totPfx = result[1].trim();
390 } catch (Exception e) {
398 key.append(BgpConstants.BGP_COUNTER_TOTAL_PFX).append(":").
399 append("Bgp_Total_Prefixes");
400 countersMap.put(key.toString(), totPfx);
404 * The below function parses the output of "show ip bgp vpnv4 all" saved in a file.
405 * Below is the sample output for the same :-
406 * show ip bgp vpnv4 all
408 BGP table version is 0, local router ID is 10.183.181.21
410 Route Distinguisher: 100:1
411 *>i15.15.15.15/32 10.183.181.25 0 100 0 ?
412 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
413 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
414 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
415 Route Distinguisher: 100:2
416 *>i16.16.16.16/32 10.183.181.25 0 100 0 ?
417 *>i18.18.18.18/32 10.183.181.25 0 100 0 ?
418 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
421 public void parse_ip_bgp_vpnv4_all() {
422 File file = new File("cmd_ip_bgp_vpnv4_all.txt");
425 List<String> inputStrs = new ArrayList<String>();
428 scanner = new Scanner(file);
429 } catch (IOException e) {
430 System.err.println("Could not process the file " + file.getAbsolutePath());
433 while (scanner.hasNextLine()) {
434 lineFromFile = scanner.nextLine();
435 inputStrs.add(lineFromFile);
439 while (i < inputStrs.size()) {
440 instr = inputStrs.get(i);
441 if (instr.contains("Route Distinguisher")) {
442 String[] result = instr.split(":");
443 rd = result[1].trim() + "_" + result[2].trim();
444 i = processRouteCount(rd, i + 1, inputStrs);
452 public int processRouteCount(String rd, int startIndex, List<String> inputStrs) {
453 int num = startIndex, route_count = 0;
455 StringBuilder key = new StringBuilder();
456 str = inputStrs.get(num);
458 while (str != null && !str.trim().equals("") &&
459 num <inputStrs.size()) {
460 if (str.contains("Route Distinguisher")) {
462 key.append(BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT).append(":").
463 append("BGP_RD_").append(rd).append("_route_count");
464 countersMap.put(key.toString(), Integer.toString(route_count));
469 if (num == inputStrs.size()) {
472 str = inputStrs.get(num);
474 if (route_count == 0) {
475 // Erroneous condition, should never happen.
476 // Implies that the file contains marker for RD without routes.
477 // will form an infinite loop if not broken
478 // by sending a big number back.
482 key.append(BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT).append(":").
483 append("BGP_RD_").append(rd).append("_route_count");
484 countersMap.put(key.toString(), Integer.toString(route_count));
488 public void resetCounters() {
490 resetFile("cmd_ip_bgp_summary.txt");
491 resetFile("cmd_bgp_ipv4_unicast_statistics.txt");
492 resetFile("cmd_ip_bgp_vpnv4_all.txt");
495 public void resetFile(String fileName) {
496 File fileHndl = (new File(fileName));
501 success = fileHndl.delete();
504 writer = new PrintWriter(fileHndl);
507 } catch (Exception e) {