check_junos_bgp.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #!/usr/bin/perl
  2. #############################################################################
  3. # (c) 2001, 2003 Juniper Networks, Inc. #
  4. # (c) 2011 Sebastian "tokkee" Harl <sh@teamix.net> #
  5. # and team(ix) GmbH, Nuernberg, Germany #
  6. # #
  7. # This file is part of "team(ix) Monitoring Plugins" #
  8. # URL: http://oss.teamix.org/projects/monitoringplugins/ #
  9. # #
  10. # All rights reserved. #
  11. # Redistribution and use in source and binary forms, with or without #
  12. # modification, are permitted provided that the following conditions #
  13. # are met: #
  14. # 1. Redistributions of source code must retain the above copyright #
  15. # notice, this list of conditions and the following disclaimer. #
  16. # 2. Redistributions in binary form must reproduce the above copyright #
  17. # notice, this list of conditions and the following disclaimer in the #
  18. # documentation and/or other materials provided with the distribution. #
  19. # 3. The name of the copyright owner may not be used to endorse or #
  20. # promote products derived from this software without specific prior #
  21. # written permission. #
  22. # #
  23. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR #
  24. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #
  25. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #
  26. # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, #
  27. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #
  28. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #
  29. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) #
  30. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, #
  31. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING #
  32. # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE #
  33. # POSSIBILITY OF SUCH DAMAGE. #
  34. #############################################################################
  35. use strict;
  36. use warnings;
  37. use utf8;
  38. use Data::Dumper;
  39. use POSIX qw( :termios_h );
  40. use Nagios::Plugin;
  41. use Regexp::Common;
  42. use Regexp::IPv6 qw( $IPv6_re );
  43. use JUNOS::Device;
  44. binmode STDOUT, ":utf8";
  45. my $valid_checks = "peers_count|prefix_count";
  46. my $plugin = Nagios::Plugin->new(
  47. plugin => 'check_junos_bgp',
  48. shortname => 'check_junos_bgp',
  49. version => '0.1',
  50. url => 'http://oss.teamix.org/projects/monitoringplugins',
  51. blurb => 'Monitor Juniper™ Router\'s BGP tables.',
  52. usage =>
  53. "Usage: %s [-v|--verbose] [-t <timeout] \
  54. [-H <host>] [-p <port>] [-U <user>] [-P <password] \
  55. [-L <logical-system-name>] [-I <name of instance>] check-tuple [...] ",
  56. license =>
  57. "This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY.
  58. It may be used, redistributed and/or modified under the terms of the 3-Clause
  59. BSD License (see http://opensource.org/licenses/BSD-3-Clause).",
  60. extra => "
  61. This plugin connects to a Juniper™ Router device and requests BGP table
  62. information using the 'show bgp neighbor' command. It then checks the
  63. specified thresholds depending on the specified checks.
  64. A check-tuple consists of the name of the check and, optionally, a \"target\"
  65. (e.g., peer address), and warning and critical thresholds:
  66. checkname[,target[,warning[,critical]]]
  67. The following checks are available:
  68. * peers_count: Total number of peers. If a target is specified, only peers
  69. matching that target are taken into account.
  70. * prefix_count: Number of active prefixes for a single peer. If multiple
  71. peers match the specified target, each of those is checked against the
  72. specified thresholds.
  73. Targets are either specified as IPv4/IPv6 addresses or regular expressions /
  74. strings. In the former case, the target is compared against the peer's
  75. address, else against the peer's description. When specifying regular
  76. expressions, they have to be enclosed in '/'. Else, the pattern is treated as
  77. verbatim string that has to be matched.
  78. Warning and critical thresholds may be specified in the format documented at
  79. http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT.",
  80. );
  81. # Predefined arguments (by Nagios::Plugin)
  82. my @predefined_args = qw(
  83. usage
  84. help
  85. version
  86. extra-opts
  87. timeout
  88. verbose
  89. );
  90. my @args = (
  91. {
  92. spec => 'host|H=s',
  93. usage => '-H, --host=HOSTNAME',
  94. desc => 'Hostname/IP of Juniper box to connect to',
  95. default => 'localhost',
  96. },
  97. {
  98. spec => 'port|p=i',
  99. usage => '-p, --port=PORT',
  100. desc => 'Port to connect to',
  101. default => 22,
  102. },
  103. {
  104. spec => 'user|U=s',
  105. usage => '-U, --user=USERNAME',
  106. desc => 'Username to log into box as',
  107. default => 'root',
  108. },
  109. {
  110. spec => 'password|P=s',
  111. usage => '-P, --password=PASSWORD',
  112. desc => 'Password for login username',
  113. default => '<prompt>',
  114. },
  115. {
  116. spec => 'logical-router|L=s',
  117. usage => '-L, --logical-router=ROUTER',
  118. desc => 'Logical Router',
  119. default => '',
  120. },
  121. {
  122. spec => 'instance|I=s',
  123. usage => '-I, --instance=INSTANCE',
  124. desc => 'Instance',
  125. default => '',
  126. },
  127. );
  128. my %conf = ();
  129. my $junos = undef;
  130. my $neigh_info = undef;
  131. my @peers = ();
  132. foreach my $arg (@args) {
  133. add_arg($plugin, $arg);
  134. }
  135. $plugin->getopts;
  136. # Initialize this first, so it may be used right away.
  137. $conf{'verbose'} = $plugin->opts->verbose;
  138. foreach my $arg (@args) {
  139. my @c = get_conf($plugin, $arg);
  140. $conf{$c[0]} = $c[1];
  141. }
  142. foreach my $arg (@predefined_args) {
  143. $conf{$arg} = $plugin->opts->$arg;
  144. }
  145. add_checks(\%conf, @ARGV);
  146. if (! $plugin->opts->password) {
  147. my $term = POSIX::Termios->new();
  148. my $lflag;
  149. print "Password: ";
  150. $term->getattr(fileno(STDIN));
  151. $lflag = $term->getlflag;
  152. $term->setlflag($lflag & ~POSIX::ECHO);
  153. $term->setattr(fileno(STDIN), TCSANOW);
  154. $conf{'password'} = <STDIN>;
  155. chomp($conf{'password'});
  156. $term->setlflag($lflag | POSIX::ECHO);
  157. $term->setattr(fileno(STDIN), TCSAFLUSH);
  158. print "\n";
  159. }
  160. verbose(1, "Connecting to host $conf{'host'} as user $conf{'user'}.");
  161. $junos = JUNOS::Device->new(
  162. hostname => $conf{'host'},
  163. login => $conf{'user'},
  164. password => $conf{'password'},
  165. access => 'ssh',
  166. 'ssh-compress' => 0);
  167. if (! ref $junos) {
  168. $plugin->die("ERROR: failed to connect to " . $conf{'host'} . "!");
  169. }
  170. verbose(1, "Querying BGP neighbor information.");
  171. $neigh_info = get_neighbor_information($junos);
  172. if (! ref $neigh_info) {
  173. $plugin->die($neigh_info);
  174. }
  175. @peers = $neigh_info->getElementsByTagName('bgp-peer');
  176. if ($conf{'verbose'} >= 3) {
  177. my @p = map { (get_peer_address($_) // "<unknown address>")
  178. . " => " . (get_peer_description($_) // "<unknown description>") } @peers;
  179. verbose(3, "Peers: " . join(", ", @p));
  180. }
  181. foreach my $check (@{$conf{'checks'}}) {
  182. my $code;
  183. my $value;
  184. my @relevant_peers = get_relevant_peers($check, @peers);
  185. if ($conf{'verbose'} >= 2) {
  186. my @p = map { (get_peer_address($_) // "<unknown address>")
  187. . " => " . (get_peer_description($_) // "<unknown description>") } @relevant_peers;
  188. verbose(2, "Relevant peers: " . join(", ", @p));
  189. }
  190. $plugin->set_thresholds(
  191. warning => $check->{'warning'},
  192. critical => $check->{'critical'},
  193. );
  194. if ($check->{'name'} eq 'peers_count') {
  195. $value = scalar(@relevant_peers);
  196. $code = $plugin->check_threshold($value);
  197. $plugin->add_message($code, "$value peer" . (($value == 1) ? "" : "s"));
  198. $plugin->add_perfdata(
  199. label => 'peers_count',
  200. value => $value,
  201. min => 0,
  202. max => undef,
  203. uom => '',
  204. threshold => $plugin->threshold(),
  205. );
  206. }
  207. elsif ($check->{'name'} eq 'prefix_count') {
  208. foreach my $peer (@relevant_peers) {
  209. my $peer_addr = get_peer_address($peer);
  210. if (! defined($peer_addr)) {
  211. $peer_addr = "<unknown address>";
  212. }
  213. $value = get_peer_element($peer, 'peer-state');
  214. if (! defined($value)) {
  215. $value = "<unknown state>";
  216. }
  217. verbose(2, "Peer $peer_addr: peer-state = $value.");
  218. if ($value eq 'Established') {
  219. $value = $peer->getElementsByTagName('bgp-rib');
  220. $value = get_peer_element($value->[0], 'active-prefix-count');
  221. if (! $value) {
  222. $value = 0;
  223. }
  224. $code = $plugin->check_threshold($value);
  225. $plugin->add_message($code, "peer $peer_addr: $value prefix"
  226. . (($value == 1) ? "" : "es"));
  227. verbose(2, "Peer $peer_addr: active-prefix-count = $value.");
  228. }
  229. else {
  230. $value = "";
  231. $code = CRITICAL;
  232. $plugin->add_message($code,
  233. "peer $peer_addr: no established connection");
  234. }
  235. $plugin->add_perfdata(
  236. label => '\'prefix_count[' . $peer_addr . ']\'',
  237. value => $value,
  238. min => 0,
  239. max => undef,
  240. uom => '',
  241. threshold => $plugin->threshold(),
  242. );
  243. }
  244. }
  245. }
  246. my ($code, $msg) = $plugin->check_messages(join => ', ');
  247. $junos->disconnect();
  248. $plugin->nagios_exit($code, $msg);
  249. sub send_query
  250. {
  251. my $device = shift;
  252. my $query = shift;
  253. my $queryargs = shift;
  254. my $res;
  255. my $err;
  256. verbose(3, "Sending query '$query' to router.");
  257. if (ref $queryargs) {
  258. $res = $device->$query(%$queryargs);
  259. } else {
  260. $res = $device->$query();
  261. }
  262. verbose(5, "Got response: " . Dumper(\$res));
  263. if (! ref $res) {
  264. return "ERROR: Failed to execute query '$query'";
  265. }
  266. $err = $res->getFirstError();
  267. if ($err) {
  268. return "ERROR: " . $err->{message};
  269. }
  270. return $res;
  271. }
  272. sub get_neighbor_information
  273. {
  274. my $device = shift;
  275. my @table;
  276. my $query = "get_bgp_summary_information";
  277. my $res;
  278. my %args;
  279. if (($conf{'logical-router'} ne '') || ($conf{'instance'} ne '')) {
  280. if ($conf{'logical-router'} ne '') {
  281. $args{'logical-router'} = $conf{'logical-router'};
  282. }
  283. if ($conf{'instance'} ne '') {
  284. $args{'instance'} = $conf{'instance'};
  285. }
  286. $res = send_query($device, $query, \%args);
  287. } else {
  288. $res = send_query($device, $query);
  289. }
  290. my $err;
  291. if (! ref $res) {
  292. return $res;
  293. }
  294. $err = $res->getFirstError();
  295. if ($err) {
  296. return "ERROR: " . $err->{message};
  297. }
  298. return $res;
  299. }
  300. sub add_arg
  301. {
  302. my $plugin = shift;
  303. my $arg = shift;
  304. my $spec = $arg->{'spec'};
  305. my $help = $arg->{'usage'};
  306. if (defined $arg->{'desc'}) {
  307. my @desc;
  308. if (ref($arg->{'desc'})) {
  309. @desc = @{$arg->{'desc'}};
  310. }
  311. else {
  312. @desc = ( $arg->{'desc'} );
  313. }
  314. foreach my $d (@desc) {
  315. $help .= "\n $d";
  316. }
  317. if (defined $arg->{'default'}) {
  318. $help .= " (default: $arg->{'default'})";
  319. }
  320. }
  321. elsif (defined $arg->{'default'}) {
  322. $help .= "\n (default: $arg->{'default'})";
  323. }
  324. $plugin->add_arg(
  325. spec => $spec,
  326. help => $help,
  327. );
  328. }
  329. sub get_conf
  330. {
  331. my $plugin = shift;
  332. my $arg = shift;
  333. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  334. my $value = $plugin->opts->$name || $arg->{'default'};
  335. if ($name eq 'password') {
  336. verbose(3, "conf: password => "
  337. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  338. }
  339. else {
  340. verbose(3, "conf: $name => $value");
  341. }
  342. return ($name => $value);
  343. }
  344. sub add_single_check
  345. {
  346. my $conf = shift;
  347. my @check = split(m/,/, shift);
  348. my %c = ();
  349. if ($check[0] !~ m/\b(?:$valid_checks)\b/) {
  350. return "ERROR: invalid check '$check[0]'";
  351. }
  352. $c{'name'} = $check[0];
  353. if ((! defined($check[1])) || ($check[1] eq "")) {
  354. $c{'target'} = qr//,
  355. $c{'ttype'} = 'address',
  356. }
  357. elsif ($check[1] =~ m/^(?:$RE{'net'}{'IPv4'}|$IPv6_re)$/) {
  358. $c{'target'} = $check[1];
  359. $c{'ttype'} = 'address';
  360. }
  361. elsif ($check[1] =~ m/^\/(.*)\/$/) {
  362. $c{'target'} = qr/$1/;
  363. $c{'ttype'} = 'description';
  364. }
  365. else {
  366. $c{'target'} = $check[1];
  367. $c{'ttype'} = 'description';
  368. }
  369. $c{'warning'} = $check[2];
  370. $c{'critical'} = $check[3];
  371. # check for valid thresholds
  372. # set_threshold() will die if any threshold is not valid
  373. $plugin->set_thresholds(
  374. warning => $c{'warning'},
  375. critical => $c{'critical'},
  376. ) || $plugin->die("ERROR: Invalid thresholds: "
  377. . "warning => $c{'warning'}, critical => $c{'critical'}");
  378. push @{$conf->{'checks'}}, \%c;
  379. }
  380. sub add_checks
  381. {
  382. my $conf = shift;
  383. my @checks = @_;
  384. my $err_str = "ERROR:";
  385. if (scalar(@checks) == 0) {
  386. $conf->{'checks'}[0] = {
  387. name => 'peers_count',
  388. target => qr//,
  389. ttype => 'address',
  390. warning => undef,
  391. critical => undef,
  392. };
  393. return 1;
  394. }
  395. $conf->{'checks'} = [];
  396. foreach my $check (@checks) {
  397. my $e;
  398. $e = add_single_check($conf, $check);
  399. if ($e =~ m/^ERROR: (.*)$/) {
  400. $err_str .= " $1,";
  401. }
  402. }
  403. if ($err_str ne "ERROR:") {
  404. $err_str =~ s/,$//;
  405. $plugin->die($err_str);
  406. }
  407. }
  408. sub get_relevant_peers
  409. {
  410. my $check = shift;
  411. my @peers = @_;
  412. my @rpeers = ();
  413. my $cmp = sub {
  414. my ($a, $b, undef) = @_;
  415. if (ref $b) {
  416. my $r = $a =~ $b;
  417. verbose(3, "Checking peer '$a' against regex '$b' -> "
  418. . ($r ? "true" : "false") . ".");
  419. return $r;
  420. }
  421. else {
  422. my $r = $a eq $b;
  423. verbose(3, "Comparing peer '$a' with string '$b' -> "
  424. . ($r ? "true" : "false") . ".");
  425. return $r;
  426. }
  427. };
  428. my $get_peer_elem;
  429. if ($check->{'ttype'} eq 'description') {
  430. $get_peer_elem = \&get_peer_description;
  431. }
  432. else {
  433. $get_peer_elem = \&get_peer_address;
  434. }
  435. @rpeers = grep { $cmp->($get_peer_elem->($_), $check->{'target'}) } @peers;
  436. return @rpeers;
  437. }
  438. sub get_peer_element
  439. {
  440. my $peer = shift;
  441. my $elem = shift;
  442. my $e;
  443. if (! $peer) {
  444. print STDERR "Cannot retrieve element '$elem' "
  445. . "from undefined value.\n";
  446. return;
  447. }
  448. $e = $peer->getElementsByTagName($elem);
  449. if ((! $e) || (! $e->item(0))) {
  450. print STDERR "Attribute '$elem' not found for peer.\n";
  451. verbose(4, "Peer was: " . Dumper($peer));
  452. return;
  453. }
  454. return $e->item(0)->getFirstChild->getNodeValue;
  455. }
  456. sub get_peer_description
  457. {
  458. my $peer = shift;
  459. return get_peer_element($peer, 'description');
  460. }
  461. sub get_peer_address
  462. {
  463. my $peer = shift;
  464. return get_peer_element($peer, 'peer-address');
  465. }
  466. sub verbose
  467. {
  468. my $level = shift;
  469. my @msgs = @_;
  470. if ($level > $conf{'verbose'}) {
  471. return;
  472. }
  473. foreach my $msg (@msgs) {
  474. print "V$level: $msg\n";
  475. }
  476. }