NetApp.pm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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::NetApp;
  34. use Carp;
  35. use POSIX qw( :termios_h );
  36. use Nagios::Plugin;
  37. use NaServer;
  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->{'srv'} = 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 NetApp filer 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. },
  105. {
  106. spec => 'user|U=s',
  107. usage => '-U, --user=USERNAME',
  108. desc => 'Username to log into box as',
  109. default => 'root',
  110. },
  111. {
  112. spec => 'password|P=s',
  113. usage => '-P, --password=PASSWORD',
  114. desc => 'Password for login username',
  115. default => '<prompt>',
  116. },
  117. {
  118. spec => 'transport|t=s',
  119. usage => '-t, --transport=TRANSPORT',
  120. desc => 'Transport for the connection',
  121. default => 'HTTP',
  122. },
  123. );
  124. foreach my $arg (@args) {
  125. $self->add_arg($arg);
  126. }
  127. }
  128. sub add_check_impl
  129. {
  130. my $self = shift;
  131. my $name = shift;
  132. my $sub = shift;
  133. if ((! $name) || (! $sub) || (ref($sub) ne "CODE")) {
  134. carp "Invalid check specification: $name -> $sub";
  135. return;
  136. }
  137. if (! defined($self->{'check_impls'})) {
  138. $self->{'check_impls'} = {};
  139. }
  140. $self->{'check_impls'}->{$name} = $sub;
  141. }
  142. sub get_check_impl
  143. {
  144. my $self = shift;
  145. my $name = shift;
  146. if (! defined($self->{'check_impls'}->{$name})) {
  147. return;
  148. }
  149. return $self->{'check_impls'}->{$name};
  150. }
  151. sub is_valid_check
  152. {
  153. my $self = shift;
  154. my $name = shift;
  155. if (defined $self->{'check_impls'}->{$name}) {
  156. return 1;
  157. }
  158. return;
  159. }
  160. sub set_default_check
  161. {
  162. my $self = shift;
  163. my $def = shift;
  164. if (! $self->is_valid_check($def)) {
  165. carp "set_default_check: Check '$def' does not exist";
  166. return;
  167. }
  168. $self->{'default_check'} = $def;
  169. }
  170. sub configure
  171. {
  172. my $self = shift;
  173. # Predefined arguments (by Nagios::Plugin)
  174. my @predefined_args = qw(
  175. usage
  176. help
  177. version
  178. extra-opts
  179. timeout
  180. verbose
  181. );
  182. $self->getopts;
  183. # Initialize this first, so it may be used right away.
  184. $self->{'conf'}->{'verbose'} = $self->opts->verbose;
  185. foreach my $arg (@{$self->{'cl_args'}}) {
  186. my @c = $self->_get_conf($arg);
  187. $self->{'conf'}->{$c[0]} = $c[1];
  188. }
  189. foreach my $arg (@predefined_args) {
  190. $self->{'conf'}->{$arg} = $self->opts->$arg;
  191. }
  192. }
  193. sub _get_conf
  194. {
  195. my $self = shift;
  196. my $arg = shift;
  197. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  198. my $value = $self->opts->$name || $arg->{'default'};
  199. if ($name eq 'password') {
  200. $self->verbose(3, "conf: password => "
  201. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  202. }
  203. else {
  204. $self->verbose(3, "conf: $name => $value");
  205. }
  206. return ($name => $value);
  207. }
  208. sub _add_single_check
  209. {
  210. my $self = shift;
  211. my @check = split(m/,/, shift);
  212. my %c = ();
  213. if (! $self->is_valid_check($check[0])) {
  214. return "ERROR: invalid check '$check[0]'";
  215. }
  216. $c{'name'} = $check[0];
  217. $c{'target'} = undef;
  218. if (defined($check[1])) {
  219. $c{'target'} = [ split(m/\+/, $check[1]) ];
  220. }
  221. $c{'warning'} = $check[2];
  222. $c{'critical'} = $check[3];
  223. # check for valid thresholds
  224. # set_threshold() will die if any threshold is not valid
  225. $self->set_thresholds(
  226. warning => $c{'warning'},
  227. critical => $c{'critical'},
  228. ) || $self->die("ERROR: Invalid thresholds: "
  229. . "warning => $c{'warning'}, critical => $c{'critical'}");
  230. push @{$self->{'conf'}->{'checks'}}, \%c;
  231. }
  232. sub set_checks
  233. {
  234. my $self = shift;
  235. my @checks = @_;
  236. my $err_str = "ERROR:";
  237. if (! defined($self->{'conf'}->{'timeout'})) {
  238. croak "No timeout set -- did you call configure()?";
  239. }
  240. if (scalar(@checks) == 0) {
  241. if ($self->{'default_check'}) {
  242. $self->{'conf'}->{'checks'}->[0] = {
  243. name => $self->{'default_check'},
  244. target => [],
  245. warning => undef,
  246. critical => undef,
  247. };
  248. }
  249. return 1;
  250. }
  251. $self->{'conf'}->{'checks'} = [];
  252. foreach my $check (@checks) {
  253. my $e;
  254. $e = $self->_add_single_check($check);
  255. if ($e =~ m/^ERROR: (.*)$/) {
  256. $err_str .= " $1,";
  257. }
  258. }
  259. if ($err_str ne "ERROR:") {
  260. $err_str =~ s/,$//;
  261. $self->die($err_str);
  262. }
  263. }
  264. sub get_checks
  265. {
  266. my $self = shift;
  267. return @{$self->{'conf'}->{'checks'}};
  268. }
  269. sub connect
  270. {
  271. my $self = shift;
  272. my $host = $self->{'conf'}->{'host'};
  273. my $user = $self->{'conf'}->{'user'};
  274. my $srv;
  275. if ((! $host) || (! $user)) {
  276. croak "Host and/or user not set -- did you call configure()?";
  277. }
  278. if (! $self->opts->password) {
  279. my $term = POSIX::Termios->new();
  280. my $lflag;
  281. print "Password: ";
  282. $term->getattr(fileno(STDIN));
  283. $lflag = $term->getlflag;
  284. $term->setlflag($lflag & ~POSIX::ECHO);
  285. $term->setattr(fileno(STDIN), TCSANOW);
  286. $self->{'conf'}->{'password'} = <STDIN>;
  287. chomp($self->{'conf'}->{'password'});
  288. $term->setlflag($lflag | POSIX::ECHO);
  289. $term->setattr(fileno(STDIN), TCSAFLUSH);
  290. print "\n";
  291. }
  292. $self->verbose(1, "Connecting to host $host as user $user.");
  293. $srv = new NaServer($host, 1, 7);
  294. if (! $srv) {
  295. $self->die("ERROR: failed to connect to $host!");
  296. }
  297. $srv->set_admin_user($user, $self->{'conf'}->{'password'});
  298. $srv->set_transport_type($self->{'conf'}->{'transport'});
  299. if ($self->{'conf'}->{'port'}) {
  300. $srv->set_port($self->{'conf'}->{'port'});
  301. }
  302. $srv->set_timeout($self->{'conf'}->{'timeout'});
  303. $self->{'srv'} = $srv;
  304. return $srv;
  305. }
  306. sub run_checks
  307. {
  308. my $self = shift;
  309. foreach my $check ($self->get_checks()) {
  310. my @targets = ();
  311. if (defined $check->{'target'}) {
  312. @targets = @{$check->{'target'}};
  313. }
  314. $self->set_thresholds(
  315. warning => $check->{'warning'},
  316. critical => $check->{'critical'},
  317. );
  318. my $sub = $self->get_check_impl($check->{'name'});
  319. $sub->($self, $self->{'srv'}, @targets);
  320. }
  321. }
  322. sub get_error
  323. {
  324. my $self = shift;
  325. my $res = shift;
  326. my $msg = shift;
  327. if (! defined($res)) {
  328. return "$msg: Unknown error";
  329. }
  330. elsif ((ref($res) eq "NaElement") && (($res->results_errno != 0)
  331. || ($res->results_status() eq "failed"))) {
  332. return "$msg: " . $res->results_reason();
  333. }
  334. return;
  335. }
  336. sub die_on_error
  337. {
  338. my $self = shift;
  339. if ($self->get_error(@_)) {
  340. $self->die($self->get_error(@_));
  341. }
  342. return 1;
  343. }
  344. sub nagios_exit
  345. {
  346. my $self = shift;
  347. if ($self->{'srv'}) {
  348. $self->{'srv'} = undef;
  349. }
  350. $self->SUPER::nagios_exit(@_);
  351. }
  352. sub verbose
  353. {
  354. my $self = shift;
  355. my $level = shift;
  356. my @msgs = @_;
  357. if ($level > $self->{'conf'}->{'verbose'}) {
  358. return;
  359. }
  360. foreach my $msg (@msgs) {
  361. print "V$level: $msg\n";
  362. }
  363. }
  364. return 1;