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;
15 import java.lang.management.ManagementFactory;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 import org.opendaylight.netvirt.bgpmanager.BgpManager;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
26 * Created by ECHIAPT on 8/4/2015.
28 public class BgpCounters extends TimerTask {
30 private static final Logger LOGGER = LoggerFactory.getLogger(BgpCounters.class);
31 public static BgpCountersBroadcaster bgpStatsBroadcaster = null;
32 public MBeanServer bgpStatsServer = null;
33 public Map <String, String> countersMap = new HashMap<String, String>();
34 private String bgpSdncMip = "127.0.0.1";
36 public BgpCounters(String mipAddress) {
37 bgpSdncMip = mipAddress;
43 if (LOGGER.isDebugEnabled()) {
44 LOGGER.debug("Fetching counters from BGP at " + new Date());
47 fetchCmdOutputs("cmd_ip_bgp_summary.txt","show ip bgp summary");
48 fetchCmdOutputs("cmd_bgp_ipv4_unicast_statistics.txt", "show bgp ipv4 unicast statistics");
49 fetchCmdOutputs("cmd_ip_bgp_vpnv4_all.txt", "show ip bgp vpnv4 all");
50 parse_ip_bgp_summary();
51 parse_bgp_ipv4_unicast_statistics();
52 parse_ip_bgp_vpnv4_all();
53 if (LOGGER.isDebugEnabled()) {
56 if (bgpStatsBroadcaster == null) {
57 //First time execution
59 bgpStatsBroadcaster = new BgpCountersBroadcaster();
60 bgpStatsServer = ManagementFactory.getPlatformMBeanServer();
61 ObjectName bgpStatsObj = new ObjectName("SDNC.PM:type=BgpCountersBroadcaster");
62 bgpStatsServer.registerMBean(bgpStatsBroadcaster, bgpStatsObj);
63 LOGGER.info("BGP Counters MBean Registered :::");
64 } catch (JMException e) {
65 LOGGER.error("Adding a NotificationBroadcaster failed." , e);
69 bgpStatsBroadcaster.setBgpCountersMap(countersMap);
70 if (LOGGER.isDebugEnabled()) {
71 LOGGER.debug("Finished updating the counters from BGP at " + new Date());
73 } catch (Exception e) {
74 LOGGER.error("Failed to publish bgp counters ", e);
78 public void dumpCounters () {
79 Iterator<Map.Entry<String, String>> entries = countersMap.entrySet().iterator();
80 while (entries.hasNext()) {
81 Map.Entry<String, String> entry = entries.next();
82 LOGGER.debug(entry.getKey() + ", Value = " + entry.getValue());
86 public void fetchCmdOutputs (String filename, String cmdName) throws IOException {
88 int serverPort = 2605;
89 String serverName = bgpSdncMip;
91 PrintWriter out_to_socket;
92 BufferedReader in_from_socket;
93 char cbuf[] = new char[10];
95 StringBuilder sb = new StringBuilder();
98 char ch, gt = '>', hash = '#';
99 String vtyPassword = BgpConstants.QBGP_VTY_PASSWORD;
100 String passwordCheckStr = "Password:";
101 String enableString = "en";
102 String prompt, replacedStr;
106 socket = new Socket(serverName, serverPort);
109 catch (UnknownHostException ioe) {
110 LOGGER.error("No host exists: " + ioe.getMessage());
113 catch (IOException ioe) {
114 LOGGER.error("I/O error occured " + ioe.getMessage());
118 socket.setSoTimeout(sockTimeout*1000);
119 out_to_socket = new PrintWriter(socket.getOutputStream(), true);
120 in_from_socket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
122 } catch (IOException ioe) {
123 LOGGER.error("IOException thrown.");
129 ret = in_from_socket.read(cbuf);
131 catch (SocketTimeoutException ste) {
132 LOGGER.error("Read from Socket timed Out while asking for password.");
136 catch (IOException ioe) {
137 LOGGER.error("Caught IOException");
142 LOGGER.error("Connection closed by BGPd.");
147 if (sb.toString().contains(passwordCheckStr)) {
154 out_to_socket.println(vtyPassword);
158 ip = in_from_socket.read();
160 catch (SocketTimeoutException ste) {
161 LOGGER.error(sb.toString());
162 LOGGER.error("Read from Socket timed Out while verifying the password.");
166 if (ip == (int)gt || ip == (int)hash) {
168 } else if (ip == -1) {
169 LOGGER.error(sb.toString());
170 LOGGER.error("Connection closed by BGPd.");
180 prompt = sb.toString();
181 prompt = prompt.trim();
183 out_to_socket.println(enableString);
187 ip = in_from_socket.read();
189 catch (SocketTimeoutException ste) {
190 LOGGER.error(sb.toString());
191 LOGGER.error("Read from Socket timed Out while keying the en keyword.");
195 if (ip == (int)hash) {
197 } else if (ip == -1) {
198 LOGGER.error(sb.toString());
199 LOGGER.error("Connection closed by BGPd.");
209 temp = new StringBuilder();
211 FileWriter fileWritter;
212 BufferedWriter bufferWritter;
215 file = new File(filename);
216 if (!file.exists()) {
217 file.createNewFile();
219 fileWritter = new FileWriter(file.getName(), true);
220 bufferWritter = new BufferedWriter(fileWritter);
221 } catch (IOException e) {
224 out_to_socket.println(cmdName);
228 op_buf = new char[100];
229 ret = in_from_socket.read(op_buf);
231 } catch (SocketTimeoutException ste) {
233 } catch (SocketException soc) {
235 } catch (IOException ioe) {
236 ioe.printStackTrace();
245 String outputStr = temp.toString();
246 StringBuffer output = new StringBuffer();
248 outputStr.replaceAll("^\\s+|\\s+$", "");
249 output.append(outputStr);
250 if (output.toString().trim().contains(prompt)) {
251 int index = output.toString().lastIndexOf(prompt);
252 String newString = output.toString().substring(0, index);
254 output.append(newString);
257 bufferWritter.write(output.toString().trim());
259 } catch (IOException e) {
264 bufferWritter.close();
268 } catch (IOException e) {
274 public static boolean validate(final String ip){
275 if (ip == null || ip.equals("")) {
278 final String PATTERN =
279 "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
280 Pattern pattern = Pattern.compile(PATTERN);
281 Matcher matcher = pattern.matcher(ip);
282 return matcher.matches();
287 * The below function parses the output of "show ip bgp summary" saved in a file.
288 * Below is the snippet for the same :-
290 BGP router identifier 10.183.254.53, local AS number 101
292 Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
293 10.183.254.76 4 100 3 4 0 0 0 00:01:27 0
295 Total number of neighbors 1
299 public void parse_ip_bgp_summary() {
300 File file = new File("cmd_ip_bgp_summary.txt");
303 List<String> inputStrs = new ArrayList<String>();
306 boolean startEntries = false;
311 scanner = new Scanner(file);
312 } catch (IOException e) {
313 LOGGER.error("Could not process the file " + file.getAbsolutePath());
316 while (scanner.hasNextLine()) {
318 lineFromFile = scanner.nextLine();
319 inputStrs.add(lineFromFile);
322 StringBuilder NbrInfoKey = new StringBuilder();
324 while (i < inputStrs.size()) {
325 str = inputStrs.get(i);
326 if (str.contains("State/PfxRcd")) {
328 } else if (startEntries == true) {
329 result = str.split("\\s+");
331 StrIP = result[0].trim();
332 if (!validate(StrIP)) {
339 NbrInfoKey.setLength(0);
340 NbrInfoKey.append(BgpConstants.BGP_COUNTER_NBR_PKTS_RX).append(":").
341 append("BGP_Nbr_IP_").append(StrIP).append("_AS_").append(as).append("_PktsReceived");
342 countersMap.put(NbrInfoKey.toString(), rx);
345 NbrInfoKey.setLength(0);
346 NbrInfoKey.append(BgpConstants.BGP_COUNTER_NBR_PKTS_TX).append(":").
347 append("BGP_Nbr_IP_").append(StrIP).append("_AS_").append(as).append("_PktsSent");
348 countersMap.put(NbrInfoKey.toString(), tx);
349 } catch (Exception e) {
357 * The below function parses the output of "show bgp ipv4 unicast statistics" saved in a file.
358 * Below is the sample output for the same :-
360 BGP IPv4 Unicast RIB statistics
367 public void parse_bgp_ipv4_unicast_statistics() {
368 File file = new File("cmd_bgp_ipv4_unicast_statistics.txt");
371 StringBuilder key = new StringBuilder();
373 List<String> inputStrs = new ArrayList<String>();
375 scanner = new Scanner(file);
376 } catch (IOException e) {
377 System.err.println("Could not process the file " + file.getAbsolutePath());
380 while (scanner.hasNextLine()) {
382 lineFromFile = scanner.nextLine();
383 inputStrs.add(lineFromFile);
388 while (i < inputStrs.size()) {
389 instr = inputStrs.get(i);
390 if (instr.contains("Total Prefixes")) {
391 String[] result = instr.split(":");
393 totPfx = result[1].trim();
394 } catch (Exception e) {
402 key.append(BgpConstants.BGP_COUNTER_TOTAL_PFX).append(":").
403 append("Bgp_Total_Prefixes");
404 countersMap.put(key.toString(), totPfx);
408 * The below function parses the output of "show ip bgp vpnv4 all" saved in a file.
409 * Below is the sample output for the same :-
410 * show ip bgp vpnv4 all
412 BGP table version is 0, local router ID is 10.183.181.21
414 Route Distinguisher: 100:1
415 *>i15.15.15.15/32 10.183.181.25 0 100 0 ?
416 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
417 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
418 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
419 Route Distinguisher: 100:2
420 *>i16.16.16.16/32 10.183.181.25 0 100 0 ?
421 *>i18.18.18.18/32 10.183.181.25 0 100 0 ?
422 *>i17.18.17.17/32 10.183.181.25 0 100 0 ?
425 public void parse_ip_bgp_vpnv4_all() {
426 File file = new File("cmd_ip_bgp_vpnv4_all.txt");
429 List<String> inputStrs = new ArrayList<String>();
432 scanner = new Scanner(file);
433 } catch (IOException e) {
434 System.err.println("Could not process the file " + file.getAbsolutePath());
437 while (scanner.hasNextLine()) {
438 lineFromFile = scanner.nextLine();
439 inputStrs.add(lineFromFile);
443 while (i < inputStrs.size()) {
444 instr = inputStrs.get(i);
445 if (instr.contains("Route Distinguisher")) {
446 String[] result = instr.split(":");
447 rd = result[1].trim() + "_" + result[2].trim();
448 i = processRouteCount(rd, i + 1, inputStrs);
456 public int processRouteCount(String rd, int startIndex, List<String> inputStrs) {
457 int num = startIndex, route_count = 0;
459 StringBuilder key = new StringBuilder();
460 str = inputStrs.get(num);
462 while (str != null && !str.trim().equals("") &&
463 num <inputStrs.size()) {
464 if (str.contains("Route Distinguisher")) {
466 key.append(BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT).append(":").
467 append("BGP_RD_").append(rd).append("_route_count");
468 countersMap.put(key.toString(), Integer.toString(route_count));
473 if (num == inputStrs.size()) {
476 str = inputStrs.get(num);
478 if (route_count == 0) {
479 // Erroneous condition, should never happen.
480 // Implies that the file contains marker for RD without routes.
481 // will form an infinite loop if not broken
482 // by sending a big number back.
486 key.append(BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT).append(":").
487 append("BGP_RD_").append(rd).append("_route_count");
488 countersMap.put(key.toString(), Integer.toString(route_count));
492 public void resetCounters() {
494 resetFile("cmd_ip_bgp_summary.txt");
495 resetFile("cmd_bgp_ipv4_unicast_statistics.txt");
496 resetFile("cmd_ip_bgp_vpnv4_all.txt");
499 public static void resetFile(String fileName) {
500 File fileHndl = (new File(fileName));
504 success = fileHndl.delete();
507 writer = new PrintWriter(fileHndl);
510 } catch (Exception e) {
517 public static Map<String, String> parse_ip_bgp_vpnv4_all_summary(Map<String, String> countMap) {
518 File file = new File("cmd_ip_bgp_vpnv4_all_summary.txt");
521 List<String> inputStrs = new ArrayList<String>();
523 boolean startEntries = false;
528 scanner = new Scanner(file);
529 } catch (IOException e) {
530 LOGGER.trace("Could not process the file " + file.getAbsolutePath());
533 while (scanner.hasNextLine()) {
535 lineFromFile = scanner.nextLine();
536 inputStrs.add(lineFromFile);
539 while (i < inputStrs.size()) {
540 str = inputStrs.get(i);
541 LOGGER.trace("str is:: {}", str);
542 if (str.contains("State/PfxRcd")) {
544 } else if (startEntries == true) {
545 result = str.split("\\s+");
547 StrIP = result[0].trim();
548 LOGGER.trace("strIP " + StrIP);
550 if (!validate(StrIP)) {
553 String state_pfxRcvd = result[9];
554 countMap.put(StrIP, state_pfxRcvd);
555 } catch (Exception e) {
556 LOGGER.trace("Exception {} caught while processing ip bgp vpnv4 all summary command output, e");