check_junos.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. }
  222. else {
  223. $plugin->add_message(OK, "interface"
  224. . (scalar(@targets) == 1 ? " " : "s ")
  225. . join(", ", @targets) . " up"
  226. . ($have_lag_ifaces
  227. ? " (including all LAG member interfaces)" : ""));
  228. }
  229. }
  230. }
  231. elsif ($check->{'name'} eq 'chassis_environment') {
  232. # XXX
  233. #show chassis environment (see check_snmp_environment)
  234. }
  235. }
  236. my ($code, $msg) = $plugin->check_messages(join => ', ');
  237. $junos->disconnect();
  238. $plugin->nagios_exit($code, $msg);
  239. sub send_query
  240. {
  241. my $device = shift;
  242. my $query = shift;
  243. my $queryargs = shift;
  244. my $res;
  245. my $err;
  246. verbose(3, "Sending query '$query' "
  247. . join(", ", map { "$_ => $queryargs->{$_}" } keys %$queryargs)
  248. . " to router.");
  249. if (ref $queryargs) {
  250. $res = $device->$query(%$queryargs);
  251. } else {
  252. $res = $device->$query();
  253. }
  254. if (! ref $res) {
  255. return "ERROR: Failed to execute query '$query'";
  256. }
  257. $err = $res->getFirstError();
  258. if ($err) {
  259. return "ERROR: " . $err->{message};
  260. }
  261. return $res;
  262. }
  263. sub check_interface {
  264. my $iface = shift;
  265. my $opts = shift || {};
  266. my @targets = @_;
  267. my $name = get_iface_name($iface);
  268. my $admin_status = get_iface_admin_status($iface);
  269. if ($admin_status !~ m/^up$/) {
  270. if ((grep { $name =~ m/^$_$/; } @targets)
  271. || ($opts->{'with_description'} &&
  272. get_iface_description($iface))) {
  273. $plugin->add_message(CRITICAL,
  274. "$name is not enabled");
  275. return -1;
  276. }
  277. return 1;
  278. }
  279. if (get_iface_status($iface) !~ m/^up$/i) {
  280. return 0;
  281. }
  282. $plugin->add_perfdata(
  283. label => "'$name-input-bytes'",
  284. value => get_iface_traffic($iface, "input"),
  285. min => 0,
  286. max => undef,
  287. uom => 'B',
  288. threshold => undef,
  289. );
  290. $plugin->add_perfdata(
  291. label => "'$name-output-bytes'",
  292. value => get_iface_traffic($iface, "output"),
  293. min => 0,
  294. max => undef,
  295. uom => 'B',
  296. threshold => undef,
  297. );
  298. return 1;
  299. }
  300. sub get_interfaces
  301. {
  302. my $device = shift;
  303. my $opts = shift || {};
  304. my @targets = @_;
  305. my @ifaces = ();
  306. my @ret = ();
  307. my $cmd = 'get_interface_information';
  308. my $res;
  309. my $args = { detail => 1 };
  310. if ((scalar(@targets) == 1) && (! $opts->{'with_description'})) {
  311. $args->{'interface_name'} = $targets[0];
  312. }
  313. $res = send_query($device, $cmd, $args);
  314. if (! ref $res) {
  315. $plugin->die($res);
  316. }
  317. @ifaces = $res->getElementsByTagName('physical-interface');
  318. @targets = map { s/\*/\.\*/g; s/\?/\./g; $_; } @targets;
  319. if (scalar(@targets)) {
  320. @ret = grep {
  321. my $i = $_;
  322. grep { get_iface_name($i) =~ m/^$_$/ } @targets;
  323. } @ifaces;
  324. }
  325. elsif (! $opts->{'with_description'}) {
  326. @ret = @ifaces;
  327. }
  328. if ($opts->{'with_description'}) {
  329. foreach my $iface (@ifaces) {
  330. my $name = get_iface_name($iface);
  331. if (get_iface_description($iface)
  332. && (! grep { m/^$name$/; } @targets)) {
  333. push @ret, $iface;
  334. }
  335. }
  336. }
  337. if ($conf{'verbose'} >= 3) {
  338. my @i = map { get_iface_name($_) . " => " . get_iface_status($_) }
  339. @ret;
  340. verbose(3, "Interfaces: " . join(", ", @i));
  341. }
  342. return @ret;
  343. }
  344. sub get_obj_element
  345. {
  346. my $obj = shift;
  347. my $elem = shift;
  348. $elem = $obj->getElementsByTagName($elem);
  349. if ((! $elem) || (! $elem->item(0))) {
  350. return;
  351. }
  352. return $elem->item(0)->getFirstChild->getNodeValue;
  353. }
  354. sub get_iface_name
  355. {
  356. my $iface = shift;
  357. return get_obj_element($iface, 'name');
  358. }
  359. sub get_iface_description
  360. {
  361. my $iface = shift;
  362. return get_obj_element($iface, 'description');
  363. }
  364. sub get_iface_status
  365. {
  366. my $iface = shift;
  367. return get_obj_element($iface, 'oper-status');
  368. }
  369. sub get_iface_admin_status
  370. {
  371. my $iface = shift;
  372. return get_obj_element($iface, 'admin-status');
  373. }
  374. sub get_iface_traffic
  375. {
  376. my $iface = shift;
  377. my $type = shift;
  378. my $stats = get_obj_element($iface, 'traffic-statistics');
  379. return get_obj_element($iface, "$type-bytes");
  380. }
  381. sub get_iface_first_logical
  382. {
  383. my $iface = shift;
  384. return $iface->getElementsByTagName('logical-interface')->item(0);
  385. }
  386. sub get_liface_marker
  387. {
  388. my $liface = shift;
  389. my $lag_stats = $liface->getElementsByTagName('lag-traffic-statistics')->item(0);
  390. if (! $lag_stats) {
  391. print STDERR "Cannot get marker for non-LACP interfaces yet!\n";
  392. return;
  393. }
  394. my @markers = $lag_stats->getElementsByTagName('lag-marker');
  395. return @markers;
  396. }
  397. sub add_arg
  398. {
  399. my $plugin = shift;
  400. my $arg = shift;
  401. my $spec = $arg->{'spec'};
  402. my $help = $arg->{'usage'};
  403. if (defined $arg->{'desc'}) {
  404. my @desc;
  405. if (ref($arg->{'desc'})) {
  406. @desc = @{$arg->{'desc'}};
  407. }
  408. else {
  409. @desc = ( $arg->{'desc'} );
  410. }
  411. foreach my $d (@desc) {
  412. $help .= "\n $d";
  413. }
  414. if (defined $arg->{'default'}) {
  415. $help .= " (default: $arg->{'default'})";
  416. }
  417. }
  418. elsif (defined $arg->{'default'}) {
  419. $help .= "\n (default: $arg->{'default'})";
  420. }
  421. $plugin->add_arg(
  422. spec => $spec,
  423. help => $help,
  424. );
  425. }
  426. sub get_conf
  427. {
  428. my $plugin = shift;
  429. my $arg = shift;
  430. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  431. my $value = $plugin->opts->$name || $arg->{'default'};
  432. if ($name eq 'password') {
  433. verbose(3, "conf: password => "
  434. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  435. }
  436. else {
  437. verbose(3, "conf: $name => $value");
  438. }
  439. return ($name => $value);
  440. }
  441. sub add_single_check
  442. {
  443. my $conf = shift;
  444. my @check = split(m/,/, shift);
  445. my %c = ();
  446. if ($check[0] !~ m/\b(?:$valid_checks)\b/) {
  447. return "ERROR: invalid check '$check[0]'";
  448. }
  449. $c{'name'} = $check[0];
  450. $c{'target'} = undef;
  451. if (defined($check[1])) {
  452. $c{'target'} = [ split(m/\+/, $check[1]) ];
  453. }
  454. $c{'warning'} = $check[2];
  455. $c{'critical'} = $check[3];
  456. # check for valid thresholds
  457. # set_threshold() will die if any threshold is not valid
  458. $plugin->set_thresholds(
  459. warning => $c{'warning'},
  460. critical => $c{'critical'},
  461. ) || $plugin->die("ERROR: Invalid thresholds: "
  462. . "warning => $c{'warning'}, critical => $c{'critical'}");
  463. push @{$conf->{'checks'}}, \%c;
  464. }
  465. sub add_checks
  466. {
  467. my $conf = shift;
  468. my @checks = @_;
  469. my $err_str = "ERROR:";
  470. if (scalar(@checks) == 0) {
  471. $conf->{'checks'}[0] = {
  472. name => 'chassis_environment',
  473. target => [],
  474. warning => undef,
  475. critical => undef,
  476. };
  477. return 1;
  478. }
  479. $conf->{'checks'} = [];
  480. foreach my $check (@checks) {
  481. my $e;
  482. $e = add_single_check($conf, $check);
  483. if ($e =~ m/^ERROR: (.*)$/) {
  484. $err_str .= " $1,";
  485. }
  486. }
  487. if ($err_str ne "ERROR:") {
  488. $err_str =~ s/,$//;
  489. $plugin->die($err_str);
  490. }
  491. }
  492. sub verbose
  493. {
  494. my $level = shift;
  495. my @msgs = @_;
  496. if ($level > $conf{'verbose'}) {
  497. return;
  498. }
  499. foreach my $msg (@msgs) {
  500. print "V$level: $msg\n";
  501. }
  502. }