check_junos.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. my $res = send_query($junos, 'get_environment_information');
  235. my %status_map = (
  236. OK => OK,
  237. Testing => UNKNOWN,
  238. Check => UNKNOWN,
  239. Failed => CRITICAL,
  240. Absent => CRITICAL,
  241. );
  242. my $items_count = 0;
  243. my $items_ok = 0;
  244. my $class = "";
  245. foreach my $item (get_object_by_spec($res, 'environment-item')) {
  246. my $name = get_object_value_by_spec($item, 'name');
  247. if (scalar(@targets) && (! grep { m/^$name$/ } @targets)) {
  248. next;
  249. }
  250. if (get_object_value_by_spec($item, 'class')) {
  251. $class = get_object_value_by_spec($item, 'class');
  252. }
  253. my $status = get_object_value_by_spec($item, 'status');
  254. if ($status eq "Absent") {
  255. if (! scalar(@targets)) {
  256. next;
  257. }
  258. # else: check this component
  259. }
  260. my $state = UNKNOWN;
  261. if (defined $status_map{$status}) {
  262. $state = $status_map{$status};
  263. }
  264. ++$items_count;
  265. if ($state == OK) {
  266. ++$items_ok;
  267. }
  268. else {
  269. $plugin->add_message($state, $class . " $name: status " .
  270. $status);
  271. }
  272. my $temp = get_object_value_by_spec($item, 'temperature');
  273. if (! $temp) {
  274. next;
  275. }
  276. ($temp) = $temp =~ m/(\d+) degrees C/;
  277. if (! defined($temp)) {
  278. next;
  279. }
  280. my $label = "$name-temp";
  281. $label =~ s/ /_/g;
  282. $plugin->add_perfdata(
  283. label => "'$label'",
  284. value => $temp,
  285. min => undef,
  286. max => undef,
  287. uom => '',
  288. threshold => undef,
  289. );
  290. }
  291. if (! $items_count) {
  292. $plugin->add_message(UNKNOWN, "no components found");
  293. }
  294. elsif ($items_count == $items_ok) {
  295. $plugin->add_message(OK, "$items_ok components OK");
  296. }
  297. else {
  298. $plugin->add_message(WARNING,
  299. "$items_ok / $items_count components OK");
  300. }
  301. }
  302. }
  303. my ($code, $msg) = $plugin->check_messages(join => ', ');
  304. $junos->disconnect();
  305. $plugin->nagios_exit($code, $msg);
  306. sub send_query
  307. {
  308. my $device = shift;
  309. my $query = shift;
  310. my $queryargs = shift;
  311. my $res;
  312. my $err;
  313. verbose(3, "Sending query '$query' "
  314. . join(", ", map { "$_ => $queryargs->{$_}" } keys %$queryargs)
  315. . " to router.");
  316. if (ref $queryargs) {
  317. $res = $device->$query(%$queryargs);
  318. } else {
  319. $res = $device->$query();
  320. }
  321. if (! ref $res) {
  322. return "ERROR: Failed to execute query '$query'";
  323. }
  324. $err = $res->getFirstError();
  325. if ($err) {
  326. return "ERROR: " . $err->{message};
  327. }
  328. return $res;
  329. }
  330. sub check_interface {
  331. my $iface = shift;
  332. my $opts = shift || {};
  333. my @targets = @_;
  334. my $name = get_iface_name($iface);
  335. my $admin_status = get_iface_admin_status($iface);
  336. if ($admin_status !~ m/^up$/) {
  337. if ((grep { $name =~ m/^$_$/; } @targets)
  338. || ($opts->{'with_description'} &&
  339. get_iface_description($iface))) {
  340. $plugin->add_message(CRITICAL,
  341. "$name is not enabled");
  342. return -1;
  343. }
  344. return 1;
  345. }
  346. if (get_iface_status($iface) !~ m/^up$/i) {
  347. return 0;
  348. }
  349. $plugin->add_perfdata(
  350. label => "'$name-input-bytes'",
  351. value => get_iface_traffic($iface, "input"),
  352. min => 0,
  353. max => undef,
  354. uom => 'B',
  355. threshold => undef,
  356. );
  357. $plugin->add_perfdata(
  358. label => "'$name-output-bytes'",
  359. value => get_iface_traffic($iface, "output"),
  360. min => 0,
  361. max => undef,
  362. uom => 'B',
  363. threshold => undef,
  364. );
  365. return 1;
  366. }
  367. sub get_interfaces
  368. {
  369. my $device = shift;
  370. my $opts = shift || {};
  371. my @targets = @_;
  372. my @ifaces = ();
  373. my @ret = ();
  374. my $cmd = 'get_interface_information';
  375. my $res;
  376. my $args = { detail => 1 };
  377. if ((scalar(@targets) == 1) && (! $opts->{'with_description'})) {
  378. $args->{'interface_name'} = $targets[0];
  379. }
  380. $res = send_query($device, $cmd, $args);
  381. if (! ref $res) {
  382. $plugin->die($res);
  383. }
  384. @ifaces = $res->getElementsByTagName('physical-interface');
  385. @targets = map { s/\*/\.\*/g; s/\?/\./g; $_; } @targets;
  386. if (scalar(@targets)) {
  387. @ret = grep {
  388. my $i = $_;
  389. grep { get_iface_name($i) =~ m/^$_$/ } @targets;
  390. } @ifaces;
  391. }
  392. elsif (! $opts->{'with_description'}) {
  393. @ret = @ifaces;
  394. }
  395. if ($opts->{'with_description'}) {
  396. foreach my $iface (@ifaces) {
  397. my $name = get_iface_name($iface);
  398. if (get_iface_description($iface)
  399. && (! grep { m/^$name$/; } @targets)) {
  400. push @ret, $iface;
  401. }
  402. }
  403. }
  404. if ($conf{'verbose'} >= 3) {
  405. my @i = map { get_iface_name($_) . " => " . get_iface_status($_) }
  406. @ret;
  407. verbose(3, "Interfaces: " . join(", ", @i));
  408. }
  409. return @ret;
  410. }
  411. sub get_obj_element
  412. {
  413. my $obj = shift;
  414. my $elem = shift;
  415. $elem = $obj->getElementsByTagName($elem);
  416. if ((! $elem) || (! $elem->item(0))) {
  417. return;
  418. }
  419. return $elem->item(0)->getFirstChild->getNodeValue;
  420. }
  421. sub get_object_value
  422. {
  423. my $res = shift;
  424. if (! $res) {
  425. return;
  426. }
  427. if (ref($res) eq "XML::DOM::NodeList") {
  428. $res = $res->item(0);
  429. }
  430. return $res->getFirstChild->getNodeValue;
  431. }
  432. sub get_object_by_spec
  433. {
  434. my $res = shift;
  435. my $spec = shift;
  436. if (! $res) {
  437. return;
  438. }
  439. if (! $spec) {
  440. return $res;
  441. }
  442. if (! ref($spec)) {
  443. $spec = [ $spec ];
  444. }
  445. my $iter = $res;
  446. for (my $i = 0; $i < scalar(@$spec) - 1; ++$i) {
  447. my $tmp = $iter->getElementsByTagName($spec->[$i]);
  448. if ((! $tmp) || (! $tmp->item(0))) {
  449. return;
  450. }
  451. $iter = $tmp->item(0);
  452. }
  453. if (wantarray) {
  454. my @ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  455. return @ret;
  456. }
  457. else {
  458. my $ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  459. if ((! $ret) || (! $ret->item(0))) {
  460. return;
  461. }
  462. return $ret->item(0);
  463. }
  464. }
  465. sub get_object_value_by_spec
  466. {
  467. my $res = get_object_by_spec(@_);
  468. return get_object_value($res);
  469. }
  470. sub get_iface_name
  471. {
  472. my $iface = shift;
  473. return get_obj_element($iface, 'name');
  474. }
  475. sub get_iface_description
  476. {
  477. my $iface = shift;
  478. return get_obj_element($iface, 'description');
  479. }
  480. sub get_iface_status
  481. {
  482. my $iface = shift;
  483. return get_obj_element($iface, 'oper-status');
  484. }
  485. sub get_iface_admin_status
  486. {
  487. my $iface = shift;
  488. return get_obj_element($iface, 'admin-status');
  489. }
  490. sub get_iface_traffic
  491. {
  492. my $iface = shift;
  493. my $type = shift;
  494. my $stats = get_obj_element($iface, 'traffic-statistics');
  495. return get_obj_element($iface, "$type-bytes");
  496. }
  497. sub get_iface_first_logical
  498. {
  499. my $iface = shift;
  500. return $iface->getElementsByTagName('logical-interface')->item(0);
  501. }
  502. sub get_liface_marker
  503. {
  504. my $liface = shift;
  505. my $lag_stats = $liface->getElementsByTagName('lag-traffic-statistics')->item(0);
  506. if (! $lag_stats) {
  507. print STDERR "Cannot get marker for non-LACP interfaces yet!\n";
  508. return;
  509. }
  510. my @markers = $lag_stats->getElementsByTagName('lag-marker');
  511. return @markers;
  512. }
  513. sub add_arg
  514. {
  515. my $plugin = shift;
  516. my $arg = shift;
  517. my $spec = $arg->{'spec'};
  518. my $help = $arg->{'usage'};
  519. if (defined $arg->{'desc'}) {
  520. my @desc;
  521. if (ref($arg->{'desc'})) {
  522. @desc = @{$arg->{'desc'}};
  523. }
  524. else {
  525. @desc = ( $arg->{'desc'} );
  526. }
  527. foreach my $d (@desc) {
  528. $help .= "\n $d";
  529. }
  530. if (defined $arg->{'default'}) {
  531. $help .= " (default: $arg->{'default'})";
  532. }
  533. }
  534. elsif (defined $arg->{'default'}) {
  535. $help .= "\n (default: $arg->{'default'})";
  536. }
  537. $plugin->add_arg(
  538. spec => $spec,
  539. help => $help,
  540. );
  541. }
  542. sub get_conf
  543. {
  544. my $plugin = shift;
  545. my $arg = shift;
  546. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  547. my $value = $plugin->opts->$name || $arg->{'default'};
  548. if ($name eq 'password') {
  549. verbose(3, "conf: password => "
  550. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  551. }
  552. else {
  553. verbose(3, "conf: $name => $value");
  554. }
  555. return ($name => $value);
  556. }
  557. sub add_single_check
  558. {
  559. my $conf = shift;
  560. my @check = split(m/,/, shift);
  561. my %c = ();
  562. if ($check[0] !~ m/\b(?:$valid_checks)\b/) {
  563. return "ERROR: invalid check '$check[0]'";
  564. }
  565. $c{'name'} = $check[0];
  566. $c{'target'} = undef;
  567. if (defined($check[1])) {
  568. $c{'target'} = [ split(m/\+/, $check[1]) ];
  569. }
  570. $c{'warning'} = $check[2];
  571. $c{'critical'} = $check[3];
  572. # check for valid thresholds
  573. # set_threshold() will die if any threshold is not valid
  574. $plugin->set_thresholds(
  575. warning => $c{'warning'},
  576. critical => $c{'critical'},
  577. ) || $plugin->die("ERROR: Invalid thresholds: "
  578. . "warning => $c{'warning'}, critical => $c{'critical'}");
  579. push @{$conf->{'checks'}}, \%c;
  580. }
  581. sub add_checks
  582. {
  583. my $conf = shift;
  584. my @checks = @_;
  585. my $err_str = "ERROR:";
  586. if (scalar(@checks) == 0) {
  587. $conf->{'checks'}[0] = {
  588. name => 'chassis_environment',
  589. target => [],
  590. warning => undef,
  591. critical => undef,
  592. };
  593. return 1;
  594. }
  595. $conf->{'checks'} = [];
  596. foreach my $check (@checks) {
  597. my $e;
  598. $e = add_single_check($conf, $check);
  599. if ($e =~ m/^ERROR: (.*)$/) {
  600. $err_str .= " $1,";
  601. }
  602. }
  603. if ($err_str ne "ERROR:") {
  604. $err_str =~ s/,$//;
  605. $plugin->die($err_str);
  606. }
  607. }
  608. sub verbose
  609. {
  610. my $level = shift;
  611. my @msgs = @_;
  612. if ($level > $conf{'verbose'}) {
  613. return;
  614. }
  615. foreach my $msg (@msgs) {
  616. print "V$level: $msg\n";
  617. }
  618. }