JUNOS.pm 8.3 KB

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