#!/usr/bin/perl

### ***system-check.pl***
###
### Basic System Check-up Script
### Copyright (c) 2008, Entinux Ltd. 
### All rights reserved.
### 
### Redistribution and use in source and binary forms, with or without modification,
### are permitted provided that the following conditions are met:
### 
### Redistributions of source code must retain the above copyright notice,
### this list of conditions and the following disclaimer. 
### 
### Redistributions in binary form must reproduce the above copyright notice,
### this list of conditions and the following disclaimer in the documentation
### and/or other materials provided with the distribution. 
### 
### Neither the name of Entinux Ltd. nor the names of its contributors may be used to endorse
### or promote products derived from this software without specific prior written permission. 
### 
### THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
### OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
### MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
### IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
### INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
### BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
### DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
### LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
### OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
### OF THE POSSIBILITY OF SUCH DAMAGE.
### 
### 

use strict 'vars', 'subs';
use Socket;

my $host = '202.177.26.103';
my $url = '/systemcheck-submit.php';
my $port = 80;

my %data = ('org' => '', 'person' => '', 'tel' => '', 'kernel' => '',
            'dist' => '', 'glibc' => '', 'np' => '', 'npc' => '',
            'ps' => '', 'psc' => '', 'df' => '');

my @desc = ('kernel:Kernel Version',
            'dist:Linux Distribution',
            'glibc:Glibc Library Version',
            'np:Network Listening Ports',
            'npc:Number of Network Listening Ports',
            'ps:Process Status',
            'psc:Number of Running Processes',
            'df:Disk Usage');

my ($ans, $post_str, $file_str) = '';

print "Gathering system information ... ";

$data{'kernel'} = `uname -a`;
$data{'dist'}   = `cat /etc/redhat-release 2>/dev/null`;
$data{'glibc'}  = `ls /lib/libc-*`;
$data{'np'}     = `netstat -l -p -n | grep tcp | grep "LISTEN"`;
$data{'npc'}    = `netstat -l -p -n | grep tcp | grep "LISTEN" -c`;
$data{'ps'}     = `ps auxf`;
$data{'psc'}    = `ps auxf | grep '\n' -c`;
$data{'df'}     = `df -h`;

print "done\n\n";

for (my $i = 0; $i < @desc; $i++)
{
    my ($key, $value) = split(':', $desc[$i], 2);
    $file_str .= "[$value]\n";
    $file_str .= $data{$key}."\n\n";
}

if (open(f, ">systemcheck.txt"))
{
    print f "$file_str\n";
    close(f);
}

print "$file_str\n";

print "Result saved at systemcheck.txt.\n\n";

print "
##############################################################

Established in 1998, Entinux is a leading pioneer in Linux
systems and application integration technologies.

Our specialist could analyse your system information
provide free consultancy for your organization,
and discuss for further support options.

Telephone Hotline: (852) 2620-9600 / Email: info\@entinux.com

##############################################################

成立於 1998 年的 Entinux，是香港 Linux 市場的先驅和領導者，
提供多元化的 Linux 系統及支援方案。

我們的技術顧問可為你進一步分析系統的資料，
為閣下的機構提供免費顧問，並商議進一步的支援方案。

電話熱線：(852) 2620-9600 / Email: info\@entinux.com

##############################################################

";

print "Do you want to submit system information to Entinux (yes/no)? ";
chomp($ans = <STDIN>);
exit(0) if (lc($ans) ne 'yes');

while ($data{'org'} eq '')
{
    print "Please enter your organization name: ";
    chomp($data{'org'} = <STDIN>);
}

while ($data{'person'} eq '')
{
    print "Please enter your contact name: ";
    chomp($data{'person'} = <STDIN>);
}

while ($data{'tel'} eq '')
{
    print "Please enter your telephone number: ";
    chomp($data{'tel'} = <STDIN>);
}

my @key_list = keys(%data);

for (my $i = 0; $i < @key_list; $i++)
{
    my $key = $key_list[$i];
    $post_str .= "$key=".urlencode($data{$key});
    $post_str .= '&' if ($i + 1 < @key_list);
}

my $iaddr = inet_aton($host) || die("Error connecting to $host.\n");
my $paddr = sockaddr_in($port, $iaddr) || die("Error connecting to $host.\n");
my $proto = getprotobyname('tcp') || die("Error connecting to $host.\n");

socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die("Error connecting to $host.\n");
connect(SOCK, $paddr) || die("Error connecting to $host.\n");

print SOCK "POST $url HTTP/1.0\n";
print SOCK "Content-Type: application/x-www-form-urlencoded\n";
print SOCK "Content-Length: ".length($post_str)."\n\n";
print SOCK "$post_str\n";

close(SOCK);

print "\n";
print "Information has been submitted successfully.\n";

exit(0);

sub urlencode
{
    my $s = $_[0];
    $s =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
    return $s; 
}

