JUNOS.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. $c{'target'} = [ split(m/\+/, $check[1]) ];
  215. }
  216. $c{'warning'} = $check[2];
  217. $c{'critical'} = $check[3];
  218. # check for valid thresholds
  219. # set_threshold() will die if any threshold is not valid
  220. $self->set_thresholds(
  221. warning => $c{'warning'},
  222. critical => $c{'critical'},
  223. ) || $self->die("ERROR: Invalid thresholds: "
  224. . "warning => $c{'warning'}, critical => $c{'critical'}");
  225. push @{$self->{'conf'}->{'checks'}}, \%c;
  226. }
  227. sub set_checks
  228. {
  229. my $self = shift;
  230. my @checks = @_;
  231. my $err_str = "ERROR:";
  232. if (! defined($self->{'conf'}->{'timeout'})) {
  233. croak "No timeout set -- did you call configure()?";
  234. }
  235. if (scalar(@checks) == 0) {
  236. if ($self->{'default_check'}) {
  237. $self->{'conf'}->{'checks'}->[0] = {
  238. name => $self->{'default_check'},
  239. target => [],
  240. warning => undef,
  241. critical => undef,
  242. };
  243. }
  244. return 1;
  245. }
  246. $self->{'conf'}->{'checks'} = [];
  247. foreach my $check (@checks) {
  248. my $e;
  249. $e = $self->_add_single_check($check);
  250. if ($e =~ m/^ERROR: (.*)$/) {
  251. $err_str .= " $1,";
  252. }
  253. }
  254. if ($err_str ne "ERROR:") {
  255. $err_str =~ s/,$//;
  256. $self->die($err_str);
  257. }
  258. }
  259. sub get_checks
  260. {
  261. my $self = shift;
  262. return @{$self->{'conf'}->{'checks'}};
  263. }
  264. sub connect
  265. {
  266. my $self = shift;
  267. my $host = $self->{'conf'}->{'host'};
  268. my $user = $self->{'conf'}->{'user'};
  269. if ((! $host) || (! $user)) {
  270. croak "Host and/or user not set -- did you call configure()?";
  271. }
  272. if (! $self->opts->password) {
  273. my $term = POSIX::Termios->new();
  274. my $lflag;
  275. print "Password: ";
  276. $term->getattr(fileno(STDIN));
  277. $lflag = $term->getlflag;
  278. $term->setlflag($lflag & ~POSIX::ECHO);
  279. $term->setattr(fileno(STDIN), TCSANOW);
  280. $self->{'conf'}->{'password'} = <STDIN>;
  281. chomp($self->{'conf'}->{'password'});
  282. $term->setlflag($lflag | POSIX::ECHO);
  283. print "\n";
  284. }
  285. $self->verbose(1, "Connecting to host $host as user $user.");
  286. $junos = JUNOS::Device->new(
  287. hostname => $host,
  288. login => $user,
  289. password => $self->{'conf'}->{'password'},
  290. access => 'ssh',
  291. 'ssh-compress' => 0);
  292. if (! ref $junos) {
  293. $self->die("ERROR: failed to connect to $host!");
  294. }
  295. $self->{'junos'} = $junos;
  296. return $junos;
  297. }
  298. sub run_checks
  299. {
  300. my $self = shift;
  301. foreach my $check ($self->get_checks()) {
  302. my @targets = ();
  303. if (defined $check->{'target'}) {
  304. @targets = @{$check->{'target'}};
  305. }
  306. $self->set_thresholds(
  307. warning => $check->{'warning'},
  308. critical => $check->{'critical'},
  309. );
  310. my $sub = $self->get_check_impl($check->{'name'});
  311. $sub->($self, $self->{'junos'}, @targets);
  312. }
  313. }
  314. sub send_query
  315. {
  316. my $self = shift;
  317. my $query = shift;
  318. my $queryargs = shift;
  319. my $res;
  320. my $err;
  321. $self->verbose(3, "Sending query '$query' "
  322. . join(", ", map { "$_ => $queryargs->{$_}" } keys %$queryargs)
  323. . " to router.");
  324. if (ref $queryargs) {
  325. $res = $self->{'junos'}->$query(%$queryargs);
  326. } else {
  327. $res = $self->{'junos'}->$query();
  328. }
  329. if (! ref $res) {
  330. return "ERROR: Failed to execute query '$query'";
  331. }
  332. $err = $res->getFirstError();
  333. if ($err) {
  334. return "ERROR: " . $err->{message};
  335. }
  336. return $res;
  337. }
  338. sub get_query_object
  339. {
  340. my $self = shift;
  341. my $res = shift;
  342. my $spec = shift;
  343. if (! $res) {
  344. return;
  345. }
  346. if (! $spec) {
  347. return $res;
  348. }
  349. if (! ref($spec)) {
  350. $spec = [ $spec ];
  351. }
  352. my $iter = $res;
  353. for (my $i = 0; $i < scalar(@$spec) - 1; ++$i) {
  354. my $tmp = $iter->getElementsByTagName($spec->[$i]);
  355. if ((! $tmp) || (! $tmp->item(0))) {
  356. return;
  357. }
  358. $iter = $tmp->item(0);
  359. }
  360. if (wantarray) {
  361. my @ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  362. return @ret;
  363. }
  364. else {
  365. my $ret = $iter->getElementsByTagName($spec->[scalar(@$spec) - 1]);
  366. if ((! $ret) || (! $ret->item(0))) {
  367. return;
  368. }
  369. return $ret->item(0);
  370. }
  371. }
  372. sub get_query_object_value
  373. {
  374. my $self = shift;
  375. my $res = $self->get_query_object(@_);
  376. if (! $res) {
  377. return;
  378. }
  379. if (ref($res) eq "XML::DOM::NodeList") {
  380. $res = $res->item(0);
  381. }
  382. return $res->getFirstChild->getNodeValue;
  383. }
  384. sub nagios_exit
  385. {
  386. my $self = shift;
  387. if ($self->{'junos'}) {
  388. $self->{'junos'}->disconnect();
  389. }
  390. $self->SUPER::nagios_exit(@_);
  391. }
  392. sub verbose
  393. {
  394. my $self = shift;
  395. my $level = shift;
  396. my @msgs = @_;
  397. if ($level > $self->{'conf'}->{'verbose'}) {
  398. return;
  399. }
  400. foreach my $msg (@msgs) {
  401. print "V$level: $msg\n";
  402. }
  403. }
  404. return 1;