JUNOS.pm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #############################################################################
  2. # (c) 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 configure
  91. {
  92. my $self = shift;
  93. # Predefined arguments (by Nagios::Plugin)
  94. my @predefined_args = qw(
  95. usage
  96. help
  97. version
  98. extra-opts
  99. timeout
  100. verbose
  101. );
  102. $self->getopts;
  103. # Initialize this first, so it may be used right away.
  104. $self->{'conf'}->{'verbose'} = $self->opts->verbose;
  105. foreach my $arg (@{$self->{'cl_args'}}) {
  106. my @c = $self->_get_conf($arg);
  107. $self->{'conf'}->{$c[0]} = $c[1];
  108. }
  109. foreach my $arg (@predefined_args) {
  110. $self->{'conf'}->{$arg} = $self->opts->$arg;
  111. }
  112. }
  113. sub _get_conf
  114. {
  115. my $self = shift;
  116. my $arg = shift;
  117. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  118. my $value = $self->opts->$name || $arg->{'default'};
  119. if ($name eq 'password') {
  120. $self->verbose(3, "conf: password => "
  121. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  122. }
  123. else {
  124. $self->verbose(3, "conf: $name => $value");
  125. }
  126. return ($name => $value);
  127. }
  128. sub _add_single_check
  129. {
  130. my $self = shift;
  131. my $valid_checks = shift;
  132. my @check = split(m/,/, shift);
  133. my %c = ();
  134. if ($check[0] !~ m/\b(?:$valid_checks)\b/) {
  135. return "ERROR: invalid check '$check[0]'";
  136. }
  137. $c{'name'} = $check[0];
  138. $c{'target'} = undef;
  139. if (defined($check[1])) {
  140. $c{'target'} = [ split(m/\+/, $check[1]) ];
  141. }
  142. $c{'warning'} = $check[2];
  143. $c{'critical'} = $check[3];
  144. # check for valid thresholds
  145. # set_threshold() will die if any threshold is not valid
  146. $self->set_thresholds(
  147. warning => $c{'warning'},
  148. critical => $c{'critical'},
  149. ) || $self->die("ERROR: Invalid thresholds: "
  150. . "warning => $c{'warning'}, critical => $c{'critical'}");
  151. push @{$self->{'conf'}->{'checks'}}, \%c;
  152. }
  153. sub set_checks
  154. {
  155. my $self = shift;
  156. my $valid_checks = shift;
  157. my $default = shift;
  158. my @checks = @_;
  159. my $err_str = "ERROR:";
  160. if (! defined($self->{'conf'}->{'timeout'})) {
  161. croak "No timeout set -- did you call configure()?";
  162. }
  163. if (scalar(@checks) == 0) {
  164. $self->{'conf'}->{'checks'}[0] = {
  165. name => $default,
  166. target => [],
  167. warning => undef,
  168. critical => undef,
  169. };
  170. return 1;
  171. }
  172. $self->{'conf'}->{'checks'} = [];
  173. foreach my $check (@checks) {
  174. my $e;
  175. $e = $self->_add_single_check($valid_checks, $check);
  176. if ($e =~ m/^ERROR: (.*)$/) {
  177. $err_str .= " $1,";
  178. }
  179. }
  180. if ($err_str ne "ERROR:") {
  181. $err_str =~ s/,$//;
  182. $self->die($err_str);
  183. }
  184. }
  185. sub get_checks
  186. {
  187. my $self = shift;
  188. return @{$self->{'conf'}->{'checks'}};
  189. }
  190. sub connect
  191. {
  192. my $self = shift;
  193. my $host = $self->{'conf'}->{'host'};
  194. my $user = $self->{'conf'}->{'user'};
  195. if ((! $host) || (! $user)) {
  196. croak "Host and/or user not set -- did you call configure()?";
  197. }
  198. if (! $self->opts->password) {
  199. my $term = POSIX::Termios->new();
  200. my $lflag;
  201. print "Password: ";
  202. $term->getattr(fileno(STDIN));
  203. $lflag = $term->getlflag;
  204. $term->setlflag($lflag & ~POSIX::ECHO);
  205. $term->setattr(fileno(STDIN), TCSANOW);
  206. $self->{'conf'}->{'password'} = <STDIN>;
  207. chomp($self->{'conf'}->{'password'});
  208. $term->setlflag($lflag | POSIX::ECHO);
  209. print "\n";
  210. }
  211. $self->verbose(1, "Connecting to host $host as user $user.");
  212. $junos = JUNOS::Device->new(
  213. hostname => $host,
  214. login => $user,
  215. password => $self->{'conf'}->{'password'},
  216. access => 'ssh',
  217. 'ssh-compress' => 0);
  218. if (! ref $junos) {
  219. $self->die("ERROR: failed to connect to $host!");
  220. }
  221. $self->{'junos'} = $junos;
  222. return $junos;
  223. }
  224. sub verbose
  225. {
  226. my $self = shift;
  227. my $level = shift;
  228. my @msgs = @_;
  229. if ($level > $self->{'conf'}->{'verbose'}) {
  230. return;
  231. }
  232. foreach my $msg (@msgs) {
  233. print "V$level: $msg\n";
  234. }
  235. }
  236. return 1;