check_junos.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 POSIX qw( :termios_h );
  39. use Nagios::Plugin;
  40. use JUNOS::Device;
  41. binmode STDOUT, ":utf8";
  42. my $valid_checks = "interfaces|chassis_environment";
  43. # TODO:
  44. # * chassis_routing_engine: show chassis routing-engine (-> number and status)
  45. #
  46. # * storage: show system storage
  47. my $plugin = Nagios::Plugin->new(
  48. plugin => 'check_junos',
  49. shortname => 'check_junos',
  50. version => '0.1',
  51. url => 'http://oss.teamix.org/projects/monitoringplugins',
  52. blurb => 'Monitor Juniper™ Switches.',
  53. usage =>
  54. "Usage: %s [-v|--verbose] [-H <host>] [-p <port>] [-t <timeout]
  55. [-U <user>] [-P <password] 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™ Switch device and checks various of its
  62. components.
  63. A check-tuple consists of the name of the check and, optionally, a \"target\"
  64. which more closely specifies which characteristics should be checked, and
  65. warning and critical thresholds:
  66. checkname[,target[,warning[,critical]]]
  67. The following checks are available:
  68. * interfaces: Status of interfaces. If a target is specified, only the
  69. specified interface is taken into account.
  70. If an aggregated interface is encountered, the physical interfaces will
  71. be checked as well.
  72. * chassis_environment: Check the status of verious system components
  73. (as provided by 'show chassis environment').
  74. Warning and critical thresholds may be specified in the format documented at
  75. http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT.",
  76. );
  77. # Predefined arguments (by Nagios::Plugin)
  78. my @predefined_args = qw(
  79. usage
  80. help
  81. version
  82. extra-opts
  83. timeout
  84. verbose
  85. );
  86. my @args = (
  87. {
  88. spec => 'host|H=s',
  89. usage => '-H, --host=HOSTNAME',
  90. desc => 'Hostname/IP of Juniper box to connect to',
  91. default => 'localhost',
  92. },
  93. {
  94. spec => 'port|p=i',
  95. usage => '-p, --port=PORT',
  96. desc => 'Port to connect to',
  97. default => 22,
  98. },
  99. {
  100. spec => 'user|U=s',
  101. usage => '-U, --user=USERNAME',
  102. desc => 'Username to log into box as',
  103. default => 'root',
  104. },
  105. {
  106. spec => 'password|P=s',
  107. usage => '-P, --password=PASSWORD',
  108. desc => 'Password for login username',
  109. default => '<prompt>',
  110. },
  111. );
  112. my %conf = ();
  113. my $junos = undef;
  114. foreach my $arg (@args) {
  115. add_arg($plugin, $arg);
  116. }
  117. $plugin->getopts;
  118. # Initialize this first, so it may be used right away.
  119. $conf{'verbose'} = $plugin->opts->verbose;
  120. foreach my $arg (@args) {
  121. my @c = get_conf($plugin, $arg);
  122. $conf{$c[0]} = $c[1];
  123. }
  124. foreach my $arg (@predefined_args) {
  125. $conf{$arg} = $plugin->opts->$arg;
  126. }
  127. add_checks(\%conf, @ARGV);
  128. if (! $plugin->opts->password) {
  129. my $term = POSIX::Termios->new();
  130. my $lflag;
  131. print "Password: ";
  132. $term->getattr(fileno(STDIN));
  133. $lflag = $term->getlflag;
  134. $term->setlflag($lflag & ~POSIX::ECHO);
  135. $term->setattr(fileno(STDIN), TCSANOW);
  136. $conf{'password'} = <STDIN>;
  137. chomp($conf{'password'});
  138. $term->setlflag($lflag | POSIX::ECHO);
  139. print "\n";
  140. }
  141. verbose(1, "Connecting to host $conf{'host'} as user $conf{'user'}.");
  142. $junos = JUNOS::Device->new(
  143. hostname => $conf{'host'},
  144. login => $conf{'user'},
  145. password => $conf{'password'},
  146. access => 'ssh',
  147. 'ssh-compress' => 0);
  148. if (! ref $junos) {
  149. $plugin->die("ERROR: failed to connect to " . $conf{'host'} . "!");
  150. }
  151. foreach my $check (@{$conf{'checks'}}) {
  152. my $code;
  153. my $value;
  154. my @targets = ();
  155. if (defined $check->{'target'}) {
  156. @targets = @{$check->{'target'}};
  157. }
  158. $plugin->set_thresholds(
  159. warning => $check->{'warning'},
  160. critical => $check->{'critical'},
  161. );
  162. if ($check->{'name'} eq 'interfaces') {
  163. my $opts = {
  164. with_description => 0,
  165. };
  166. if (grep { m/^\@with_description$/; } @targets) {
  167. $opts->{'with_description'} = 1;
  168. @targets = grep { ! m/^\@with_description$/; } @targets;
  169. }
  170. my @interfaces = get_interfaces($junos, $opts, @targets);;
  171. my $down_count = 0;
  172. my @down_ifaces = ();
  173. my $phys_down_count = 0;
  174. my @phys_down_ifaces = ();
  175. my $have_lag_ifaces = 0;
  176. foreach my $iface (@interfaces) {
  177. my $name = get_iface_name($iface);
  178. my $status = check_interface($iface, $opts, @targets);
  179. if ($status == 0) {
  180. ++$down_count;
  181. push @down_ifaces, $name;
  182. }
  183. if ($status <= 0) {
  184. # disabled or down
  185. next;
  186. }
  187. if ($name !~ m/^ae/) {
  188. next;
  189. }
  190. $have_lag_ifaces = 1;
  191. my @markers = get_liface_marker(get_iface_first_logical($iface));
  192. if (! @markers) {
  193. next;
  194. }
  195. foreach my $marker (@markers) {
  196. my $phy_name = get_iface_name($marker);
  197. $phy_name =~ s/\.\d+$//;
  198. verbose(3, "Quering physical interface '$phy_name' "
  199. . "for $name.");
  200. my @phy_interfaces = get_interfaces($junos, {}, $phy_name);
  201. foreach my $phy_iface (@phy_interfaces) {
  202. if (check_interface($phy_iface, {}, $phy_name) == 0) {
  203. ++$phys_down_count;
  204. push @phys_down_ifaces, "$name -> $phy_name";
  205. }
  206. }
  207. }
  208. }
  209. if ($down_count > 0) {
  210. $plugin->add_message(CRITICAL, $down_count
  211. . " interfaces down (" . join(", ", @down_ifaces) . ")");
  212. }
  213. if ($phys_down_count > 0) {
  214. $plugin->add_message(WARNING, $phys_down_count
  215. . " LAG member interfaces down ("
  216. . join(", ", @phys_down_ifaces) . ")");
  217. }
  218. if ((! $down_count) && (! $phys_down_count)) {
  219. if (! scalar(@targets)) {
  220. $plugin->add_message(OK, "all interfaces up"
  221. . ($have_lag_ifaces
  222. ? " (including all LAG member interfaces)" : ""));
  223. }
  224. else {
  225. $plugin->add_message(OK, "interface"
  226. . (scalar(@targets) == 1 ? " " : "s ")
  227. . join(", ", @targets) . " up"
  228. . ($have_lag_ifaces
  229. ? " (including all LAG member interfaces)" : ""));
  230. }
  231. }
  232. }
  233. elsif ($check->{'name'} eq 'chassis_environment') {
  234. # XXX
  235. #show chassis environment (see check_snmp_environment)
  236. }
  237. }
  238. my ($code, $msg) = $plugin->check_messages(join => ', ');
  239. $junos->disconnect();
  240. $plugin->nagios_exit($code, $msg);
  241. sub send_query
  242. {
  243. my $device = shift;
  244. my $query = shift;
  245. my $queryargs = shift;
  246. my $res;
  247. my $err;
  248. verbose(3, "Sending query '$query' "
  249. . join(", ", map { "$_ => $queryargs->{$_}" } keys %$queryargs)
  250. . " to router.");
  251. if (ref $queryargs) {
  252. $res = $device->$query(%$queryargs);
  253. } else {
  254. $res = $device->$query();
  255. }
  256. if (! ref $res) {
  257. return "ERROR: Failed to execute query '$query'";
  258. }
  259. $err = $res->getFirstError();
  260. if ($err) {
  261. return "ERROR: " . $err->{message};
  262. }
  263. return $res;
  264. }
  265. sub check_interface {
  266. my $iface = shift;
  267. my $opts = shift || {};
  268. my @targets = @_;
  269. my $name = get_iface_name($iface);
  270. my $admin_status = get_iface_admin_status($iface);
  271. if ($admin_status !~ m/^up$/) {
  272. if ((grep { $name =~ m/^$_$/; } @targets)
  273. || ($opts->{'with_description'} &&
  274. get_iface_description($iface))) {
  275. $plugin->add_message(CRITICAL,
  276. "$name is not enabled");
  277. return -1;
  278. }
  279. return 1;
  280. }
  281. if (get_iface_status($iface) !~ m/^up$/i) {
  282. return 0;
  283. }
  284. $plugin->add_perfdata(
  285. label => "'$name-input-bytes'",
  286. value => get_iface_traffic($iface, "input"),
  287. min => 0,
  288. max => undef,
  289. uom => 'B',
  290. threshold => undef,
  291. );
  292. $plugin->add_perfdata(
  293. label => "'$name-output-bytes'",
  294. value => get_iface_traffic($iface, "output"),
  295. min => 0,
  296. max => undef,
  297. uom => 'B',
  298. threshold => undef,
  299. );
  300. return 1;
  301. }
  302. sub get_interfaces
  303. {
  304. my $device = shift;
  305. my $opts = shift || {};
  306. my @targets = @_;
  307. my @ifaces = ();
  308. my @ret = ();
  309. my $cmd = 'get_interface_information';
  310. my $res;
  311. my $args = { detail => 1 };
  312. if ((scalar(@targets) == 1) && (! $opts->{'with_description'})) {
  313. $args->{'interface_name'} = $targets[0];
  314. }
  315. $res = send_query($device, $cmd, $args);
  316. if (! ref $res) {
  317. $plugin->die($res);
  318. }
  319. @ifaces = $res->getElementsByTagName('physical-interface');
  320. @targets = map { s/\*/\.\*/g; s/\?/\./g; $_; } @targets;
  321. if (scalar(@targets)) {
  322. @ret = grep {
  323. my $i = $_;
  324. grep { get_iface_name($i) =~ m/^$_$/ } @targets;
  325. } @ifaces;
  326. }
  327. elsif (! $opts->{'with_description'}) {
  328. @ret = @ifaces;
  329. }
  330. if ($opts->{'with_description'}) {
  331. foreach my $iface (@ifaces) {
  332. my $name = get_iface_name($iface);
  333. if (get_iface_description($iface)
  334. && (! grep { m/^$name$/; } @targets)) {
  335. push @ret, $iface;
  336. }
  337. }
  338. }
  339. if ($conf{'verbose'} >= 3) {
  340. my @i = map { get_iface_name($_) . " => " . get_iface_status($_) }
  341. @ret;
  342. verbose(3, "Interfaces: " . join(", ", @i));
  343. }
  344. return @ret;
  345. }
  346. sub get_obj_element
  347. {
  348. my $obj = shift;
  349. my $elem = shift;
  350. $elem = $obj->getElementsByTagName($elem);
  351. if ((! $elem) || (! $elem->item(0))) {
  352. return;
  353. }
  354. return $elem->item(0)->getFirstChild->getNodeValue;
  355. }
  356. sub get_iface_name
  357. {
  358. my $iface = shift;
  359. return get_obj_element($iface, 'name');
  360. }
  361. sub get_iface_description
  362. {
  363. my $iface = shift;
  364. return get_obj_element($iface, 'description');
  365. }
  366. sub get_iface_status
  367. {
  368. my $iface = shift;
  369. return get_obj_element($iface, 'oper-status');
  370. }
  371. sub get_iface_admin_status
  372. {
  373. my $iface = shift;
  374. return get_obj_element($iface, 'admin-status');
  375. }
  376. sub get_iface_traffic
  377. {
  378. my $iface = shift;
  379. my $type = shift;
  380. my $stats = get_obj_element($iface, 'traffic-statistics');
  381. return get_obj_element($iface, "$type-bytes");
  382. }
  383. sub get_iface_first_logical
  384. {
  385. my $iface = shift;
  386. return $iface->getElementsByTagName('logical-interface')->item(0);
  387. }
  388. sub get_liface_marker
  389. {
  390. my $liface = shift;
  391. my $lag_stats = $liface->getElementsByTagName('lag-traffic-statistics')->item(0);
  392. if (! $lag_stats) {
  393. print STDERR "Cannot get marker for non-LACP interfaces yet!\n";
  394. return;
  395. }
  396. my @markers = $lag_stats->getElementsByTagName('lag-marker');
  397. return @markers;
  398. }
  399. sub add_arg
  400. {
  401. my $plugin = shift;
  402. my $arg = shift;
  403. my $spec = $arg->{'spec'};
  404. my $help = $arg->{'usage'};
  405. if (defined $arg->{'desc'}) {
  406. my @desc;
  407. if (ref($arg->{'desc'})) {
  408. @desc = @{$arg->{'desc'}};
  409. }
  410. else {
  411. @desc = ( $arg->{'desc'} );
  412. }
  413. foreach my $d (@desc) {
  414. $help .= "\n $d";
  415. }
  416. if (defined $arg->{'default'}) {
  417. $help .= " (default: $arg->{'default'})";
  418. }
  419. }
  420. elsif (defined $arg->{'default'}) {
  421. $help .= "\n (default: $arg->{'default'})";
  422. }
  423. $plugin->add_arg(
  424. spec => $spec,
  425. help => $help,
  426. );
  427. }
  428. sub get_conf
  429. {
  430. my $plugin = shift;
  431. my $arg = shift;
  432. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  433. my $value = $plugin->opts->$name || $arg->{'default'};
  434. if ($name eq 'password') {
  435. verbose(3, "conf: password => "
  436. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  437. }
  438. else {
  439. verbose(3, "conf: $name => $value");
  440. }
  441. return ($name => $value);
  442. }
  443. sub add_single_check
  444. {
  445. my $conf = shift;
  446. my @check = split(m/,/, shift);
  447. my %c = ();
  448. if ($check[0] !~ m/\b(?:$valid_checks)\b/) {
  449. return "ERROR: invalid check '$check[0]'";
  450. }
  451. $c{'name'} = $check[0];
  452. $c{'target'} = undef;
  453. if (defined($check[1])) {
  454. $c{'target'} = [ split(m/\+/, $check[1]) ];
  455. }
  456. $c{'warning'} = $check[2];
  457. $c{'critical'} = $check[3];
  458. # check for valid thresholds
  459. # set_threshold() will die if any threshold is not valid
  460. $plugin->set_thresholds(
  461. warning => $c{'warning'},
  462. critical => $c{'critical'},
  463. ) || $plugin->die("ERROR: Invalid thresholds: "
  464. . "warning => $c{'warning'}, critical => $c{'critical'}");
  465. push @{$conf->{'checks'}}, \%c;
  466. }
  467. sub add_checks
  468. {
  469. my $conf = shift;
  470. my @checks = @_;
  471. my $err_str = "ERROR:";
  472. if (scalar(@checks) == 0) {
  473. $conf->{'checks'}[0] = {
  474. name => 'chassis_environment',
  475. target => [],
  476. warning => undef,
  477. critical => undef,
  478. };
  479. return 1;
  480. }
  481. $conf->{'checks'} = [];
  482. foreach my $check (@checks) {
  483. my $e;
  484. $e = add_single_check($conf, $check);
  485. if ($e =~ m/^ERROR: (.*)$/) {
  486. $err_str .= " $1,";
  487. }
  488. }
  489. if ($err_str ne "ERROR:") {
  490. $err_str =~ s/,$//;
  491. $plugin->die($err_str);
  492. }
  493. }
  494. sub verbose
  495. {
  496. my $level = shift;
  497. my @msgs = @_;
  498. if ($level > $conf{'verbose'}) {
  499. return;
  500. }
  501. foreach my $msg (@msgs) {
  502. print "V$level: $msg\n";
  503. }
  504. }