using IPC::Cmd I am writing a nagios check that will warn whenever one of the systemd services is not running.

use IPC::Cmd qw[can_run run run_forked]; my $cmd = "systemctl --failed --no-legend"; my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = run( command => $cmd, verbose => 0 ); if ($success) { my $nr_els = @$full_buf; if ( $nr_els == 0 ) { print "OK: all systemd units in their desired state\n"; } else { print "WARNING: "; print "$nr_els systemd units not running\n"; for (@$full_buf) { print "$_\n"; } } }
Quite straight forward. I have killed -9 (yes, with signal 1 or 15 then they are not in a failed state, you need to really kill them) two services and then I run this and get this:
WARNING: 1 systemd units not running avahi-daemon.service loaded failed failed Avahi mDNS/DNS-SD Stack packagekit.service loaded failed failed PackageKit Daemon
Why do I get '1' instead of 2? When looping throught the elements of @$full_buf I get two lines of text, so I should also get '2' in $nr_els but I get '1'.

I must be missing something very basic ..., but I cannot see it. Thanks for your input!




 

In reply to number of elements in array ref wtf by natxo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.