File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/ipcheck
#!/usr/bin/perl
# cpanel - ipcheck Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use strict;
use warnings;
use Text::Wrap; # Perl core lib module
use Cpanel::SafeRun::Errors ();
use Cpanel::DIp::MainIP ();
use Cpanel::FindBin ();
use Cpanel::Sys::Hostname ();
use Cpanel::iContact ();
use Cpanel::Usage ();
use Socket ();
my $LOCALHOST = "127.0.0.1";
$Text::Wrap::columns = 68;
sub usage {
die <<'EOM';
ipcheck - Report on various error conditions relating to hostname / IP resolution
ipcheck [options]
Options:
--help Brief help message
--test Test email formatting by generating an email with random simulated IP configuration errors
(NOTE: The subject and body of the email will both clearly indicate that it is only a simulation.)
EOM
}
sub test_mode {
# The purpose of the test is purely to exercise the email paragraph formatting
# functionality we have now implemented in this script, to address a long-standing
# problem of poorly formatted email text.
my ( $hostname, $rightip ) = @_;
my $ip = "192.168.22.22";
my $dns_resip = "192.168.33.33";
$rightip = "192.168.44.44" unless $rightip =~ /^\d+[.]\d+[.]\d+[.]\d+$/;
$hostname = "TestHost123" unless $hostname;
srand(time);
my $problems = "";
( rand(5) > 2.9 ) && do { $problems .= bad_resolve_msg( $hostname, $ip, $rightip ) };
( rand(5) > 2.9 ) && do { $problems .= unable_resolve_msg1($hostname) };
( rand(5) > 2.9 ) && do { $problems .= unable_resolve_msg2($hostname) };
( rand(5) > 2.9 ) && do { $problems .= host_not_found_msg($hostname) };
( rand(5) > 2.9 ) && do { $problems .= dns_resip_msg( $hostname, $dns_resip, $rightip ) };
return $problems;
}
sub end_the_msg {
my $msg = shift;
$msg =~ s/\s*\n*\z//g;
return "$msg\n\n"; # Text::Wrap::fill() requires this
}
sub bad_resolve_msg {
my ( $hostname, $ip, $rightip ) = @_;
my $problems = "The hostname ($hostname) resolves to $ip. It should resolve to $rightip. Please be sure to correct /etc/hosts, and also the 'A' entry in the zone file for the domain.";
return end_the_msg($problems);
}
sub unable_resolve_msg1 {
my $hostname = shift;
my $problems = "Your hostname ($hostname) could not be resolved to an IP address. This means that /etc/hosts is not set up correctly as well as there is no dns entry for $hostname. Please check /etc/hosts and add an 'A' entry in your dns.";
return end_the_msg($problems);
}
sub unable_resolve_msg2 {
my $hostname = shift;
my $problems = "Your hostname ($hostname) could not be resolved to an IP address; no 'host' binary found.";
return end_the_msg($problems);
}
sub host_not_found_msg {
my $hostname = shift;
my $problems = "Your hostname ($hostname) could not be resolved to an IP address. This means that there is no dns entry for $hostname. Please add an 'A' entry in your dns.";
return end_the_msg($problems);
}
sub dns_resip_msg {
my ( $hostname, $dns_resip, $rightip ) = @_;
my $problems = "The hostname ($hostname) resolves to $dns_resip. It should resolve to $rightip. Please be sure to correct /etc/hosts as well as the 'A' entry in zone file for the domain.";
return end_the_msg($problems);
}
sub concluding_msg1 {
my $msg = "Some are all of these problems can be caused by /etc/resolv.conf being setup incorrectly. Please check this file if you believe everything else is correct.\n\n";
return end_the_msg($msg);
}
sub concluding_msg2 {
my $msg = "You may be able to automatically correct this problem by using the 'Add an A entry for your hostname' option under 'Dns Functions' in your Web Host Manager.\n\n";
return end_the_msg($msg);
}
my $test_mode = 0;
Cpanel::Usage::wrap_options(
\@ARGV,
\&usage,
{
'test' => \$test_mode,
}
);
# The following is needed because Cpanel::Usage::wrap_options()
# does not remove the --xxx option args from @ARGV after
# collecting them.
#
# However, note that the following two approaches give subtly
# different results; that is, where --xxx option args are freely
# intermingled on the command line with args not beginning with
# "--", rather than congregating all together on the left, as
# one would more typically expect.
#
# We'll stick with precedent formerly set, and use grep; see,
# e.g., /scripts/setupnameserver.
#if ( 1 ) { # see above comment
@ARGV = grep { !/^--/ } @ARGV;
#}
#else {
# shift @ARGV while @ARGV && $ARGV[0] =~ /^--/;
#}
# ALL args to this script must be --xxx option args. If any
# other args are left after we've eliminated those, we have a
# problem.
@ARGV && usage();
my $rightip = Cpanel::DIp::MainIP::getmainserverip();
my $hostname = Cpanel::Sys::Hostname::gethostname();
my $problems = '';
if ($test_mode) { # devel/test
$problems = test_mode( $hostname, $rightip );
}
else { # production
my $iaddr;
if ( my $iaddr = gethostbyname($hostname) ) {
my $ip = Socket::inet_ntoa($iaddr);
$problems .= bad_resolve_msg( $hostname, $ip, $rightip )
if $ip ne $rightip && $ip ne $LOCALHOST;
}
else {
$problems .= unable_resolve_msg1($hostname);
}
my $host_bin = Cpanel::FindBin::findbin('host');
if ( !$host_bin ) {
$problems .= unable_resolve_msg2($hostname);
}
else {
my $dnsres = Cpanel::SafeRun::Errors::saferunallerrors( $host_bin, $hostname );
my ($dns_resip) = $dnsres =~ /(\d+\.\d+\.\d+\.\d+)/;
if ( $dnsres =~ /Host not found/i ) {
$problems .= host_not_found_msg($hostname);
}
elsif ( $dns_resip ne $rightip ) {
$problems .= dns_resip_msg( $hostname, $dns_resip, $rightip );
}
}
}
if ($problems) {
require Cpanel::iContact;
my ( $simulated, $top_msg );
if ($test_mode) {
$simulated = " [SIMULATED TEST]";
$top_msg = "You may safely ignore this email. The errors it reports are not real, they are only a simulation for test purposes.";
}
else {
$simulated = "";
$top_msg = "Do not ignore this email.";
}
my $subject = "[ipcheck] Problem with DNS setup on $hostname$simulated";
substr $problems, 0, 0, "IMPORTANT: $top_msg\n\n";
$problems .= concluding_msg1();
$problems .= concluding_msg2();
my @problems = split /\n/, $problems;
$problems = Text::Wrap::fill( '', '', @problems );
print "[ipcheck] sent email:\n\n$problems\n\n";
Cpanel::iContact::icontact(
'application' => 'ipcheck',
'level' => 2,
'subject' => $subject,
'message' => $problems,
'msgtype' => ''
);
}