JUNOS.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 POSIX qw( :termios_h );
  36. use Nagios::Plugin;
  37. use JUNOS::Device;
  38. use Nagios::Plugin::Functions qw( %ERRORS %STATUS_TEXT @STATUS_CODES );
  39. # re-export Nagios::Plugin's (default) exports
  40. use Exporter;
  41. our @ISA = qw( Nagios::Plugin Exporter );
  42. our @EXPORT = (@STATUS_CODES);
  43. our @EXPORT_OK = qw( %ERRORS %STATUS_TEXT );
  44. sub new
  45. {
  46. my $class = shift;
  47. my %args = @_;
  48. my $self = Nagios::Plugin->new(%args);
  49. $self->{'conf'} = { verbose => 0 };
  50. $self->{'cl_args'} = [];
  51. $self->{'junos'} = undef;
  52. return bless($self, $class);
  53. }
  54. sub add_arg
  55. {
  56. my $self = shift;
  57. my $arg = shift;
  58. my $spec = $arg->{'spec'};
  59. my $help;
  60. push @{$self->{'cl_args'}}, $arg;
  61. if (defined $arg->{'usage'}) {
  62. $help = $arg->{'usage'};
  63. }
  64. else {
  65. $help = $arg->{'help'};
  66. }
  67. if (defined $arg->{'desc'}) {
  68. my @desc;
  69. if (ref($arg->{'desc'})) {
  70. @desc = @{$arg->{'desc'}};
  71. }
  72. else {
  73. @desc = ( $arg->{'desc'} );
  74. }
  75. foreach my $d (@desc) {
  76. $help .= "\n $d";
  77. }
  78. if (defined $arg->{'default'}) {
  79. $help .= " (default: $arg->{'default'})";
  80. }
  81. }
  82. elsif (defined $arg->{'default'}) {
  83. $help .= "\n (default: $arg->{'default'})";
  84. }
  85. $self->SUPER::add_arg(
  86. spec => $spec,
  87. help => $help,
  88. );
  89. }
  90. sub add_common_args
  91. {
  92. my $self = shift;
  93. my @args = (
  94. {
  95. spec => 'host|H=s',
  96. usage => '-H, --host=HOSTNAME',
  97. desc => 'Hostname/IP of Juniper box to connect to',
  98. default => 'localhost',
  99. },
  100. {
  101. spec => 'port|p=i',
  102. usage => '-p, --port=PORT',
  103. desc => 'Port to connect to',
  104. default => 22,
  105. },
  106. {
  107. spec => 'user|U=s',
  108. usage => '-U, --user=USERNAME',
  109. desc => 'Username to log into box as',
  110. default => 'root',
  111. },
  112. {
  113. spec => 'password|P=s',
  114. usage => '-P, --password=PASSWORD',
  115. desc => 'Password for login username',
  116. default => '<prompt>',
  117. },
  118. );
  119. foreach my $arg (@args) {
  120. $self->add_arg($arg);
  121. }
  122. }
  123. sub add_check_impl
  124. {
  125. my $self = shift;
  126. my $name = shift;
  127. my $sub = shift;
  128. if ((! $name) || (! $sub) || (ref($sub) ne "CODE")) {
  129. carp "Invalid check specification: $name -> $sub";
  130. return;
  131. }
  132. if (! defined($self->{'check_impls'})) {
  133. $self->{'check_impls'} = {};
  134. }
  135. $self->{'check_impls'}->{$name} = $sub;
  136. }
  137. sub get_check_impl
  138. {
  139. my $self = shift;
  140. my $name = shift;
  141. if (! defined($self->{'check_impls'}->{$name})) {
  142. return;
  143. }
  144. return $self->{'check_impls'}->{$name};
  145. }
  146. sub is_valid_check
  147. {
  148. my $self = shift;
  149. my $name = shift;
  150. if (defined $self->{'check_impls'}->{$name}) {
  151. return 1;
  152. }
  153. return;
  154. }
  155. sub set_default_check
  156. {
  157. my $self = shift;
  158. my $def = shift;
  159. if (! $self->is_valid_check($def)) {
  160. carp "set_default_check: Check '$def' does not exist";
  161. return;
  162. }
  163. $self->{'default_check'} = $def;
  164. }
  165. sub configure
  166. {
  167. my $self = shift;
  168. # Predefined arguments (by Nagios::Plugin)
  169. my @predefined_args = qw(
  170. usage
  171. help
  172. version
  173. extra-opts
  174. timeout
  175. verbose
  176. );
  177. $self->getopts;
  178. # Initialize this first, so it may be used right away.
  179. $self->{'conf'}->{'verbose'} = $self->opts->verbose;
  180. foreach my $arg (@{$self->{'cl_args'}}) {
  181. my @c = $self->_get_conf($arg);
  182. $self->{'conf'}->{$c[0]} = $c[1];
  183. }
  184. foreach my $arg (@predefined_args) {
  185. $self->{'conf'}->{$arg} = $self->opts->$arg;
  186. }
  187. }
  188. sub _get_conf
  189. {
  190. my $self = shift;
  191. my $arg = shift;
  192. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  193. my $value = $self->opts->$name || $arg->{'default'};
  194. if ($name eq 'password') {
  195. $self->verbose(3, "conf: password => "
  196. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  197. }
  198. else {
  199. $self->verbose(3, "conf: $name => $value");
  200. }
  201. return ($name => $value);
  202. }
  203. sub _add_single_check
  204. {
  205. my $self = shift;
  206. my @check = split(m/,/, shift);
  207. my %c = ();
  208. if (! $self->is_valid_check($check[0])) {
  209. return "ERROR: invalid check '$check[0]'";
  210. }
  211. $c{'name'} = $check[0];
  212. $c{'target'} = undef;
  213. if (defined($check[1])) {
  214. my @tmp = split(m/(\+|\~)/, $check[1]);
  215. $c{'target'} = [];
  216. $c{'exclude'} = [];
  217. for (my $i = 0; $i < scalar(@tmp); ++$i) {
  218. my $t = $tmp[$i];
  219. if ((($t ne "+") && ($t ne "~")) || ($i == $#tmp)) {
  220. push @{$c{'target'}}, $t;
  221. next;
  222. }
  223. ++$i;
  224. if ($t eq "+") {
  225. push @{$c{'target'}}, $tmp[$i];
  226. }
  227. else {
  228. push @{$c{'exclude'}}, $tmp[$i];
  229. }
  230. }
  231. }
  232. $c{'warning'} = $check[2];
  233. $c{'critical'} = $check[3];
  234. # check for valid thresholds
  235. # set_threshold() will die if any threshold is not valid
  236. $self->set_thresholds(
  237. warning => $c{'warning'},
  238. critical => $c{'critical'},
  239. ) || $self->die("ERROR: Invalid thresholds: "
  240. . "warning => $c{'warning'}, critical => $c{'critical'}");
  241. push @{$self->{'conf'}->{'checks'}}, \%c;
  242. }
  243. sub set_checks
  244. {
  245. my $self = shift;
  246. my @checks = @_;
  247. my $err_str = "ERROR:";
  248. if (! defined($self->{'conf'}->{'timeout'})) {
  249. croak "No timeout set -- did you call configure()?";
  250. }
  251. if (scalar(@checks) == 0) {
  252. if ($self->{'default_check'}) {
  253. $self->{'conf'}->{'checks'}->[0] = {
  254. name => $self->{'default_check'},
  255. target => [],
  256. exclude => [],
  257. warning => undef,
  258. critical => undef,
  259. };
  260. }
  261. return 1;
  262. }
  263. $self->{'conf'}->{'checks'} = [];
  264. foreach my $check (@checks) {
  265. my $e;
  266. $e = $self->_add_single_check($check);
  267. if ($e =~ m/^ERROR: (.*)$/) {
  268. $err_str .= " $1,";
  269. }
  270. }
  271. if ($err_str ne "ERROR:") {
  272. $err_str =~ s/,$//;
  273. $self->die($err_str);
  274. }
  275. }
  276. sub get_checks
  277. {
  278. my $self = shift;
  279. return @{$self->{'conf'}->{'checks'}};
  280. }
  281. sub connect
  282. {
  283. my $self = shift;
  284. my $host = $self->{'conf'}->{'host'};
  285. my $user = $self->{'conf'}->{'user'};
  286. if ((! $host) || (! $user)) {
  287. croak "Host and/or user not set -- did you call configure()?";
  288. }
  289. if (! $self->opts->password) {
  290. my $term = POSIX::Termios->new();
  291. my $lflag;
  292. print "Password: ";
  293. $term->getattr(fileno(STDIN));
  294. $lflag = $term->getlflag;
  295. $term->setlflag($lflag & ~POSIX::ECHO);
  296. $term->setattr(fileno(STDIN), TCSANOW);
  297. $self->{'conf'}->{'password'} = <STDIN>;
  298. chomp($self->{'conf'}->{'password'});
  299. $term->setlflag($lflag | POSIX::ECHO);
  300. $term->setattr(fileno(STDIN), TCSAFLUSH);
  301. print "\n";
  302. }
  303. $self->verbose(1, "Connecting to host $host as user $user.");
  304. $junos = JUNOS::Device->new(
  305. hostname => $host,
  306. login => $user,
  307. password => $self->{'conf'}->{'password'},
  308. access => 'ssh',
  309. 'ssh-compress' => 0);
  310. if (! ref $junos) {
  311. $self->die("ERROR: failed to connect to $host!");
  312. }
  313. $self->{'junos'} = $junos;
  314. return $junos;
  315. }
  316. sub run_checks
  317. {
  318. my $self = shift;
  319. foreach my $check ($self->get_checks()) {
  320. my $targets = [];
  321. my $exclude = [];
  322. if (defined $check->{'target'}) {
  323. $targets = $check->{'target'};
  324. }
  325. if (defined $check->{'exclude'}) {
  326. $exclude = $check->{'exclude'};
  327. }
  328. $self->set_thresholds(
  329. warning => $check->{'warning'},
  330. critical => $check->{'critical'},
  331. );
  332. my $sub = $self->get_check_impl($check->{'name'});
  333. $sub->($self, $self->{'junos'}, $targets, $exclude);
  334. }
  335. }
  336. sub send_query
  337. {
  338. my $self = shift;
  339. my $query = shift;
  340. my $queryargs = shift || {};
  341. my $res;
  342. my $err;
  343. $self->verbose(3, "Sending query '$query' "
  344. . join(", ", map { "$_ => $queryargs->{$_}" } keys %$queryargs)
  345. . " to router.");
  346. if (scalar(keys %$queryargs)) {
  347. $res = $self->{'junos'}->$query(%$queryargs);
  348. } else {
  349. eval {
  350. $res = $self->{'junos'}->$query();
  351. };
  352. if ($@) {
  353. $res = $self->{'junos'}->command($query);
  354. }
  355. }
  356. if (! ref $res) {
  357. return "ERROR: Failed to execute query '$query'";
  358. }
  359. $err = $res->getFirstError();
  360. if ($err) {
  361. return "ERROR: " . $err->{message};
  362. }
  363. return $res;
  364. }
  365. sub get_query_object
  366. {
  367. my $self = shift;
  368. my $res = shift;
  369. my $spec = shift;
  370. if (! $res) {
  371. return;
  372. }
  373. if (! $spec) {
  374. return $res;
  375. }
  376. if (! ref($spec)) {
  377. $spec = [ $spec ];
  378. }
  379. my $iter = $res;
  380. for (my $i = 0; $i < scalar(@$spec) - 1; ++$i) {
  381. my $tmp = $iter->getElementsByTagName($spec->[$i]);
  382. if ((! $tmp) || (! $tmp->item(0))) {
  383. return;
  384. }
  385. $iter = $tmp->item(0);
  386. }
  387. if (wantarray) {
  388. my @ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  389. return @ret;
  390. }
  391. else {
  392. my $ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  393. if ((! $ret) || (! $ret->item(0))) {
  394. return;
  395. }
  396. return $ret->item(0);
  397. }
  398. }
  399. sub get_query_object_value
  400. {
  401. my $self = shift;
  402. my $res = $self->get_query_object(@_);
  403. if (! $res) {
  404. return;
  405. }
  406. if (ref($res) eq "XML::DOM::NodeList") {
  407. $res = $res->item(0);
  408. }
  409. return $res->getFirstChild->getNodeValue;
  410. }
  411. sub nagios_exit
  412. {
  413. my $self = shift;
  414. if ($self->{'junos'}) {
  415. $self->{'junos'}->disconnect();
  416. }
  417. $self->SUPER::nagios_exit(@_);
  418. }
  419. sub verbose
  420. {
  421. my $self = shift;
  422. my $level = shift;
  423. my @msgs = @_;
  424. if ($level > $self->{'conf'}->{'verbose'}) {
  425. return;
  426. }
  427. foreach my $msg (@msgs) {
  428. print "V$level: $msg\n";
  429. }
  430. }
  431. return 1;