check_junos.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 @interfaces = get_interfaces($junos, @targets);;
  164. my $down_count = 0;
  165. my @down_ifaces = ();
  166. foreach my $iface (@interfaces) {
  167. my $name = get_iface_name($iface);
  168. my $status = check_interface($iface, @targets);
  169. if ($status == 0) {
  170. ++$down_count;
  171. push @down_ifaces, $name;
  172. }
  173. if ($status <= 0) {
  174. # disabled or down
  175. next;
  176. }
  177. if ($name !~ m/^ae/) {
  178. next;
  179. }
  180. # XXX: check physical interfaces of the aggregate
  181. }
  182. if ($down_count > 0) {
  183. $plugin->add_message(CRITICAL, $down_count
  184. . " interfaces down (" . join(", ", @down_ifaces) . ")");
  185. }
  186. }
  187. elsif ($check->{'name'} eq 'chassis_environment') {
  188. # XXX
  189. #show chassis environment (see check_snmp_environment)
  190. }
  191. }
  192. my ($code, $msg) = $plugin->check_messages(join => ', ');
  193. $junos->disconnect();
  194. $plugin->nagios_exit($code, $msg);
  195. sub send_query
  196. {
  197. my $device = shift;
  198. my $query = shift;
  199. my $queryargs = shift;
  200. my $res;
  201. my $err;
  202. verbose(3, "Sending query '$query' to router.");
  203. if (ref $queryargs) {
  204. $res = $device->$query(%$queryargs);
  205. } else {
  206. $res = $device->$query();
  207. }
  208. if (! ref $res) {
  209. return "ERROR: Failed to execute query '$query'";
  210. }
  211. $err = $res->getFirstError();
  212. if ($err) {
  213. return "ERROR: " . $err->{message};
  214. }
  215. return $res;
  216. }
  217. sub check_interface {
  218. my $iface = shift;
  219. my @targets = @_;
  220. my $name = get_iface_name($iface);
  221. my $admin_status = get_iface_admin_status($iface);
  222. if ($admin_status !~ m/^up$/) {
  223. if (grep { $name =~ m/^$_$/; } @targets) {
  224. $plugin->add_message(CRITICAL,
  225. "$name is not enabled");
  226. return -1;
  227. }
  228. return 1;
  229. }
  230. if (get_iface_status($iface) !~ m/^up$/i) {
  231. return 0;
  232. }
  233. $plugin->add_perfdata(
  234. label => "$name-input-bytes",
  235. value => get_iface_traffic($iface, "input"),
  236. min => 0,
  237. max => undef,
  238. uom => 'B',
  239. threshold => undef,
  240. );
  241. $plugin->add_perfdata(
  242. label => "$name-output-bytes",
  243. value => get_iface_traffic($iface, "output"),
  244. min => 0,
  245. max => undef,
  246. uom => 'B',
  247. threshold => undef,
  248. );
  249. return 1;
  250. }
  251. sub get_interfaces
  252. {
  253. my $device = shift;
  254. my @targets = @_;
  255. my @ifaces = ();
  256. my $cmd = 'get_interface_information';
  257. my $res;
  258. my $args = { detail => 1 };
  259. if (scalar(@targets) == 1) {
  260. $args->{'interface_name'} = $targets[0];
  261. }
  262. $res = send_query($device, $cmd, $args);
  263. if (! ref $res) {
  264. $plugin->die($res);
  265. }
  266. @ifaces = $res->getElementsByTagName('physical-interface');
  267. @targets = map { s/\*/\.\*/g; s/\?/\./g; $_; } @targets;
  268. if (scalar(@targets)) {
  269. @ifaces = grep {
  270. my $i = $_;
  271. grep { get_iface_name($i) =~ m/^$_$/ } @targets;
  272. } @ifaces;
  273. }
  274. if ($conf{'verbose'} >= 3) {
  275. my @i = map { get_iface_name($_) . " => " . get_iface_status($_) }
  276. @ifaces;
  277. verbose(3, "Interfaces: " . join(", ", @i));
  278. }
  279. return @ifaces;
  280. }
  281. sub get_obj_element
  282. {
  283. my $obj = shift;
  284. my $elem = shift;
  285. $elem = $obj->getElementsByTagName($elem);
  286. return $elem->item(0)->getFirstChild->getNodeValue;
  287. }
  288. sub get_iface_name
  289. {
  290. my $iface = shift;
  291. return get_obj_element($iface, 'name');
  292. }
  293. sub get_iface_status
  294. {
  295. my $iface = shift;
  296. return get_obj_element($iface, 'oper-status');
  297. }
  298. sub get_iface_admin_status
  299. {
  300. my $iface = shift;
  301. return get_obj_element($iface, 'admin-status');
  302. }
  303. sub get_iface_traffic
  304. {
  305. my $iface = shift;
  306. my $type = shift;
  307. my $stats = get_obj_element($iface, 'traffic-statistics');
  308. return get_obj_element($iface, "$type-bytes");
  309. }
  310. sub add_arg
  311. {
  312. my $plugin = shift;
  313. my $arg = shift;
  314. my $spec = $arg->{'spec'};
  315. my $help = $arg->{'usage'};
  316. if (defined $arg->{'desc'}) {
  317. my @desc;
  318. if (ref($arg->{'desc'})) {
  319. @desc = @{$arg->{'desc'}};
  320. }
  321. else {
  322. @desc = ( $arg->{'desc'} );
  323. }
  324. foreach my $d (@desc) {
  325. $help .= "\n $d";
  326. }
  327. if (defined $arg->{'default'}) {
  328. $help .= " (default: $arg->{'default'})";
  329. }
  330. }
  331. elsif (defined $arg->{'default'}) {
  332. $help .= "\n (default: $arg->{'default'})";
  333. }
  334. $plugin->add_arg(
  335. spec => $spec,
  336. help => $help,
  337. );
  338. }
  339. sub get_conf
  340. {
  341. my $plugin = shift;
  342. my $arg = shift;
  343. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  344. my $value = $plugin->opts->$name || $arg->{'default'};
  345. if ($name eq 'password') {
  346. verbose(3, "conf: password => "
  347. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  348. }
  349. else {
  350. verbose(3, "conf: $name => $value");
  351. }
  352. return ($name => $value);
  353. }
  354. sub add_single_check
  355. {
  356. my $conf = shift;
  357. my @check = split(m/,/, shift);
  358. my %c = ();
  359. if ($check[0] !~ m/\b(?:$valid_checks)\b/) {
  360. return "ERROR: invalid check '$check[0]'";
  361. }
  362. $c{'name'} = $check[0];
  363. $c{'target'} = undef;
  364. if (defined($check[1])) {
  365. $c{'target'} = [ split(m/\+/, $check[1]) ];
  366. }
  367. $c{'warning'} = $check[2];
  368. $c{'critical'} = $check[3];
  369. # check for valid thresholds
  370. # set_threshold() will die if any threshold is not valid
  371. $plugin->set_thresholds(
  372. warning => $c{'warning'},
  373. critical => $c{'critical'},
  374. ) || $plugin->die("ERROR: Invalid thresholds: "
  375. . "warning => $c{'warning'}, critical => $c{'critical'}");
  376. push @{$conf->{'checks'}}, \%c;
  377. }
  378. sub add_checks
  379. {
  380. my $conf = shift;
  381. my @checks = @_;
  382. my $err_str = "ERROR:";
  383. if (scalar(@checks) == 0) {
  384. $conf->{'checks'}[0] = {
  385. name => 'chassis_environment',
  386. target => [],
  387. warning => undef,
  388. critical => undef,
  389. };
  390. return 1;
  391. }
  392. $conf->{'checks'} = [];
  393. foreach my $check (@checks) {
  394. my $e;
  395. $e = add_single_check($conf, $check);
  396. if ($e =~ m/^ERROR: (.*)$/) {
  397. $err_str .= " $1,";
  398. }
  399. }
  400. if ($err_str ne "ERROR:") {
  401. $err_str =~ s/,$//;
  402. $plugin->die($err_str);
  403. }
  404. }
  405. sub verbose
  406. {
  407. my $level = shift;
  408. my @msgs = @_;
  409. if ($level > $conf{'verbose'}) {
  410. return;
  411. }
  412. foreach my $msg (@msgs) {
  413. print "V$level: $msg\n";
  414. }
  415. }