check_junos.pl 15 KB

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