check_junos_vc.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. #!/usr/bin/perl
  2. #############################################################################
  3. # (c) 2001, 2003 Juniper Networks, Inc. #
  4. # (c) 2011 Sebastian "tokkee" Harl <sh@teamix.net> #
  5. # and team(ix) GmbH, Nuernberg, Germany #
  6. # #
  7. # This file is part of "team(ix) Monitoring Plugins" #
  8. # URL: http://oss.teamix.org/projects/monitoringplugins/ #
  9. # #
  10. # All rights reserved. #
  11. # Redistribution and use in source and binary forms, with or without #
  12. # modification, are permitted provided that the following conditions #
  13. # are met: #
  14. # 1. Redistributions of source code must retain the above copyright #
  15. # notice, this list of conditions and the following disclaimer. #
  16. # 2. Redistributions in binary form must reproduce the above copyright #
  17. # notice, this list of conditions and the following disclaimer in the #
  18. # documentation and/or other materials provided with the distribution. #
  19. # 3. The name of the copyright owner may not be used to endorse or #
  20. # promote products derived from this software without specific prior #
  21. # written permission. #
  22. # #
  23. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR #
  24. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #
  25. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #
  26. # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, #
  27. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #
  28. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #
  29. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) #
  30. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, #
  31. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING #
  32. # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE #
  33. # POSSIBILITY OF SUCH DAMAGE. #
  34. #############################################################################
  35. use strict;
  36. use warnings;
  37. use utf8;
  38. use POSIX qw( :termios_h );
  39. use Nagios::Plugin;
  40. use JUNOS::Device;
  41. binmode STDOUT, ":utf8";
  42. my $valid_checks = "members_count|master|backup|interfaces|version";
  43. my $plugin = Nagios::Plugin->new(
  44. plugin => 'check_junos_vc',
  45. shortname => 'check_junos_vc',
  46. version => '0.1',
  47. url => 'http://oss.teamix.org/projects/monitoringplugins',
  48. blurb => 'Monitor Juniper™ Switch Virtual Chassis.',
  49. usage =>
  50. "Usage: %s [-v|--verbose] [-H <host>] [-p <port>] [-t <timeout]
  51. [-U <user>] [-P <password] check-tuple [...]",
  52. license =>
  53. "This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY.
  54. It may be used, redistributed and/or modified under the terms of the 3-Clause
  55. BSD License (see http://opensource.org/licenses/BSD-3-Clause).",
  56. extra => "
  57. This plugin connects to a Juniper™ Switch device and and checks Virtual
  58. Chassis information.
  59. A check-tuple consists of the name of the check and, optionally, a \"target\"
  60. which more closely specifies which characteristics should be checked, and
  61. warning and critical thresholds:
  62. checkname[,target[,warning[,critical]]]
  63. The following checks are available:
  64. * members_count: Total number of members in the Virtual Chassis. If a target
  65. is specified, only peers whose status (NotPrsnt, Prsnt) matches one of the
  66. specified targets are taken into account.
  67. * master, backup: Check the number or assignment of master resp. backup
  68. members. If a target is specified, check that those members whose serial
  69. number matches the specified target have the requested role (master,
  70. backup) assigned to them. Else, check the number of master resp. backup
  71. members against the specified thresholds.
  72. * interfaces: Check that all VCP interfaces are up. If warning or critical
  73. thresholds have been specified, also check the number of VCP interfaces
  74. against the thresholds.
  75. * version: Check the version of all physically connected members.
  76. Warning and critical thresholds may be specified in the format documented at
  77. http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT.",
  78. );
  79. # Predefined arguments (by Nagios::Plugin)
  80. my @predefined_args = qw(
  81. usage
  82. help
  83. version
  84. extra-opts
  85. timeout
  86. verbose
  87. );
  88. my @args = (
  89. {
  90. spec => 'host|H=s',
  91. usage => '-H, --host=HOSTNAME',
  92. desc => 'Hostname/IP of Juniper box to connect to',
  93. default => 'localhost',
  94. },
  95. {
  96. spec => 'port|p=i',
  97. usage => '-p, --port=PORT',
  98. desc => 'Port to connect to',
  99. default => 22,
  100. },
  101. {
  102. spec => 'user|U=s',
  103. usage => '-U, --user=USERNAME',
  104. desc => 'Username to log into box as',
  105. default => 'root',
  106. },
  107. {
  108. spec => 'password|P=s',
  109. usage => '-P, --password=PASSWORD',
  110. desc => 'Password for login username',
  111. default => '<prompt>',
  112. },
  113. );
  114. my %conf = ();
  115. my $junos = undef;
  116. foreach my $arg (@args) {
  117. add_arg($plugin, $arg);
  118. }
  119. $plugin->getopts;
  120. # Initialize this first, so it may be used right away.
  121. $conf{'verbose'} = $plugin->opts->verbose;
  122. foreach my $arg (@args) {
  123. my @c = get_conf($plugin, $arg);
  124. $conf{$c[0]} = $c[1];
  125. }
  126. foreach my $arg (@predefined_args) {
  127. $conf{$arg} = $plugin->opts->$arg;
  128. }
  129. add_checks(\%conf, @ARGV);
  130. if (! $plugin->opts->password) {
  131. my $term = POSIX::Termios->new();
  132. my $lflag;
  133. print "Password: ";
  134. $term->getattr(fileno(STDIN));
  135. $lflag = $term->getlflag;
  136. $term->setlflag($lflag & ~POSIX::ECHO);
  137. $term->setattr(fileno(STDIN), TCSANOW);
  138. $conf{'password'} = <STDIN>;
  139. chomp($conf{'password'});
  140. $term->setlflag($lflag | POSIX::ECHO);
  141. print "\n";
  142. }
  143. verbose(1, "Connecting to host $conf{'host'} as user $conf{'user'}.");
  144. $junos = JUNOS::Device->new(
  145. hostname => $conf{'host'},
  146. login => $conf{'user'},
  147. password => $conf{'password'},
  148. access => 'ssh',
  149. 'ssh-compress' => 0);
  150. if (! ref $junos) {
  151. $plugin->die("ERROR: failed to connect to " . $conf{'host'} . "!");
  152. }
  153. my $vc = undef;
  154. my @vc_members = ();
  155. my $have_vc_members = 0;
  156. foreach my $check (@{$conf{'checks'}}) {
  157. my $code;
  158. my $value;
  159. my @targets = ();
  160. if (defined $check->{'target'}) {
  161. @targets = @{$check->{'target'}};
  162. }
  163. $plugin->set_thresholds(
  164. warning => $check->{'warning'},
  165. critical => $check->{'critical'},
  166. );
  167. if ($check->{'name'} eq 'members_count') {
  168. my @relevant_members = ();
  169. my $value = 0;
  170. my $code;
  171. @vc_members = get_vc_members($junos);
  172. if (scalar(@targets)) {
  173. foreach my $member (@vc_members) {
  174. my $role = get_member_status($member);
  175. if (scalar(grep { $role eq $_ } @targets)) {
  176. push @relevant_members, $member;
  177. $value++;
  178. }
  179. }
  180. }
  181. else {
  182. @relevant_members = @vc_members;
  183. $value = scalar(@vc_members);
  184. }
  185. $code = $plugin->check_threshold($value);
  186. $plugin->add_message($code, "$value " . join(" + ", @targets)
  187. . " member" . (($value == 1) ? "" : "s"));
  188. my $label = 'members';
  189. if (scalar(@targets)) {
  190. $label .= '[' . join('+', @targets) . ']';
  191. }
  192. $plugin->add_perfdata(
  193. label => $label,
  194. value => $value,
  195. min => 0,
  196. max => undef,
  197. uom => '',
  198. threshold => $plugin->threshold(),
  199. );
  200. }
  201. elsif (($check->{'name'} eq 'master') || ($check->{'name'} eq 'backup')) {
  202. my $wanted_role = ($check->{'name'} eq 'master')
  203. ? 'Master' : 'Backup';
  204. my @wanted_members = ();
  205. my $value;
  206. my $code;
  207. @vc_members = get_vc_members($junos);
  208. foreach my $member (@vc_members) {
  209. my $role = get_member_role($member);
  210. if ($role eq $wanted_role) {
  211. push @wanted_members, $member;
  212. }
  213. }
  214. if (scalar(@targets)) {
  215. my @ok_targets = ();
  216. my @fail_targets = ();
  217. $code = UNKNOWN;
  218. foreach my $target (@targets) {
  219. if (scalar(grep { $target eq get_member_serial($_) } @wanted_members)) {
  220. # requested target does have wanted role assigned
  221. if (($code == UNKNOWN) || ($code == OK)) {
  222. $code = OK;
  223. }
  224. else {
  225. # we've had previous errors
  226. $code = WARNING;
  227. }
  228. push @ok_targets, $target;
  229. }
  230. else {
  231. if (($code == OK) || ($code == WARNING)) {
  232. # we've had previous success
  233. $code = WARNING;
  234. }
  235. else {
  236. $code = CRITICAL;
  237. }
  238. push @fail_targets, $target;
  239. }
  240. }
  241. $plugin->add_message($code, scalar(@fail_targets)
  242. . " missing/failed-over " . $check->{'name'}
  243. . ((scalar(@fail_targets) == 1) ? "" : "s")
  244. . (scalar(@fail_targets)
  245. ? " (" . join(", ", @fail_targets) . ")" : "")
  246. . ", " . scalar(@ok_targets) . " active " . $check->{'name'}
  247. . ((scalar(@ok_targets) == 1) ? "" : "s")
  248. . (scalar(@ok_targets)
  249. ? " (" . join(", ", @ok_targets) . ")" : ""));
  250. $plugin->add_perfdata(
  251. label => 'active_' . $check->{'name'},
  252. value => scalar(@ok_targets),
  253. min => 0,
  254. max => undef,
  255. uom => '',
  256. threshold => undef,
  257. );
  258. $plugin->add_perfdata(
  259. label => 'failed_' . $check->{'name'},
  260. value => scalar(@fail_targets),
  261. min => 0,
  262. max => undef,
  263. uom => '',
  264. threshold => undef,
  265. );
  266. }
  267. else {
  268. $value = scalar @wanted_members;
  269. $code = $plugin->check_threshold($value);
  270. $plugin->add_message($code, "$value " . $check->{'name'} . " member"
  271. . (($value == 1) ? "" : "s"));
  272. $plugin->add_perfdata(
  273. label => $check->{'name'},
  274. value => $value,
  275. min => 0,
  276. max => undef,
  277. uom => '',
  278. threshold => $plugin->threshold(),
  279. );
  280. }
  281. }
  282. elsif ($check->{'name'} eq 'interfaces') {
  283. my @up_ifaces = ();
  284. my @down_ifaces = ();
  285. my @vc_interfaces = get_vc_interfaces($junos);
  286. foreach my $iface (@vc_interfaces) {
  287. my $status = get_iface_status($iface);
  288. if ($status eq 'up') {
  289. push @up_ifaces, get_iface_name($iface);
  290. next;
  291. }
  292. # else:
  293. push @down_ifaces, {
  294. name => get_iface_name($iface),
  295. status => $status,
  296. };
  297. }
  298. if (scalar(@down_ifaces)) {
  299. $plugin->add_message(CRITICAL, scalar(@down_ifaces)
  300. . " VCP interface" . ((scalar(@down_ifaces) == 1) ? "" : "s")
  301. . " not up ("
  302. . join(", ",
  303. map { "$_->{'name'} $_->{'status'}" } @down_ifaces)
  304. . ")");
  305. }
  306. elsif ($check->{'warning'} || $check->{'critical'}) {
  307. my $value = scalar @vc_interfaces;
  308. my $code = $plugin->check_threshold($value);
  309. $plugin->add_message($code, "$value VCP interface"
  310. . (($value == 1) ? "" : "s") . " found ("
  311. . scalar(@up_ifaces) . " up, "
  312. . scalar(@down_ifaces) . " down)");
  313. }
  314. elsif (! scalar(@up_ifaces)) {
  315. # no VCP interfaces at all
  316. $plugin->add_message(CRITICAL, "no VCP interfaces found");
  317. }
  318. else {
  319. $plugin->add_message(OK, "all VCP interfaces up");
  320. }
  321. $plugin->add_perfdata(
  322. label => 'vcp_interfaces',
  323. value => scalar(@vc_interfaces),
  324. min => 0,
  325. max => undef,
  326. uom => '',
  327. threshold => $plugin->threshold(),
  328. );
  329. $plugin->add_perfdata(
  330. label => 'up_interfaces',
  331. value => scalar(@up_ifaces),
  332. min => 0,
  333. max => undef,
  334. uom => '',
  335. threshold => undef,
  336. );
  337. $plugin->add_perfdata(
  338. label => 'down_interfaces',
  339. value => scalar(@down_ifaces),
  340. min => 0,
  341. max => undef,
  342. uom => '',
  343. threshold => undef,
  344. );
  345. }
  346. elsif ($check->{'name'} eq 'version') {
  347. my %versions = get_versions($junos);
  348. my @v_keys = keys %versions;
  349. my $first = undef;
  350. my @base_mismatch = ();
  351. my %mismatches = ();
  352. foreach my $k (@v_keys) {
  353. my $base = $versions{$k}->{'base'};
  354. my $other = $versions{$k}->{'other'};
  355. foreach my $o (keys %$other) {
  356. if ($other->{$o} ne $base) {
  357. $mismatches{$k}->{$base} = 1;
  358. $mismatches{$k}->{$other->{$o}} = 1;
  359. }
  360. }
  361. }
  362. $first = shift @v_keys;
  363. $first = $versions{$first};
  364. foreach my $k (@v_keys) {
  365. if ($first->{'base'} ne $versions{$k}->{'base'}) {
  366. push @base_mismatch, $k;
  367. }
  368. }
  369. if (scalar @base_mismatch) {
  370. my @first_match = grep {
  371. $versions{$_}->{'base'} eq $first->{'base'}
  372. } keys %versions;
  373. my %mismatches = ();
  374. foreach my $m (@base_mismatch) {
  375. push @{$mismatches{$versions{$m}->{'base'}}}, $m;
  376. }
  377. $plugin->add_message(CRITICAL, "version mismatch detected: "
  378. . $first->{'base'} . " @ ("
  379. . join(", ", @first_match) . ") != "
  380. . join(" != ", map {
  381. $_ . " @ (" . join(", ", @{$mismatches{$_}}) . ")"
  382. } keys %mismatches));
  383. }
  384. elsif (scalar(keys %mismatches)) {
  385. $plugin->add_message(WARNING, "version mismatches detected: "
  386. . join(" / ", map {
  387. "$_: " . join(" != ", keys %{$mismatches{$_}})
  388. } keys %mismatches));
  389. }
  390. else {
  391. $plugin->add_message(OK, "all members at version "
  392. . $first->{'base'});
  393. }
  394. }
  395. }
  396. # add total numbers to perfdata to ease graphing stuff
  397. if ($have_vc_members) {
  398. $plugin->add_perfdata(
  399. label => 'members',
  400. value => scalar(@vc_members),
  401. min => 0,
  402. max => undef,
  403. uom => '',
  404. threshold => undef,
  405. );
  406. }
  407. my ($code, $msg) = $plugin->check_messages(join => ', ');
  408. $junos->disconnect();
  409. $plugin->nagios_exit($code, $msg);
  410. sub send_query
  411. {
  412. my $device = shift;
  413. my $query = shift;
  414. my $queryargs = shift;
  415. my $res;
  416. my $err;
  417. verbose(3, "Sending query '$query' to router.");
  418. if (ref $queryargs) {
  419. $res = $device->$query(%$queryargs);
  420. } else {
  421. $res = $device->$query();
  422. }
  423. if (! ref $res) {
  424. return "ERROR: Failed to execute query '$query'";
  425. }
  426. $err = $res->getFirstError();
  427. if ($err) {
  428. return "ERROR: " . $err->{message};
  429. }
  430. return $res;
  431. }
  432. sub send_command
  433. {
  434. my $device = shift;
  435. my $cmd = shift;
  436. my $res;
  437. my $err;
  438. verbose(3, "Sending command '$cmd' to router.");
  439. $res = $device->command($cmd);
  440. if (! ref $res) {
  441. return "ERROR: Failed to execute command '$cmd'";
  442. }
  443. $err = $res->getFirstError();
  444. if ($err) {
  445. return "ERROR: " . $err->{message};
  446. }
  447. return $res;
  448. }
  449. sub get_vc_information
  450. {
  451. my $device = shift;
  452. my $cmd = "show virtual-chassis status";
  453. my $res = send_command($device, $cmd);
  454. my $err;
  455. if (! ref $res) {
  456. return $res;
  457. }
  458. return $res;
  459. }
  460. sub get_vc_members
  461. {
  462. my $device = shift;
  463. if ($have_vc_members) {
  464. return @vc_members;
  465. }
  466. $vc = get_vc_information($device);
  467. if (! ref $vc) {
  468. $plugin->die($vc);
  469. }
  470. my $vc_id = ($vc->getElementsByTagName('virtual-chassis-id-information'))[0];
  471. $vc_id = ($vc_id->getElementsByTagName('virtual-chassis-id'))[0];
  472. $vc_id = $vc_id->getFirstChild->getNodeValue;
  473. verbose(3, "Analyzing data from virtual chassis $vc_id.");
  474. @vc_members = ($vc->getElementsByTagName('member-list'))[0]->getElementsByTagName('member');
  475. if ($conf{'verbose'} >= 3) {
  476. my @m = map { get_member_id($_) . " => " . get_member_serial($_) }
  477. @vc_members;
  478. verbose(3, "Members: " . join(", ", @m));
  479. }
  480. $have_vc_members = 1;
  481. return @vc_members;
  482. }
  483. sub get_vc_interfaces
  484. {
  485. my $device = shift;
  486. my @ifaces = ();
  487. my $cmd = "get_interface_information";
  488. my %args = ( interface_name => 'vcp*' );
  489. my $res = send_query($device, $cmd, \%args);
  490. if (! ref $res) {
  491. $plugin->die($res);
  492. }
  493. @ifaces = $res->getElementsByTagName('physical-interface');
  494. if ($conf{'verbose'} >= 3) {
  495. my @i = map { get_iface_name($_) . " => " . get_iface_status($_) }
  496. @ifaces;
  497. verbose(3, "VCP Interfaces: " . join(", ", @i));
  498. }
  499. return @ifaces;
  500. }
  501. sub get_versions
  502. {
  503. my $device = shift;
  504. my %versions = ();
  505. my $cmd = "show version";
  506. my $res = send_command($device, $cmd);
  507. my @v = ();
  508. if (! ref $res) {
  509. $plugin->die($res);
  510. }
  511. @v = $res->getElementsByTagName('multi-routing-engine-item');
  512. foreach my $i (@v) {
  513. my $name = get_obj_element($i, 're-name');
  514. my @infos = $i->getElementsByTagName('software-information');
  515. @infos = $infos[0]->getElementsByTagName('package-information');
  516. foreach my $j (@infos) {
  517. my $comment = get_obj_element($j, 'comment');
  518. my ($desc, $version);
  519. $comment =~ m/^(.*) \[([^]]+)\]$/;
  520. $desc = $1;
  521. $version = $2;
  522. if ($desc eq "JUNOS Base OS boot") {
  523. $versions{$name}->{'base'} = $version;
  524. }
  525. else {
  526. $versions{$name}->{'other'}->{$desc} = $version;
  527. }
  528. }
  529. }
  530. return %versions;
  531. }
  532. sub add_arg
  533. {
  534. my $plugin = shift;
  535. my $arg = shift;
  536. my $spec = $arg->{'spec'};
  537. my $help = $arg->{'usage'};
  538. if (defined $arg->{'desc'}) {
  539. my @desc;
  540. if (ref($arg->{'desc'})) {
  541. @desc = @{$arg->{'desc'}};
  542. }
  543. else {
  544. @desc = ( $arg->{'desc'} );
  545. }
  546. foreach my $d (@desc) {
  547. $help .= "\n $d";
  548. }
  549. if (defined $arg->{'default'}) {
  550. $help .= " (default: $arg->{'default'})";
  551. }
  552. }
  553. elsif (defined $arg->{'default'}) {
  554. $help .= "\n (default: $arg->{'default'})";
  555. }
  556. $plugin->add_arg(
  557. spec => $spec,
  558. help => $help,
  559. );
  560. }
  561. sub get_conf
  562. {
  563. my $plugin = shift;
  564. my $arg = shift;
  565. my ($name, undef) = split(m/\|/, $arg->{'spec'});
  566. my $value = $plugin->opts->$name || $arg->{'default'};
  567. if ($name eq 'password') {
  568. verbose(3, "conf: password => "
  569. . (($value eq '<prompt>') ? '<prompt>' : '<hidden>'));
  570. }
  571. else {
  572. verbose(3, "conf: $name => $value");
  573. }
  574. return ($name => $value);
  575. }
  576. sub add_single_check
  577. {
  578. my $conf = shift;
  579. my @check = split(m/,/, shift);
  580. my %c = ();
  581. if ($check[0] !~ m/\b(?:$valid_checks)\b/) {
  582. return "ERROR: invalid check '$check[0]'";
  583. }
  584. $c{'name'} = $check[0];
  585. $c{'target'} = undef;
  586. if (defined($check[1])) {
  587. $c{'target'} = [ split(m/\+/, $check[1]) ];
  588. }
  589. $c{'warning'} = $check[2];
  590. $c{'critical'} = $check[3];
  591. # check for valid thresholds
  592. # set_threshold() will die if any threshold is not valid
  593. $plugin->set_thresholds(
  594. warning => $c{'warning'},
  595. critical => $c{'critical'},
  596. ) || $plugin->die("ERROR: Invalid thresholds: "
  597. . "warning => $c{'warning'}, critical => $c{'critical'}");
  598. push @{$conf->{'checks'}}, \%c;
  599. }
  600. sub add_checks
  601. {
  602. my $conf = shift;
  603. my @checks = @_;
  604. my $err_str = "ERROR:";
  605. if (scalar(@checks) == 0) {
  606. $conf->{'checks'}[0] = {
  607. name => 'members_count',
  608. target => [],
  609. warning => undef,
  610. critical => undef,
  611. };
  612. return 1;
  613. }
  614. $conf->{'checks'} = [];
  615. foreach my $check (@checks) {
  616. my $e;
  617. $e = add_single_check($conf, $check);
  618. if ($e =~ m/^ERROR: (.*)$/) {
  619. $err_str .= " $1,";
  620. }
  621. }
  622. if ($err_str ne "ERROR:") {
  623. $err_str =~ s/,$//;
  624. $plugin->die($err_str);
  625. }
  626. }
  627. sub get_obj_element
  628. {
  629. my $obj = shift;
  630. my $elem = shift;
  631. $elem = $obj->getElementsByTagName($elem);
  632. return $elem->item(0)->getFirstChild->getNodeValue;
  633. }
  634. sub get_member_id
  635. {
  636. my $member = shift;
  637. return get_obj_element($member, 'member-id');
  638. }
  639. sub get_member_serial
  640. {
  641. my $member = shift;
  642. return get_obj_element($member, 'member-serial-number');
  643. }
  644. sub get_member_status
  645. {
  646. my $member = shift;
  647. return get_obj_element($member, 'member-status');
  648. }
  649. sub get_member_role
  650. {
  651. my $member = shift;
  652. my $elem;
  653. $elem = $member->getElementsByTagName('member-role');
  654. if ($elem && $elem->item(0)) {
  655. $elem = $elem->item(0)->getFirstChild->getNodeValue;
  656. # e.g., '*' may be appended to the member-role
  657. $elem =~ s/\W//g;
  658. return $elem;
  659. }
  660. else {
  661. return "";
  662. }
  663. }
  664. sub get_iface_name
  665. {
  666. my $iface = shift;
  667. return get_obj_element($iface, 'name');
  668. }
  669. sub get_iface_status
  670. {
  671. my $iface = shift;
  672. return get_obj_element($iface, 'admin-status');
  673. }
  674. sub verbose
  675. {
  676. my $level = shift;
  677. my @msgs = @_;
  678. if ($level > $conf{'verbose'}) {
  679. return;
  680. }
  681. foreach my $msg (@msgs) {
  682. print "V$level: $msg\n";
  683. }
  684. }