Using Perl v5.20.2 on OpenBSD 5.8.

I've written a management script called by a cron job that pulls values from a database. The ID of each server is represented by the hostname. During periods of network outages, or poor performance otherwise, I've receive the following warning via the cron job:

Use of uninitialized value $host in concatenation (.) or string at /etc/mrtg/testing.pl line 128.

Along with this warning I receive an error from DBI which indicates it couldn't make a connection. That is what leads me to believe it may be related to network errors and performance.

The cron job itself fires off twice a minute with two separate sets of parameters, so it is succeeding >90% of the time. The code itself is fairly simple, and writes status and event logs to /var/log/daemon. When line 128 is being run the daemon log is being updated by my management script. I would post those too, but my newsyslog is a bit aggressive (sorry). The log is being updated normally as we speak, with no errors.

# Perl libraries we need - Please note that Date::Parse is not # standard and needs to be installed as of OpenBSD 5.7 use utf8; use Path::Class; use strict; use warnings; use Time::Piece; use Date::Parse; use Socket; use Sys::Hostname; use POSIX qw(strftime); use DBI; use IO::Socket::INET; use Storable; use Storable qw(nstore store_fd nstore_fd freeze thaw dclone); use IPC::Open2; use Symbol 'gensym'; use String::Util 'trim'; use Net::SNMP; use Data::Dumper; # Get our hostname for C2 operations our $host = hostname(); # Get our local IP address by connecting to the MRTG Database our $localip = ''; sub get_local_ip(){ my $sock = IO::Socket::INET->new( PeerAddr=> $mrtgc2, PeerPort=> 5432, Proto => "tcp"); our $localip = ''; $::localip = $sock->sockhost; } get_local_ip(); ...... ...... ...... ## The following line is #128 from the above error ## print $fd $daemon_log_timestamp.' '.our $host.' mrtg[]:['.$log_entry_c +ategory.']: '.$log_entry.'.'."\n";
Anybody have an idea as to why the value would be uninitialized? Sys::Hostname uses many methods to get the hostname, so I'm surprised at the error, and stumped about the intermittent nature of it.

In reply to Uninitialized value from hostname() by eepstein

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.