check_junos.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 $code;
  114. my $value;
  115. my @targets = ();
  116. if (defined $check->{'target'}) {
  117. @targets = @{$check->{'target'}};
  118. }
  119. $plugin->set_thresholds(
  120. warning => $check->{'warning'},
  121. critical => $check->{'critical'},
  122. );
  123. if ($check->{'name'} eq 'interfaces') {
  124. my $opts = {
  125. with_description => 0,
  126. };
  127. if (grep { m/^\@with_description$/; } @targets) {
  128. $opts->{'with_description'} = 1;
  129. @targets = grep { ! m/^\@with_description$/; } @targets;
  130. }
  131. my @interfaces = get_interfaces($junos, $opts, @targets);;
  132. my $down_count = 0;
  133. my @down_ifaces = ();
  134. my $phys_down_count = 0;
  135. my @phys_down_ifaces = ();
  136. my $have_lag_ifaces = 0;
  137. foreach my $iface (@interfaces) {
  138. my $name = get_iface_name($iface);
  139. my $status = check_interface($iface, $opts, @targets);
  140. if ($status == 0) {
  141. ++$down_count;
  142. push @down_ifaces, $name;
  143. }
  144. if ($status <= 0) {
  145. # disabled or down
  146. next;
  147. }
  148. if ($name !~ m/^ae/) {
  149. next;
  150. }
  151. $have_lag_ifaces = 1;
  152. my @markers = get_liface_marker(get_iface_first_logical($iface));
  153. if (! @markers) {
  154. next;
  155. }
  156. foreach my $marker (@markers) {
  157. my $phy_name = get_iface_name($marker);
  158. $phy_name =~ s/\.\d+$//;
  159. $plugin->verbose(3, "Quering physical interface '$phy_name' "
  160. . "for $name.");
  161. my @phy_interfaces = get_interfaces($junos, {}, $phy_name);
  162. foreach my $phy_iface (@phy_interfaces) {
  163. if (check_interface($phy_iface, {}, $phy_name) == 0) {
  164. ++$phys_down_count;
  165. push @phys_down_ifaces, "$name -> $phy_name";
  166. }
  167. }
  168. }
  169. }
  170. if ($down_count > 0) {
  171. $plugin->add_message(CRITICAL, $down_count
  172. . " interfaces down (" . join(", ", @down_ifaces) . ")");
  173. }
  174. if ($phys_down_count > 0) {
  175. $plugin->add_message(WARNING, $phys_down_count
  176. . " LAG member interfaces down ("
  177. . join(", ", @phys_down_ifaces) . ")");
  178. }
  179. if ((! $down_count) && (! $phys_down_count)) {
  180. if (! scalar(@targets)) {
  181. $plugin->add_message(OK, "all interfaces up"
  182. . ($have_lag_ifaces
  183. ? " (including all LAG member interfaces)" : ""));
  184. }
  185. else {
  186. $plugin->add_message(OK, "interface"
  187. . (scalar(@targets) == 1 ? " " : "s ")
  188. . join(", ", @targets) . " up"
  189. . ($have_lag_ifaces
  190. ? " (including all LAG member interfaces)" : ""));
  191. }
  192. }
  193. }
  194. elsif ($check->{'name'} eq 'chassis_environment') {
  195. my $res = send_query($junos, 'get_environment_information');
  196. my %status_map = (
  197. OK => OK,
  198. Testing => UNKNOWN,
  199. Check => UNKNOWN,
  200. Failed => CRITICAL,
  201. Absent => CRITICAL,
  202. );
  203. my $items_count = 0;
  204. my $items_ok = 0;
  205. my $class = "";
  206. foreach my $item (get_object_by_spec($res, 'environment-item')) {
  207. my $name = get_object_value_by_spec($item, 'name');
  208. if (scalar(@targets) && (! grep { m/^$name$/ } @targets)) {
  209. next;
  210. }
  211. if (get_object_value_by_spec($item, 'class')) {
  212. $class = get_object_value_by_spec($item, 'class');
  213. }
  214. my $status = get_object_value_by_spec($item, 'status');
  215. if ($status eq "Absent") {
  216. if (! scalar(@targets)) {
  217. next;
  218. }
  219. # else: check this component
  220. }
  221. my $state = UNKNOWN;
  222. if (defined $status_map{$status}) {
  223. $state = $status_map{$status};
  224. }
  225. ++$items_count;
  226. if ($state == OK) {
  227. ++$items_ok;
  228. }
  229. else {
  230. $plugin->add_message($state, $class . " $name: status " .
  231. $status);
  232. }
  233. my $temp = get_object_value_by_spec($item, 'temperature');
  234. if (! $temp) {
  235. next;
  236. }
  237. ($temp) = $temp =~ m/(\d+) degrees C/;
  238. if (! defined($temp)) {
  239. next;
  240. }
  241. $state = $plugin->check_threshold($temp);
  242. if ($state != OK) {
  243. $plugin->add_message($state, $class
  244. . " $name: ${temp} degrees C");
  245. }
  246. my $label = "$name-temp";
  247. $label =~ s/ /_/g;
  248. $plugin->add_perfdata(
  249. label => "'$label'",
  250. value => $temp,
  251. min => undef,
  252. max => undef,
  253. uom => '',
  254. threshold => $plugin->threshold(),
  255. );
  256. }
  257. if (! $items_count) {
  258. $plugin->add_message(UNKNOWN, "no components found");
  259. }
  260. elsif ($items_count == $items_ok) {
  261. $plugin->add_message(OK, "$items_ok components OK");
  262. }
  263. else {
  264. $plugin->add_message(WARNING,
  265. "$items_ok / $items_count components OK");
  266. }
  267. }
  268. elsif ($check->{'name'} eq 'system_storage') {
  269. my $res = send_query($junos, 'get_system_storage');
  270. foreach my $re (get_object_by_spec($res,
  271. 'multi-routing-engine-item')) {
  272. my $re_name = get_object_value_by_spec($re, 're-name');
  273. foreach my $fs (get_object_by_spec($re,
  274. ['system-storage-information', 'filesystem'])) {
  275. my $name = get_object_value_by_spec($fs, 'filesystem-name');
  276. my $mnt_pt = get_object_value_by_spec($fs, 'mounted-on');
  277. if (scalar(@targets) && (! grep { m/^$name$/ } @targets)
  278. && (! grep { m/^$mnt_pt$/ } @targets)) {
  279. next;
  280. }
  281. my $used = get_object_value_by_spec($fs, 'used-percent') + 0;
  282. my $state = $plugin->check_threshold($used);
  283. if ($state != OK) {
  284. $plugin->add_message($state, "$re_name $mnt_pt: "
  285. . "$used\% used");
  286. }
  287. $plugin->add_perfdata(
  288. label => "'$re_name-$mnt_pt'",
  289. value => $used,
  290. min => 0,
  291. max => 100,
  292. uom => '%',
  293. threshold => $plugin->threshold(),
  294. );
  295. }
  296. }
  297. }
  298. }
  299. my ($code, $msg) = $plugin->check_messages(join => ', ');
  300. $junos->disconnect();
  301. $plugin->nagios_exit($code, $msg);
  302. sub send_query
  303. {
  304. my $device = shift;
  305. my $query = shift;
  306. my $queryargs = shift;
  307. my $res;
  308. my $err;
  309. $plugin->verbose(3, "Sending query '$query' "
  310. . join(", ", map { "$_ => $queryargs->{$_}" } keys %$queryargs)
  311. . " to router.");
  312. if (ref $queryargs) {
  313. $res = $device->$query(%$queryargs);
  314. } else {
  315. $res = $device->$query();
  316. }
  317. if (! ref $res) {
  318. return "ERROR: Failed to execute query '$query'";
  319. }
  320. $err = $res->getFirstError();
  321. if ($err) {
  322. return "ERROR: " . $err->{message};
  323. }
  324. return $res;
  325. }
  326. sub check_interface {
  327. my $iface = shift;
  328. my $opts = shift || {};
  329. my @targets = @_;
  330. my $name = get_iface_name($iface);
  331. my $admin_status = get_iface_admin_status($iface);
  332. if ($admin_status !~ m/^up$/) {
  333. if ((grep { $name =~ m/^$_$/; } @targets)
  334. || ($opts->{'with_description'} &&
  335. get_iface_description($iface))) {
  336. $plugin->add_message(CRITICAL,
  337. "$name is not enabled");
  338. return -1;
  339. }
  340. return 1;
  341. }
  342. if (get_iface_status($iface) !~ m/^up$/i) {
  343. return 0;
  344. }
  345. $plugin->add_perfdata(
  346. label => "'$name-input-bytes'",
  347. value => get_iface_traffic($iface, "input"),
  348. min => 0,
  349. max => undef,
  350. uom => 'B',
  351. threshold => undef,
  352. );
  353. $plugin->add_perfdata(
  354. label => "'$name-output-bytes'",
  355. value => get_iface_traffic($iface, "output"),
  356. min => 0,
  357. max => undef,
  358. uom => 'B',
  359. threshold => undef,
  360. );
  361. return 1;
  362. }
  363. sub get_interfaces
  364. {
  365. my $device = shift;
  366. my $opts = shift || {};
  367. my @targets = @_;
  368. my @ifaces = ();
  369. my @ret = ();
  370. my $cmd = 'get_interface_information';
  371. my $res;
  372. my $args = { detail => 1 };
  373. if ((scalar(@targets) == 1) && (! $opts->{'with_description'})) {
  374. $args->{'interface_name'} = $targets[0];
  375. }
  376. $res = send_query($device, $cmd, $args);
  377. if (! ref $res) {
  378. $plugin->die($res);
  379. }
  380. @ifaces = $res->getElementsByTagName('physical-interface');
  381. @targets = map { s/\*/\.\*/g; s/\?/\./g; $_; } @targets;
  382. if (scalar(@targets)) {
  383. @ret = grep {
  384. my $i = $_;
  385. grep { get_iface_name($i) =~ m/^$_$/ } @targets;
  386. } @ifaces;
  387. }
  388. elsif (! $opts->{'with_description'}) {
  389. @ret = @ifaces;
  390. }
  391. if ($opts->{'with_description'}) {
  392. foreach my $iface (@ifaces) {
  393. my $name = get_iface_name($iface);
  394. if (get_iface_description($iface)
  395. && (! grep { m/^$name$/; } @targets)) {
  396. push @ret, $iface;
  397. }
  398. }
  399. }
  400. {
  401. my @i = map { get_iface_name($_) . " => " . get_iface_status($_) }
  402. @ret;
  403. $plugin->verbose(3, "Interfaces: " . join(", ", @i));
  404. }
  405. return @ret;
  406. }
  407. sub get_obj_element
  408. {
  409. my $obj = shift;
  410. my $elem = shift;
  411. $elem = $obj->getElementsByTagName($elem);
  412. if ((! $elem) || (! $elem->item(0))) {
  413. return;
  414. }
  415. return $elem->item(0)->getFirstChild->getNodeValue;
  416. }
  417. sub get_object_value
  418. {
  419. my $res = shift;
  420. if (! $res) {
  421. return;
  422. }
  423. if (ref($res) eq "XML::DOM::NodeList") {
  424. $res = $res->item(0);
  425. }
  426. return $res->getFirstChild->getNodeValue;
  427. }
  428. sub get_object_by_spec
  429. {
  430. my $res = shift;
  431. my $spec = shift;
  432. if (! $res) {
  433. return;
  434. }
  435. if (! $spec) {
  436. return $res;
  437. }
  438. if (! ref($spec)) {
  439. $spec = [ $spec ];
  440. }
  441. my $iter = $res;
  442. for (my $i = 0; $i < scalar(@$spec) - 1; ++$i) {
  443. my $tmp = $iter->getElementsByTagName($spec->[$i]);
  444. if ((! $tmp) || (! $tmp->item(0))) {
  445. return;
  446. }
  447. $iter = $tmp->item(0);
  448. }
  449. if (wantarray) {
  450. my @ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  451. return @ret;
  452. }
  453. else {
  454. my $ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  455. if ((! $ret) || (! $ret->item(0))) {
  456. return;
  457. }
  458. return $ret->item(0);
  459. }
  460. }
  461. sub get_object_value_by_spec
  462. {
  463. my $res = get_object_by_spec(@_);
  464. return get_object_value($res);
  465. }
  466. sub get_iface_name
  467. {
  468. my $iface = shift;
  469. return get_obj_element($iface, 'name');
  470. }
  471. sub get_iface_description
  472. {
  473. my $iface = shift;
  474. return get_obj_element($iface, 'description');
  475. }
  476. sub get_iface_status
  477. {
  478. my $iface = shift;
  479. return get_obj_element($iface, 'oper-status');
  480. }
  481. sub get_iface_admin_status
  482. {
  483. my $iface = shift;
  484. return get_obj_element($iface, 'admin-status');
  485. }
  486. sub get_iface_traffic
  487. {
  488. my $iface = shift;
  489. my $type = shift;
  490. my $stats = get_obj_element($iface, 'traffic-statistics');
  491. return get_obj_element($iface, "$type-bytes");
  492. }
  493. sub get_iface_first_logical
  494. {
  495. my $iface = shift;
  496. return $iface->getElementsByTagName('logical-interface')->item(0);
  497. }
  498. sub get_liface_marker
  499. {
  500. my $liface = shift;
  501. my $lag_stats = $liface->getElementsByTagName('lag-traffic-statistics')->item(0);
  502. if (! $lag_stats) {
  503. print STDERR "Cannot get marker for non-LACP interfaces yet!\n";
  504. return;
  505. }
  506. my @markers = $lag_stats->getElementsByTagName('lag-marker');
  507. return @markers;
  508. }