JUNOS.pm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 POSIX qw( :termios_h );
  35. use Nagios::Plugin;
  36. use JUNOS::Device;
  37. use Nagios::Plugin::Functions qw( %ERRORS %STATUS_TEXT @STATUS_CODES );
  38. # re-export Nagios::Plugin's (default) exports
  39. use Exporter;
  40. our @ISA = qw( Nagios::Plugin Exporter );
  41. our @EXPORT = (@STATUS_CODES);
  42. our @EXPORT_OK = qw( %ERRORS %STATUS_TEXT );
  43. sub new
  44. {
  45. my $class = shift;
  46. my %args = @_;
  47. my $self = Nagios::Plugin->new(%args);
  48. $self->{'conf'} = { verbose => 0 };
  49. $self->{'cl_args'} = [];
  50. $self->{'junos'} = undef;
  51. return bless($self, $class);
  52. }
  53. sub add_arg
  54. {
  55. my $self = shift;
  56. my $arg = shift;
  57. my $spec = $arg->{'spec'};
  58. my $help;
  59. push @{$self->{'cl_args'}}, $arg;
  60. if (defined $arg->{'usage'}) {
  61. $help = $arg->{'usage'};
  62. }
  63. else {
  64. $help = $arg->{'help'};
  65. }
  66. if (defined $arg->{'desc'}) {
  67. my @desc;
  68. if (ref($arg->{'desc'})) {
  69. @desc = @{$arg->{'desc'}};
  70. }
  71. else {
  72. @desc = ( $arg->{'desc'} );
  73. }
  74. foreach my $d (@desc) {
  75. $help .= "\n $d";
  76. }
  77. if (defined $arg->{'default'}) {
  78. $help .= " (default: $arg->{'default'})";
  79. }
  80. }
  81. elsif (defined $arg->{'default'}) {
  82. $help .= "\n (default: $arg->{'default'})";
  83. }
  84. $self->SUPER::add_arg(
  85. spec => $spec,
  86. help => $help,
  87. );
  88. }
  89. sub configure
  90. {
  91. my $self = shift;
  92. # Predefined arguments (by Nagios::Plugin)
  93. my @predefined_args = qw(
  94. usage
  95. help
  96. version
  97. extra-opts
  98. timeout
  99. verbose
  100. );
  101. $self->getopts;
  102. # Initialize this first, so it may be used right away.
  103. $self->{'conf'}->{'verbose'} = $self->opts->verbose;
  104. foreach my $arg (@{$self->{'cl_args'}}) {
  105. my @c = $self->_get_conf($arg);
  106. $self->{'conf'}->{$c[0]} = $c[1];
  107. }
  108. foreach my $arg (@predefined_args) {
  109. $self->{'conf'}->{$arg} = $self->opts->$arg;
  110. }
  111. }
  112. sub _get_conf
  113. {
  114. my $self = shift;
  115. my $arg = shift;
  116. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  117. my $value = $self->opts->$name || $arg->{'default'};
  118. if ($name eq 'password') {
  119. $self->verbose(3, "conf: password => "
  120. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  121. }
  122. else {
  123. $self->verbose(3, "conf: $name => $value");
  124. }
  125. return ($name => $value);
  126. }
  127. sub _add_single_check
  128. {
  129. my $self = shift;
  130. my $valid_checks = shift;
  131. my @check = split(m/,/, shift);
  132. my %c = ();
  133. if ($check[0] !~ m/\b(?:$valid_checks)\b/) {
  134. return "ERROR: invalid check '$check[0]'";
  135. }
  136. $c{'name'} = $check[0];
  137. $c{'target'} = undef;
  138. if (defined($check[1])) {
  139. $c{'target'} = [ split(m/\+/, $check[1]) ];
  140. }
  141. $c{'warning'} = $check[2];
  142. $c{'critical'} = $check[3];
  143. # check for valid thresholds
  144. # set_threshold() will die if any threshold is not valid
  145. $self->set_thresholds(
  146. warning => $c{'warning'},
  147. critical => $c{'critical'},
  148. ) || $self->die("ERROR: Invalid thresholds: "
  149. . "warning => $c{'warning'}, critical => $c{'critical'}");
  150. push @{$self->{'conf'}->{'checks'}}, \%c;
  151. }
  152. sub set_checks
  153. {
  154. my $self = shift;
  155. my $valid_checks = shift;
  156. my $default = shift;
  157. my @checks = @_;
  158. my $err_str = "ERROR:";
  159. if (scalar(@checks) == 0) {
  160. $self->{'conf'}->{'checks'}[0] = {
  161. name => $default,
  162. target => [],
  163. warning => undef,
  164. critical => undef,
  165. };
  166. return 1;
  167. }
  168. $self->{'conf'}->{'checks'} = [];
  169. foreach my $check (@checks) {
  170. my $e;
  171. $e = $self->_add_single_check($valid_checks, $check);
  172. if ($e =~ m/^ERROR: (.*)$/) {
  173. $err_str .= " $1,";
  174. }
  175. }
  176. if ($err_str ne "ERROR:") {
  177. $err_str =~ s/,$//;
  178. $self->die($err_str);
  179. }
  180. }
  181. sub get_checks
  182. {
  183. my $self = shift;
  184. return @{$self->{'conf'}->{'checks'}};
  185. }
  186. sub connect
  187. {
  188. my $self = shift;
  189. my $host = $self->{'conf'}->{'host'};
  190. my $user = $self->{'conf'}->{'user'};
  191. if (! $self->opts->password) {
  192. my $term = POSIX::Termios->new();
  193. my $lflag;
  194. print "Password: ";
  195. $term->getattr(fileno(STDIN));
  196. $lflag = $term->getlflag;
  197. $term->setlflag($lflag & ~POSIX::ECHO);
  198. $term->setattr(fileno(STDIN), TCSANOW);
  199. $self->{'conf'}->{'password'} = <STDIN>;
  200. chomp($self->{'conf'}->{'password'});
  201. $term->setlflag($lflag | POSIX::ECHO);
  202. print "\n";
  203. }
  204. $self->verbose(1, "Connecting to host $host as user $user.");
  205. $junos = JUNOS::Device->new(
  206. hostname => $host,
  207. login => $user,
  208. password => $self->{'conf'}->{'password'},
  209. access => 'ssh',
  210. 'ssh-compress' => 0);
  211. if (! ref $junos) {
  212. $self->die("ERROR: failed to connect to $host!");
  213. }
  214. $self->{'junos'} = $junos;
  215. return $junos;
  216. }
  217. sub verbose
  218. {
  219. my $self = shift;
  220. my $level = shift;
  221. my @msgs = @_;
  222. if ($level > $self->{'conf'}->{'verbose'}) {
  223. return;
  224. }
  225. foreach my $msg (@msgs) {
  226. print "V$level: $msg\n";
  227. }
  228. }
  229. return 1;