JUNOS.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #############################################################################
  2. # (c) 2011-2012 Sebastian "tokkee" Harl <sh@teamix.net> #
  3. # and team(ix) GmbH, Nuernberg, Germany #
  4. # #
  5. # This file is part of "team(ix) Monitoring Plugins" #
  6. # URL: http://oss.teamix.org/projects/monitoringplugins/ #
  7. # #
  8. # All rights reserved. #
  9. # Redistribution and use in source and binary forms, with or without #
  10. # modification, are permitted provided that the following conditions #
  11. # are met: #
  12. # 1. Redistributions of source code must retain the above copyright #
  13. # notice, this list of conditions and the following disclaimer. #
  14. # 2. Redistributions in binary form must reproduce the above copyright #
  15. # notice, this list of conditions and the following disclaimer in the #
  16. # documentation and/or other materials provided with the distribution. #
  17. # 3. The name of the copyright owner may not be used to endorse or #
  18. # promote products derived from this software without specific prior #
  19. # written permission. #
  20. # #
  21. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR #
  22. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #
  23. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #
  24. # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, #
  25. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #
  26. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #
  27. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) #
  28. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, #
  29. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING #
  30. # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE #
  31. # POSSIBILITY OF SUCH DAMAGE. #
  32. #############################################################################
  33. package Nagios::Plugin::JUNOS;
  34. use Carp;
  35. use Data::Dumper;
  36. use POSIX qw( :termios_h );
  37. use Nagios::Plugin;
  38. use JUNOS::Device;
  39. use Nagios::Plugin::Functions qw( %ERRORS %STATUS_TEXT @STATUS_CODES );
  40. # re-export Nagios::Plugin's (default) exports
  41. use Exporter;
  42. our @ISA = qw( Nagios::Plugin Exporter );
  43. our @EXPORT = (@STATUS_CODES);
  44. our @EXPORT_OK = qw( %ERRORS %STATUS_TEXT );
  45. sub new
  46. {
  47. my $class = shift;
  48. my %args = @_;
  49. my $self = Nagios::Plugin->new(%args);
  50. $self->{'conf'} = { verbose => 0 };
  51. $self->{'cl_args'} = [];
  52. $self->{'junos'} = undef;
  53. return bless($self, $class);
  54. }
  55. sub add_arg
  56. {
  57. my $self = shift;
  58. my $arg = shift;
  59. my $spec = $arg->{'spec'};
  60. my $help;
  61. push @{$self->{'cl_args'}}, $arg;
  62. if (defined $arg->{'usage'}) {
  63. $help = $arg->{'usage'};
  64. }
  65. else {
  66. $help = $arg->{'help'};
  67. }
  68. if (defined $arg->{'desc'}) {
  69. my @desc;
  70. if (ref($arg->{'desc'})) {
  71. @desc = @{$arg->{'desc'}};
  72. }
  73. else {
  74. @desc = ( $arg->{'desc'} );
  75. }
  76. foreach my $d (@desc) {
  77. $help .= "\n $d";
  78. }
  79. if (defined $arg->{'default'}) {
  80. $help .= " (default: $arg->{'default'})";
  81. }
  82. }
  83. elsif (defined $arg->{'default'}) {
  84. $help .= "\n (default: $arg->{'default'})";
  85. }
  86. $self->SUPER::add_arg(
  87. spec => $spec,
  88. help => $help,
  89. );
  90. }
  91. sub add_common_args
  92. {
  93. my $self = shift;
  94. my @args = (
  95. {
  96. spec => 'host|H=s',
  97. usage => '-H, --host=HOSTNAME',
  98. desc => 'Hostname/IP of Juniper box to connect to',
  99. default => 'localhost',
  100. },
  101. {
  102. spec => 'port|p=i',
  103. usage => '-p, --port=PORT',
  104. desc => 'Port to connect to',
  105. default => 22,
  106. },
  107. {
  108. spec => 'user|U=s',
  109. usage => '-U, --user=USERNAME',
  110. desc => 'Username to log into box as',
  111. default => 'root',
  112. },
  113. {
  114. spec => 'password|P=s',
  115. usage => '-P, --password=PASSWORD',
  116. desc => 'Password for login username',
  117. default => '<prompt>',
  118. },
  119. );
  120. foreach my $arg (@args) {
  121. $self->add_arg($arg);
  122. }
  123. }
  124. sub add_check_impl
  125. {
  126. my $self = shift;
  127. my $name = shift;
  128. my $sub = shift;
  129. if ((! $name) || (! $sub) || (ref($sub) ne "CODE")) {
  130. carp "Invalid check specification: $name -> $sub";
  131. return;
  132. }
  133. if (! defined($self->{'check_impls'})) {
  134. $self->{'check_impls'} = {};
  135. }
  136. $self->{'check_impls'}->{$name} = $sub;
  137. }
  138. sub get_check_impl
  139. {
  140. my $self = shift;
  141. my $name = shift;
  142. if (! defined($self->{'check_impls'}->{$name})) {
  143. return;
  144. }
  145. return $self->{'check_impls'}->{$name};
  146. }
  147. sub is_valid_check
  148. {
  149. my $self = shift;
  150. my $name = shift;
  151. if (defined $self->{'check_impls'}->{$name}) {
  152. return 1;
  153. }
  154. return;
  155. }
  156. sub set_default_check
  157. {
  158. my $self = shift;
  159. my $def = shift;
  160. if (! $self->is_valid_check($def)) {
  161. carp "set_default_check: Check '$def' does not exist";
  162. return;
  163. }
  164. $self->{'default_check'} = $def;
  165. }
  166. sub configure
  167. {
  168. my $self = shift;
  169. # Predefined arguments (by Nagios::Plugin)
  170. my @predefined_args = qw(
  171. usage
  172. help
  173. version
  174. extra-opts
  175. timeout
  176. verbose
  177. );
  178. $self->getopts;
  179. # Initialize this first, so it may be used right away.
  180. $self->{'conf'}->{'verbose'} = $self->opts->verbose;
  181. foreach my $arg (@{$self->{'cl_args'}}) {
  182. my @c = $self->_get_conf($arg);
  183. $self->{'conf'}->{$c[0]} = $c[1];
  184. }
  185. foreach my $arg (@predefined_args) {
  186. $self->{'conf'}->{$arg} = $self->opts->$arg;
  187. }
  188. }
  189. sub _get_conf
  190. {
  191. my $self = shift;
  192. my $arg = shift;
  193. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  194. my $value = $self->opts->$name || $arg->{'default'};
  195. if ($name eq 'password') {
  196. $self->verbose(3, "conf: password => "
  197. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  198. }
  199. else {
  200. $self->verbose(3, "conf: $name => $value");
  201. }
  202. return ($name => $value);
  203. }
  204. sub _add_single_check
  205. {
  206. my $self = shift;
  207. my @check = split(m/,/, shift);
  208. my %c = ();
  209. if (! $self->is_valid_check($check[0])) {
  210. return "ERROR: invalid check '$check[0]'";
  211. }
  212. $c{'name'} = $check[0];
  213. $c{'target'} = undef;
  214. if (defined($check[1])) {
  215. my @tmp = split(m/(\+|\~)/, $check[1]);
  216. $c{'target'} = [];
  217. $c{'exclude'} = [];
  218. for (my $i = 0; $i < scalar(@tmp); ++$i) {
  219. my $t = $tmp[$i];
  220. if ((($t ne "+") && ($t ne "~")) || ($i == $#tmp)) {
  221. push @{$c{'target'}}, $t;
  222. next;
  223. }
  224. ++$i;
  225. if ($t eq "+") {
  226. push @{$c{'target'}}, $tmp[$i];
  227. }
  228. else {
  229. push @{$c{'exclude'}}, $tmp[$i];
  230. }
  231. }
  232. }
  233. $c{'warning'} = $check[2];
  234. $c{'critical'} = $check[3];
  235. # check for valid thresholds
  236. # set_threshold() will die if any threshold is not valid
  237. $self->set_thresholds(
  238. warning => $c{'warning'},
  239. critical => $c{'critical'},
  240. ) || $self->die("ERROR: Invalid thresholds: "
  241. . "warning => $c{'warning'}, critical => $c{'critical'}");
  242. push @{$self->{'conf'}->{'checks'}}, \%c;
  243. }
  244. sub set_checks
  245. {
  246. my $self = shift;
  247. my @checks = @_;
  248. my $err_str = "ERROR:";
  249. if (! defined($self->{'conf'}->{'timeout'})) {
  250. croak "No timeout set -- did you call configure()?";
  251. }
  252. if (scalar(@checks) == 0) {
  253. if ($self->{'default_check'}) {
  254. $self->{'conf'}->{'checks'}->[0] = {
  255. name => $self->{'default_check'},
  256. target => [],
  257. exclude => [],
  258. warning => undef,
  259. critical => undef,
  260. };
  261. }
  262. return 1;
  263. }
  264. $self->{'conf'}->{'checks'} = [];
  265. foreach my $check (@checks) {
  266. my $e;
  267. $e = $self->_add_single_check($check);
  268. if ($e =~ m/^ERROR: (.*)$/) {
  269. $err_str .= " $1,";
  270. }
  271. }
  272. if ($err_str ne "ERROR:") {
  273. $err_str =~ s/,$//;
  274. $self->die($err_str);
  275. }
  276. }
  277. sub get_checks
  278. {
  279. my $self = shift;
  280. return @{$self->{'conf'}->{'checks'}};
  281. }
  282. sub connect
  283. {
  284. my $self = shift;
  285. my $host = $self->{'conf'}->{'host'};
  286. my $user = $self->{'conf'}->{'user'};
  287. if ((! $host) || (! $user)) {
  288. croak "Host and/or user not set -- did you call configure()?";
  289. }
  290. if (! $self->opts->password) {
  291. my $term = POSIX::Termios->new();
  292. my $lflag;
  293. print "Password: ";
  294. $term->getattr(fileno(STDIN));
  295. $lflag = $term->getlflag;
  296. $term->setlflag($lflag & ~POSIX::ECHO);
  297. $term->setattr(fileno(STDIN), TCSANOW);
  298. $self->{'conf'}->{'password'} = <STDIN>;
  299. chomp($self->{'conf'}->{'password'});
  300. $term->setlflag($lflag | POSIX::ECHO);
  301. $term->setattr(fileno(STDIN), TCSAFLUSH);
  302. print "\n";
  303. }
  304. $self->verbose(1, "Connecting to host $host as user $user.");
  305. $junos = JUNOS::Device->new(
  306. hostname => $host,
  307. login => $user,
  308. password => $self->{'conf'}->{'password'},
  309. access => 'ssh',
  310. 'ssh-compress' => 0);
  311. if (! ref $junos) {
  312. $self->die("ERROR: failed to connect to $host!");
  313. }
  314. $self->{'junos'} = $junos;
  315. return $junos;
  316. }
  317. sub run_checks
  318. {
  319. my $self = shift;
  320. foreach my $check ($self->get_checks()) {
  321. my $targets = [];
  322. my $exclude = [];
  323. if (defined $check->{'target'}) {
  324. $targets = $check->{'target'};
  325. }
  326. if (defined $check->{'exclude'}) {
  327. $exclude = $check->{'exclude'};
  328. }
  329. $self->set_thresholds(
  330. warning => $check->{'warning'},
  331. critical => $check->{'critical'},
  332. );
  333. my $sub = $self->get_check_impl($check->{'name'});
  334. $sub->($self, $self->{'junos'}, $targets, $exclude);
  335. }
  336. }
  337. sub send_query
  338. {
  339. my $self = shift;
  340. my $query = shift;
  341. my $queryargs = shift || {};
  342. my $res;
  343. my $err;
  344. $self->verbose(3, "Sending query '$query' "
  345. . join(", ", map { "$_ => $queryargs->{$_}" } keys %$queryargs)
  346. . " to router.");
  347. if (scalar(keys %$queryargs)) {
  348. $res = $self->{'junos'}->$query(%$queryargs);
  349. } else {
  350. eval {
  351. $res = $self->{'junos'}->$query();
  352. };
  353. if ($@) {
  354. $res = $self->{'junos'}->command($query);
  355. }
  356. }
  357. $self->verbose(5, "Got response: " . Dumper(\$res));
  358. if (! ref $res) {
  359. return "ERROR: Failed to execute query '$query'";
  360. }
  361. $err = $res->getFirstError();
  362. if ($err) {
  363. return "ERROR: " . $err->{message};
  364. }
  365. return $res;
  366. }
  367. sub get_query_object
  368. {
  369. my $self = shift;
  370. my $res = shift;
  371. my $spec = shift;
  372. if (! $res) {
  373. return;
  374. }
  375. if (! $spec) {
  376. return $res;
  377. }
  378. if (! ref($spec)) {
  379. $spec = [ $spec ];
  380. }
  381. my $iter = $res;
  382. for (my $i = 0; $i < scalar(@$spec) - 1; ++$i) {
  383. my $tmp = $iter->getElementsByTagName($spec->[$i]);
  384. if ((! $tmp) || (! $tmp->item(0))) {
  385. return;
  386. }
  387. $iter = $tmp->item(0);
  388. }
  389. if (wantarray) {
  390. my @ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  391. return @ret;
  392. }
  393. else {
  394. my $ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  395. if ((! $ret) || (! $ret->item(0))) {
  396. return;
  397. }
  398. return $ret->item(0);
  399. }
  400. }
  401. sub get_query_object_value
  402. {
  403. my $self = shift;
  404. my $res = $self->get_query_object(@_);
  405. if (! $res) {
  406. return;
  407. }
  408. if (ref($res) eq "XML::DOM::NodeList") {
  409. $res = $res->item(0);
  410. }
  411. return $res->getFirstChild->getNodeValue;
  412. }
  413. sub nagios_exit
  414. {
  415. my $self = shift;
  416. if ($self->{'junos'}) {
  417. $self->{'junos'}->disconnect();
  418. }
  419. $self->SUPER::nagios_exit(@_);
  420. }
  421. sub verbose
  422. {
  423. my $self = shift;
  424. my $level = shift;
  425. my @msgs = @_;
  426. if ($level > $self->{'conf'}->{'verbose'}) {
  427. return;
  428. }
  429. foreach my $msg (@msgs) {
  430. print "V$level: $msg\n";
  431. }
  432. }
  433. return 1;